misc/git_hook: extract shared githook package; auto-rebuild on version bump (#19440)
Pull the hook logic into a reusable githook library package so tailscale/corp can share it via a thin wrapper main instead of keeping a forked copy in sync. The install flow also changes: a wrapper scripts now build the binary and reinstall the git hooks. Pulling new shared code no longer requires re-running the installer. Updates tailscale/corp#39860 Change-Id: I4d606d11c8c883015c190c54e3387a7f9fe4dd32 Signed-off-by: Fernando Serboncini <fserb@tailscale.com>
This commit is contained in:
committed by
GitHub
parent
1fbb834dc3
commit
514d7d28e7
@@ -0,0 +1,62 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package githook
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/sourcegraph/go-diff/diff"
|
||||
)
|
||||
|
||||
var preCommitForbiddenPatterns = [][]byte{
|
||||
// Concatenation avoids tripping the check on this file.
|
||||
[]byte("NOCOM" + "MIT"),
|
||||
[]byte("DO NOT " + "SUBMIT"),
|
||||
}
|
||||
|
||||
// CheckForbiddenMarkers scans the staged diff for forbidden markers
|
||||
// and returns an error if any are found.
|
||||
//
|
||||
// Intended as a pre-commit hook.
|
||||
// https://git-scm.com/docs/githooks#_pre_commit
|
||||
func CheckForbiddenMarkers() error {
|
||||
diffOut, err := exec.Command("git", "diff", "--cached").Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get git diff: %w", err)
|
||||
}
|
||||
|
||||
diffs, err := diff.ParseMultiFileDiff(diffOut)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not parse diff: %w", err)
|
||||
}
|
||||
|
||||
foundForbidden := false
|
||||
for _, d := range diffs {
|
||||
for _, hunk := range d.Hunks {
|
||||
lines := bytes.Split(hunk.Body, []byte{'\n'})
|
||||
for i, line := range lines {
|
||||
if len(line) == 0 || line[0] != '+' {
|
||||
continue
|
||||
}
|
||||
for _, forbidden := range preCommitForbiddenPatterns {
|
||||
if bytes.Contains(line, forbidden) {
|
||||
if !foundForbidden {
|
||||
color.New(color.Bold, color.FgRed, color.Underline).Printf("%s found:\n", forbidden)
|
||||
}
|
||||
fmt.Printf("%s:%d: %s\n", d.NewName[2:], int(hunk.NewStartLine)+i, line[1:])
|
||||
foundForbidden = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if foundForbidden {
|
||||
return errors.New("found forbidden string")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user