Merge "Add a test for named vs. positional parameter whitespace stripping"
[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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 class GitInfo {
27
28 /**
29 * Singleton for the repo at $IP
30 */
31 protected static $repo = null;
32
33 /**
34 * Location of the .git directory
35 */
36 protected $basedir;
37
38 /**
39 * Map of repo URLs to viewer URLs. Access via static method getViewers().
40 */
41 private static $viewers = false;
42
43 /**
44 * @param $dir string The root directory of the repo where the .git dir can be found
45 */
46 public function __construct( $dir ) {
47 $this->basedir = "{$dir}/.git/";
48 }
49
50 /**
51 * Return a singleton for the repo at $IP
52 * @return GitInfo
53 */
54 public static function repo() {
55 global $IP;
56 if ( is_null( self::$repo ) ) {
57 self::$repo = new self( $IP );
58 }
59 return self::$repo;
60 }
61
62 /**
63 * Check if a string looks like a hex encoded SHA1 hash
64 *
65 * @param $str string The string to check
66 * @return bool Whether or not the string looks like a SHA1
67 */
68 public static function isSHA1( $str ) {
69 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
70 }
71
72 /**
73 * Return the HEAD of the repo (without any opening "ref: ")
74 * @return string The HEAD
75 */
76 public function getHead() {
77 $HEADfile = "{$this->basedir}/HEAD";
78
79 if ( !is_readable( $HEADfile ) ) {
80 return false;
81 }
82
83 $HEAD = file_get_contents( $HEADfile );
84
85 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
86 return rtrim( $m[1] );
87 } else {
88 return rtrim( $HEAD );
89 }
90 }
91
92 /**
93 * Return the SHA1 for the current HEAD of the repo
94 * @return string A SHA1 or false
95 */
96 public function getHeadSHA1() {
97 $HEAD = $this->getHead();
98
99 // If detached HEAD may be a SHA1
100 if ( self::isSHA1( $HEAD ) ) {
101 return $HEAD;
102 }
103
104 // If not a SHA1 it may be a ref:
105 $REFfile = "{$this->basedir}{$HEAD}";
106 if ( !is_readable( $REFfile ) ) {
107 return false;
108 }
109
110 $sha1 = rtrim( file_get_contents( $REFfile ) );
111
112 return $sha1;
113 }
114
115 /**
116 * Return the name of the current branch, or HEAD if not found
117 * @return string The branch name, HEAD, or false
118 */
119 public function getCurrentBranch() {
120 $HEAD = $this->getHead();
121 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
122 return $m[1];
123 } else {
124 return $HEAD;
125 }
126 }
127
128 /**
129 * Get an URL to a web viewer link to the HEAD revision.
130 *
131 * @return string|bool string if an URL is available or false otherwise.
132 */
133 public function getHeadViewUrl() {
134 $config = "{$this->basedir}/config";
135 if ( !is_readable( $config ) ) {
136 return false;
137 }
138
139 $configArray = parse_ini_file( $config, true );
140 $remote = false;
141
142 // Use the "origin" remote repo if available or any other repo if not.
143 if ( isset( $configArray['remote origin'] ) ) {
144 $remote = $configArray['remote origin'];
145 } else {
146 foreach( $configArray as $sectionName => $sectionConf ) {
147 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
148 $remote = $sectionConf;
149 }
150 }
151 }
152
153 if ( $remote === false || !isset( $remote['url'] ) ) {
154 return false;
155 }
156
157 $url = $remote['url'];
158 if ( substr( $url, -4 ) !== '.git' ) {
159 $url .= '.git';
160 }
161 foreach( self::getViewers() as $repo => $viewer ) {
162 $pattern = '#^' . $repo . '$#';
163 if ( preg_match( $pattern, $url ) ) {
164 $viewerUrl = preg_replace( $pattern, $viewer, $url );
165 $headSHA1 = $this->getHeadSHA1();
166 $replacements = array(
167 '%h' => substr( $headSHA1, 0, 7 ),
168 '%H' => $headSHA1
169 );
170 return strtr( $viewerUrl, $replacements );
171 }
172 }
173 return false;
174 }
175
176 /**
177 * @see self::getHeadSHA1
178 * @return string
179 */
180 public static function headSHA1() {
181 return self::repo()->getHeadSHA1();
182 }
183
184 /**
185 * @see self::getCurrentBranch
186 * @return string
187 */
188 public static function currentBranch() {
189 return self::repo()->getCurrentBranch();
190 }
191
192 /**
193 * @see self::getHeadViewUrl()
194 * @return bool|string
195 */
196 public static function headViewUrl() {
197 return self::repo()->getHeadViewUrl();
198 }
199
200 /**
201 * Gets the list of repository viewers
202 * @return array
203 */
204 protected static function getViewers() {
205 global $wgGitRepositoryViewers;
206
207 if( self::$viewers === false ) {
208 self::$viewers = $wgGitRepositoryViewers;
209 wfRunHooks( 'GitViewers', array( &self::$viewers ) );
210 }
211
212 return self::$viewers;
213 }
214 }