Created
April 13, 2016 14:51
-
-
Save Langmans/7da2d9ffb622f8827a8804381d866f0c to your computer and use it in GitHub Desktop.
returns the current git branch, remote name, hash and timestamp for a git repository dir or git submodule dir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function git_status($dir) { | |
| $git_line = 'Unable to determine git hash'; | |
| $git_dir = $dir . '/.git'; | |
| $repo = null; | |
| $submodule = null; | |
| if (is_file($git_dir) && preg_match('@gitdir: (.*)@', trim(file_get_contents($git_dir)), $m)) { | |
| $repo = basename($m[1]); | |
| $git_dir = $m[1]; | |
| $submodule = true; | |
| } else { | |
| // detect repo for root git... | |
| $config = parse_ini_file($git_dir . '/config', true); | |
| if (isset($config['remote origin'], $config['remote origin']['url'])) { | |
| $repo = pathinfo(parse_url($config['remote origin']['url'], PHP_URL_PATH), PATHINFO_FILENAME); | |
| } | |
| } | |
| $head_file = $git_dir . '/HEAD'; | |
| if (!is_file($head_file)) { | |
| return $git_line; | |
| } | |
| if (!preg_match('@ref:\s*(.*)@', file_get_contents($head_file), $m)) { | |
| return $git_line; | |
| } | |
| $head_file = $git_dir . '/' . $m[1]; | |
| $hash = trim(file_get_contents($head_file)); | |
| $mtime = filemtime($head_file); | |
| $branch = basename($head_file); | |
| return sprintf('<strong>%s</strong>@<em>%s</em>:<code>%s</code> <datetime>%s</datetime>', | |
| $repo, $branch, $hash, date(DATE_COOKIE, $mtime)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment