Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions lib/git.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}
}