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

Commit

Permalink
Merge pull request #157 from guilhermebruzzi/gandalf-pull
Browse files Browse the repository at this point in the history
Adding the path option to repository log
  • Loading branch information
andrewsmedina committed Aug 27, 2014
2 parents 2b38b8a + 7ee20fd commit 91d6ed9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
3 changes: 2 additions & 1 deletion api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

2 changes: 1 addition & 1 deletion repository/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,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
Expand Down Expand Up @@ -708,7 +708,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
}
Expand All @@ -724,7 +724,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 {
Expand Down Expand Up @@ -864,6 +864,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)
}
39 changes: 36 additions & 3 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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}")
Expand All @@ -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}")
Expand All @@ -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}")
}

0 comments on commit 91d6ed9

Please sign in to comment.