From d5e96f5d5bd7e86867c29c9c7dce095d21b9da1f Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Mon, 16 Nov 2026 01:38:04 -0800 Subject: [PATCH] A commit a shallow clone was cut at reads SKIP, not PASS, for whitespace, with a warning In a shallow clone the commit the clone was cut at cannot have its whitespace checked: its parents are not there, and git shows it as adding the whole tree. The rule skipped it, said so under -D only, and reported a pass. A run with no range in a clone of depth 1, as actions/checkout makes by default, so checked no whitespace, printed PASS and said nothing. A result has a third state now: not checked, neither a pass nor a failure. - git.Check returns ErrParentsNotHere for such a commit, where it returned nothing. - The rule's result is not checked. Under -v its line is ` - SKIP - whitespace not checked: the commit's parents are not in this shallow clone`. - The commit's own line, and the exit status, are the verdict of the rules that ran. - A warning on standard error names the commit and what to do, at every verbosity, -q included: fetch the history, or give a range the clone holds. A deeper clone only moves the commit: a run with no range reaches the cut whatever the depth, so the warning asks for the history or a range and not for one more commit. What a consumer of the report sees: one `SKIP` rule line under -v where a `PASS` line was, in a shallow clone. Commit lines and the exit status do not move. TestContractWhitespaceNotCheckedInAShallowClone holds, through the command: a clone of depth 1 under -v, at normal verbosity and under -q (the SKIP line or its absence, the warning with the commit each time, exit 0); every rule together (the commit passes on the others); a clone of depth 2 with no range (the head checked, the cut skipped, one warning) and with a range it holds (checked, nothing logged). TestContractANotCheckedResultIsNeither holds how such a result is counted. Measured one at a time, files restored: with the skipped check reported as a pass, with the warning lowered to debug, and with a not-checked result counted as a failure, the command's test fails each time, and the counting test with the last. UPSTREAM.md's row on -D loses the skipped check, which is no longer a debug record; the README says what a shallow clone prints. For https://github.com/forkcloser/git-validation/issues/93. Co-Authored-By: Claude Fable 5.2 Signed-off-by: closer-claudio --- README.md | 12 +++ UPSTREAM.md | 2 +- cmd/git-validation/contract_test.go | 113 ++++++++++++++++++++++++---- cmd/git-validation/main.go | 13 +++- git/commits.go | 19 +++-- git/contract_test.go | 19 ++--- rules/danglingwhitespace/rule.go | 17 +++++ validate/contract_test.go | 21 ++++++ validate/rules.go | 16 ++-- 9 files changed, 187 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index d71cb07..f88ed50 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,18 @@ vbatts@valse ~/src/vb/git-validation (master) $ GIT_CHECK_EXCLUDE="./vendor:./gi using the `GIT_CHECK_EXCLUDE` environment variable. Multiple paths should be separated by colon(`:`) +In a shallow clone, the commit the clone was cut at has no parent to compare with, so its +whitespace cannot be checked. Under `-v` the rule's line says so, and a warning on standard +error names the commit at every verbosity: + +```text + * 7652388 "A clean change" ... PASS + - SKIP - whitespace not checked: the commit's parents are not in this shallow clone +``` + +The commit's own verdict is that of the rules that ran. Give a `--range` the clone holds with +its parents, or fetch the history (`fetch-depth: 0` with actions/checkout). + ## contributing When making a change, verify it with: diff --git a/UPSTREAM.md b/UPSTREAM.md index 1447725..c859d84 100644 --- a/UPSTREAM.md +++ b/UPSTREAM.md @@ -23,7 +23,7 @@ Fork of https://github.com/vbatts/git-validation (`main`). | On Windows, `GIT_CHECK_EXCLUDE` works (git's version was compared by a parser that refused `2.42.0.windows.2`); hashicorp/go-version leaves the dependencies | https://github.com/forkcloser/git-validation/pull/8 | | Logs through `log/slog` (colored on a terminal, JSON otherwise) instead of logrus; errors name the git command that failed | https://github.com/forkcloser/git-validation/pull/12 | | Flags parsed by kong: long flags take two dashes (`--run`, `--range`; `-run` is refused), `-v`/`-D`/`-q`/`-d` gain long forms, `--no-travis-pr-only` | https://github.com/forkcloser/git-validation/pull/16 | -| `-D` records each git command run, the skipped whitespace check of a commit a shallow clone was cut at and the commit a rangeless run starts from, as debug records through `log/slog` | https://github.com/forkcloser/git-validation/pull/20 | +| `-D` records each git command run and the commit a rangeless run starts from, as debug records through `log/slog` | https://github.com/forkcloser/git-validation/pull/20 | | A `--run` name no rule has is refused, naming it and listing the rules there are, where it ran nothing and passed; a `message_regexp` value that does not compile is refused before any commit is read, with the regexp package's words, where it panicked on the first commit; the final count is of commits with a failing result, not of results; "using commit range" is logged at debug | https://github.com/forkcloser/git-validation/pull/24 | | The short-subject rule counts characters, not bytes, against its limits; a `--range` git cannot resolve is an error naming the range and the failed git command in a repository with its whole history, nothing validated | https://github.com/forkcloser/git-validation/pull/39 | | A `--range` git cannot resolve is an error in a shallow clone too, where it used to validate what the clone had from its oldest commit, so a depth of one passed the head commit alone; the error names the end the clone does not have and what to fetch (`fetch-depth: 0`, `git fetch --deepen` or `--unshallow`), and a shallow clone deep enough for the range runs as before | https://github.com/forkcloser/git-validation/pull/47 | diff --git a/cmd/git-validation/contract_test.go b/cmd/git-validation/contract_test.go index bc9d165..a8df642 100644 --- a/cmd/git-validation/contract_test.go +++ b/cmd/git-validation/contract_test.go @@ -124,6 +124,15 @@ func (r repo) commit(file, content, message string, signed bool) string { func (r repo) run(env []string, args ...string) (int, string) { r.t.Helper() + code, out, _ := r.runAll(env, args...) + + return code, out +} + +// runAll is run, with what the binary logged to standard error. +func (r repo) runAll(env []string, args ...string) (code int, stdout, logged string) { + r.t.Helper() + cmd := exec.CommandContext(r.t.Context(), binary, args...) cmd.Dir = r.dir @@ -139,13 +148,13 @@ func (r repo) run(env []string, args ...string) (int, string) { switch { case err == nil: - return 0, string(out) + return 0, string(out), stderr.String() case errors.As(err, &exit): - return exit.ExitCode(), string(out) + return exit.ExitCode(), string(out), stderr.String() default: r.t.Fatalf("running %s: %v\n%s", binary, err, stderr.String()) - return -1, "" + return -1, "", "" } } @@ -242,8 +251,7 @@ func TestContractWhitespaceErrorsAreNamed(t *testing.T) { // TestContractWhitespaceIsEachCommitsOwn: a merge is not failed for the // whitespace errors of the commits it brings in, which are not in the -// range; a first commit is checked as any other; the commit a shallow -// clone was cut at is not failed for the tree before it. +// range; a first commit is checked as any other. // //nolint:paralleltest // newRepo sets the environment with t.Setenv func TestContractWhitespaceIsEachCommitsOwn(t *testing.T) { @@ -282,18 +290,95 @@ func TestContractWhitespaceIsEachCommitsOwn(t *testing.T) { if line := " * " + root + " \"Root with a trailing blank\" ... FAIL\n"; code != 1 || !strings.Contains(out, line) { t.Errorf("a first commit with a trailing blank: exit %d, want 1 and %q:\n%s", code, line, out) } +} - // A clone of depth 1, as a CI checks out: its one commit was cut from - // its parents, and is not failed for the tree they left it. - r.git("checkout", "-q", "main") +// TestContractWhitespaceNotCheckedInAShallowClone: the commit a shallow +// clone was cut at cannot have its whitespace checked, and is not said to +// pass it: the rule's line is SKIP under -v, a warning on standard error +// names the commit and what to do, at every verbosity, and the exit +// status is that of the rules that ran; a clone one commit deeper checks +// its head, and a range it holds with its parents skips nothing. +// +//nolint:paralleltest // newRepo sets the environment with t.Setenv +func TestContractWhitespaceNotCheckedInAShallowClone(t *testing.T) { + r := newRepo(t) + + r.commit("a", "a \n", "Root with a trailing blank", true) + middle := r.commit("b", "b\n", "A clean change", true) + last := r.commit("d", "d\n", "Another clean change", true) + full := r.git("rev-parse", "HEAD") + + const ( + skipped = " - SKIP - whitespace not checked: the commit's parents are not in this shallow clone\n" + warning = "whitespace not checked: the commit's parents are not in this shallow clone; " + + "fetch the history (actions/checkout fetch-depth: 0, git fetch --unshallow) or give a range it holds" + ) + + // A clone of depth 1, as a CI checks out. + cut := repo{t: t, dir: filepath.Join(t.TempDir(), "clone")} + r.git("clone", "-q", "--no-local", "--depth", "1", r.dir, cut.dir) + + commit := " * " + last + " \"Another clean change\" ... PASS\n" - last := r.commit("d", "d\n", "A clean change", true) - clone := repo{t: t, dir: filepath.Join(t.TempDir(), "clone")} - r.git("clone", "-q", "--no-local", "--depth", "1", r.dir, clone.dir) + code, out, logged := cut.runAll(nil, "--run", "dangling-whitespace", "-v") + if code != 0 || out != commit+skipped { + t.Errorf("depth 1, -v: exit %d and %q, want 0 and %q", code, out, commit+skipped) + } + + if !strings.Contains(logged, warning) || !strings.Contains(logged, full) { + t.Errorf("depth 1, -v: logged %q, want the warning and the commit %s", logged, full) + } + + code, out, logged = cut.runAll(nil, "--run", "dangling-whitespace") + if code != 0 || out != commit || !strings.Contains(logged, warning) { + t.Errorf("depth 1: exit %d, %q, logged %q; want 0, the commit's line alone and the warning", code, out, logged) + } + + code, out, logged = cut.runAll(nil, "--run", "dangling-whitespace", "-q") + if code != 0 || out != "" || !strings.Contains(logged, warning) || !strings.Contains(logged, full) { + t.Errorf("depth 1, -q: exit %d, %q, logged %q; want 0, nothing and the warning", code, out, logged) + } + + // With every rule, the commit's verdict is that of the rules that ran. + code, out, _ = cut.runAll(nil, "-v") + if code != 0 || !strings.Contains(out, commit) || !strings.Contains(out, skipped) || + strings.Count(out, " - PASS - ") == 0 { + t.Errorf( + "depth 1, every rule: exit %d and %q, want 0, the commit passing, one SKIP and the others' PASS", + code, + out, + ) + } + + // One commit deeper the head has its parent, and is checked. A run with + // no range still reaches the commit the clone was cut at, one further + // back; a range the clone holds with its parents does not. + deeper := repo{t: t, dir: filepath.Join(t.TempDir(), "clone")} + r.git("clone", "-q", "--no-local", "--depth", "2", r.dir, deeper.dir) + + checked := commit + " - PASS - commit does not have any whitespace errors\n" + before := " * " + middle + " \"A clean change\" ... PASS\n" + + code, out, logged = deeper.runAll(nil, "--run", "dangling-whitespace", "-v") + if code != 0 || out != checked+before+skipped || strings.Count(logged, warning) != 1 { + t.Errorf( + "depth 2: exit %d, %q, logged %q; want 0, %q and one warning", + code, + out, + logged, + checked+before+skipped, + ) + } - code, out = clone.run(nil, "--run", "dangling-whitespace", "-v") - if line := " * " + last + " \"A clean change\" ... PASS\n"; code != 0 || !strings.Contains(out, line) { - t.Errorf("a clone of depth 1 over a tree with trailing blanks: exit %d, want 0 and %q:\n%s", code, line, out) + code, out, logged = deeper.runAll(nil, "--run", "dangling-whitespace", "-v", "--range", "HEAD~1..HEAD") + if code != 0 || out != checked || logged != "" { + t.Errorf( + "depth 2, a range it holds: exit %d, %q, logged %q; want 0, %q and nothing logged", + code, + out, + logged, + checked, + ) } } diff --git a/cmd/git-validation/main.go b/cmd/git-validation/main.go index c5eee8c..b047552 100644 --- a/cmd/git-validation/main.go +++ b/cmd/git-validation/main.go @@ -142,16 +142,17 @@ func run(ctx context.Context, flags *cli) error { return nil } -// report prints one commit's results: the commit and its verdict, then each -// failing rule, and each passing one under verbose. Quiet prints nothing for -// a passing commit and one line per failing rule, the hash and the reason, +// report prints one commit's results: the commit and its verdict, which is +// that of the rules that ran, then each failing rule, and under verbose each +// passing one and each that could not be run. Quiet prints nothing for a +// passing commit and one line per failing rule, the hash and the reason, // whatever verbose says. func report(out io.Writer, commit git.CommitEntry, results validate.Results, verbose, quiet bool) { _, fail := results.PassFail() if quiet { for _, res := range results { - if !res.Pass { + if !res.Pass && !res.NotChecked { _, _ = fmt.Fprintf(out, " %s - FAIL - %s\n", commit["abbreviated_commit"], res.Msg) } } @@ -168,6 +169,10 @@ func report(out io.Writer, commit git.CommitEntry, results validate.Results, ver for _, res := range results { switch { + case res.NotChecked: + if verbose { + _, _ = fmt.Fprintf(out, " - SKIP - %s\n", res.Msg) + } case !res.Pass: _, _ = fmt.Fprintf(out, " - FAIL - %s\n", res.Msg) case verbose: diff --git a/git/commits.go b/git/commits.go index ef4a01d..637cbaf 100644 --- a/git/commits.go +++ b/git/commits.go @@ -196,12 +196,17 @@ func versionNumbers(gitVersion string) ([]int, error) { return numbers, nil } +// ErrParentsNotHere is what Check returns for a commit a shallow clone was +// cut at: its parents are not here to tell its change from the tree it +// started from, so nothing was checked. +var ErrParentsNotHere = errors.New("the commit's parents are not in this shallow clone") + // Check runs `git log --check` on the commit alone: the output is what git // found, and the error its exit, 2 when there are whitespace errors. A commit // with no parent is checked whole. A merge answers for nothing: the commits -// it brings in are not its own, and git shows no change for a merge. Nor -// does a commit a shallow clone was cut at: its parents are not here to -// tell its change from the tree it started from. +// it brings in are not its own, and git shows no change for a merge. A commit +// a shallow clone was cut at is not checked, and Check says so with +// ErrParentsNotHere. func Check(ctx context.Context, commit string) ([]byte, error) { var err error @@ -243,13 +248,7 @@ func Check(ctx context.Context, commit string) ([]byte, error) { // git shows a commit whose parents are not here as adding the whole // tree, so what it found there is any commit's before it. if cutAt(ctx, commit) { - slog.DebugContext( - ctx, - "skipping the whitespace check of a commit whose parents are not in this shallow clone", - "commit", commit, - ) - - return nil, nil + return nil, ErrParentsNotHere } return out, fmt.Errorf("git log --check %s: %w", commit, err) diff --git a/git/contract_test.go b/git/contract_test.go index 112211b..4880ea5 100644 --- a/git/contract_test.go +++ b/git/contract_test.go @@ -416,9 +416,9 @@ func TestContractCheckFindsWhitespaceErrors(t *testing.T) { // TestContractCheckOfAFirstCommitAndOfACut: a first commit is checked // whatever log.showRoot says; the commit a shallow clone was cut at is -// not a first commit, and answers for nothing, as its own change cannot -// be told from the tree before it; a clone deep enough to hold the first -// commit checks it, at the very depth of the history too. +// not a first commit, and is not checked, which Check says, as its own +// change cannot be told from the tree before it; a clone deep enough to +// hold the first commit checks it, at the very depth of the history too. // //nolint:paralleltest // newRepo sets the environment with t.Setenv, and the test enters each clone func TestContractCheckOfAFirstCommitAndOfACut(t *testing.T) { @@ -451,16 +451,13 @@ func TestContractCheckOfAFirstCommitAndOfACut(t *testing.T) { t.Fatalf("the premise: a clone of depth %s cut after the first commit", depth) } - if _, err := git.Check(t.Context(), cut); err != nil { - t.Errorf( - "depth %s, the commit the clone was cut at: %v, want none: the tree before it is not its own", - depth, - err, - ) + if _, err := git.Check(t.Context(), cut); !errors.Is(err, git.ErrParentsNotHere) { + t.Errorf("depth %s, the commit the clone was cut at: %v, want ErrParentsNotHere", depth, err) } - if _, err := git.Check(t.Context(), head); err != nil { - t.Errorf("depth %s, a clean commit: %v", depth, err) + // At depth 1 the head is the commit the clone was cut at. + if _, err := git.Check(t.Context(), head); err != nil && head != cut { + t.Errorf("depth %s, a clean commit with its parent: %v", depth, err) } } diff --git a/rules/danglingwhitespace/rule.go b/rules/danglingwhitespace/rule.go index 59f2be0..08219c1 100644 --- a/rules/danglingwhitespace/rule.go +++ b/rules/danglingwhitespace/rule.go @@ -3,6 +3,7 @@ package danglingwhitespace import ( "context" "errors" + "log/slog" "os/exec" "github.com/forkcloser/git-validation/git" @@ -13,6 +14,12 @@ import ( // whitespace errors. const whitespaceErrors = 2 +// notCheckedWarning is logged for a commit whose whitespace could not be +// checked. One commit deeper would only move the cut, so it asks for the +// history or a range. +const notCheckedWarning = "whitespace not checked: the commit's parents are not in this shallow clone; " + + "fetch the history (actions/checkout fetch-depth: 0, git fetch --unshallow) or give a range it holds" + // DanglingWhitespace is the rule for checking the presence of dangling // whitespaces on line endings. var DanglingWhitespace = validate.Rule{ @@ -33,6 +40,16 @@ func ValidateDanglingWhitespace(ctx context.Context, r validate.Rule, c git.Comm vr.Pass = true _, err := git.Check(ctx, c["commit"]) + if errors.Is(err, git.ErrParentsNotHere) { + vr.Pass = false + vr.NotChecked = true + vr.Msg = "whitespace not checked: " + err.Error() + + slog.WarnContext(ctx, notCheckedWarning, "commit", c["commit"]) + + return vr + } + if err != nil { vr.Pass = false diff --git a/validate/contract_test.go b/validate/contract_test.go index 0a253fd..9307e5f 100644 --- a/validate/contract_test.go +++ b/validate/contract_test.go @@ -355,3 +355,24 @@ func TestContractCommitsToFixCountsCommits(t *testing.T) { t.Errorf("PassFail counts %d failing results, want 2", fail) } } + +// TestContractANotCheckedResultIsNeither: a result of a rule that could not +// be run is no pass and no failure, and its commit is not one to fix for it. +func TestContractANotCheckedResultIsNeither(t *testing.T) { + t.Parallel() + + results := validate.Results{ + {CommitEntry: git.CommitEntry{"commit": "a"}, Pass: true}, + {CommitEntry: git.CommitEntry{"commit": "a"}, NotChecked: true}, + {CommitEntry: git.CommitEntry{"commit": "b"}, NotChecked: true}, + {CommitEntry: git.CommitEntry{"commit": "c"}}, + } + + if pass, fail := results.PassFail(); pass != 1 || fail != 1 { + t.Errorf("PassFail() = %d, %d, want 1, 1", pass, fail) + } + + if toFix := results.CommitsToFix(); toFix != 1 { + t.Errorf("CommitsToFix() = %d, want 1", toFix) + } +} diff --git a/validate/rules.go b/validate/rules.go index 65badaf..b7040aa 100644 --- a/validate/rules.go +++ b/validate/rules.go @@ -62,18 +62,24 @@ func Commit(ctx context.Context, c git.CommitEntry, rules []Rule) Results { type Result struct { CommitEntry git.CommitEntry Pass bool - Msg string + // NotChecked says the rule could not be run on the commit: the result is + // neither a pass nor a failure, whatever Pass holds, and Msg says why. + NotChecked bool + Msg string } // Results is a set of results. This is type makes it easy for the following function. type Results []Result -// PassFail gives a quick over/under of passes and failures of the results in this set. +// PassFail gives a quick over/under of passes and failures of the results in +// this set. A result that is not checked is neither. func (vr Results) PassFail() (pass, fail int) { for _, res := range vr { - if res.Pass { + switch { + case res.NotChecked: + case res.Pass: pass++ - } else { + default: fail++ } } @@ -86,7 +92,7 @@ func (vr Results) CommitsToFix() int { failing := map[string]struct{}{} for _, res := range vr { - if !res.Pass { + if !res.Pass && !res.NotChecked { failing[res.CommitEntry["commit"]] = struct{}{} } }