Skip to content
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion UPSTREAM.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
113 changes: 99 additions & 14 deletions cmd/git-validation/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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, "", ""
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
)
}
}

Expand Down
13 changes: 9 additions & 4 deletions cmd/git-validation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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:
Expand Down
19 changes: 9 additions & 10 deletions git/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 8 additions & 11 deletions git/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down
17 changes: 17 additions & 0 deletions rules/danglingwhitespace/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package danglingwhitespace
import (
"context"
"errors"
"log/slog"
"os/exec"

"github.com/forkcloser/git-validation/git"
Expand All @@ -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{
Expand All @@ -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

Expand Down
21 changes: 21 additions & 0 deletions validate/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
16 changes: 11 additions & 5 deletions validate/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
}
Expand All @@ -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{}{}
}
}
Expand Down
Loading