aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorAlberto Cortés <alcortesm@gmail.com>2016-08-02 10:58:49 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-02 10:58:49 +0200
commit5413c7aeadb7cb18a6d51dae0bc313f2e129a337 (patch)
tree9c4f02ea046f23974075856126e439ec270a74ab /repository.go
parentdc1e2bd485f8345c14cf7b22a5b71fd03028cfdf (diff)
downloadgo-git-5413c7aeadb7cb18a6d51dae0bc313f2e129a337.tar.gz
Repository head (#61)v3.1.1
* add Repository.Head() tests * add head support for remote repos and more tests * add head support for local repos * clean code * remove dead code
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/repository.go b/repository.go
index c05afdb..866f9ff 100644
--- a/repository.go
+++ b/repository.go
@@ -218,3 +218,32 @@ func (r *Repository) Object(h core.Hash) (Object, error) {
return nil, core.ErrInvalidType
}
}
+
+// Head returns the hash of the HEAD of the repository or the head of a
+// remote, if one is passed.
+func (r *Repository) Head(remote string) (core.Hash, error) {
+ if remote == "" {
+ return r.localHead()
+ }
+
+ return r.remoteHead(remote)
+}
+
+func (r *Repository) remoteHead(remote string) (core.Hash, error) {
+ rem, ok := r.Remotes[remote]
+ if !ok {
+ return core.ZeroHash, fmt.Errorf("unable to find remote %q", remote)
+ }
+
+ return rem.Head()
+}
+
+func (r *Repository) localHead() (core.Hash, error) {
+ storage, ok := r.Storage.(*seekable.ObjectStorage)
+ if !ok {
+ return core.ZeroHash,
+ fmt.Errorf("cannot retrieve local head: no local data found")
+ }
+
+ return storage.Head()
+}