From: Daniel Friesen Date: Fri, 23 Mar 2012 11:19:13 +0000 (-0700) Subject: Add GitInfo class. X-Git-Tag: 1.31.0-rc.0~24149 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/?a=commitdiff_plain;h=36b1172147831e97d5fac21bf098016c2c0bee84;p=lhc%2Fweb%2Fwiklou.git Add GitInfo class. This adds GitInfo which is used by Special:Version to get the SHA1 of the git repo. It's also useful for development to put blocks of config in LocalSettings.php that only apply to branches where you are developing large features: if ( GitInfo::currentBranch() == 'myrewriteproject' ) { // […] } Change-Id: I2a76662bb40080be6556d4edf60ac6714f36a727 --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 4ab5de868b..e7918730d7 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -95,6 +95,7 @@ $wgAutoloadLocalClasses = array( 'FormAction' => 'includes/Action.php', 'FormOptions' => 'includes/FormOptions.php', 'FormSpecialPage' => 'includes/SpecialPage.php', + 'GitInfo' => 'includes/GitInfo.php', 'HashtableReplacer' => 'includes/StringUtils.php', 'HistoryBlob' => 'includes/HistoryBlob.php', 'HistoryBlobCurStub' => 'includes/HistoryBlob.php', diff --git a/includes/GitInfo.php b/includes/GitInfo.php new file mode 100644 index 0000000000..bc3f35e3d1 --- /dev/null +++ b/includes/GitInfo.php @@ -0,0 +1,122 @@ +basedir = "{$dir}/.git/"; + } + + /** + * Return a singleton for the repo at $IP + * @return GitInfo + */ + public static function repo() { + global $IP; + if ( is_null( self::$repo ) ) { + self::$repo = new self( $IP ); + } + return self::$repo; + } + + /** + * Check if a string looks like a hex encoded SHA1 hash + * + * @param $str The string to check + * @return bool Whether or not the string looks like a SHA1 + */ + public static function isSHA1( $str ) { + return !!preg_match( '/^[0-9A-Z]{40}$/i', $str ); + } + + /** + * Return the HEAD of the repo (without any opening "ref: ") + * @return string The HEAD + */ + public function getHead() { + $HEADfile = "{$this->basedir}/HEAD"; + + if ( !is_readable( $HEADfile ) ) { + return false; + } + + $HEAD = file_get_contents( $HEADfile ); + + if ( preg_match( "/ref: (.*)/", $HEAD, $m ) ) { + return rtrim( $m[1] ); + } else { + return $HEAD; + } + } + + /** + * Return the SHA1 for the current HEAD of the repo + * @return string A SHA1 or false + */ + public function getHeadSHA1() { + $HEAD = $this->getHead(); + + // If detached HEAD may be a SHA1 + if ( self::isSHA1( $HEAD ) ) { + return $HEAD; + } + + // If not a SHA1 it may be a ref: + $REFfile = "{$this->basedir}{$HEAD}"; + if ( !is_readable( $REFfile ) ) { + return false; + } + + $sha1 = rtrim( file_get_contents( $REFfile ) ); + + return $sha1; + } + + /** + * Return the name of the current branch, or HEAD if not found + * @return string The branch name, HEAD, or false + */ + public function getCurrentBranch() { + $HEAD = $this->getHead(); + if ( $HEAD && preg_match( "#^refs/heads/(.*)$#", $HEAD, $m ) ) { + return $m[1]; + } else { + return $HEAD; + } + } + + /** + * @see self::getHeadSHA1 + */ + public static function headSHA1() { + return self::repo()->getHeadSHA1(); + } + + /** + * @see self::getCurrentBranch + */ + public static function currentBranch() { + return self::repo()->getCurrentBranch(); + } + +} \ No newline at end of file diff --git a/includes/specials/SpecialVersion.php b/includes/specials/SpecialVersion.php index f88e9da4f2..9181de0b75 100644 --- a/includes/specials/SpecialVersion.php +++ b/includes/specials/SpecialVersion.php @@ -721,24 +721,8 @@ class SpecialVersion extends SpecialPage { * @return bool|String sha1 of commit HEAD points to */ public static function getGitHeadSha1( $dir ) { - $BASEDIR = "{$dir}/.git/"; - $HEADfile = "{$BASEDIR}/HEAD"; - - if( !file_exists( $HEADfile ) ) { - return false; - } - - preg_match( "/ref: (.*)/", - file_get_contents( $HEADfile ), $m ); - - $REFfile = "{$BASEDIR}{$m[1]}"; - if( !file_exists( $REFfile ) ) { - return false; - } - - $sha1 = rtrim( file_get_contents( $REFfile ) ); - - return $sha1; + $repo = new GitInfo( $dir ); + return $repo->getHeadSHA1(); } function showEasterEgg() {