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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down
60 changes: 60 additions & 0 deletions internal/cli/noverify_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}
}
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-<name> 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)
Expand Down
18 changes: 15 additions & 3 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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") }
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions skills/git-stack/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>` once. Exit code 9 means the command needs a provider and none applies.
Expand Down
Loading