Repositories

Modified repository/git.go

@@ -111,6 +111,19 @@ func (repo *GitRepo) HasUncommittedChanges() bool {
return false
}
// VerifyCommit verifies that the supplied hash points to a known commit.
func (repo *GitRepo) VerifyCommit(hash string) error {
out, err := repo.runGitCommand("cat-file", "-t", hash)
if err != nil {
return err
}
objectType := strings.TrimSpace(string(out))
if objectType != "commit" {
return fmt.Errorf("Hash %q points to a non-commit object of type %q", hash, objectType)
}
return nil
}
// VerifyGitRef verifies that the supplied ref points to a known commit.
func (repo *GitRepo) VerifyGitRef(ref string) error {
_, err := repo.runGitCommand("show-ref", "--verify", ref)

Modified repository/mock_repo.go

@@ -186,6 +186,14 @@ func (r mockRepoForTest) resolveLocalRef(ref string) (string, error) {
return "", fmt.Errorf("The ref %q does not exist", ref)
}
// VerifyCommit verifies that the supplied hash points to a known commit.
func (r mockRepoForTest) VerifyCommit(hash string) error {
if _, ok := r.Commits[hash]; !ok {
return fmt.Errorf("The given hash %q is not a known commit", hash)
}
return nil
}
// VerifyGitRef verifies that the supplied ref points to a known commit.
func (r mockRepoForTest) VerifyGitRef(ref string) error {
_, err := r.resolveLocalRef(ref)

Modified repository/repo.go

@@ -43,6 +43,9 @@ type Repo interface {
// HasUncommittedChanges returns true if there are local, uncommitted changes.
HasUncommittedChanges() bool
// VerifyCommit verifies that the supplied hash points to a known commit.
VerifyCommit(hash string) error
// VerifyGitRef verifies that the supplied ref points to a known commit.
VerifyGitRef(ref string) error

Modified review/review.go

@@ -300,6 +300,9 @@ func (r *Review) GetJson() (string, error) {
// and all of the commits that are referenced in the given comment threads.
func (r *Review) findLastCommit(latestCommit string, commentThreads []CommentThread) string {
isLater := func(commit string) bool {
if err := r.Repo.VerifyCommit(commit); err != nil {
return false
}
if r.Repo.IsAncestor(latestCommit, commit) {
return true
}