Skip to content

Commit 870ac0f

Browse files
authored
Merge pull request #12 from infosiftr/go-git-fetch-commit
Use new go-git functionality to fetch commits
2 parents 0fc0b09 + daa5333 commit 870ac0f

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

cmd/bashbrew/git.go

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
180180
}
181181
}
182182

183-
fetchString := entry.ArchGitFetch(arch) + ":"
184-
if entry.ArchGitCommit(arch) == "FETCH_HEAD" {
183+
fetchStrings := []string{
184+
entry.ArchGitFetch(arch) + ":",
185+
}
186+
if entryArchGitCommit := entry.ArchGitCommit(arch); entryArchGitCommit == "FETCH_HEAD" {
185187
// fetch remote tag references to a local tag ref so that we can cache them and not re-fetch every time
186188
localRef := "refs/tags/" + gitNormalizeForTagUsage(cacheKey)
187189
commit, err := getGitCommit(localRef)
@@ -190,7 +192,7 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
190192
entry.SetGitCommit(arch, commit)
191193
return commit, nil
192194
}
193-
fetchString += localRef
195+
fetchStrings[0] += localRef
194196
} else {
195197
// we create a temporary remote dir so that we can clean it up completely afterwards
196198
refBase := "refs/remotes"
@@ -210,10 +212,19 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
210212
tempRef := path.Join(refBase, filepath.Base(tempRefDir))
211213
if entry.ArchGitFetch(arch) == manifest.DefaultLineBasedFetch {
212214
// backwards compat (see manifest/line-based.go in go-dockerlibrary)
213-
fetchString += tempRef + "/*"
215+
fetchStrings[0] += tempRef + "/*"
214216
} else {
215-
fetchString += tempRef + "/temp"
217+
fetchStrings[0] += tempRef + "/temp"
216218
}
219+
220+
fetchStrings = append([]string{
221+
// Git (and more recently, GitHub) support "git fetch"ing a specific commit directly!
222+
// (The "actions/checkout@v2" GitHub action uses this to fetch commits for running workflows even after branches are deleted!)
223+
// https://github.com/git/git/commit/f8edeaa05d8623a9f6dad408237496c51101aad8
224+
// https://github.com/go-git/go-git/pull/58
225+
// If that works, we want to prefer it (since it'll be much more efficient at getting us the commit we care about), so we prepend it to our list of "things to try fetching"
226+
entryArchGitCommit + ":" + tempRef + "/temp",
227+
}, fetchStrings...)
217228
}
218229

219230
if strings.HasPrefix(entry.ArchGitRepo(arch), "git://github.com/") {
@@ -229,27 +240,32 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
229240
return "", err
230241
}
231242

232-
err = gitRemote.Fetch(&goGit.FetchOptions{
233-
RefSpecs: []goGitConfig.RefSpec{goGitConfig.RefSpec(fetchString)},
234-
Tags: goGit.NoTags,
243+
var commit string
244+
fetchErrors := []error{}
245+
for _, fetchString := range fetchStrings {
246+
err := gitRemote.Fetch(&goGit.FetchOptions{
247+
RefSpecs: []goGitConfig.RefSpec{goGitConfig.RefSpec(fetchString)},
248+
Tags: goGit.NoTags,
235249

236-
//Progress: os.Stdout,
237-
})
238-
if err != nil {
239-
if fetchErr := fetchGitCommit(arch, entry, gitRemote.Config().URLs[0], fetchString); fetchErr != nil {
240-
return "", cli.NewMultiError(err, fetchErr)
250+
//Progress: os.Stdout,
251+
})
252+
if err != nil {
253+
fetchErrors = append(fetchErrors, err)
254+
continue
241255
}
242-
}
243256

244-
commit, err := getGitCommit(entry.ArchGitCommit(arch))
245-
if err != nil {
246-
if fetchErr := fetchGitCommit(arch, entry, gitRemote.Config().URLs[0], fetchString); fetchErr != nil {
247-
return "", cli.NewMultiError(err, fetchErr)
248-
}
249257
commit, err = getGitCommit(entry.ArchGitCommit(arch))
250258
if err != nil {
251-
return "", err
259+
fetchErrors = append(fetchErrors, err)
260+
continue
252261
}
262+
263+
fetchErrors = nil
264+
break
265+
}
266+
267+
if len(fetchErrors) > 0 {
268+
return "", cli.NewMultiError(fetchErrors...)
253269
}
254270

255271
gitTag := gitNormalizeForTagUsage(path.Join(arch, namespace, r.RepoName, entry.Tags[0]))
@@ -263,20 +279,3 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
263279
entry.SetGitCommit(arch, commit)
264280
return commit, nil
265281
}
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-
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ go 1.11
55
require (
66
github.com/codegangsta/cli v1.20.0
77
github.com/docker-library/go-dockerlibrary v0.0.0-20200415185511-8f28c0fe22db
8-
github.com/go-git/go-git/v5 v5.0.0
8+
github.com/go-git/go-git/v5 v5.0.1-0.20200510222821-568154cab876
9+
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
10+
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect
11+
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
912
pault.ag/go/debian v0.0.0-20190109175134-a131cb0ae041
1013
pault.ag/go/topsort v0.0.0-20160530003732-f98d2ad46e1a
1114
)

go.sum

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agR
2424
github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
2525
github.com/go-git/go-git-fixtures/v4 v4.0.1 h1:q+IFMfLx200Q3scvt2hN79JsEzy4AmBTp/pqnefH+Bc=
2626
github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw=
27-
github.com/go-git/go-git/v5 v5.0.0 h1:k5RWPm4iJwYtfWoxIJy4wJX9ON7ihPeZZYC1fLYDnpg=
28-
github.com/go-git/go-git/v5 v5.0.0/go.mod h1:oYD8y9kWsGINPFJoLdaScGCN6dlKg23blmClfZwtUVA=
27+
github.com/go-git/go-git/v5 v5.0.1-0.20200510222821-568154cab876 h1:+NeGJnaRbcjcD3wDtc8HOJveibQbgJMKwotDaFPi/w4=
28+
github.com/go-git/go-git/v5 v5.0.1-0.20200510222821-568154cab876/go.mod h1:oYD8y9kWsGINPFJoLdaScGCN6dlKg23blmClfZwtUVA=
2929
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
3030
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
3131
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
@@ -62,15 +62,22 @@ golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnf
6262
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6363
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
6464
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
65+
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
66+
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
6567
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
6668
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
6769
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
70+
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f h1:QBjCr1Fz5kw158VqdE9JfI9cJnl/ymnJWAdMuinqL7Y=
71+
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
6872
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
6973
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7074
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7175
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7276
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
7377
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
78+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
79+
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f h1:mOhmO9WsBaJCNmaZHPtHs9wOcdqdKCjF6OPJlmDM3KI=
80+
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7481
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
7582
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
7683
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=

0 commit comments

Comments
 (0)