aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2024-02-29 16:13:03 +0100
committerAleksandr Nogikh <nogikh@google.com>2024-03-05 10:06:46 +0000
commitbc28da7b1d62423d9e6dc6420803e11311d5408f (patch)
treead4b36373a4e228eeec0b6663a252c5a9818b83e /pkg
parent5fc5366972c874b919f93165bb4ed4e2bcb7c350 (diff)
pkg/vcs: support fetches by a short git hash
The approach we used works perfectly for all commits, but they must be referenced by the full 40 character hash. In almost all cases, users would prefer to use the shorter one. If the commit hash is not 40 characters long, fetch the whole git tree. The only unsupported scenario is fetching a commit that is referenced by custom refs/* by its short hash. It's unlikely there's anything we can do here.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/vcs/git.go7
-rw-r--r--pkg/vcs/git_test.go19
2 files changed, 24 insertions, 2 deletions
diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go
index 0bd72d9a6..840a8592e 100644
--- a/pkg/vcs/git.go
+++ b/pkg/vcs/git.go
@@ -145,7 +145,8 @@ func (git *git) fetchRemote(repo, commit string) error {
// Ignore error as we can double add the same remote and that will fail.
git.git("remote", "add", repoHash, repo)
fetchArgs := []string{"fetch", "--force", "--tags", repoHash}
- if commit != "" {
+ if commit != "" && gitFullHashRe.MatchString(commit) {
+ // This trick only works with full commit hashes.
fetchArgs = append(fetchArgs, commit)
}
_, err := git.git(fetchArgs...)
@@ -526,6 +527,8 @@ func (git *git) Bisect(bad, good string, dt debugtracer.DebugTracer, pred func()
}
}
+var gitFullHashRe = regexp.MustCompile("[a-f0-9]{40}")
+
func (git *git) bisectInconclusive(output []byte) ([]*Commit, error) {
// For inconclusive bisection git prints the following message:
//
@@ -540,7 +543,7 @@ func (git *git) bisectInconclusive(output []byte) ([]*Commit, error) {
//
// 7c3850adbcccc2c6c9e7ab23a7dcbc4926ee5b96 is the first bad commit
var commits []*Commit
- for _, hash := range regexp.MustCompile("[a-f0-9]{40}").FindAll(output, -1) {
+ for _, hash := range gitFullHashRe.FindAll(output, -1) {
com, err := git.getCommit(string(hash))
if err != nil {
return nil, err
diff --git a/pkg/vcs/git_test.go b/pkg/vcs/git_test.go
index 5e9d30fa7..73670dd4a 100644
--- a/pkg/vcs/git_test.go
+++ b/pkg/vcs/git_test.go
@@ -426,3 +426,22 @@ func TestGitRemoteTags(t *testing.T) {
sort.Strings(tags)
assert.Equal(t, []string{"v1.0", "v2.0"}, tags)
}
+
+func TestGitFetchShortHash(t *testing.T) {
+ remoteRepoDir := t.TempDir()
+ remote := MakeTestRepo(t, remoteRepoDir)
+ remote.Git("commit", "--no-edit", "--allow-empty", "-m", "base commit")
+ remote.Git("checkout", "-b", "base_branch")
+ 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()
+
+ // Create a local repo.
+ localRepoDir := t.TempDir()
+ local := newGit(localRepoDir, nil, nil)
+
+ // Fetch the commit from the custom ref.
+ _, err := local.CheckoutCommit(remoteRepoDir, refCommit.Hash[:12])
+ assert.NoError(t, err)
+}