aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2024-10-14 18:09:10 +0200
committerDmitry Vyukov <dvyukov@google.com>2024-10-15 09:08:36 +0000
commit42d3a37026616a8cb3a9da11ab8e5565ca6b6074 (patch)
tree6be4d678b1a05ef78450a4e9133eba3590509589 /pkg
parent7eb57b4aa460c38365f8c5aa77d06c3b750daa4b (diff)
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.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bisect/bisect.go6
-rw-r--r--pkg/bisect/bisect_test.go2
-rw-r--r--pkg/vcs/fuchsia.go4
-rw-r--r--pkg/vcs/git.go28
-rw-r--r--pkg/vcs/git_repo_test.go6
-rw-r--r--pkg/vcs/git_test.go20
-rw-r--r--pkg/vcs/git_test_util.go4
-rw-r--r--pkg/vcs/linux_patches_test.go6
-rw-r--r--pkg/vcs/vcs.go5
9 files changed, 39 insertions, 42 deletions
diff --git a/pkg/bisect/bisect.go b/pkg/bisect/bisect.go
index a0dd29bb5..1abd65e6b 100644
--- a/pkg/bisect/bisect.go
+++ b/pkg/bisect/bisect.go
@@ -186,7 +186,7 @@ func runImpl(cfg *Config, repo vcs.Repo, inst instance.Env) (*Result, error) {
BuildCPUs: cfg.BuildCPUs,
},
}
- head, err := repo.HeadCommit()
+ head, err := repo.Commit(vcs.HEAD)
if err != nil {
return nil, err
}
@@ -602,7 +602,7 @@ type testResult struct {
}
func (env *env) build() (*vcs.Commit, string, error) {
- current, err := env.repo.HeadCommit()
+ current, err := env.repo.Commit(vcs.HEAD)
if err != nil {
return nil, "", err
}
@@ -782,7 +782,7 @@ func (env *env) revisionHadBug() (bool, error) {
// But let's first see how many extra test() runs we get without it.
// We'll likely change the revision below. Ensure we get back to the original one.
- curr, err := env.repo.HeadCommit()
+ curr, err := env.repo.Commit(vcs.HEAD)
if err != nil {
return false, err
}
diff --git a/pkg/bisect/bisect_test.go b/pkg/bisect/bisect_test.go
index 6731eb235..5160cffed 100644
--- a/pkg/bisect/bisect_test.go
+++ b/pkg/bisect/bisect_test.go
@@ -133,7 +133,7 @@ func (env *testEnv) Test(numVMs int, reproSyz, reproOpts, reproC []byte) ([]inst
}
func (env *testEnv) headCommit() int {
- com, err := env.r.HeadCommit()
+ com, err := env.r.Commit(vcs.HEAD)
if err != nil {
env.t.Fatal(err)
}
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