Skip to content

Commit 0386241

Browse files
authored
Merge pull request #4 from infosiftr/git-fetch-commit
Fall back to "git fetch ... +SHA:..." (like GitHub Actions do!) if we fail to find the commit
2 parents 68878f4 + 5ab18b3 commit 0386241

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ jobs:
2727
./bashbrew list "$image"
2828
./bashbrew list --uniq "$image"
2929
./bashbrew cat "$image"
30+
./bashbrew from --uniq "$image"

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 [https://github.com/go-git/go-git/issues/56], 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)