HTMLForm: Add "cloner" type
[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 string $dir The root directory of the repo where the .git dir can be found
45 */
46 public function __construct( $dir ) {
47 $this->basedir = $dir . DIRECTORY_SEPARATOR . '.git';
48 if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
49 $GITfile = file_get_contents( $this->basedir );
50 if ( strlen( $GITfile ) > 8 && substr( $GITfile, 0, 8 ) === 'gitdir: ' ) {
51 $path = rtrim( substr( $GITfile, 8 ), "\r\n" );
52 $isAbsolute = $path[0] === '/' || substr( $path, 1, 1 ) === ':';
53 $this->basedir = $isAbsolute ? $path : $dir . DIRECTORY_SEPARATOR . $path;
54 }
55 }
56 }
57
58 /**
59 * Return a singleton for the repo at $IP
60 * @return GitInfo
61 */
62 public static function repo() {
63 global $IP;
64 if ( is_null( self::$repo ) ) {
65 self::$repo = new self( $IP );
66 }
67 return self::$repo;
68 }
69
70 /**
71 * Check if a string looks like a hex encoded SHA1 hash
72 *
73 * @param string $str The string to check
74 * @return bool Whether or not the string looks like a SHA1
75 */
76 public static function isSHA1( $str ) {
77 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
78 }
79
80 /**
81 * Return the HEAD of the repo (without any opening "ref: ")
82 * @return string The HEAD
83 */
84 public function getHead() {
85 $headFile = "{$this->basedir}/HEAD";
86
87 if ( !is_readable( $headFile ) ) {
88 return false;
89 }
90
91 $head = file_get_contents( $headFile );
92
93 if ( preg_match( "/ref: (.*)/", $head, $m ) ) {
94 return rtrim( $m[1] );
95 } else {
96 return rtrim( $head );
97 }
98 }
99
100 /**
101 * Return the SHA1 for the current HEAD of the repo
102 * @return string A SHA1 or false
103 */
104 public function getHeadSHA1() {
105 $head = $this->getHead();
106
107 // If detached HEAD may be a SHA1
108 if ( self::isSHA1( $head ) ) {
109 return $head;
110 }
111
112 // If not a SHA1 it may be a ref:
113 $refFile = "{$this->basedir}/{$head}";
114 if ( !is_readable( $refFile ) ) {
115 return false;
116 }
117
118 $sha1 = rtrim( file_get_contents( $refFile ) );
119
120 return $sha1;
121 }
122
123 /**
124 * Return the commit date of HEAD entry of the git code repository
125 *
126 * @since 1.22
127 * @return int|bool Commit date (UNIX timestamp) or false
128 */
129 public function getHeadCommitDate() {
130 global $wgGitBin;
131
132 if ( !is_file( $wgGitBin ) || !is_executable( $wgGitBin ) ) {
133 return false;
134 }
135
136 $environment = array( "GIT_DIR" => $this->basedir );
137 $cmd = wfEscapeShellArg( $wgGitBin ) . " show -s --format=format:%ct HEAD";
138 $retc = false;
139 $commitDate = wfShellExec( $cmd, $retc, $environment );
140
141 if ( $retc !== 0 ) {
142 return false;
143 } else {
144 return (int)$commitDate;
145 }
146 }
147
148 /**
149 * Return the name of the current branch, or HEAD if not found
150 * @return string The branch name, HEAD, or false
151 */
152 public function getCurrentBranch() {
153 $head = $this->getHead();
154 if ( $head && preg_match( "#^refs/heads/(.*)$#", $head, $m ) ) {
155 return $m[1];
156 } else {
157 return $head;
158 }
159 }
160
161 /**
162 * Get an URL to a web viewer link to the HEAD revision.
163 *
164 * @return string|bool string if a URL is available or false otherwise.
165 */
166 public function getHeadViewUrl() {
167 $config = "{$this->basedir}/config";
168 if ( !is_readable( $config ) ) {
169 return false;
170 }
171
172 wfSuppressWarnings();
173 $configArray = parse_ini_file( $config, true );
174 wfRestoreWarnings();
175 $remote = false;
176
177 // Use the "origin" remote repo if available or any other repo if not.
178 if ( isset( $configArray['remote origin'] ) ) {
179 $remote = $configArray['remote origin'];
180 } elseif ( is_array( $configArray ) ) {
181 foreach ( $configArray as $sectionName => $sectionConf ) {
182 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
183 $remote = $sectionConf;
184 }
185 }
186 }
187
188 if ( $remote === false || !isset( $remote['url'] ) ) {
189 return false;
190 }
191
192 $url = $remote['url'];
193 if ( substr( $url, -4 ) !== '.git' ) {
194 $url .= '.git';
195 }
196 foreach ( self::getViewers() as $repo => $viewer ) {
197 $pattern = '#^' . $repo . '$#';
198 if ( preg_match( $pattern, $url, $matches ) ) {
199 $viewerUrl = preg_replace( $pattern, $viewer, $url );
200 $headSHA1 = $this->getHeadSHA1();
201 $replacements = array(
202 '%h' => substr( $headSHA1, 0, 7 ),
203 '%H' => $headSHA1,
204 '%r' => urlencode( $matches[1] ),
205 );
206 return strtr( $viewerUrl, $replacements );
207 }
208 }
209 return false;
210 }
211
212 /**
213 * @see self::getHeadSHA1
214 * @return string
215 */
216 public static function headSHA1() {
217 return self::repo()->getHeadSHA1();
218 }
219
220 /**
221 * @see self::getCurrentBranch
222 * @return string
223 */
224 public static function currentBranch() {
225 return self::repo()->getCurrentBranch();
226 }
227
228 /**
229 * @see self::getHeadViewUrl()
230 * @return bool|string
231 */
232 public static function headViewUrl() {
233 return self::repo()->getHeadViewUrl();
234 }
235
236 /**
237 * Gets the list of repository viewers
238 * @return array
239 */
240 protected static function getViewers() {
241 global $wgGitRepositoryViewers;
242
243 if ( self::$viewers === false ) {
244 self::$viewers = $wgGitRepositoryViewers;
245 wfRunHooks( 'GitViewers', array( &self::$viewers ) );
246 }
247
248 return self::$viewers;
249 }
250 }