Merge "Show Log toolbox link for anon users, fix toolbox on DeletedContribs"
[lhc/web/wiklou.git] / includes / GitInfo.php
1 <?php
2 /**
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.
7 *
8 * @file
9 */
10
11 class GitInfo {
12
13 /**
14 * Singleton for the repo at $IP
15 */
16 protected static $repo = null;
17
18 /**
19 * Location of the .git directory
20 */
21 protected $basedir;
22
23 /**
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.
30 */
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',
34 );
35
36 /**
37 * @param $dir The root directory of the repo where the .git dir can be found
38 */
39 public function __construct( $dir ) {
40 $this->basedir = "{$dir}/.git/";
41 }
42
43 /**
44 * Return a singleton for the repo at $IP
45 * @return GitInfo
46 */
47 public static function repo() {
48 global $IP;
49 if ( is_null( self::$repo ) ) {
50 self::$repo = new self( $IP );
51 }
52 return self::$repo;
53 }
54
55 /**
56 * Check if a string looks like a hex encoded SHA1 hash
57 *
58 * @param $str The string to check
59 * @return bool Whether or not the string looks like a SHA1
60 */
61 public static function isSHA1( $str ) {
62 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
63 }
64
65 /**
66 * Return the HEAD of the repo (without any opening "ref: ")
67 * @return string The HEAD
68 */
69 public function getHead() {
70 $HEADfile = "{$this->basedir}/HEAD";
71
72 if ( !is_readable( $HEADfile ) ) {
73 return false;
74 }
75
76 $HEAD = file_get_contents( $HEADfile );
77
78 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
79 return rtrim( $m[1] );
80 } else {
81 return $HEAD;
82 }
83 }
84
85 /**
86 * Return the SHA1 for the current HEAD of the repo
87 * @return string A SHA1 or false
88 */
89 public function getHeadSHA1() {
90 $HEAD = $this->getHead();
91
92 // If detached HEAD may be a SHA1
93 if ( self::isSHA1( $HEAD ) ) {
94 return $HEAD;
95 }
96
97 // If not a SHA1 it may be a ref:
98 $REFfile = "{$this->basedir}{$HEAD}";
99 if ( !is_readable( $REFfile ) ) {
100 return false;
101 }
102
103 $sha1 = rtrim( file_get_contents( $REFfile ) );
104
105 return $sha1;
106 }
107
108 /**
109 * Return the name of the current branch, or HEAD if not found
110 * @return string The branch name, HEAD, or false
111 */
112 public function getCurrentBranch() {
113 $HEAD = $this->getHead();
114 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
115 return $m[1];
116 } else {
117 return $HEAD;
118 }
119 }
120
121 /**
122 * Get an URL to a web viewer link to the HEAD revision.
123 *
124 * @return string|false string if an URL is available or false otherwise.
125 */
126 public function getHeadViewUrl() {
127 $config = "{$this->basedir}/config";
128 if ( !is_readable( $config ) ) {
129 return false;
130 }
131
132 $configArray = parse_ini_file( $config, true );
133 $remote = false;
134
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'];
138 } else {
139 foreach( $configArray as $sectionName => $sectionConf ) {
140 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
141 $remote = $sectionConf;
142 }
143 }
144 }
145
146 if ( $remote === false || !isset( $remote['url'] ) ) {
147 return false;
148 }
149
150 $url = $remote['url'];
151 foreach( $this->viewers as $repo => $viewer ) {
152 $m = array();
153 $pattern = '#^' . $repo . '$#';
154 if ( preg_match( $pattern, $url ) ) {
155 $viewerUrl = preg_replace( $pattern, $viewer, $url );
156 $headSHA1 = $this->getHeadSHA1();
157 $replacements = array(
158 '%h' => substr( $headSHA1, 0, 7 ),
159 '%H' => $headSHA1
160 );
161 return strtr( $viewerUrl, $replacements );
162 }
163 }
164 return false;
165 }
166
167 /**
168 * @see self::getHeadSHA1
169 */
170 public static function headSHA1() {
171 return self::repo()->getHeadSHA1();
172 }
173
174 /**
175 * @see self::getCurrentBranch
176 */
177 public static function currentBranch() {
178 return self::repo()->getCurrentBranch();
179 }
180
181 }