Skip to content

Commit

Permalink
Merge pull request #354 from saschagrunert/next-commit
Browse files Browse the repository at this point in the history
git: add API to retrieve `NextCommit`
  • Loading branch information
k8s-ci-robot authored Jun 10, 2024
2 parents 1b6fb25 + 1da7efb commit 3361b18
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
16 changes: 15 additions & 1 deletion git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ func (r *Repo) ShowLastCommit() (logData string, err error) {

// LastCommitSha returns the sha of the last commit in the repository
func (r *Repo) LastCommitSha() (string, error) {
shaval, err := r.runGitCmd("log", "--pretty=format:'%H'", "-n1")
shaval, err := r.runGitCmd("log", "--pretty=format:%H", "-n1")
if err != nil {
return "", fmt.Errorf("trying to retrieve the last commit sha: %w", err)
}
Expand Down Expand Up @@ -1631,3 +1631,17 @@ func (r *Repo) LatestReleaseBranch() (string, error) {
func semverToReleaseBranch(v semver.Version) string {
return fmt.Sprintf("%s%d.%d", releaseBranchPrefix, v.Major, v.Minor)
}

// NextCommit determines the next child commit for the provided rev and branch.
func (r *Repo) NextCommit(rev, branch string) (string, error) {
shaList, err := r.runGitCmd("log", "--reverse", "--ancestry-path", "--pretty=format:%H", fmt.Sprintf("%s...%s", rev, branch))
if err != nil {
return "", fmt.Errorf("get next commit from log: %w", err)
}
shaSplit := strings.Fields(shaList)
if len(shaSplit) == 0 {
// no child available
return "", nil
}
return shaSplit[0], nil
}
65 changes: 65 additions & 0 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,68 @@ func TestLastCommitSha(t *testing.T) {
require.Equal(t, shas[0], lastCommit, "Checking HEAD~1 sha matches commit #1")
require.NotEqual(t, shas[1], lastCommit, "Checking HEAD~1 sha does not matches commit #2")
}

func TestNextCommit(t *testing.T) {
// Create a test repository
rawRepoDir, err := os.MkdirTemp("", "k8s-test-repo-")
require.Nil(t, err)
defer os.RemoveAll(rawRepoDir)
_, err = gogit.PlainInit(rawRepoDir, false)
require.Nil(t, err)

repo, err := git.OpenRepo(rawRepoDir)
require.Nil(t, err)

// Create commits in the repository
shas := make([]string, 3)
for _, i := range []int{0, 1, 2} {
require.Nil(t, repo.CommitEmpty(fmt.Sprintf("Empty commit %d", i+1)))
shas[i], err = repo.LastCommitSha()
require.Nil(t, err)
require.NotEmpty(t, shas[i])
require.Nil(t, repo.Tag(fmt.Sprintf("tag-%d", i), "New tag"))
}
require.Len(t, shas, 3)

// shas[0] is the child of shas[1]
nextCommit, err := repo.NextCommit(shas[0], git.DefaultBranch)
require.Nil(t, err)
require.Equal(t, shas[1], nextCommit)

// shas[1] is the child of shas[2]
nextCommit, err = repo.NextCommit(shas[1], git.DefaultBranch)
require.Nil(t, err)
require.Equal(t, shas[2], nextCommit)

// shas[2] has no child
nextCommit, err = repo.NextCommit(shas[2], git.DefaultBranch)
require.Nil(t, err)
require.Empty(t, nextCommit)

// tag-0 is the child of shas[1]
nextCommit, err = repo.NextCommit("tag-0", git.DefaultBranch)
require.Nil(t, err)
require.Equal(t, shas[1], nextCommit)

// tag-1 is the child of shas[2]
nextCommit, err = repo.NextCommit("tag-1", git.DefaultBranch)
require.Nil(t, err)
require.Equal(t, shas[2], nextCommit)

// tag-2 has no child
nextCommit, err = repo.NextCommit("tag-2", git.DefaultBranch)
require.Nil(t, err)
require.Empty(t, nextCommit)

// branch does not exist
nextCommit, err = repo.NextCommit(shas[0], "does-not-exist")
require.NotNil(t, err)
require.Contains(t, err.Error(), "unknown revision")
require.Empty(t, nextCommit)

// commit does not exist
nextCommit, err = repo.NextCommit("does-not-exist", git.DefaultBranch)
require.NotNil(t, err)
require.Contains(t, err.Error(), "unknown revision")
require.Empty(t, nextCommit)
}

0 comments on commit 3361b18

Please sign in to comment.