From eded887462eccffa411f434dcd163f96d24cff19 Mon Sep 17 00:00:00 2001 From: Mads Hvelplund Date: Sun, 25 Oct 2026 11:36:52 +0100 Subject: [PATCH] feat(cli): add --no-verify to run git without hooks A global --no-verify flag passes core.hooksPath (pointing at a directory that is never created) on the command line of every git command git-stack runs, so pre-commit, commit-msg, pre-push and the other hooks are skipped for that one invocation. No config is changed, so hooks cannot be left disabled, even if git-stack crashes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HT5Zbe3D4H8mAHGZQxKhqm --- README.md | 6 ++++ internal/cli/app.go | 14 +++++++- internal/cli/noverify_test.go | 60 +++++++++++++++++++++++++++++++++++ internal/cli/root.go | 1 + internal/git/git.go | 18 +++++++++-- internal/git/git_test.go | 20 ++++++++++++ skills/git-stack/SKILL.md | 3 ++ 7 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 internal/cli/noverify_test.go diff --git a/README.md b/README.md index 7c0870c..5d9c2cc 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,12 @@ Work in progress. Available today: This covers every command of gh-stack v0.2.1. Every command takes `--provider` (see [Providers](#providers)). +Every command also takes `--no-verify`, which runs git without hooks (pre-commit, commit-msg, pre-push, ...). This +is like `git commit -n` and `git push --no-verify`, for example when a rebase in an old codebase trips hooks over +unrelated files. The flag applies only to that one command: git-stack passes `core.hooksPath` on git's command line +and never changes the config, so hooks can't be left off, even if git-stack crashes. A paused rebase or modify +doesn't remember the flag, so pass it again to `--continue` (`git stack --no-verify rebase --continue`). + ## Requirements - Git 2.36 or later, on Windows, Linux or macOS diff --git a/internal/cli/app.go b/internal/cli/app.go index fcc74d3..935717c 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "path/filepath" "strings" "time" @@ -36,11 +37,14 @@ type App struct { // Provider is the --provider flag: a provider name, "none", or "" to use // git config or detect it from the remote URL. Provider string + // NoVerify is the --no-verify flag: run git without hooks. + NoVerify bool // NewForge overrides how forges are built (tests use a fake). NewForge func(remote string) (*forge.Forge, error) forges map[string]*forge.Forge lastForge *forge.Forge + hooksPath string in *bufio.Reader } @@ -74,7 +78,15 @@ func (a *App) runTUI(m tea.Model) (tea.Model, error) { return tui.Run(m, a.In, a.Err) } -func (a *App) repo() *git.Repo { return &git.Repo{Dir: a.Dir} } +func (a *App) repo() *git.Repo { + if a.NoVerify && a.hooksPath == "" { + // A directory that is never created, so git finds no hooks. + if dir, err := (&git.Repo{Dir: a.Dir}).CommonDir(); err == nil { + a.hooksPath = filepath.Join(dir, "git-stack-no-hooks") + } + } + return &git.Repo{Dir: a.Dir, HooksPath: a.hooksPath} +} func (a *App) store() (stack.Store, error) { dir, err := a.repo().CommonDir() diff --git a/internal/cli/noverify_test.go b/internal/cli/noverify_test.go new file mode 100644 index 0000000..9f41c98 --- /dev/null +++ b/internal/cli/noverify_test.go @@ -0,0 +1,60 @@ +package cli + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// failingHook installs a hook that always rejects. +func failingHook(t *testing.T, h *harness, name string) { + t.Helper() + hook := filepath.Join(h.repo.Dir, ".git", "hooks", name) + if err := os.MkdirAll(filepath.Dir(hook), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(hook, []byte("#!/bin/sh\necho "+name+" failed >&2\nexit 1\n"), 0o755); err != nil { + t.Fatal(err) + } +} + +// --no-verify skips the pre-commit hook for that command only. +func TestNoVerifySkipsCommitHook(t *testing.T) { + t.Parallel() + h := newHarness(t) + h.must("init", "a") + h.repo.Commit("a.txt", "a\n", "a1") + failingHook(t, h, "pre-commit") + + writeFile(t, h.repo.Dir+"/b.txt", "b\n") + h.must("--no-verify", "add", "-A", "-m", "second layer", "b") + if got := h.repo.Git("log", "-1", "--format=%s"); got != "second layer" { + t.Fatalf("last commit = %q", got) + } + if cfg, err := os.ReadFile(filepath.Join(h.repo.Dir, ".git", "config")); err != nil || strings.Contains(string(cfg), "hooksPath") { + t.Fatalf("config changed (%v): %s", err, cfg) + } + + writeFile(t, h.repo.Dir+"/c.txt", "c\n") + if r := h.run("add", "-A", "-m", "third layer", "c"); r.e == nil || !strings.Contains(r.err+r.e.Error(), "pre-commit failed") { + t.Fatalf("hook did not run without --no-verify: %v %s", r.e, r.err) + } +} + +// --no-verify skips the pre-push hook. +func TestNoVerifySkipsPushHook(t *testing.T) { + t.Parallel() + h := newHarness(t) + h.must("init", "a") + h.repo.Commit("a.txt", "a\n", "a1") + failingHook(t, h, "pre-push") + + if r := h.run("push"); r.e == nil { + t.Fatal("push succeeded although the pre-push hook failed") + } + h.must("--no-verify", "push") + if remote := h.repo.Git("rev-parse", "origin/a"); remote != h.sha("a") { + t.Fatalf("origin/a = %s, want %s", remote, h.sha("a")) + } +} diff --git a/internal/cli/root.go b/internal/cli/root.go index 7d53ce1..26a944b 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -21,6 +21,7 @@ func NewRoot(app *App) *cobra.Command { SilenceErrors: true, } root.PersistentFlags().StringVar(&app.Provider, "provider", "", "pull request provider: github, gitlab, none, or another installed git-stack- plugin (default: git config git-stack.provider, else detected from the remote URL)") + root.PersistentFlags().BoolVar(&app.NoVerify, "no-verify", false, "run git without hooks (pre-commit, commit-msg, pre-push, ...) for this command only; no config is changed") root.SetFlagErrorFunc(flagError) root.SetOut(app.Out) root.SetErr(app.Err) diff --git a/internal/git/git.go b/internal/git/git.go index e601efc..a8b7c5a 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -16,6 +16,10 @@ import ( // Repo runs git commands in Dir. type Repo struct { Dir string + // HooksPath, if set, is passed to every git command as core.hooksPath; + // pointing it at a missing directory runs git without hooks. It only + // lives on the command line, so no config is ever changed. + HooksPath string } // Error is returned when git exits non-zero. @@ -37,7 +41,7 @@ func (e *Error) Unwrap() error { return e.Err } // Run executes git with args and returns trimmed stdout. func (r *Repo) Run(args ...string) (string, error) { - cmd := exec.Command("git", args...) + cmd := exec.Command("git", r.args(args)...) cmd.Dir = r.Dir cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") var stdout, stderr bytes.Buffer @@ -50,7 +54,15 @@ func (r *Repo) Run(args ...string) (string, error) { } // At returns a Repo rooted at dir, e.g. another worktree. -func (r *Repo) At(dir string) *Repo { return &Repo{Dir: dir} } +func (r *Repo) At(dir string) *Repo { return &Repo{Dir: dir, HooksPath: r.HooksPath} } + +// args prepends the per-command config to args. +func (r *Repo) args(args []string) []string { + if r.HooksPath == "" { + return args + } + return append([]string{"-c", "core.hooksPath=" + r.HooksPath}, args...) +} // TopLevel returns the root of the current worktree. func (r *Repo) TopLevel() (string, error) { return r.Run("rev-parse", "--show-toplevel") } @@ -301,7 +313,7 @@ func (r *Repo) ValidBranchName(name string) bool { // RunEnv executes git with extra environment variables. func (r *Repo) RunEnv(env []string, args ...string) (string, error) { - cmd := exec.Command("git", args...) + cmd := exec.Command("git", r.args(args)...) cmd.Dir = r.Dir cmd.Env = append(append(os.Environ(), "GIT_TERMINAL_PROMPT=0"), env...) var stdout, stderr bytes.Buffer diff --git a/internal/git/git_test.go b/internal/git/git_test.go index d08698d..4d7cf69 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -201,3 +201,23 @@ func TestCheckoutMissingBranchNamedLikePath(t *testing.T) { t.Fatal("the uncommitted edit under docs/ was discarded") } } + +func TestHooksPath(t *testing.T) { + t.Parallel() + tr := testutil.NewRepo(t) + hook := filepath.Join(tr.Dir, ".git", "hooks", "pre-commit") + if err := os.MkdirAll(filepath.Dir(hook), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(hook, []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil { + t.Fatal(err) + } + g := &git.Repo{Dir: tr.Dir} + if _, err := g.Run("commit", "--allow-empty", "-m", "hooked"); err == nil { + t.Fatal("commit succeeded although the hook failed") + } + off := (&git.Repo{HooksPath: filepath.Join(tr.Dir, ".git", "no-hooks")}).At(tr.Dir) + if _, err := off.Run("commit", "--allow-empty", "-m", "unhooked"); err != nil { + t.Fatal(err) + } +} diff --git a/skills/git-stack/SKILL.md b/skills/git-stack/SKILL.md index 51e9fe4..cbf1742 100644 --- a/skills/git-stack/SKILL.md +++ b/skills/git-stack/SKILL.md @@ -33,6 +33,9 @@ git stack merge --yes --squash # merge bottom-up, restacking between merg `git stack rebase` to reproduce the conflict and resolve it as above. - `push`, `submit` and `sync` refuse to push a layer whose remote branch has commits the local branch never had (for example an applied review suggestion). Integrate them with `git pull --rebase` on that layer, then retry. +- If git hooks fail on changes unrelated to the work (e.g. old files touched by a rebase), and only if the user agrees + to skip them, pass `--no-verify` to that command (`git stack --no-verify submit --auto`). It applies to one + invocation only, so pass it again to `--continue`. - Never edit the block between `git-stack:begin` and `git-stack:end` in PR descriptions; git-stack rewrites it. - For a self-hosted GitHub or GitLab that is not detected, pass `--provider github` or `--provider gitlab`, or set `git config git-stack.provider ` once. Exit code 9 means the command needs a provider and none applies.