From 7843453c00d5cb5954c9674a9f08008224e27d1e Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 23 Mar 2016 19:08:38 +0300 Subject: [PATCH] version: do not hardcode master branch --- include/version.php | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/include/version.php b/include/version.php index dfef9da5..71cf3dc7 100644 --- a/include/version.php +++ b/include/version.php @@ -5,18 +5,37 @@ date_default_timezone_set('UTC'); $root_dir = dirname(dirname(__FILE__)); - if (is_dir("$root_dir/.git") && file_exists("$root_dir/.git/refs/heads/master")) { + if (is_dir("$root_dir/.git") && file_exists("$root_dir/.git/HEAD")) { + $head = trim(file_get_contents("$root_dir/.git/HEAD")); - $suffix = substr(trim(file_get_contents("$root_dir/.git/refs/heads/master")), 0, 7); - $timestamp = filemtime("$root_dir/.git/refs/heads/master"); + if ($head) { + $matches = array(); - define("GIT_VERSION_HEAD", $suffix); - define("GIT_VERSION_TIMESTAMP", $timestamp); + if (preg_match("/^ref: (.*)/", $head, $matches)) { + $ref = $matches[1]; - return VERSION_STATIC . " ($suffix)"; - } else { - return VERSION_STATIC; + $suffix = substr(trim(file_get_contents("$root_dir/.git/$ref")), 0, 7); + $timestamp = filemtime("$root_dir/.git/$ref"); + + define("GIT_VERSION_HEAD", $suffix); + define("GIT_VERSION_TIMESTAMP", $timestamp); + + return VERSION_STATIC . " ($suffix)"; + + } else { + $suffix = trim($head, 0, 7); + $timestamp = filemtime("$root_dir/.git/HEAD"); + + define("GIT_VERSION_HEAD", $suffix); + define("GIT_VERSION_TIMESTAMP", $timestamp); + + return VERSION_STATIC . " ($suffix)"; + } + } } + + return VERSION_STATIC; + } define('VERSION', get_version()); -- 2.39.2