Merge "Use local context to get messages"
[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. Access via static method getViewers().
25 */
26 private static $viewers = false;
27
28 /**
29 * @param $dir string The root directory of the repo where the .git dir can be found
30 */
31 public function __construct( $dir ) {
32 $this->basedir = "{$dir}/.git/";
33 }
34
35 /**
36 * Return a singleton for the repo at $IP
37 * @return GitInfo
38 */
39 public static function repo() {
40 global $IP;
41 if ( is_null( self::$repo ) ) {
42 self::$repo = new self( $IP );
43 }
44 return self::$repo;
45 }
46
47 /**
48 * Check if a string looks like a hex encoded SHA1 hash
49 *
50 * @param $str string The string to check
51 * @return bool Whether or not the string looks like a SHA1
52 */
53 public static function isSHA1( $str ) {
54 return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
55 }
56
57 /**
58 * Return the HEAD of the repo (without any opening "ref: ")
59 * @return string The HEAD
60 */
61 public function getHead() {
62 $HEADfile = "{$this->basedir}/HEAD";
63
64 if ( !is_readable( $HEADfile ) ) {
65 return false;
66 }
67
68 $HEAD = file_get_contents( $HEADfile );
69
70 if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) {
71 return rtrim( $m[1] );
72 } else {
73 return rtrim( $HEAD );
74 }
75 }
76
77 /**
78 * Return the SHA1 for the current HEAD of the repo
79 * @return string A SHA1 or false
80 */
81 public function getHeadSHA1() {
82 $HEAD = $this->getHead();
83
84 // If detached HEAD may be a SHA1
85 if ( self::isSHA1( $HEAD ) ) {
86 return $HEAD;
87 }
88
89 // If not a SHA1 it may be a ref:
90 $REFfile = "{$this->basedir}{$HEAD}";
91 if ( !is_readable( $REFfile ) ) {
92 return false;
93 }
94
95 $sha1 = rtrim( file_get_contents( $REFfile ) );
96
97 return $sha1;
98 }
99
100 /**
101 * Return the name of the current branch, or HEAD if not found
102 * @return string The branch name, HEAD, or false
103 */
104 public function getCurrentBranch() {
105 $HEAD = $this->getHead();
106 if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) {
107 return $m[1];
108 } else {
109 return $HEAD;
110 }
111 }
112
113 /**
114 * Get an URL to a web viewer link to the HEAD revision.
115 *
116 * @return string|bool string if an URL is available or false otherwise.
117 */
118 public function getHeadViewUrl() {
119 $config = "{$this->basedir}/config";
120 if ( !is_readable( $config ) ) {
121 return false;
122 }
123
124 $configArray = parse_ini_file( $config, true );
125 $remote = false;
126
127 // Use the "origin" remote repo if available or any other repo if not.
128 if ( isset( $configArray['remote origin'] ) ) {
129 $remote = $configArray['remote origin'];
130 } else {
131 foreach( $configArray as $sectionName => $sectionConf ) {
132 if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
133 $remote = $sectionConf;
134 }
135 }
136 }
137
138 if ( $remote === false || !isset( $remote['url'] ) ) {
139 return false;
140 }
141
142 $url = $remote['url'];
143 if ( substr( $url, -4 ) !== '.git' ) {
144 $url .= '.git';
145 }
146 foreach( self::getViewers() as $repo => $viewer ) {
147 $pattern = '#^' . $repo . '$#';
148 if ( preg_match( $pattern, $url ) ) {
149 $viewerUrl = preg_replace( $pattern, $viewer, $url );
150 $headSHA1 = $this->getHeadSHA1();
151 $replacements = array(
152 '%h' => substr( $headSHA1, 0, 7 ),
153 '%H' => $headSHA1
154 );
155 return strtr( $viewerUrl, $replacements );
156 }
157 }
158 return false;
159 }
160
161 /**
162 * @see self::getHeadSHA1
163 * @return string
164 */
165 public static function headSHA1() {
166 return self::repo()->getHeadSHA1();
167 }
168
169 /**
170 * @see self::getCurrentBranch
171 * @return string
172 */
173 public static function currentBranch() {
174 return self::repo()->getCurrentBranch();
175 }
176
177 /**
178 * @see self::getHeadViewUrl()
179 * @return bool|string
180 */
181 public static function headViewUrl() {
182 return self::repo()->getHeadViewUrl();
183 }
184
185 /**
186 * Gets the list of repository viewers
187 * @return array
188 */
189 protected static function getViewers() {
190 global $wgGitRepositoryViewers;
191
192 if( self::$viewers === false ) {
193 self::$viewers = $wgGitRepositoryViewers;
194 wfRunHooks( 'GitViewers', array( &self::$viewers ) );
195 }
196
197 return self::$viewers;
198 }
199 }