Skip to content

Instantly share code, notes, and snippets.

@django-wong
Created July 6, 2017 09:31
Show Gist options
  • Save django-wong/58cfe6569f31939dc2b5816fa9e39206 to your computer and use it in GitHub Desktop.
Save django-wong/58cfe6569f31939dc2b5816fa9e39206 to your computer and use it in GitHub Desktop.
helpers to retrieve git commmit infomation
<?php
/**
* @Author: Django Wong
* @Date: 2017-05-03 13:00:26
* @Last Modified by: django-wong
* @Last Modified time: 2017-07-06 17:01:40
*/
if(!function_exists('git_commit_hash')){
/**
* current git commit hash (long)
* @return string
*/
function git_commit_hash() {
$root = base_path();
return trim(`cd "$root" && git rev-parse HEAD 2>&1`);
}
}
if(!function_exists('git_commit_hash_short')){
/**
* current git commit hash (short)
* @return string [description]
*/
function git_commit_hash_short() {
$root = base_path();
return `cd "$root" && git log --pretty=format:'%h' -n 1 2>&1`;
}
}
if(!function_exists('git_branch_name')){
/**
* current git branch name
* @return string
*/
function git_branch_name() {
$root = base_path();
return `cd "$root" && git rev-parse --abbrev-ref HEAD 2>&1`;
}
}
if(!function_exists('git_tags_on_head')){
/**
* return tags on HEAD
* @return array<string>
*/
function git_tags_on_head() {
$root = base_path();
$result = trim(`cd "$root" && git tag --points-at HEAD 2>&1`);
return $result ? explode("\n", $result) : [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment