Add support for +refs Change-Id: I828efe243c12c66c134a1f12f69499fa96860220
diff --git a/gitiles/client.go b/gitiles/client.go index 7fec489..9ecf39b 100644 --- a/gitiles/client.go +++ b/gitiles/client.go
@@ -365,3 +365,22 @@ panic("unreachable.") } + +// Refs returns the refs of a repository, optionally filtered by prefix. +func (s *RepoService) Refs(prefix string) (map[string]*RefData, error) { + + jsonURL := s.service.addr + jsonURL.Path = path.Join(jsonURL.Path, s.Name, "+refs") + if prefix != "" { + jsonURL.Path = path.Join(jsonURL.Path, prefix) + } + jsonURL.RawQuery = "format=JSON" + + result := map[string]*RefData{} + err := s.service.getJSON(&jsonURL, &result) + if err != nil { + return nil, err + } + + return result, err +}
diff --git a/gitiles/prod_test.go b/gitiles/prod_test.go index c4fc68a..43ccbef 100644 --- a/gitiles/prod_test.go +++ b/gitiles/prod_test.go
@@ -81,3 +81,25 @@ t.Fatalf("got %v, want %v", got, want) } } + +func TestProductionRefs(t *testing.T) { + gs, err := NewService(Options{ + Address: "https://gerrit.googlesource.com", + }) + if err != nil { + t.Fatal(err) + } + + repo := gs.NewRepoService("gitiles") + if err != nil { + t.Fatal(err) + } + got, err := repo.Refs("refs/heads") + if err != nil { + t.Fatal(err) + } + + if got["master"] == nil { + t.Errorf("got %v, want key 'master'", got) + } +}
diff --git a/gitiles/types.go b/gitiles/types.go index 56a3da5..ab9721d 100644 --- a/gitiles/types.go +++ b/gitiles/types.go
@@ -117,3 +117,15 @@ return buf.String() } + +// A git reference +type RefData struct { + // The value to which a reference points. + Value string + + // If the value points to a tag, the commit that the tag points to. + Peeled string + + // If the ref is symbolic, eg. HEAD, the ref to which it points. + Target string +}