Skip to content

Commit ce6033a

Browse files
committed
Fall back to "git fetch ... +SHA:..." (like GitHub Actions do!) if we fail to find the commit
1 parent 68878f4 commit ce6033a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

cmd/bashbrew/git.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"regexp"
1212
"strings"
1313

14+
"github.com/codegangsta/cli"
15+
1416
"github.com/docker-library/go-dockerlibrary/manifest"
1517
"github.com/docker-library/go-dockerlibrary/pkg/execpipe"
1618

@@ -234,12 +236,20 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
234236
//Progress: os.Stdout,
235237
})
236238
if err != nil {
237-
return "", err
239+
if fetchErr := fetchGitCommit(arch, entry, gitRemote.Config().URLs[0], fetchString); fetchErr != nil {
240+
return "", cli.NewMultiError(err, fetchErr)
241+
}
238242
}
239243

240244
commit, err := getGitCommit(entry.ArchGitCommit(arch))
241245
if err != nil {
242-
return "", err
246+
if fetchErr := fetchGitCommit(arch, entry, gitRemote.Config().URLs[0], fetchString); fetchErr != nil {
247+
return "", cli.NewMultiError(err, fetchErr)
248+
}
249+
commit, err = getGitCommit(entry.ArchGitCommit(arch))
250+
if err != nil {
251+
return "", err
252+
}
243253
}
244254

245255
gitTag := gitNormalizeForTagUsage(path.Join(arch, namespace, r.RepoName, entry.Tags[0]))
@@ -253,3 +263,20 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
253263
entry.SetGitCommit(arch, commit)
254264
return commit, nil
255265
}
266+
267+
// this is used as a fallback if using github.com/go-git/go-git/v5 to fetch the branch fails to find the commit
268+
// Git (and more recently, GitHub) support "git fetch"ing a specific commit directly!
269+
// (The "actions/checkout@v2" GitHub action uses this to fetch commits for running workflows even after branches are deleted!)
270+
// https://github.com/git/git/commit/f8edeaa05d8623a9f6dad408237496c51101aad8
271+
// (Unfortunately, github.com/go-git/go-git/v5 does not support fetching a commit like this from what I can figure, so we have to shell out.)
272+
func fetchGitCommit(arch string, entry *manifest.Manifest2822Entry, gitRemote, fetchString string) error {
273+
commit := entry.ArchGitCommit(arch)
274+
if commit == "FETCH_HEAD" {
275+
return fmt.Errorf("cannot fetch line-based entry commit when fetching by tag")
276+
}
277+
278+
fetchString = "+" + commit + ":" + strings.SplitN(fetchString, ":", 2)[1]
279+
280+
_, err := git(`fetch`, `--quiet`, gitRemote, fetchString)
281+
return err
282+
}

0 commit comments

Comments
 (0)