Skip to content
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
3 changes: 1 addition & 2 deletions .lint-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ golangci:
- gocognit # 1
- revive # 1
- path: ^git/commits\.go$
linters: # 26 findings
- noctx # 10
linters: # 16 findings
- gosec # 8
- varnamelen # 3
- gochecknoglobals # 2
Expand Down
14 changes: 7 additions & 7 deletions cmd/git-validation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func main() {

// shutdown runs the command and exits with the status an ExitError asks
// for, silently, and 1 for any other error, which it logs.
shutdown.Run(shutdown.SetDefaults(context.Background()), func(context.Context) error {
return run(&flags)
shutdown.Run(shutdown.SetDefaults(context.Background()), func(ctx context.Context) error {
return run(ctx, &flags)
})
}

func run(flags *cli) error {
func run(ctx context.Context, flags *cli) error {
if flags.ListRules {
for _, r := range validate.RegisteredRules {
fmt.Printf("%q -- %s\n", r.Name, r.Description)
Expand Down Expand Up @@ -101,7 +101,7 @@ func run(flags *cli) error {
}
}

slog.Debug("rules to run", "rules", ruleNames(rules))
slog.DebugContext(ctx, "rules to run", "rules", ruleNames(rules))

commitRange := flags.Range
if commitRange == "" {
Expand All @@ -118,9 +118,9 @@ func run(flags *cli) error {
}
}

slog.Debug("using commit range", "range", commitRange)
slog.DebugContext(ctx, "using commit range", "range", commitRange)

runner, err := validate.NewRunner(flags.Dir, rules, commitRange)
runner, err := validate.NewRunner(ctx, flags.Dir, rules, commitRange)
if err != nil {
return fmt.Errorf("preparing the run: %w", err)
}
Expand All @@ -129,7 +129,7 @@ func run(flags *cli) error {
report(os.Stdout, commit, results, flags.Verbose, flags.Quiet)
}

if err := runner.Run(); err != nil {
if err := runner.Run(ctx); err != nil {
return fmt.Errorf("validating commits: %w", err)
}

Expand Down
113 changes: 78 additions & 35 deletions git/commits.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"context"
"errors"
"fmt"
"log/slog"
Expand All @@ -15,16 +16,18 @@ import (
// If commitrange is a git still range 12345...54321, then it will be isolated set of commits.
// If commitrange is a single commit, all ancestor commits up through the hash provided.
// If commitrange is an empty commit range, then nil is returned.
func Commits(commitrange string) ([]CommitEntry, error) {
func Commits(ctx context.Context, commitrange string) ([]CommitEntry, error) {
// TEST if it has _enough_ of a range from rev-list
commitrange, err := checkRevList(commitrange)
commitrange, err := checkRevList(ctx, commitrange)
if err != nil {
return nil, fmt.Errorf("validating the commit range: %w", err)
}

cmdArgs := []string{"git", "rev-list", commitrange}

output, err := command(cmdArgs[1:]...).Output()
output, err := command(ctx, cmdArgs[1:]...).Output()

err = ended(ctx, err)
if err != nil {
return nil, fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err)
}
Expand All @@ -37,7 +40,7 @@ func Commits(commitrange string) ([]CommitEntry, error) {

commits := make([]CommitEntry, len(commitHashes))
for i, commitHash := range commitHashes {
c, err := LogCommit(commitHash)
c, err := LogCommit(ctx, commitHash)
if err != nil {
return commits, err
}
Expand All @@ -57,14 +60,21 @@ var errRange = errors.New("the commit range does not resolve")
// what the repository had, so a typo, or a shallow clone whose depth did not
// reach the range's start, validated the whole history and passed. In a
// shallow clone the error says what to fetch.
func checkRevList(commitrange string) (string, error) {
func checkRevList(ctx context.Context, commitrange string) (string, error) {
cmdArgs := []string{"git", "rev-list", commitrange}

_, err := command(cmdArgs[1:]...).Output()
_, err := command(ctx, cmdArgs[1:]...).Output()

err = ended(ctx, err)
if err == nil {
return commitrange, nil
}

// A git that was stopped says nothing of the range.
if ctx.Err() != nil {
return "", fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err)
}

// The git command that failed is named, and its exit unwraps, as every
// git failure here does; git's own words follow.
detail := ""
Expand All @@ -73,12 +83,12 @@ func checkRevList(commitrange string) (string, error) {
detail = " (" + strings.TrimSpace(string(exitErr.Stderr)) + ")"
}

if shallowRepository() {
if shallowRepository(ctx) {
detail += "; the repository is a shallow clone"

// A typo and an end past the depth look the same from here: both
// are not in the clone.
switch missing := missingEnds(commitrange); len(missing) {
switch missing := missingEnds(ctx, commitrange); len(missing) {
case 0:
case 1:
detail += " and " + missing[0] + " is not in it"
Expand All @@ -94,15 +104,16 @@ func checkRevList(commitrange string) (string, error) {

// missingEnds returns the ends of a range (a..b, a...b, or a single revision)
// that do not name a commit the repository has.
func missingEnds(commitrange string) []string {
func missingEnds(ctx context.Context, commitrange string) []string {
var missing []string

for end := range strings.SplitSeq(strings.ReplaceAll(commitrange, "...", ".."), "..") {
if end == "" {
continue
}

if err := command("rev-parse", "--verify", "--quiet", end+"^{commit}").Run(); err != nil {
err := command(ctx, "rev-parse", "--verify", "--quiet", end+"^{commit}").Run()
if err != nil {
missing = append(missing, end)
}
}
Expand All @@ -112,8 +123,9 @@ func missingEnds(commitrange string) []string {

// shallowRepository reports whether the repository was cloned with a depth,
// so that its history may not reach a range's start.
func shallowRepository() bool {
output, err := command("rev-parse", "--is-shallow-repository").Output()
func shallowRepository(ctx context.Context) bool {
output, err := command(ctx, "rev-parse", "--is-shallow-repository").Output()
err = ended(ctx, err)

return err == nil && strings.TrimSpace(string(output)) == "true"
}
Expand Down Expand Up @@ -143,11 +155,13 @@ var FieldNames = map[string]string{
"%G?": "verification_flag",
}

func gitVersion() (string, error) {
cmd := command("version")
func gitVersion(ctx context.Context) (string, error) {
cmd := command(ctx, "version")
cmd.Stderr = os.Stderr

buf, err := cmd.Output()

err = ended(ctx, err)
if err != nil {
return "", fmt.Errorf("git version: %w", err)
}
Expand All @@ -158,8 +172,8 @@ func gitVersion() (string, error) {
// https://github.com/vbatts/git-validation/issues/37
var versionWithExcludes = "1.9.5"

func gitVersionNewerThan(otherV string) (bool, error) {
gv, err := gitVersion()
func gitVersionNewerThan(ctx context.Context, otherV string) (bool, error) {
gv, err := gitVersion(ctx)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -208,14 +222,19 @@ func versionNumbers(gitVersion string) ([]int, error) {

// Check warns if changes introduce whitespace errors.
// Returns non-zero if any issues are found.
func Check(commit string) ([]byte, error) {
logs, err := LogUntil(commit)
func Check(ctx context.Context, commit string) ([]byte, error) {
logs, err := LogUntil(ctx, commit)
if err != nil {
return nil, err
}

if len(logs) == 1 {
slog.Debug("skipping the whitespace check of the initial commit, which has no parent", "commit", commit)
slog.DebugContext(
ctx,
"skipping the whitespace check of the initial commit, which has no parent",
"commit",
commit,
)

return []byte{}, nil
}
Expand All @@ -228,7 +247,7 @@ func Check(commit string) ([]byte, error) {
if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
var gitNewEnough bool

gitNewEnough, err = gitVersionNewerThan(versionWithExcludes)
gitNewEnough, err = gitVersionNewerThan(ctx, versionWithExcludes)
if err != nil {
return nil, err
}
Expand All @@ -244,13 +263,15 @@ func Check(commit string) ([]byte, error) {
}
}

cmd := command(args...)
cmd := command(ctx, args...)

cmd.Stderr = os.Stderr

// The output holds what git found when it exits non-zero, which is how
// it says there are whitespace errors: both go back to the caller.
out, err := cmd.Output()

err = ended(ctx, err)
if err != nil {
return out, fmt.Errorf("git log --check %s: %w", commit, err)
}
Expand All @@ -261,12 +282,14 @@ func Check(commit string) ([]byte, error) {
// Show returns the diff of a commit.
//
// NOTE: This could be expensive for very large commits.
func Show(commit string) ([]byte, error) {
cmd := command("--no-pager", "show", commit)
func Show(ctx context.Context, commit string) ([]byte, error) {
cmd := command(ctx, "--no-pager", "show", commit)

cmd.Stderr = os.Stderr

out, err := cmd.Output()

err = ended(ctx, err)
if err != nil {
return out, fmt.Errorf("git show %s: %w", commit, err)
}
Expand All @@ -279,12 +302,14 @@ func Show(commit string) ([]byte, error) {
type CommitEntry map[string]string

// LogUntil returns the array of oneline commits from initial until the provided commit.
func LogUntil(commit string) ([]string, error) {
cmd := command("--no-pager", "log", "--oneline", "--no-show-signature", commit)
func LogUntil(ctx context.Context, commit string) ([]string, error) {
cmd := command(ctx, "--no-pager", "log", "--oneline", "--no-show-signature", commit)

cmd.Stderr = os.Stderr

out, err := cmd.Output()

err = ended(ctx, err)
if err != nil {
return nil, fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err)
}
Expand All @@ -293,15 +318,17 @@ func LogUntil(commit string) ([]string, error) {
}

// LogCommit assembles the full information on a commit from its commit hash.
func LogCommit(commit string) (*CommitEntry, error) {
func LogCommit(ctx context.Context, commit string) (*CommitEntry, error) {
c := CommitEntry{}

for k, v := range FieldNames {
cmd := command("--no-pager", "log", "-1", `--pretty=format:`+k+``, commit)
cmd := command(ctx, "--no-pager", "log", "-1", `--pretty=format:`+k+``, commit)

cmd.Stderr = os.Stderr

out, err := cmd.Output()

err = ended(ctx, err)
if err != nil {
return nil, fmt.Errorf("%s: %w", strings.Join(cmd.Args, " "), err)
}
Expand All @@ -314,10 +341,12 @@ func LogCommit(commit string) (*CommitEntry, error) {
}

// FetchHeadCommit returns the hash of FETCH_HEAD.
func FetchHeadCommit() (string, error) {
func FetchHeadCommit(ctx context.Context) (string, error) {
cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "FETCH_HEAD"}

output, err := command(cmdArgs[1:]...).Output()
output, err := command(ctx, cmdArgs[1:]...).Output()

err = ended(ctx, err)
if err != nil {
return "", fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err)
}
Expand All @@ -326,20 +355,34 @@ func FetchHeadCommit() (string, error) {
}

// HeadCommit returns the hash of HEAD.
func HeadCommit() (string, error) {
func HeadCommit(ctx context.Context) (string, error) {
cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "HEAD"}

output, err := command(cmdArgs[1:]...).Output()
output, err := command(ctx, cmdArgs[1:]...).Output()

err = ended(ctx, err)
if err != nil {
return "", fmt.Errorf("%s: %w", strings.Join(cmdArgs, " "), err)
}

return strings.TrimSpace(string(output)), nil
}

// command returns the git command for args, recorded at debug level.
func command(args ...string) *exec.Cmd {
slog.Debug("running git", "cmd", "git "+strings.Join(args, " "))
// ended is err, or the context's error with it when the context is done: a
// git that was killed because its context ended says "signal: killed", and
// the caller asked whether its context ended.
func ended(ctx context.Context, err error) error {
if err == nil || ctx.Err() == nil {
return err
}

return fmt.Errorf("%w: %w", ctx.Err(), err)
}

// command returns the git command for args, recorded at debug level. git is
// killed when ctx ends.
func command(ctx context.Context, args ...string) *exec.Cmd {
slog.DebugContext(ctx, "running git", "cmd", "git "+strings.Join(args, " "))

return exec.Command("git", args...)
return exec.CommandContext(ctx, "git", args...)
}
12 changes: 6 additions & 6 deletions git/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import (
func TestCommitEntry(t *testing.T) {
newRepository(t, "first commit", "second commit")

head, err := HeadCommit()
head, err := HeadCommit(t.Context())
if err != nil {
t.Fatal(err)
}

commits, err := Commits(head)
commits, err := Commits(t.Context(), head)
if err != nil {
t.Fatal(err)
}

if len(commits) != 2 {
t.Fatalf("Commits(HEAD) = %d commits, want 2", len(commits))
t.Fatalf("Commits(t.Context(), HEAD) = %d commits, want 2", len(commits))
}

for _, commit := range commits {
Expand Down Expand Up @@ -64,12 +64,12 @@ func TestCommandsAreRecordedAtDebug(t *testing.T) {
slog.SetDefault(slog.New(slog.NewTextHandler(&records, &slog.HandlerOptions{Level: slog.LevelDebug})))
t.Cleanup(func() { slog.SetDefault(previous) })

head, err := HeadCommit()
head, err := HeadCommit(t.Context())
if err != nil {
t.Fatal(err)
}

if _, err := Check(head); err != nil {
if _, err := Check(t.Context(), head); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func TestCommitsOutsideARepository(t *testing.T) {
// No repository above it either.
t.Setenv("GIT_CEILING_DIRECTORIES", filepath.Dir(dir))

_, err := Commits("HEAD~1..HEAD")
_, err := Commits(t.Context(), "HEAD~1..HEAD")
if err == nil {
t.Fatal("Commits outside a repository succeeded")
}
Expand Down
Loading
Loading