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

Commit

Permalink
Fix some bugs on repository.GetLogs
Browse files Browse the repository at this point in the history
Also increase its coverage
  • Loading branch information
scorphus committed Sep 1, 2014
1 parent 7841932 commit 35f9ec8
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 30 deletions.
54 changes: 27 additions & 27 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,14 @@ func (*GitContentRetriever) CommitZip(repo string, z *multipart.FileHeader, c Gi
}

func (*GitContentRetriever) GetLogs(repo, hash string, total int, path string) (*GitHistory, error) {
if hash == "" {
hash = "master"
}
if total < 1 {
total = 1
}
totalPagination := total + 1
var last, ref, committerName, committerEmail, committerDate, authorName, authorEmail, authorDate, subject, parent string
var last string
gitPath, err := exec.LookPath("git")
if err != nil {
return nil, fmt.Errorf("Error when trying to obtain the log of repository %s (%s).", repo, err)
Expand Down Expand Up @@ -754,46 +757,43 @@ func (*GitContentRetriever) GetLogs(repo, hash string, total int, path string) (
commits := make([]GitLog, objectCount)
objectCount = 0
for _, line := range lines {
var parent, subject string
if strings.TrimSpace(line) == "" {
continue
}
fields := strings.Split(line, "\t")
if len(fields) > 8 { // let there be commits with empty subject
ref = fields[0]
authorName = fields[1]
authorEmail = fields[2]
authorDate = fields[3]
committerName = fields[4]
committerEmail = fields[5]
committerDate = fields[6]
if len(fields) < 7 { // let there be commits with empty subject and no parents
return nil, fmt.Errorf("Error when trying to obtain the log of repository %s (Invalid git log output [%s]).", repo, out)
}
if len(fields) > 8 {
parent = 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 log of repository %s (Invalid git log output [%s]).", repo, out)
}
commit := GitLog{}
commit.Ref = ref
commit.Ref = fields[0]
commit.Subject = subject
commit.CreatedAt = authorDate
commit.CreatedAt = fields[3]
commit.Committer = &GitUser{
Name: committerName,
Email: committerEmail,
Date: committerDate,
Name: fields[4],
Email: fields[5],
Date: fields[6],
}
commit.Author = &GitUser{
Name: authorName,
Email: authorEmail,
Date: authorDate,
Name: fields[1],
Email: fields[2],
Date: fields[3],
}
parents := strings.Split(parent, " ")
parentCount := len(parents)
aux := make([]string, parentCount)
parentCount = 0
for _, item := range parents {
aux[parentCount] = item
parentCount++
if len(parent) > 0 {
parents := strings.Split(parent, " ")
parentCount := len(parents)
aux := make([]string, parentCount)
parentCount = 0
for _, item := range parents {
aux[parentCount] = item
parentCount++
}
commit.Parent = aux
}
commit.Parent = aux
commits[objectCount] = commit
objectCount++
}
Expand Down
147 changes: 144 additions & 3 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1997,8 +1997,7 @@ func (s *S) TestGetLogs(c *gocheck.C) {
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.Equals, "")
c.Assert(history.Commits[0].Parent, gocheck.HasLen, 0)
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")
Expand All @@ -2013,7 +2012,7 @@ func (s *S) TestGetLogsWithFile(c *gocheck.C) {
bare = "/tmp"
repo := "gandalf-test-repo"
file := "README"
content := "will\tbark"
content := "will bark"
object1 := "You should read this README"
object2 := "Seriously, read this file!"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content)
Expand All @@ -2040,3 +2039,145 @@ func (s *S) TestGetLogsWithFile(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}")
}

func (s *S) TestGetLogsWithFileAndEmptyParameters(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "README"
content := "will bark"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
history, err := GetLogs(repo, "", 0, "")
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, 0)
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, "will bark")
c.Assert(history.Commits[0].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date)
c.Assert(history.Next, gocheck.Equals, "")
}

func (s *S) TestGetLogsWithAllSortsOfSubjects(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "README"
content1 := ""
content2 := "will\tbark"
content3 := "will bark"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content1)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
errCreateCommit := CreateCommit(bare, repo, file, content2)
c.Assert(errCreateCommit, gocheck.IsNil)
errCreateCommit = CreateCommit(bare, repo, file, content3)
c.Assert(errCreateCommit, gocheck.IsNil)
history, err := GetLogs(repo, "master", 3, "README")
c.Assert(err, gocheck.IsNil)
c.Assert(history.Commits, gocheck.HasLen, 3)
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, "will bark")
c.Assert(history.Commits[0].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date)
c.Assert(history.Commits[1].Ref, gocheck.Matches, "[a-f0-9]{40}")
c.Assert(history.Commits[1].Parent, gocheck.HasLen, 1)
c.Assert(history.Commits[1].Parent[0], gocheck.Matches, "[a-f0-9]{40}")
c.Assert(history.Commits[1].Committer.Name, gocheck.Equals, "doge")
c.Assert(history.Commits[1].Committer.Email, gocheck.Equals, "much@email.com")
c.Assert(history.Commits[1].Author.Name, gocheck.Equals, "doge")
c.Assert(history.Commits[1].Author.Email, gocheck.Equals, "much@email.com")
c.Assert(history.Commits[1].Subject, gocheck.Equals, "will\tbark")
c.Assert(history.Commits[1].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date)
c.Assert(history.Commits[2].Ref, gocheck.Matches, "[a-f0-9]{40}")
c.Assert(history.Commits[2].Parent, gocheck.HasLen, 0)
c.Assert(history.Commits[2].Committer.Name, gocheck.Equals, "doge")
c.Assert(history.Commits[2].Committer.Email, gocheck.Equals, "much@email.com")
c.Assert(history.Commits[2].Author.Name, gocheck.Equals, "doge")
c.Assert(history.Commits[2].Author.Email, gocheck.Equals, "much@email.com")
c.Assert(history.Commits[2].Subject, gocheck.Equals, "")
c.Assert(history.Commits[2].CreatedAt, gocheck.Equals, history.Commits[0].Author.Date)
c.Assert(history.Next, gocheck.Equals, "")
}

func (s *S) TestGetLogsWhenOutputInvalid(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "README"
content := "much WOW"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
tmpdir, err := commandmocker.Add("git", "-")
c.Assert(err, gocheck.IsNil)
defer commandmocker.Remove(tmpdir)
_, err = GetLogs(repo, "master", 3, "README")
c.Assert(err.Error(), gocheck.Equals, "Error when trying to obtain the log of repository gandalf-test-repo (Invalid git log output [-]).")
}

func (s *S) TestGetLogsWhenOutputEmpty(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "README"
content := "much WOW"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
tmpdir, err := commandmocker.Add("git", "\n")
c.Assert(err, gocheck.IsNil)
defer commandmocker.Remove(tmpdir)
history, err := GetLogs(repo, "master", 1, "README")
c.Assert(err, gocheck.IsNil)
c.Assert(history.Commits, gocheck.HasLen, 0)
c.Assert(history.Next, gocheck.HasLen, 0)
}

func (s *S) TestGetLogsWhenGitError(c *gocheck.C) {
oldBare := bare
bare = "/tmp"
repo := "gandalf-test-repo"
file := "README"
content := "much WOW"
cleanUp, errCreate := CreateTestRepository(bare, repo, file, content)
defer func() {
cleanUp()
bare = oldBare
}()
c.Assert(errCreate, gocheck.IsNil)
tmpdir, err := commandmocker.Error("git", "much error", 1)
c.Assert(err, gocheck.IsNil)
defer commandmocker.Remove(tmpdir)
expectedErr := fmt.Sprintf("Error when trying to obtain the log of repository %s (exit status 1).", repo)
_, err = GetLogs(repo, "master", 1, "README")
c.Assert(err.Error(), gocheck.Equals, expectedErr)
}

func (s *S) TestGetLogsWhenRepoInvalid(c *gocheck.C) {
expectedErr := fmt.Sprintf("Error when trying to obtain the log of repository invalid-repo (Repository does not exist).")
_, err := GetLogs("invalid-repo", "master", 1, "README")
c.Assert(err.Error(), gocheck.Equals, expectedErr)
}

0 comments on commit 35f9ec8

Please sign in to comment.