diff --git a/api/handler.go b/api/handler.go index 2b78b7f..cf30d52 100644 --- a/api/handler.go +++ b/api/handler.go @@ -71,7 +71,7 @@ func SetupRouter() *pat.Router { router.Get("/repository/{name:[^/]*/?[^/]+}/tags", http.HandlerFunc(getTags)) router.Get("/repository/{name:[^/]*/?[^/]+}/diff/commits", http.HandlerFunc(getDiff)) router.Post("/repository/{name:[^/]*/?[^/]+}/commit", http.HandlerFunc(commit)) - router.Get("/repository/{name:[^/]*/?[^/]+}/logs", http.HandlerFunc(getLog)) + router.Get("/repository/{name:[^/]*/?[^/]+}/logs", http.HandlerFunc(getLogs)) router.Post("/repository/grant", http.HandlerFunc(grantAccess)) router.Post("/repository", http.HandlerFunc(newRepository)) router.Get("/repository/{name:[^/]*/?[^/]+}", http.HandlerFunc(getRepository)) @@ -508,25 +508,25 @@ func commit(w http.ResponseWriter, r *http.Request) { w.Write(b) } -func getLog(w http.ResponseWriter, r *http.Request) { +func getLogs(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) + err := fmt.Errorf("Error when trying to obtain logs for ref %s of repository %s (%s).", ref, repo, err) http.Error(w, err.Error(), http.StatusBadRequest) return } - logs, err := repository.GetLog(repo, ref, total, path) + logs, err := repository.GetLogs(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) + err := fmt.Errorf("Error when trying to obtain logs for ref %s of repository %s (%s).", ref, repo, err) http.Error(w, err.Error(), http.StatusBadRequest) return } b, err := json.Marshal(logs) if err != nil { - err := fmt.Errorf("Error when trying to obtain log for ref %s of repository %s (%s).", ref, repo, err) + err := fmt.Errorf("Error when trying to obtain logs for ref %s of repository %s (%s).", ref, repo, err) http.Error(w, err.Error(), http.StatusInternalServerError) return } diff --git a/api/handler_test.go b/api/handler_test.go index 2532c6d..6ef3c8f 100644 --- a/api/handler_test.go +++ b/api/handler_test.go @@ -226,7 +226,7 @@ func (s *S) TestGetRepositoryWithNamespace(c *gocheck.C) { } func (s *S) TestGetRepositoryDoesNotExist(c *gocheck.C) { - recorder, request := get("/repository/doesnotexists", nil, c) + recorder, request := get("/repository/doesnotexist", nil, c) s.router.ServeHTTP(recorder, request) c.Assert(recorder.Code, gocheck.Equals, 500) } @@ -623,7 +623,7 @@ func (s *S) TestAddInvalidOldFormatHook(c *gocheck.C) { c.Assert(recorder.Code, gocheck.Equals, 400) } -func (s *S) TestAddKeyShouldReturnErrorWhenUserDoesNotExists(c *gocheck.C) { +func (s *S) TestAddKeyShouldReturnErrorWhenUserDoesNotExist(c *gocheck.C) { b := strings.NewReader(`{"key": "a public key"}`) recorder, request := post("/user/Frodo/key", b, c) s.router.ServeHTTP(recorder, request) @@ -872,7 +872,7 @@ func (s *S) TestRemoveRepositoryShouldReturn400OnFailure(c *gocheck.C) { c.Assert(recorder.Code, gocheck.Equals, 400) } -func (s *S) TestRemoveRepositoryShouldReturnErrorMsgWhenRepoDoesNotExists(c *gocheck.C) { +func (s *S) TestRemoveRepositoryShouldReturnErrorMsgWhenRepoDoesNotExist(c *gocheck.C) { url := "/repository/foo" request, err := http.NewRequest("DELETE", url, nil) c.Assert(err, gocheck.IsNil) @@ -1509,7 +1509,7 @@ func (s *S) TestPostNewCommitWithEmptyBranch(c *gocheck.C) { c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest) } -func (s *S) TestLog(c *gocheck.C) { +func (s *S) TestLogs(c *gocheck.C) { url := "/repository/repo/logs?ref=HEAD&total=1" objects := repository.GitHistory{} parent := make([]string, 2) @@ -1551,7 +1551,7 @@ func (s *S) TestLog(c *gocheck.C) { c.Assert(obj.Commits[0], gocheck.DeepEquals, commits[0]) } -func (s *S) TestLogWithPath(c *gocheck.C) { +func (s *S) TestLogsWithPath(c *gocheck.C) { url := "/repository/repo/logs?ref=HEAD&total=1&path=README.txt" objects := repository.GitHistory{} parent := make([]string, 2) diff --git a/api/utils_test.go b/api/utils_test.go index ec96442..8208527 100644 --- a/api/utils_test.go +++ b/api/utils_test.go @@ -70,7 +70,7 @@ func (s *S) TestGetUserOr404(c *gocheck.C) { c.Assert(rUser.Name, gocheck.Equals, "umi") } -func (s *S) TestGetUserOr404ShouldReturn404WhenUserDoesntExists(c *gocheck.C) { +func (s *S) TestGetUserOr404ShouldReturn404WhenUserDoesntExist(c *gocheck.C) { _, e := getUserOr404("umi") expected := "User umi not found" got := e.Error() diff --git a/bin/gandalf_test.go b/bin/gandalf_test.go index 4167382..322ae8c 100644 --- a/bin/gandalf_test.go +++ b/bin/gandalf_test.go @@ -163,7 +163,7 @@ func (s *S) TestRequestedRepositoryShouldReturnErrorWhenThereIsNoCommandPassedTo c.Assert(err, gocheck.ErrorMatches, "^You've tried to execute some weird command, I'm deliberately denying you to do that, get over it.$") } -func (s *S) TestRequestedRepositoryShouldReturnFormatedErrorWhenRepositoryDoesNotExists(c *gocheck.C) { +func (s *S) TestRequestedRepositoryShouldReturnFormatedErrorWhenRepositoryDoesNotExist(c *gocheck.C) { os.Setenv("SSH_ORIGINAL_COMMAND", "git-receive-pack 'inexistent-repo.git'") defer os.Setenv("SSH_ORIGINAL_COMMAND", "") _, err := requestedRepository() @@ -248,7 +248,7 @@ func (s *S) TestExecuteActionShouldExecuteGitReceivePackWhenUserHasWritePermissi c.Assert(stdout.String(), gocheck.Equals, expected) } -func (s *S) TestExecuteActionShouldNotCallSSH_ORIGINAL_COMMANDWhenUserDoesNotExists(c *gocheck.C) { +func (s *S) TestExecuteActionShouldNotCallSSH_ORIGINAL_COMMANDWhenUserDoesNotExist(c *gocheck.C) { dir, err := commandmocker.Add("git-receive-pack", "$*") c.Check(err, gocheck.IsNil) defer commandmocker.Remove(dir) @@ -264,7 +264,7 @@ func (s *S) TestExecuteActionShouldNotCallSSH_ORIGINAL_COMMANDWhenUserDoesNotExi c.Assert(commandmocker.Ran(dir), gocheck.Equals, false) } -func (s *S) TestExecuteActionShouldNotCallSSH_ORIGINAL_COMMANDWhenRepositoryDoesNotExists(c *gocheck.C) { +func (s *S) TestExecuteActionShouldNotCallSSH_ORIGINAL_COMMANDWhenRepositoryDoesNotExist(c *gocheck.C) { dir, err := commandmocker.Add("git-receive-pack", "$*") c.Check(err, gocheck.IsNil) defer commandmocker.Remove(dir) diff --git a/docs/source/api.rst b/docs/source/api.rst index 4000f0a..ed68fae 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -167,8 +167,8 @@ Example URLs (http://gandalf-server omitted for clarity):: $ curl /repository/myrepository/archive?ref=master&format=tar.gz # gets master and tar.gz format $ curl /repository/myrepository/archive?ref=0.1.0&format=zip # gets 0.1.0 tag and zip format -Get branch ------------ +Get branches +------------ Returns a list of all the branches of the specified `repository`. @@ -207,8 +207,8 @@ Example URL (http://gandalf-server omitted for clarity):: $ curl /repository/myrepository/branches # gets list of branches -Get tag -------- +Get tags +-------- Returns a list of all the tags of the specified `repository`. @@ -341,7 +341,7 @@ Logs Returns a list of all commits into `repository`. * Method: GET -* URI: /repository/`:name`/log?ref=:ref&total=:total +* URI: /repository/`:name`/logs?ref=:ref&total=:total * Format: JSON Where: diff --git a/repository/mocks.go b/repository/mocks.go index e3aff48..45e34f0 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, path string) (*GitHistory, error) { +func (r *MockContentRetriever) GetLogs(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 1d102c5..e5c0c8f 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -286,7 +286,7 @@ func (r *Repository) isValid() (bool, error) { } // GrantAccess gives full or read-only permission for users in all specified repositories. -// If any of the repositories/users do not exists, GrantAccess just skips it. +// If any of the repositories/users does not exist, GrantAccess just skips it. func GrantAccess(rNames, uNames []string, readOnly bool) error { conn, err := db.Conn() if err != nil { @@ -345,7 +345,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, path string) (*GitHistory, error) + GetLogs(repo, hash string, total int, path string) (*GitHistory, error) } var Retriever ContentRetriever @@ -718,12 +718,15 @@ 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, path string) (*GitHistory, error) { +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) @@ -754,46 +757,43 @@ func (*GitContentRetriever) GetLog(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++ } @@ -874,6 +874,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, path string) (*GitHistory, error) { - return retriever().GetLog(repo, hash, total, path) +func GetLogs(repo, hash string, total int, path string) (*GitHistory, error) { + return retriever().GetLogs(repo, hash, total, path) } diff --git a/repository/repository_test.go b/repository/repository_test.go index ccaf2bb..0b9ac32 100644 --- a/repository/repository_test.go +++ b/repository/repository_test.go @@ -380,7 +380,7 @@ func (s *S) TestRemoveShouldRemoveRepositoryFromDatabase(c *gocheck.C) { c.Assert(err, gocheck.ErrorMatches, "^not found$") } -func (s *S) TestRemoveShouldReturnMeaningfulErrorWhenRepositoryDoesNotExistsInDatabase(c *gocheck.C) { +func (s *S) TestRemoveShouldReturnMeaningfulErrorWhenRepositoryDoesNotExistInDatabase(c *gocheck.C) { rfs := &fstesting.RecordingFs{FileContent: "foo"} fs.Fsystem = rfs defer func() { fs.Fsystem = nil }() @@ -939,7 +939,6 @@ func (s *S) TestGetArchiveIntegrationWhenZip(c *gocheck.C) { zipReader, err := zip.NewReader(reader, int64(len(zipContents))) c.Assert(err, gocheck.IsNil) for _, f := range zipReader.File { - //fmt.Printf("Contents of %s:\n", f.Name) rc, err := f.Open() c.Assert(err, gocheck.IsNil) defer rc.Close() @@ -1260,6 +1259,45 @@ func (s *S) TestGetForEachRefIntegrationWhenPatternInvalid(c *gocheck.C) { c.Assert(err.Error(), gocheck.Equals, "Error when trying to obtain the refs of repository gandalf-test-repo (exit status 129).") } +func (s *S) TestGetForEachRefOutputInvalid(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 = GetForEachRef(repo, "") + c.Assert(err.Error(), gocheck.Equals, "Error when trying to obtain the refs of repository gandalf-test-repo (Invalid git for-each-ref output [-]).") +} + +func (s *S) TestGetForEachRefOutputEmpty(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) + refs, err := GetForEachRef(repo, "") + c.Assert(err, gocheck.IsNil) + c.Assert(refs, gocheck.HasLen, 0) +} + func (s *S) TestGetDiffIntegration(c *gocheck.C) { oldBare := bare bare = "/tmp" @@ -1948,7 +1986,7 @@ func (s *S) TestCommitZipIntegrationWhenFileEmpty(c *gocheck.C) { c.Assert(err.Error(), gocheck.Equals, expectedErr) } -func (s *S) TestGetLog(c *gocheck.C) { +func (s *S) TestGetLogs(c *gocheck.C) { oldBare := bare bare = "/tmp" repo := "gandalf-test-repo" @@ -1966,7 +2004,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 := GetLogs(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}") @@ -1980,7 +2018,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 = GetLogs(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}") @@ -1994,12 +2032,11 @@ 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 = GetLogs(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}") - 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") @@ -2009,12 +2046,12 @@ func (s *S) TestGetLog(c *gocheck.C) { c.Assert(history.Next, gocheck.Equals, "") } -func (s *S) TestGetLogWithFile(c *gocheck.C) { +func (s *S) TestGetLogsWithFile(c *gocheck.C) { oldBare := bare 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) @@ -2027,7 +2064,7 @@ func (s *S) TestGetLogWithFile(c *gocheck.C) { c.Assert(errCreateCommit, gocheck.IsNil) errCreateCommit = CreateCommit(bare, repo, file, object2) c.Assert(errCreateCommit, gocheck.IsNil) - history, err := GetLog(repo, "master", 1, "README") + history, err := GetLogs(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}") @@ -2041,3 +2078,145 @@ func (s *S) TestGetLogWithFile(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) +} diff --git a/user/user.go b/user/user.go index fef3608..063bf7d 100644 --- a/user/user.go +++ b/user/user.go @@ -122,7 +122,7 @@ func (u *User) handleAssociatedRepositories() error { // // Stores the key in the user's document and write it in authorized_keys. // -// Returns an error in case the user does not exists. +// Returns an error in case the user does not exist. func AddKey(uName string, k map[string]string) error { var u User conn, err := db.Conn() diff --git a/user/user_test.go b/user/user_test.go index d316a14..7c472a8 100644 --- a/user/user_test.go +++ b/user/user_test.go @@ -299,7 +299,7 @@ func (s *S) TestAddKeyShouldWriteKeyInAuthorizedKeys(c *gocheck.C) { c.Assert(content, gocheck.Equals, key.format()) } -func (s *S) TestAddKeyShouldReturnCustomErrorWhenUserDoesNotExists(c *gocheck.C) { +func (s *S) TestAddKeyShouldReturnCustomErrorWhenUserDoesNotExist(c *gocheck.C) { err := AddKey("umi", map[string]string{"somekey": "ssh-rsa mykey umi@host"}) c.Assert(err, gocheck.Equals, ErrUserNotFound) } @@ -342,7 +342,7 @@ func (s *S) TestRemoveUnknownKeyFromUser(c *gocheck.C) { c.Assert(err, gocheck.Equals, ErrKeyNotFound) } -func (s *S) TestRemoveKeyShouldReturnFormatedErrorMsgWhenUserDoesNotExists(c *gocheck.C) { +func (s *S) TestRemoveKeyShouldReturnFormatedErrorMsgWhenUserDoesNotExist(c *gocheck.C) { err := RemoveKey("luke", "homekey") c.Assert(err, gocheck.Equals, ErrUserNotFound) }