diff --git a/lib/git.class.php b/lib/git.class.php index 8828fa8..6833859 100644 --- a/lib/git.class.php +++ b/lib/git.class.php @@ -402,15 +402,37 @@ public function getObject($name) return $object; } + /** + * @brief Look up a tag. + * + * @param $commit_obj (GitObject) The commit to look up. + * @returns (string) The commit of the tag. + */ + public function getTag($commit_obj) + { + $commit = sha1_hex($commit_obj->getName()); + $dir = $this->dir.'/refs/tags'; + $d = opendir($dir); + while( ($file = readdir($d))!==false ) { + $fn = $dir."/".$file; + if( is_file($fn) ) { + if( $commit==trim(file_get_contents($fn)) ) { + return $file; + } + } + } + return ""; + } + /** * @brief Look up a branch. * * @param $branch (string) The branch to look up, defaulting to @em master. * @returns (string) The tip of the branch (binary sha1). */ - public function getTip($branch='master') + public function getTip($tip='master', $is_tag=false) { - $subpath = sprintf('refs/heads/%s', $branch); + $subpath = sprintf('refs/%s/%s', $is_tag?"tags":"heads", $tip); $path = sprintf('%s/%s', $this->dir, $subpath); if (file_exists($path)) return sha1_bin(file_get_contents($path)); @@ -432,7 +454,7 @@ public function getTip($branch='master') if ($head !== NULL) return $head; } - throw new Exception(sprintf('no such branch: %s', $branch)); + throw new Exception(sprintf('no such %s: %s', $is_tag?"tag":"branch", $tip)); } }