Skip to content

Fall back to "git fetch ... +SHA:..." (like GitHub Actions do!) if we fail to find the commit #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ jobs:
./bashbrew list "$image"
./bashbrew list --uniq "$image"
./bashbrew cat "$image"
./bashbrew from --uniq "$image"
31 changes: 29 additions & 2 deletions cmd/bashbrew/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"regexp"
"strings"

"github.com/codegangsta/cli"

"github.com/docker-library/go-dockerlibrary/manifest"
"github.com/docker-library/go-dockerlibrary/pkg/execpipe"

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

commit, err := getGitCommit(entry.ArchGitCommit(arch))
if err != nil {
return "", err
if fetchErr := fetchGitCommit(arch, entry, gitRemote.Config().URLs[0], fetchString); fetchErr != nil {
return "", cli.NewMultiError(err, fetchErr)
}
commit, err = getGitCommit(entry.ArchGitCommit(arch))
if err != nil {
return "", err
}
}

gitTag := gitNormalizeForTagUsage(path.Join(arch, namespace, r.RepoName, entry.Tags[0]))
Expand All @@ -253,3 +263,20 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
entry.SetGitCommit(arch, commit)
return commit, nil
}

// this is used as a fallback if using github.com/go-git/go-git/v5 to fetch the branch fails to find the commit
// Git (and more recently, GitHub) support "git fetch"ing a specific commit directly!
// (The "actions/checkout@v2" GitHub action uses this to fetch commits for running workflows even after branches are deleted!)
// https://github.com/git/git/commit/f8edeaa05d8623a9f6dad408237496c51101aad8
// (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.)
func fetchGitCommit(arch string, entry *manifest.Manifest2822Entry, gitRemote, fetchString string) error {
commit := entry.ArchGitCommit(arch)
if commit == "FETCH_HEAD" {
return fmt.Errorf("cannot fetch line-based entry commit when fetching by tag")
}

fetchString = "+" + commit + ":" + strings.SplitN(fetchString, ":", 2)[1]

_, err := git(`fetch`, `--quiet`, gitRemote, fetchString)
return err
}