chief icon indicating copy to clipboard operation
chief copied to clipboard

feat(config): configurable worktree prompt branch policy

Open Magentron opened this issue 7 months ago • 0 comments

Summary

Replaces the hardcoded "is this main or master?" check that decides whether to prompt the user about creating a git worktree. Two new fields under worktree: in .chief/config.yaml:

  • worktree.alwaysPrompt (bool, default false) — when true, Chief always shows the worktree prompt before starting a loop, regardless of branch name.
  • worktree.promptBranchPattern (string, default "^(main|master)$") — RE2 regex matched against the current branch. When it matches, Chief prompts. Empty string disables matching. Ignored when alwaysPrompt is true. Invalid regex causes a clean startup failure naming the offending field.

Defaults preserve the prior behaviour. Existing config files without the new keys inherit the default pattern on load — the protected-branch safety isn't silently dropped on upgrade. An explicit promptBranchPattern: "" is the documented opt-out.

Both fields are also editable in the Settings TUI (,). The regex field validates inline: invalid input is rejected with invalid regex: … shown under the editor, the previous value is preserved, and the editor stays open for correction. Save validates the same way, so a bad regex never makes it to disk.

Internal cleanup carried with this change

  • Removed the exported git.IsProtectedBranch in favour of an unexported isDefaultBranchName used only inside the git package for diff-base detection. The TUI call site now consults Config.ShouldPromptForWorktree(branch).
  • Renamed DialogProtectedBranch → DialogWorktreePrompt (internal). Dialog title generalised to "⚠️ Worktree Recommended" so it stays accurate when the trigger is a regex match or alwaysPrompt: true on a non-default branch.
  • NewAppWithOptions now returns errors for invalid config (was os.Exit) and reliably releases the fsnotify watcher on early-return paths. prd.Watcher.Stop and prd.ProgressWatcher.Stop now release the underlying fsnotify watcher even when Start was never called, and are idempotent across repeated calls.
  • Save validates before writing, so future code paths can't persist a malformed pattern.
  • config.ValidateBranchPattern is the single source of truth for the regex rule (used by both Load/Save and the Settings TUI).
  • Documentation updated with the new keys, examples (regex pattern, alwaysPrompt), an RE2 syntax note, and the markdown-escape clarification for the default ^(main\|master)$ cell.

Test plan

  • [ ] go test ./... passes (internal/config ≥ 90% coverage on the new code)
  • [ ] On a fresh project (no .chief/config.yaml), starting a loop on main shows the worktree prompt; on feature/x it does not (default-behaviour preserved)
  • [ ] On a project with a legacy .chief/config.yaml containing only worktree.setup: "...", starting a loop on main still shows the worktree prompt (no silent regression on upgrade)
  • [ ] With promptBranchPattern: "^release/.*$" configured, release/v1 triggers the prompt and main does not
  • [ ] With alwaysPrompt: true configured, every branch (including feature/x) shows the prompt; the dialog title reads "Worktree Recommended" without claiming the branch is "protected"
  • [ ] With an explicitly empty promptBranchPattern: "", no branch triggers the prompt unless alwaysPrompt: true
  • [ ] Hand-editing .chief/config.yaml with a malformed regex (e.g. "[unclosed") fails Chief startup with a clear error naming worktree.promptBranchPattern
  • [ ] In the Settings TUI: editing Prompt branch pattern to "[bad" shows invalid regex: … inline, editor stays open, original value preserved; editing to "^release/.*$" saves and exits the editor
  • [ ] In the Settings TUI: toggling Always prompt for worktree persists to .chief/config.yaml

Magentron avatar Apr 29 '26 21:04 Magentron