Skip to content

Commit

Permalink
Bump go-git to latest, includes workaround for x/crypto ssh hostkey s…
Browse files Browse the repository at this point in the history
…election (#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
  • Loading branch information
JohnStarich authored Jun 3, 2024
1 parent 0cfead8 commit 7150f7c
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 113 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ jobs:
platform:
- ubuntu-latest
go:
- 1.17.x
- 1.18.x
- 1.19.x
- 1.20.x
- 1.21.x
- 1.22.x
include:
- platform: macos-latest
go: 1.19.x
go: 1.22.x
- platform: windows-latest
go: 1.19.x
go: 1.22.x
name: Test (Go ${{ matrix.go }} on ${{ matrix.platform }})
runs-on: ${{ matrix.platform }}
permissions:
Expand Down
2 changes: 1 addition & 1 deletion gopages/cmd/gendoc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func run(templatePath, outPath string) error {
return err
}

const genDocPerm = 0644
const genDocPerm = 0o644
f, err := os.OpenFile(outPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, genDocPerm)
if err != nil {
return err
Expand Down
39 changes: 23 additions & 16 deletions gopages/go.mod
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
module github.com/johnstarich/go/gopages

go 1.17
go 1.19

require (
github.com/fsnotify/fsnotify v1.4.9
github.com/go-git/go-billy/v5 v5.0.0
github.com/go-git/go-git/v5 v5.0.0
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.12.0
github.com/johnstarich/go/pipe v0.2.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
golang.org/x/mod v0.7.0
golang.org/x/net v0.7.0
golang.org/x/tools v0.4.0
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.12.0
golang.org/x/net v0.22.0
golang.org/x/tools v0.13.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.2.1 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
166 changes: 101 additions & 65 deletions gopages/go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gopages/internal/flags/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func testFile(t *testing.T, contents string) string {
t.Helper()
p := filepath.Join(t.TempDir(), "file")
err := os.WriteFile(p, []byte(contents), 0600)
err := os.WriteFile(p, []byte(contents), 0o600)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions gopages/internal/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
)

const (
scrapeDirPermission = 0700
scrapeFilePermission = 0600
scrapeDirPermission = 0o700
scrapeFilePermission = 0o600
)

var (
Expand Down
14 changes: 7 additions & 7 deletions gopages/internal/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ func JSONFunc() {
outputFS := memfs.New()
writeFile := func(path, contents string) {
path = filepath.Join(thing, path)
err := os.MkdirAll(filepath.Dir(path), 0700)
err := os.MkdirAll(filepath.Dir(path), 0o700)
require.NoError(t, err)
err = os.WriteFile(path, []byte(contents), 0600)
err = os.WriteFile(path, []byte(contents), 0o600)
require.NoError(t, err)
}

Expand Down Expand Up @@ -343,7 +343,7 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
t.Parallel()
outputFS := memfs.New()
const outputDir = "foo"
require.NoError(t, outputFS.MkdirAll(outputDir, 0700))
require.NoError(t, outputFS.MkdirAll(outputDir, 0o700))
err := generateDocs(t, outputFS, outputDir)
assert.EqualError(t, err, outputPathOKError)
})
Expand All @@ -352,7 +352,7 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
t.Parallel()
outputFS := memfs.New()
const outputDir = "foo"
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0700))
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0o700))
err := generateDocs(t, outputFS, outputDir)
assert.EqualError(t, err, `pipe: refusing to clean output directory "foo" - directory does not resemble a gopages result; remove the directory to continue`)
})
Expand All @@ -361,8 +361,8 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
t.Parallel()
outputFS := memfs.New()
const outputDir = "foo"
require.NoError(t, outputFS.MkdirAll(outputDir, 0700))
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0700)) // include other contents, which are ignored if expected files present
require.NoError(t, outputFS.MkdirAll(outputDir, 0o700))
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, "bar"), 0o700)) // include other contents, which are ignored if expected files present
f, err := outputFS.Create(outputFS.Join(outputDir, "index.html"))
require.NoError(t, err)
require.NoError(t, f.Close())
Expand All @@ -371,7 +371,7 @@ func TestGenerateDocsAvoidOverwritingExistingOutput(t *testing.T) {
"pkg",
"src",
} {
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, dirName), 0700))
require.NoError(t, outputFS.MkdirAll(outputFS.Join(outputDir, dirName), 0o700))
}

err = generateDocs(t, outputFS, outputDir)
Expand Down
21 changes: 13 additions & 8 deletions gopages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,16 @@ var generateMemfsDocsPipe = pipe.New(pipe.Options{}).
return args[0].(memfsDocsArgs)
}).
Append(func(args memfsDocsArgs) (memfsDocsArgs, *git.Repository, error) {
repo, err := git.Clone(memory.NewStorage(), args.FS, &git.CloneOptions{
cloneOpts := &git.CloneOptions{
URL: args.Remote,
ReferenceName: plumbing.NewBranchReferenceName(ghPagesBranch),
SingleBranch: true,
Auth: getAuth(args.Flags),
})
}
basicAuth, ok := getAuth(args.Flags)
if ok {
cloneOpts.Auth = basicAuth
}
repo, err := git.Clone(memory.NewStorage(), args.FS, cloneOpts)
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")
}).
Append(func(args memfsDocsArgs, repo *git.Repository) (memfsDocsArgs, *git.Repository, *git.Worktree, error) {
Expand Down Expand Up @@ -148,17 +152,18 @@ var generateMemfsDocsPipe = pipe.New(pipe.Options{}).
}).
Append(func(args memfsDocsArgs, repo *git.Repository) error {
pushOpts := &git.PushOptions{}
pushOpts.Auth = getAuth(args.Flags)
if basicAuth, ok := getAuth(args.Flags); ok {
pushOpts.Auth = basicAuth
}
err := repo.Push(pushOpts)
return errors.Wrap(err, "Failed to push gopages commit")
})

func getAuth(args flags.Args) *gitHTTP.BasicAuth {
var auth *gitHTTP.BasicAuth
func getAuth(args flags.Args) (*gitHTTP.BasicAuth, bool) {
if args.GitHubPagesUser != "" || args.GitHubPagesToken != "" {
auth = &gitHTTP.BasicAuth{Username: args.GitHubPagesUser, Password: args.GitHubPagesToken}
return &gitHTTP.BasicAuth{Username: args.GitHubPagesUser, Password: args.GitHubPagesToken}, true
}
return auth
return nil, false
}

func run(modulePath string, args flags.Args) error {
Expand Down
39 changes: 30 additions & 9 deletions gopages/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,27 @@ func testRun(t *testing.T, tc testRunTestCase) {
ghPagesDir, err := os.MkdirTemp("", "")
require.NoError(t, err)
defer os.RemoveAll(ghPagesDir)
ghPagesRepo, err := git.PlainInit(ghPagesDir, false)
const defaultBranch = "refs/heads/main"
ghPagesRepo, err := git.PlainInitWithOptions(ghPagesDir, &git.PlainInitOptions{
InitOptions: git.InitOptions{
DefaultBranch: defaultBranch,
},
})
require.NoError(t, err)
workTree, err := ghPagesRepo.Worktree()
require.NoError(t, err)
_, err = workTree.Commit("Initial commit", &git.CommitOptions{
Author: commitAuthor(),
Author: commitAuthor(),
AllowEmptyCommits: true,
})
require.NoError(t, err)
require.NoError(t, workTree.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(ghPagesBranch),
Create: true,
}))
require.NoError(t, workTree.Checkout(&git.CheckoutOptions{
Branch: defaultBranch,
}))

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

writeFile := func(path, contents string) {
path = filepath.Join(modulePath, path)
err := os.MkdirAll(filepath.Dir(path), 0700)
err := os.MkdirAll(filepath.Dir(path), 0o700)
require.NoError(t, err)
err = os.WriteFile(path, []byte(contents), 0600)
err = os.WriteFile(path, []byte(contents), 0o600)
require.NoError(t, err)
}

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

func TestAuth(t *testing.T) {
t.Parallel()
assert.Nil(t, getAuth(flags.Args{}))
assert.Equal(t,
&gitHTTP.BasicAuth{Username: "user", Password: "token"},
getAuth(flags.Args{
t.Run("no auth flags", func(t *testing.T) {
t.Parallel()
basicAuth, ok := getAuth(flags.Args{})
assert.Nil(t, basicAuth)
assert.False(t, ok)
})

t.Run("basic auth flags", func(t *testing.T) {
t.Parallel()
basicAuth, ok := getAuth(flags.Args{
GitHubPagesToken: "token",
GitHubPagesUser: "user",
}))
})
assert.Equal(t, &gitHTTP.BasicAuth{Username: "user", Password: "token"}, basicAuth)
assert.True(t, ok)
})
}

0 comments on commit 7150f7c

Please sign in to comment.