Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:joaopaulovieira/gandalf
Browse files Browse the repository at this point in the history
Conflicts:
	repository/mocks.go
  • Loading branch information
joaopaulovieira committed Jul 25, 2014
2 parents 2b3d41b + 10461ab commit 18a0b71
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 56 deletions.
22 changes: 22 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,25 @@ func GetTree(w http.ResponseWriter, r *http.Request) {
}
w.Write(b)
}

func GetBranch(w http.ResponseWriter, r *http.Request) {
repo := r.URL.Query().Get(":name")
if repo == "" {
err := fmt.Errorf("Error when trying to obtain the branches of repository %s (repository is required).", repo)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
branches, err := repository.GetBranch(repo)
if err != nil {
err := fmt.Errorf("Error when trying to obtain the branches of repository %s (%s).", repo, err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
b, err := json.Marshal(branches)
if err != nil {
err := fmt.Errorf("Error when trying to obtain the branches of repository %s (%s).", repo, err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(b)
}
75 changes: 75 additions & 0 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,3 +971,78 @@ func (s *S) TestGetTreeWhenCommandFails(c *gocheck.C) {
c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest)
c.Assert(recorder.Body.String(), gocheck.Equals, "Error when trying to obtain tree for path /test on ref master of repository repo (output error).\n")
}

func (s *S) TestGetBranch(c *gocheck.C) {
url := "/repository/repo/branch?:name=repo"
refs := make([]map[string]string, 1)
refs[0] = make(map[string]string)
refs[0]["ref"] = "a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9"
refs[0]["name"] = "doge_barks"
refs[0]["commiterName"] = "doge"
refs[0]["commiterEmail"] = "<much@email.com>"
refs[0]["authorName"] = "doge"
refs[0]["authorEmail"] = "<much@email.com>"
refs[0]["subject"] = "will bark"
mockRetriever := repository.MockContentRetriever{
Refs: refs,
}
repository.Retriever = &mockRetriever
defer func() {
repository.Retriever = nil
}()
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetBranch(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
var obj []map[string]string
json.Unmarshal(recorder.Body.Bytes(), &obj)
c.Assert(len(obj), gocheck.Equals, 1)
c.Assert(obj[0]["ref"], gocheck.Equals, refs[0]["ref"])
c.Assert(obj[0]["name"], gocheck.Equals, refs[0]["name"])
c.Assert(obj[0]["commiterName"], gocheck.Equals, refs[0]["commiterName"])
c.Assert(obj[0]["commiterEmail"], gocheck.Equals, refs[0]["commiterEmail"])
c.Assert(obj[0]["authorName"], gocheck.Equals, refs[0]["authorName"])
c.Assert(obj[0]["authorEmail"], gocheck.Equals, refs[0]["authorEmail"])
c.Assert(obj[0]["subject"], gocheck.Equals, refs[0]["subject"])
}

func (s *S) TestGetBranchWhenRepoNotSupplied(c *gocheck.C) {
url := "/repository//branch?:name="
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetBranch(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest)
expected := "Error when trying to obtain the branches of repository (repository is required).\n"
c.Assert(recorder.Body.String(), gocheck.Equals, expected)
}

func (s *S) TestGetBranchWhenRepoNonExistent(c *gocheck.C) {
url := "/repository/repo/branch?:name=repo"
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetBranch(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest)
expected := "Error when trying to obtain the branches of repository repo (Error when trying to obtain the refs of repository repo (Repository does not exist).).\n"
c.Assert(recorder.Body.String(), gocheck.Equals, expected)
}

func (s *S) TestGetBranchWhenCommandFails(c *gocheck.C) {
url := "/repository/repo/branch/?:name=repo"
expected := fmt.Errorf("output error")
mockRetriever := repository.MockContentRetriever{
OutputError: expected,
}
repository.Retriever = &mockRetriever
defer func() {
repository.Retriever = nil
}()
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetBranch(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest)
c.Assert(recorder.Body.String(), gocheck.Equals, "Error when trying to obtain the branches of repository repo (output error).\n")
}
17 changes: 17 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,20 @@ Example URLs (http://gandalf-server omitted for clarity)::
$ curl /repository/myrepository/archive/master.zip # gets master and zip format
$ curl /repository/myrepository/archive/master.tar.gz # gets master and tar.gz format
$ curl /repository/myrepository/archive/0.1.0.zip # gets 0.1.0 tag and zip format

Get branch
-----------

Returns a list of all the branches of the specified `repository`.

* Method: GET
* URI: /repository/`:name`/branch
* Format: JSON

Where:

* `:name` is the name of the repository.

Example URL (http://gandalf-server omitted for clarity)::

$ curl /repository/myrepository/branch # gets list of branches
29 changes: 2 additions & 27 deletions repository/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func CreateTestRepository(tmp_path string, repo string, file string, content str
return cleanup, err
}

func CreateCommitOnTestRepository(tmpPath string, repo string, file string, content string) ([]byte, error) {
func CreateCommitOnTestRepository(tmpPath, repo, file, content string) ([]byte, error) {
testPath := path.Join(tmpPath, repo+".git")
gitPath, err := exec.LookPath("git")
if err != nil {
Expand Down Expand Up @@ -134,7 +134,7 @@ func CreateCommitOnTestRepository(tmpPath string, repo string, file string, cont
return out, nil
}

func CreateBranchesOnTestRepository(tmp_path string, repo string, file string, content string, branches ...string) error {
func CreateBranchesOnTestRepository(tmp_path string, repo string, branches ...string) error {
testPath := path.Join(tmp_path, repo+".git")
gitPath, err := exec.LookPath("git")
if err != nil {
Expand All @@ -147,37 +147,12 @@ func CreateBranchesOnTestRepository(tmp_path string, repo string, file string, c
return err
}
for _, branch := range branches {
fp, err := os.OpenFile(path.Join(testPath, file), os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer fp.Close()
_, err = fp.WriteString("such string")
if err != nil {
return err
}
cmd = exec.Command(gitPath, "checkout", "-b", branch)
cmd.Dir = testPath
err = cmd.Run()
if err != nil {
return err
}
cmd = exec.Command(gitPath, "add", ".")
cmd.Dir = testPath
err = cmd.Run()
if err != nil {
return err
}
if len(content) > 0 {
cmd = exec.Command(gitPath, "commit", "-m", content+" on "+branch)
} else {
cmd = exec.Command(gitPath, "commit", "-m", "", "--allow-empty-message")
}
cmd.Dir = testPath
err = cmd.Run()
if err != nil {
return err
}
}
return err
}
Expand Down
18 changes: 11 additions & 7 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,22 @@ func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]st
var ref, name, commiterName, commiterEmail, commiterDate, authorName, authorEmail, authorDate, subject string
gitPath, err := exec.LookPath("git")
if err != nil {
return nil, fmt.Errorf("Error when trying to obtain the branches of repository %s (%s).", repo, err)
return nil, fmt.Errorf("Error when trying to obtain the refs of repository %s (%s).", repo, err)
}
cwd := barePath(repo)
repoExists, err := exists(cwd)
if err != nil || !repoExists {
return nil, fmt.Errorf("Error when trying to obtain the branches of repository %s (Repository does not exist).", repo)
return nil, fmt.Errorf("Error when trying to obtain the refs of repository %s (Repository does not exist).", repo)
}
format := "%(objectname)%09%(refname:short)%09%(committername)%09%(committeremail)%09%(committerdate)%09%(authorname)%09%(authoremail)%09%(authordate)%09%(contents:subject)"
cmd := exec.Command(gitPath, "for-each-ref", "--sort=-committerdate", "--format", format)
if len(pattern) > 0 {
cmd.Args = append(cmd.Args, pattern)
}
cmd := exec.Command(gitPath, "for-each-ref", "--sort=-committerdate", "--format", "%(objectname)%09%(refname)%09%(committername)%09%(committeremail)%09%(committerdate)%09%(authorname)%09%(authoremail)%09%(authordate)%09%(contents:subject)", pattern)
cmd.Dir = cwd
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("Error when trying to obtain the branches of repository %s (%s).", repo, err)
return nil, fmt.Errorf("Error when trying to obtain the refs of repository %s (%s).", repo, err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
objectCount := len(lines)
Expand All @@ -378,9 +382,9 @@ func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]st
continue
}
fields := strings.Split(line, "\t")
if len(fields) > 4 { // let there be commits with empty subject
if len(fields) > 7 { // let there be commits with empty subject
ref = fields[0]
name = strings.Replace(fields[1], pattern, "", 1)
name = fields[1]
commiterName = fields[2]
commiterEmail = fields[3]
commiterDate = fields[4]
Expand All @@ -389,7 +393,7 @@ func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]st
authorDate = fields[7]
subject = strings.Join(fields[8:], "\t") // let there be subjects with \t
} else {
return nil, fmt.Errorf("Error when trying to obtain the branches of repository %s (Invalid git for-each-ref output [%s]).", repo, out)
return nil, fmt.Errorf("Error when trying to obtain the refs of repository %s (Invalid git for-each-ref output [%s]).", repo, out)
}
object := make(map[string]string)
object["ref"] = ref
Expand Down
Loading

0 comments on commit 18a0b71

Please sign in to comment.