Skip to content

Commit 7150f7c

Browse files
authored
Bump go-git to latest, includes workaround for x/crypto ssh hostkey selection (#65)
* Bump go-git to latest, includes workaround for x/crypto ssh hostkey selection #45 (review) * Explicitly check out default branch before clone in tests. Fixes pipe: Failed to push gopages commit: command error on refs/heads/gh-pages: branch is currently checked out * Reformat octal numbers with latest gofmt * Bump minimum supported Go version to 1.19 to match dependencies * Use nil auth method if no gh-pages token or user provided
1 parent 0cfead8 commit 7150f7c

File tree

9 files changed

+183
-113
lines changed

9 files changed

+183
-113
lines changed

.github/workflows/ci.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ jobs:
2323
platform:
2424
- ubuntu-latest
2525
go:
26-
- 1.17.x
27-
- 1.18.x
2826
- 1.19.x
27+
- 1.20.x
28+
- 1.21.x
29+
- 1.22.x
2930
include:
3031
- platform: macos-latest
31-
go: 1.19.x
32+
go: 1.22.x
3233
- platform: windows-latest
33-
go: 1.19.x
34+
go: 1.22.x
3435
name: Test (Go ${{ matrix.go }} on ${{ matrix.platform }})
3536
runs-on: ${{ matrix.platform }}
3637
permissions:

gopages/cmd/gendoc/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func run(templatePath, outPath string) error {
3535
return err
3636
}
3737

38-
const genDocPerm = 0644
38+
const genDocPerm = 0o644
3939
f, err := os.OpenFile(outPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, genDocPerm)
4040
if err != nil {
4141
return err

gopages/go.mod

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
module github.com/johnstarich/go/gopages
22

3-
go 1.17
3+
go 1.19
44

55
require (
66
github.com/fsnotify/fsnotify v1.4.9
7-
github.com/go-git/go-billy/v5 v5.0.0
8-
github.com/go-git/go-git/v5 v5.0.0
7+
github.com/go-git/go-billy/v5 v5.5.0
8+
github.com/go-git/go-git/v5 v5.12.0
99
github.com/johnstarich/go/pipe v0.2.0
1010
github.com/pkg/errors v0.9.1
11-
github.com/stretchr/testify v1.5.1
12-
golang.org/x/mod v0.7.0
13-
golang.org/x/net v0.7.0
14-
golang.org/x/tools v0.4.0
11+
github.com/stretchr/testify v1.9.0
12+
golang.org/x/mod v0.12.0
13+
golang.org/x/net v0.22.0
14+
golang.org/x/tools v0.13.0
1515
)
1616

1717
require (
18+
dario.cat/mergo v1.0.0 // indirect
19+
github.com/Microsoft/go-winio v0.6.1 // indirect
20+
github.com/ProtonMail/go-crypto v1.0.0 // indirect
21+
github.com/cloudflare/circl v1.3.7 // indirect
22+
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
1823
github.com/davecgh/go-spew v1.1.1 // indirect
19-
github.com/emirpasic/gods v1.12.0 // indirect
20-
github.com/go-git/gcfg v1.5.0 // indirect
24+
github.com/emirpasic/gods v1.18.1 // indirect
25+
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
26+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
2127
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
22-
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
23-
github.com/mitchellh/go-homedir v1.1.0 // indirect
28+
github.com/kevinburke/ssh_config v1.2.0 // indirect
29+
github.com/pjbgf/sha1cd v0.3.0 // indirect
2430
github.com/pmezard/go-difflib v1.0.0 // indirect
25-
github.com/sergi/go-diff v1.1.0 // indirect
26-
github.com/xanzy/ssh-agent v0.2.1 // indirect
31+
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
32+
github.com/skeema/knownhosts v1.2.2 // indirect
33+
github.com/xanzy/ssh-agent v0.3.3 // indirect
2734
github.com/yuin/goldmark v1.4.13 // indirect
28-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
29-
golang.org/x/sys v0.5.0 // indirect
35+
golang.org/x/crypto v0.21.0 // indirect
36+
golang.org/x/sys v0.18.0 // indirect
3037
gopkg.in/warnings.v0 v0.1.2 // indirect
31-
gopkg.in/yaml.v2 v2.4.0 // indirect
38+
gopkg.in/yaml.v3 v3.0.1 // indirect
3239
)

gopages/go.sum

+101-65
Large diffs are not rendered by default.

gopages/internal/flags/file_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func testFile(t *testing.T, contents string) string {
1515
t.Helper()
1616
p := filepath.Join(t.TempDir(), "file")
17-
err := os.WriteFile(p, []byte(contents), 0600)
17+
err := os.WriteFile(p, []byte(contents), 0o600)
1818
if err != nil {
1919
t.Fatal(err)
2020
}

gopages/internal/generate/generate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
)
3030

3131
const (
32-
scrapeDirPermission = 0700
33-
scrapeFilePermission = 0600
32+
scrapeDirPermission = 0o700
33+
scrapeFilePermission = 0o600
3434
)
3535

3636
var (

gopages/internal/generate/generate_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ func JSONFunc() {
275275
outputFS := memfs.New()
276276
writeFile := func(path, contents string) {
277277
path = filepath.Join(thing, path)
278-
err := os.MkdirAll(filepath.Dir(path), 0700)
278+
err := os.MkdirAll(filepath.Dir(path), 0o700)
279279
require.NoError(t, err)
280-
err = os.WriteFile(path, []byte(contents), 0600)
280+
err = os.WriteFile(path, []byte(contents), 0o600)
281281
require.NoError(t, err)
282282
}
283283

@@ -343,7 +343,7 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
343343
t.Parallel()
344344
outputFS := memfs.New()
345345
const outputDir = "foo"
346-
require.NoError(t, outputFS.MkdirAll(outputDir, 0700))
346+
require.NoError(t, outputFS.MkdirAll(outputDir, 0o700))
347347
err := generateDocs(t, outputFS, outputDir)
348348
assert.EqualError(t, err, outputPathOKError)
349349
})
@@ -352,7 +352,7 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
352352
t.Parallel()
353353
outputFS := memfs.New()
354354
const outputDir = "foo"
355-
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0700))
355+
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0o700))
356356
err := generateDocs(t, outputFS, outputDir)
357357
assert.EqualError(t, err, `pipe: refusing to clean output directory "foo" - directory does not resemble a gopages result; remove the directory to continue`)
358358
})
@@ -361,8 +361,8 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
361361
t.Parallel()
362362
outputFS := memfs.New()
363363
const outputDir = "foo"
364-
require.NoError(t, outputFS.MkdirAll(outputDir, 0700))
365-
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0700)) // include other contents, which are ignored if expected files present
364+
require.NoError(t, outputFS.MkdirAll(outputDir, 0o700))
365+
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0o700)) // include other contents, which are ignored if expected files present
366366
f, err := outputFS.Create(outputFS.Join(outputDir, "index.html"))
367367
require.NoError(t, err)
368368
require.NoError(t, f.Close())
@@ -371,7 +371,7 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
371371
"pkg",
372372
"src",
373373
} {
374-
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, dirName), 0700))
374+
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, dirName), 0o700))
375375
}
376376

377377
err = generateDocs(t, outputFS, outputDir)

gopages/main.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ var generateMemfsDocsPipe = pipe.New(pipe.Options{}).
114114
return args[0].(memfsDocsArgs)
115115
}).
116116
Append(func(args memfsDocsArgs) (memfsDocsArgs, *git.Repository, error) {
117-
repo, err := git.Clone(memory.NewStorage(), args.FS, &git.CloneOptions{
117+
cloneOpts := &git.CloneOptions{
118118
URL: args.Remote,
119119
ReferenceName: plumbing.NewBranchReferenceName(ghPagesBranch),
120120
SingleBranch: true,
121-
Auth: getAuth(args.Flags),
122-
})
121+
}
122+
basicAuth, ok := getAuth(args.Flags)
123+
if ok {
124+
cloneOpts.Auth = basicAuth
125+
}
126+
repo, err := git.Clone(memory.NewStorage(), args.FS, cloneOpts)
123127
return args, repo, errors.Wrap(err, "Failed to clone in-memory copy of repo. Be sure the 'gh-pages' orphaned branch exists: https://help.github.com/en/github/working-with-github-pages/creating-a-github-pages-site-with-jekyll#creating-your-site")
124128
}).
125129
Append(func(args memfsDocsArgs, repo *git.Repository) (memfsDocsArgs, *git.Repository, *git.Worktree, error) {
@@ -148,17 +152,18 @@ var generateMemfsDocsPipe = pipe.New(pipe.Options{}).
148152
}).
149153
Append(func(args memfsDocsArgs, repo *git.Repository) error {
150154
pushOpts := &git.PushOptions{}
151-
pushOpts.Auth = getAuth(args.Flags)
155+
if basicAuth, ok := getAuth(args.Flags); ok {
156+
pushOpts.Auth = basicAuth
157+
}
152158
err := repo.Push(pushOpts)
153159
return errors.Wrap(err, "Failed to push gopages commit")
154160
})
155161

156-
func getAuth(args flags.Args) *gitHTTP.BasicAuth {
157-
var auth *gitHTTP.BasicAuth
162+
func getAuth(args flags.Args) (*gitHTTP.BasicAuth, bool) {
158163
if args.GitHubPagesUser != "" || args.GitHubPagesToken != "" {
159-
auth = &gitHTTP.BasicAuth{Username: args.GitHubPagesUser, Password: args.GitHubPagesToken}
164+
return &gitHTTP.BasicAuth{Username: args.GitHubPagesUser, Password: args.GitHubPagesToken}, true
160165
}
161-
return auth
166+
return nil, false
162167
}
163168

164169
func run(modulePath string, args flags.Args) error {

gopages/main_test.go

+30-9
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,27 @@ func testRun(t *testing.T, tc testRunTestCase) {
114114
ghPagesDir, err := os.MkdirTemp("", "")
115115
require.NoError(t, err)
116116
defer os.RemoveAll(ghPagesDir)
117-
ghPagesRepo, err := git.PlainInit(ghPagesDir, false)
117+
const defaultBranch = "refs/heads/main"
118+
ghPagesRepo, err := git.PlainInitWithOptions(ghPagesDir, &git.PlainInitOptions{
119+
InitOptions: git.InitOptions{
120+
DefaultBranch: defaultBranch,
121+
},
122+
})
118123
require.NoError(t, err)
119124
workTree, err := ghPagesRepo.Worktree()
120125
require.NoError(t, err)
121126
_, err = workTree.Commit("Initial commit", &git.CommitOptions{
122-
Author: commitAuthor(),
127+
Author: commitAuthor(),
128+
AllowEmptyCommits: true,
123129
})
124130
require.NoError(t, err)
125131
require.NoError(t, workTree.Checkout(&git.CheckoutOptions{
126132
Branch: plumbing.NewBranchReferenceName(ghPagesBranch),
127133
Create: true,
128134
}))
135+
require.NoError(t, workTree.Checkout(&git.CheckoutOptions{
136+
Branch: defaultBranch,
137+
}))
129138

130139
modulePath, err := os.MkdirTemp("", "")
131140
require.NoError(t, err)
@@ -145,9 +154,9 @@ func testRun(t *testing.T, tc testRunTestCase) {
145154

146155
writeFile := func(path, contents string) {
147156
path = filepath.Join(modulePath, path)
148-
err := os.MkdirAll(filepath.Dir(path), 0700)
157+
err := os.MkdirAll(filepath.Dir(path), 0o700)
149158
require.NoError(t, err)
150-
err = os.WriteFile(path, []byte(contents), 0600)
159+
err = os.WriteFile(path, []byte(contents), 0o600)
151160
require.NoError(t, err)
152161
}
153162

@@ -182,6 +191,9 @@ func Hello() {
182191
var fileNames []string
183192
if contains(tc.args, "-gh-pages") {
184193
// fetch the new head commit and walk the files in the diff
194+
require.NoError(t, workTree.Checkout(&git.CheckoutOptions{
195+
Branch: plumbing.NewBranchReferenceName(ghPagesBranch),
196+
}))
185197
head, err := ghPagesRepo.Head()
186198
require.NoError(t, err)
187199
headCommit, err := ghPagesRepo.CommitObject(head.Hash())
@@ -248,11 +260,20 @@ func contains(strs []string, s string) bool {
248260

249261
func TestAuth(t *testing.T) {
250262
t.Parallel()
251-
assert.Nil(t, getAuth(flags.Args{}))
252-
assert.Equal(t,
253-
&gitHTTP.BasicAuth{Username: "user", Password: "token"},
254-
getAuth(flags.Args{
263+
t.Run("no auth flags", func(t *testing.T) {
264+
t.Parallel()
265+
basicAuth, ok := getAuth(flags.Args{})
266+
assert.Nil(t, basicAuth)
267+
assert.False(t, ok)
268+
})
269+
270+
t.Run("basic auth flags", func(t *testing.T) {
271+
t.Parallel()
272+
basicAuth, ok := getAuth(flags.Args{
255273
GitHubPagesToken: "token",
256274
GitHubPagesUser: "user",
257-
}))
275+
})
276+
assert.Equal(t, &gitHTTP.BasicAuth{Username: "user", Password: "token"}, basicAuth)
277+
assert.True(t, ok)
278+
})
258279
}

0 commit comments

Comments
 (0)