From 42d3a37026616a8cb3a9da11ab8e5565ca6b6074 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 14 Oct 2024 18:09:10 +0200 Subject: pkg/vcs: change HeadCommit to Commit Currently we have HeadCommit function that returns info about the HEAD commit. Change it to a more flexible Commit function that can return info about any commit. This will be used in future changes. --- pkg/vcs/fuchsia.go | 4 ++-- pkg/vcs/git.go | 28 ++++++++++++---------------- pkg/vcs/git_repo_test.go | 6 +++--- pkg/vcs/git_test.go | 20 ++++++++++---------- pkg/vcs/git_test_util.go | 4 ++-- pkg/vcs/linux_patches_test.go | 6 +++--- pkg/vcs/vcs.go | 5 +++-- 7 files changed, 35 insertions(+), 38 deletions(-) (limited to 'pkg/vcs') diff --git a/pkg/vcs/fuchsia.go b/pkg/vcs/fuchsia.go index 704c0a9e6..dd9a667b3 100644 --- a/pkg/vcs/fuchsia.go +++ b/pkg/vcs/fuchsia.go @@ -33,7 +33,7 @@ func (ctx *fuchsia) Poll(repo, branch string) (*Commit, error) { return nil, err } } - return ctx.repo.HeadCommit() + return ctx.repo.Commit(HEAD) } func (ctx *fuchsia) initRepo() error { @@ -68,7 +68,7 @@ func (ctx *fuchsia) SwitchCommit(commit string) (*Commit, error) { return nil, fmt.Errorf("not implemented for fuchsia") } -func (ctx *fuchsia) HeadCommit() (*Commit, error) { +func (ctx *fuchsia) Commit(com string) (*Commit, error) { return nil, fmt.Errorf("not implemented for fuchsia") } diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index f8362dfb2..dd1681157 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -96,7 +96,7 @@ func (git *git) Poll(repo, branch string) (*Commit, error) { if _, err := git.git("submodule", "update", "--init"); err != nil { return nil, err } - return git.HeadCommit() + return git.Commit(HEAD) } func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) { @@ -127,7 +127,7 @@ func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) { if err := git.repair(); err != nil { return nil, err } - return git.HeadCommit() + return git.Commit(HEAD) } func (git *git) CheckoutCommit(repo, commit string) (*Commit, error) { @@ -176,7 +176,7 @@ func (git *git) SwitchCommit(commit string) (*Commit, error) { if _, err := git.git("submodule", "update", "--init"); err != nil { return nil, err } - return git.HeadCommit() + return git.Commit(HEAD) } func (git *git) clone(repo, branch string) error { @@ -237,16 +237,12 @@ func (git *git) initRepo(reason error) error { } func (git *git) Contains(commit string) (bool, error) { - _, err := git.git("merge-base", "--is-ancestor", commit, "HEAD") + _, err := git.git("merge-base", "--is-ancestor", commit, HEAD) return err == nil, nil } -func (git *git) HeadCommit() (*Commit, error) { - return git.getCommit("HEAD") -} - -func (git *git) getCommit(commit string) (*Commit, error) { - output, err := git.git("log", "--format=%H%n%s%n%ae%n%an%n%ad%n%P%n%cd%n%b", "-n", "1", commit) +func (git *git) Commit(com string) (*Commit, error) { + output, err := git.git("log", "--format=%H%n%s%n%ae%n%an%n%ad%n%P%n%cd%n%b", "-n", "1", com) if err != nil { return nil, err } @@ -351,7 +347,7 @@ func (git *git) GetCommitsByTitles(titles []string) ([]*Commit, []string, error) m[canonical] = title } since := time.Now().Add(-time.Hour * 24 * 365 * fetchCommitsMaxAgeInYears).Format("01-02-2006") - commits, err := git.fetchCommits(since, "HEAD", "", "", greps, true) + commits, err := git.fetchCommits(since, HEAD, "", "", greps, true) if err != nil { return nil, nil, err } @@ -479,7 +475,7 @@ func splitEmail(email string) (user, domain string, err error) { func (git *git) Bisect(bad, good string, dt debugtracer.DebugTracer, pred func() (BisectResult, error)) ([]*Commit, error) { git.reset() - firstBad, err := git.getCommit(bad) + firstBad, err := git.Commit(bad) if err != nil { return nil, err } @@ -489,7 +485,7 @@ func (git *git) Bisect(bad, good string, dt debugtracer.DebugTracer, pred func() } defer git.reset() dt.Log("# git bisect start %v %v\n%s", bad, good, output) - current, err := git.HeadCommit() + current, err := git.Commit(HEAD) if err != nil { return nil, err } @@ -516,7 +512,7 @@ func (git *git) Bisect(bad, good string, dt debugtracer.DebugTracer, pred func() } return nil, err } - next, err := git.HeadCommit() + next, err := git.Commit(HEAD) if err != nil { return nil, err } @@ -544,7 +540,7 @@ func (git *git) bisectInconclusive(output []byte) ([]*Commit, error) { // 7c3850adbcccc2c6c9e7ab23a7dcbc4926ee5b96 is the first bad commit var commits []*Commit for _, hash := range gitFullHashRe.FindAll(output, -1) { - com, err := git.getCommit(string(hash)) + com, err := git.Commit(string(hash)) if err != nil { return nil, err } @@ -611,7 +607,7 @@ func (git *git) MergeBases(firstCommit, secondCommit string) ([]*Commit, error) } ret := []*Commit{} for _, hash := range strings.Fields(string(output)) { - commit, err := git.getCommit(hash) + commit, err := git.Commit(hash) if err != nil { return nil, err } diff --git a/pkg/vcs/git_repo_test.go b/pkg/vcs/git_repo_test.go index 315cea5df..6409b59cf 100644 --- a/pkg/vcs/git_repo_test.go +++ b/pkg/vcs/git_repo_test.go @@ -130,7 +130,7 @@ func TestMetadata(t *testing.T) { prevHash := "" for i, test := range metadataTests { repo.CommitChange(test.description) - com, err := repo.repo.HeadCommit() + com, err := repo.repo.Commit(HEAD) if err != nil { t.Fatal(err) } @@ -315,7 +315,7 @@ func TestBisect(t *testing.T) { var commits []string for i := 0; i < 5; i++ { repo.CommitChange(fmt.Sprintf("commit %v", i)) - com, err := repo.repo.HeadCommit() + com, err := repo.repo.Commit(HEAD) if err != nil { t.Fatal(err) } @@ -329,7 +329,7 @@ func TestBisect(t *testing.T) { } makePred := func(res1, res2, res3 BisectResult) predFunc { return func() (BisectResult, error) { - current, err := repo.repo.HeadCommit() + current, err := repo.repo.Commit(HEAD) if err != nil { t.Fatal(err) } diff --git a/pkg/vcs/git_test.go b/pkg/vcs/git_test.go index 73670dd4a..8ff4dbb6e 100644 --- a/pkg/vcs/git_test.go +++ b/pkg/vcs/git_test.go @@ -186,7 +186,7 @@ func TestGetCommitsByTitles(t *testing.T) { repo.Git("commit", "--no-edit", "--allow-empty", "-m", "target") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "abc") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "target") - commitA, _ := repo.repo.HeadCommit() + commitA, _ := repo.repo.Commit(HEAD) results, missing, err := repo.repo.GetCommitsByTitles([]string{"target"}) validateSuccess(commitA, results, missing, err) @@ -222,7 +222,7 @@ func TestContains(t *testing.T) { // We expect Contains to return true, if commit is in current checkout. repo.Git("checkout", "-b", "branch-a") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "target") - commitA, _ := repo.repo.HeadCommit() + commitA, _ := repo.repo.Commit(HEAD) if contained, _ := repo.repo.Contains(commitA.Hash); !contained { t.Fatalf("contains claims commit that should be present is not") } @@ -233,7 +233,7 @@ func TestContains(t *testing.T) { // Commits must only be searched for from the checkedout HEAD. repo.Git("checkout", "-b", "branch-b") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "target") - commitB, _ := repo.repo.HeadCommit() + commitB, _ := repo.repo.Commit(HEAD) repo.Git("checkout", "branch-a") if contained, _ := repo.repo.Contains(commitB.Hash); contained { t.Fatalf("contains found commit that is not in current branch") @@ -325,12 +325,12 @@ func TestMergeBase(t *testing.T) { // Create base branch. repo.Git("checkout", "-b", "base") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "target") - baseCommit, _ := repo.repo.HeadCommit() + baseCommit, _ := repo.repo.Commit(HEAD) // Fork off another branch. repo.Git("checkout", "-b", "fork") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "target") - forkCommit, _ := repo.repo.HeadCommit() + forkCommit, _ := repo.repo.Commit(HEAD) // Ensure that merge base points to the base commit. mergeCommits, err := repo.repo.MergeBases(baseCommit.Hash, forkCommit.Hash) @@ -343,11 +343,11 @@ func TestMergeBase(t *testing.T) { // Let branches diverge. repo.Git("checkout", "base") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "newBase") - newBaseCommit, _ := repo.repo.HeadCommit() + newBaseCommit, _ := repo.repo.Commit(HEAD) repo.Git("checkout", "fork") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "newFork") - newForkCommit, _ := repo.repo.HeadCommit() + newForkCommit, _ := repo.repo.Commit(HEAD) // The merge base should remain the same. mergeCommits, err = repo.repo.MergeBases(newBaseCommit.Hash, newForkCommit.Hash) @@ -363,7 +363,7 @@ func TestMergeBase(t *testing.T) { // And advance the fork branch. repo.Git("commit", "--no-edit", "--allow-empty", "-m", "target") - newNewForkCommit, _ := repo.repo.HeadCommit() + newNewForkCommit, _ := repo.repo.Commit(HEAD) // The merge base should point to the last commit in `base`. mergeCommits, err = repo.repo.MergeBases(newBaseCommit.Hash, newNewForkCommit.Hash) @@ -387,7 +387,7 @@ func TestGitCustomRefs(t *testing.T) { remote.Git("commit", "--no-edit", "--allow-empty", "-m", "detached commit") // Add a ref to prevent the commit from getting garbage collected. remote.Git("update-ref", "refs/custom/test", "temp_branch") - refCommit, _ := remote.repo.HeadCommit() + refCommit, _ := remote.repo.Commit(HEAD) // Remove the branch, let the commit stay only in refs. remote.Git("checkout", "base_branch") @@ -435,7 +435,7 @@ func TestGitFetchShortHash(t *testing.T) { remote.Git("tag", "base_tag") remote.Git("checkout", "-b", "temp_branch") remote.Git("commit", "--no-edit", "--allow-empty", "-m", "detached commit") - refCommit, _ := remote.repo.HeadCommit() + refCommit, _ := remote.repo.Commit(HEAD) // Create a local repo. localRepoDir := t.TempDir() diff --git a/pkg/vcs/git_test_util.go b/pkg/vcs/git_test_util.go index d341c6144..79b2d958a 100644 --- a/pkg/vcs/git_test_util.go +++ b/pkg/vcs/git_test_util.go @@ -69,7 +69,7 @@ func (repo *TestRepo) CommitFileChange(branch, change string) { if repo.Commits[branch] == nil { repo.Commits[branch] = make(map[string]*Commit) } - com, err := repo.repo.HeadCommit() + com, err := repo.repo.Commit(HEAD) if err != nil { repo.t.Fatal(err) } @@ -78,7 +78,7 @@ func (repo *TestRepo) CommitFileChange(branch, change string) { func (repo *TestRepo) CommitChange(description string) *Commit { repo.Git("commit", "--allow-empty", "-m", description) - com, err := repo.repo.HeadCommit() + com, err := repo.repo.Commit(HEAD) if err != nil { repo.t.Fatal(err) } diff --git a/pkg/vcs/linux_patches_test.go b/pkg/vcs/linux_patches_test.go index f5a27219d..663cc3544 100644 --- a/pkg/vcs/linux_patches_test.go +++ b/pkg/vcs/linux_patches_test.go @@ -26,7 +26,7 @@ func TestFixBackport(t *testing.T) { } repo.Git("add", "object.txt") repo.Git("commit", "--no-edit", "-m", "fix title") - fixCommit, _ := repo.repo.HeadCommit() + fixCommit, _ := repo.repo.Commit(HEAD) // Return to the original branch. repo.Git("checkout", "main") @@ -66,7 +66,7 @@ func TestConditionalFixBackport(t *testing.T) { } repo.Git("add", "object.txt") repo.Git("commit", "--no-edit", "-m", "fix title") - fixCommit, _ := repo.repo.HeadCommit() + fixCommit, _ := repo.repo.Commit(HEAD) // Create a branch without a bug. repo.Git("checkout", "main") @@ -76,7 +76,7 @@ func TestConditionalFixBackport(t *testing.T) { // Create a branch with a bug. repo.Git("checkout", "-b", "branch-with-bug") repo.Git("commit", "--no-edit", "--allow-empty", "-m", "bad commit") - badCommit, _ := repo.repo.HeadCommit() + badCommit, _ := repo.repo.Commit(HEAD) repo.Git("commit", "--no-edit", "--allow-empty", "-m", "some other commit") // Ensure we do not cherry-pick the fix when there's no bug. diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go index 23a302cbd..369765b0e 100644 --- a/pkg/vcs/vcs.go +++ b/pkg/vcs/vcs.go @@ -36,8 +36,9 @@ type Repo interface { // SwitchCommit checkouts the specified commit without fetching. SwitchCommit(commit string) (*Commit, error) - // HeadCommit returns info about the HEAD commit of the current branch of git repository. - HeadCommit() (*Commit, error) + // Commit returns info about the specified commit hash. + // The commit may be the special value HEAD for the current commit. + Commit(com string) (*Commit, error) // GetCommitByTitle finds commit info by the title. If the commit is not found, nil is returned. // Remote is not fetched and only commits reachable from the checked out HEAD are searched -- cgit mrf-deployment