3 * A class to help return information about a git repo MediaWiki may be inside
4 * This is used by Special:Version and is also useful for the LocalSettings.php
5 * of anyone working on large branches in git to setup config that show up only
6 * when specific branches are currently checked out.
14 * Singleton for the repo at $IP
16 protected static $repo = null;
19 * Location of the .git directory
24 * Map of repo URLs to viewer URLs.
25 * Key is a pattern passed to preg_match() and preg_replace(),
26 * without the delimiters (which are #) and must match the whole URL.
27 * The value is the replacement for the key (it can contain $1, etc.)
28 * %h will be replaced by the short SHA-1 (7 first chars) and %H by the
29 * full SHA-1 of the HEAD revision.
31 protected $viewers = array(
32 'https://gerrit.wikimedia.org/r/p/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%H',
33 'ssh://(?:[a-z0-9_]+@)?gerrit.wikimedia.org:29418/(.*)' => 'https://gerrit.wikimedia.org/r/gitweb?p=$1;h=%H',
37 * @param $dir The root directory of the repo where the .git dir can be found
39 public function __construct( $dir ) {
40 $this->basedir
= "{$dir}/.git/";
44 * Return a singleton for the repo at $IP
47 public static function repo() {
49 if ( is_null( self
::$repo ) ) {
50 self
::$repo = new self( $IP );
56 * Check if a string looks like a hex encoded SHA1 hash
58 * @param $str The string to check
59 * @return bool Whether or not the string looks like a SHA1
61 public static function isSHA1( $str ) {
62 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
66 * Return the HEAD of the repo (without any opening "ref: ")
67 * @return string The HEAD
69 public function getHead() {
70 $HEADfile = "{$this->basedir}/HEAD";
72 if ( !is_readable( $HEADfile ) ) {
76 $HEAD = file_get_contents( $HEADfile );
78 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
79 return rtrim( $m[1] );
86 * Return the SHA1 for the current HEAD of the repo
87 * @return string A SHA1 or false
89 public function getHeadSHA1() {
90 $HEAD = $this->getHead();
92 // If detached HEAD may be a SHA1
93 if ( self
::isSHA1( $HEAD ) ) {
97 // If not a SHA1 it may be a ref:
98 $REFfile = "{$this->basedir}{$HEAD}";
99 if ( !is_readable( $REFfile ) ) {
103 $sha1 = rtrim( file_get_contents( $REFfile ) );
109 * Return the name of the current branch, or HEAD if not found
110 * @return string The branch name, HEAD, or false
112 public function getCurrentBranch() {
113 $HEAD = $this->getHead();
114 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
122 * Get an URL to a web viewer link to the HEAD revision.
124 * @return string|false string if an URL is available or false otherwise.
126 public function getHeadViewUrl() {
127 $config = "{$this->basedir}/config";
128 if ( !is_readable( $config ) ) {
132 $configArray = parse_ini_file( $config, true );
135 // Use the "origin" remote repo if available or any other repo if not.
136 if ( isset( $configArray['remote origin'] ) ) {
137 $remote = $configArray['remote origin'];
139 foreach( $configArray as $sectionName => $sectionConf ) {
140 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
141 $remote = $sectionConf;
146 if ( $remote === false ||
!isset( $remote['url'] ) ) {
150 $url = $remote['url'];
151 if ( substr( $url, -4 ) !== '.git' ) {
154 foreach( $this->viewers
as $repo => $viewer ) {
156 $pattern = '#^' . $repo . '$#';
157 if ( preg_match( $pattern, $url ) ) {
158 $viewerUrl = preg_replace( $pattern, $viewer, $url );
159 $headSHA1 = $this->getHeadSHA1();
160 $replacements = array(
161 '%h' => substr( $headSHA1, 0, 7 ),
164 return strtr( $viewerUrl, $replacements );
171 * @see self::getHeadSHA1
173 public static function headSHA1() {
174 return self
::repo()->getHeadSHA1();
178 * @see self::getCurrentBranch
180 public static function currentBranch() {
181 return self
::repo()->getCurrentBranch();
185 * @see self::getHeadViewUrl()
187 public static function headViewUrl() {
188 return self
::repo()->getHeadViewUrl();