From 7ee20fd2f642a285fb8c226f074cf4b71d6d2b28 Mon Sep 17 00:00:00 2001 From: Guilherme Heynemann Bruzzi Date: Tue, 26 Aug 2014 20:04:48 -0300 Subject: [PATCH] Adding the path option to repository log --- api/handler.go | 3 ++- api/handler_test.go | 43 +++++++++++++++++++++++++++++++++++ repository/mocks.go | 2 +- repository/repository.go | 10 ++++---- repository/repository_test.go | 39 ++++++++++++++++++++++++++++--- 5 files changed, 87 insertions(+), 10 deletions(-) diff --git a/api/handler.go b/api/handler.go index f4084a1..4827ace 100644 --- a/api/handler.go +++ b/api/handler.go @@ -500,6 +500,7 @@ func Commit(w http.ResponseWriter, r *http.Request) { func GetLog(w http.ResponseWriter, r *http.Request) { repo := r.URL.Query().Get(":name") ref := r.URL.Query().Get("ref") + path := r.URL.Query().Get("path") total, err := strconv.Atoi(r.URL.Query().Get("total")) if err != nil { err := fmt.Errorf("Error when trying to obtain log for ref %s of repository %s (%s).", ref, repo, err) @@ -511,7 +512,7 @@ func GetLog(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - logs, err := repository.GetLog(repo, ref, total) + logs, err := repository.GetLog(repo, ref, total, path) if err != nil { err := fmt.Errorf("Error when trying to obtain log for ref %s of repository %s (%s).", ref, repo, err) http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/api/handler_test.go b/api/handler_test.go index d50b69d..e2fcafe 100644 --- a/api/handler_test.go +++ b/api/handler_test.go @@ -1501,3 +1501,46 @@ func (s *S) TestLog(c *gocheck.C) { c.Assert(obj.Commits, gocheck.HasLen, 1) c.Assert(obj.Commits[0], gocheck.DeepEquals, commits[0]) } + +func (s *S) TestLogWithPath(c *gocheck.C) { + url := "/repository/repo/logs?&:name=repo&ref=HEAD&total=1&path=README.txt" + objects := repository.GitHistory{} + parent := make([]string, 2) + parent[0] = "a367b5de5943632e47cb6f8bf5b2147bc0be5cf8" + parent[1] = "b267b5de5943632e47cb6f8bf5b2147bc0be5cf2" + commits := make([]repository.GitLog, 1) + commits[0] = repository.GitLog{ + Ref: "a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9", + CreatedAt: "Mon Jul 28 10:13:27 2014 -0300", + Committer: &repository.GitUser{ + Name: "doge", + Email: "much@email.com", + }, + Author: &repository.GitUser{ + Name: "doge", + Email: "much@email.com", + }, + Subject: "will bark", + Parent: parent, + } + objects.Commits = commits + objects.Next = "b231c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9" + mockRetriever := repository.MockContentRetriever{ + History: objects, + } + repository.Retriever = &mockRetriever + defer func() { + repository.Retriever = nil + }() + request, err := http.NewRequest("GET", url, nil) + c.Assert(err, gocheck.IsNil) + recorder := httptest.NewRecorder() + GetLog(recorder, request) + c.Assert(recorder.Code, gocheck.Equals, http.StatusOK) + var obj repository.GitHistory + json.Unmarshal(recorder.Body.Bytes(), &obj) + c.Assert(obj.Next, gocheck.Equals, "b231c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9") + c.Assert(obj.Commits, gocheck.HasLen, 1) + c.Assert(obj.Commits[0], gocheck.DeepEquals, commits[0]) +} + diff --git a/repository/mocks.go b/repository/mocks.go index dc995d2..e3aff48 100644 --- a/repository/mocks.go +++ b/repository/mocks.go @@ -397,7 +397,7 @@ func (r *MockContentRetriever) CommitZip(repo string, z *multipart.FileHeader, c return &r.Ref, nil } -func (r *MockContentRetriever) GetLog(repo, hash string, total int) (*GitHistory, error) { +func (r *MockContentRetriever) GetLog(repo, hash string, total int, path string) (*GitHistory, error) { if r.LookPathError != nil { return nil, r.LookPathError } diff --git a/repository/repository.go b/repository/repository.go index 567064a..5b7cc64 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -331,7 +331,7 @@ type ContentRetriever interface { Commit(cloneDir, message string, author GitUser) error Push(cloneDir, branch string) error CommitZip(repo string, z *multipart.FileHeader, c GitCommit) (*Ref, error) - GetLog(repo, hash string, total int) (*GitHistory, error) + GetLog(repo, hash string, total int, path string) (*GitHistory, error) } var Retriever ContentRetriever @@ -704,7 +704,7 @@ func (*GitContentRetriever) CommitZip(repo string, z *multipart.FileHeader, c Gi return nil, fmt.Errorf("Error when trying to commit zip to repository %s, could not check branch: %s", repo, err) } -func (*GitContentRetriever) GetLog(repo, hash string, total int) (*GitHistory, error) { +func (*GitContentRetriever) GetLog(repo, hash string, total int, path string) (*GitHistory, error) { if total < 1 { total = 1 } @@ -720,7 +720,7 @@ func (*GitContentRetriever) GetLog(repo, hash string, total int) (*GitHistory, e return nil, fmt.Errorf("Error when trying to obtain the log of repository %s (Repository does not exist).", repo) } format := "%H%x09%an%x09%ae%x09%ad%x09%cn%x09%ce%x09%cd%x09%P%x09%s" - cmd := exec.Command(gitPath, "--no-pager", "log", fmt.Sprintf("-n %d", totalPagination), fmt.Sprintf("--format=%s", format), hash) + cmd := exec.Command(gitPath, "--no-pager", "log", fmt.Sprintf("-n %d", totalPagination), fmt.Sprintf("--format=%s", format), hash, "--", path) cmd.Dir = cwd out, err := cmd.Output() if err != nil { @@ -860,6 +860,6 @@ func CommitZip(repo string, z *multipart.FileHeader, c GitCommit) (*Ref, error) return retriever().CommitZip(repo, z, c) } -func GetLog(repo, hash string, total int) (*GitHistory, error) { - return retriever().GetLog(repo, hash, total) +func GetLog(repo, hash string, total int, path string) (*GitHistory, error) { + return retriever().GetLog(repo, hash, total, path) } diff --git a/repository/repository_test.go b/repository/repository_test.go index b1fc40a..ed4d115 100644 --- a/repository/repository_test.go +++ b/repository/repository_test.go @@ -1901,7 +1901,7 @@ func (s *S) TestGetLog(c *gocheck.C) { c.Assert(errCreateCommit, gocheck.IsNil) errCreateCommit = CreateCommit(bare, repo, file, object2) c.Assert(errCreateCommit, gocheck.IsNil) - history, err := GetLog(repo, "HEAD", 1) + history, err := GetLog(repo, "HEAD", 1, "") c.Assert(err, gocheck.IsNil) c.Assert(history.Commits, gocheck.HasLen, 1) c.Assert(history.Commits[0].Ref, gocheck.Matches, "[a-f0-9]{40}") @@ -1915,7 +1915,7 @@ func (s *S) TestGetLog(c *gocheck.C) { c.Assert(history.Commits[0].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date) c.Assert(history.Next, gocheck.Matches, "[a-f0-9]{40}") // Next - history, err = GetLog(repo, history.Next, 1) + history, err = GetLog(repo, history.Next, 1, "") c.Assert(err, gocheck.IsNil) c.Assert(history.Commits, gocheck.HasLen, 1) c.Assert(history.Commits[0].Ref, gocheck.Matches, "[a-f0-9]{40}") @@ -1929,7 +1929,7 @@ func (s *S) TestGetLog(c *gocheck.C) { c.Assert(history.Commits[0].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date) c.Assert(history.Next, gocheck.Matches, "[a-f0-9]{40}") // Next - history, err = GetLog(repo, history.Next, 1) + history, err = GetLog(repo, history.Next, 1, "") c.Assert(err, gocheck.IsNil) c.Assert(history.Commits, gocheck.HasLen, 1) c.Assert(history.Commits[0].Ref, gocheck.Matches, "[a-f0-9]{40}") @@ -1943,3 +1943,36 @@ func (s *S) TestGetLog(c *gocheck.C) { c.Assert(history.Commits[0].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date) c.Assert(history.Next, gocheck.Equals, "") } + +func (s *S) TestGetLogWithFile(c *gocheck.C) { + oldBare := bare + bare = "/tmp" + repo := "gandalf-test-repo" + file := "README" + content := "will\tbark" + object1 := "You should read this README" + object2 := "Seriously, read this file!" + cleanUp, errCreate := CreateTestRepository(bare, repo, file, content) + defer func() { + cleanUp() + bare = oldBare + }() + c.Assert(errCreate, gocheck.IsNil) + errCreateCommit := CreateCommit(bare, repo, file, object1) + c.Assert(errCreateCommit, gocheck.IsNil) + errCreateCommit = CreateCommit(bare, repo, file, object2) + c.Assert(errCreateCommit, gocheck.IsNil) + history, err := GetLog(repo, "master", 1, "README") + c.Assert(err, gocheck.IsNil) + c.Assert(history.Commits, gocheck.HasLen, 1) + c.Assert(history.Commits[0].Ref, gocheck.Matches, "[a-f0-9]{40}") + c.Assert(history.Commits[0].Parent, gocheck.HasLen, 1) + c.Assert(history.Commits[0].Parent[0], gocheck.Matches, "[a-f0-9]{40}") + c.Assert(history.Commits[0].Committer.Name, gocheck.Equals, "doge") + c.Assert(history.Commits[0].Committer.Email, gocheck.Equals, "much@email.com") + c.Assert(history.Commits[0].Author.Name, gocheck.Equals, "doge") + c.Assert(history.Commits[0].Author.Email, gocheck.Equals, "much@email.com") + c.Assert(history.Commits[0].Subject, gocheck.Equals, "Seriously, read this file!") + c.Assert(history.Commits[0].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date) + c.Assert(history.Next, gocheck.Matches, "[a-f0-9]{40}") +}