From 76602755becb1431da40fefc7d1680cd0eac853c Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 5 May 2012 23:01:03 +0200 Subject: [PATCH] Get tags and trees --- public_html/libgit/base.php | 1 + public_html/libgit/class.branch.php | 5 +++++ public_html/libgit/class.commit.php | 5 +++++ public_html/libgit/class.repository.php | 17 ++++++++++++++++- public_html/libgit/class.tag.php | 10 ++++++++++ public_html/test.php | 2 +- 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/public_html/libgit/base.php b/public_html/libgit/base.php index f861d14..1524b66 100644 --- a/public_html/libgit/base.php +++ b/public_html/libgit/base.php @@ -24,6 +24,7 @@ function sha1_from_bin($bin) } class GitBranchNotFoundException extends Exception {} +class GitTagNotFoundException extends Exception {} require(dirname(__FILE__) . "/class.repository.php"); require(dirname(__FILE__) . "/class.branch.php"); diff --git a/public_html/libgit/class.branch.php b/public_html/libgit/class.branch.php index 5e4ace2..d3f7355 100644 --- a/public_html/libgit/class.branch.php +++ b/public_html/libgit/class.branch.php @@ -14,4 +14,9 @@ class GitBranch { return $this->repo->GetObject($this->sha); } + + function GetTree() + { + return $this->GetCommit()->GetTree(); + } } diff --git a/public_html/libgit/class.commit.php b/public_html/libgit/class.commit.php index f120993..784da98 100644 --- a/public_html/libgit/class.commit.php +++ b/public_html/libgit/class.commit.php @@ -54,4 +54,9 @@ class GitCommit extends GitObject $this->message = implode("\n", $message_parts); } + + function GetTree() + { + return $this->repo->GetObject($this->tree); + } } diff --git a/public_html/libgit/class.repository.php b/public_html/libgit/class.repository.php index 6a8c981..b35c032 100644 --- a/public_html/libgit/class.repository.php +++ b/public_html/libgit/class.repository.php @@ -63,7 +63,22 @@ class GitRepository } else { - throw new GitBranchNotFoundException("The {$name} branch does not exist."); + throw new GitBranchNotFoundException("The '{$name}' branch does not exist."); + } + } + + function GetTag($name) + { + $filename = "{$this->path}/refs/tags/{$name}"; + + if(file_exists($filename)) + { + $sha = trim(file_get_contents($filename)); + return $this->GetObject($sha); + } + else + { + throw new GitTagNotFoundException("The '{$name}' tag does not exist."); } } } diff --git a/public_html/libgit/class.tag.php b/public_html/libgit/class.tag.php index 42be7b8..2d68d59 100644 --- a/public_html/libgit/class.tag.php +++ b/public_html/libgit/class.tag.php @@ -54,4 +54,14 @@ class GitTag extends GitObject $this->message = implode("\n", $message_parts); } + + function GetCommit() + { + return $this->repo->GetObject($this->target); + } + + function GetTree() + { + return $this->GetCommit()->GetTree(); + } } diff --git a/public_html/test.php b/public_html/test.php index cef38a2..56bf6d7 100644 --- a/public_html/test.php +++ b/public_html/test.php @@ -2,7 +2,7 @@ require("libgit/base.php"); $repo = new GitRepository("/home/occupy/testrepo.git"); -pretty_dump($repo->GetBranch("master")->GetLastCommit()); +pretty_dump($repo->GetTag("1.0")->GetCommit()); /*pretty_dump($s->GetObject("54e03e490b1bee1c154c3545bf258cab0629ee02")); pretty_dump($s->GetObject("98d99489382a3541e6783bb2083554785f3eb72a"));