Skip to content

feat(bare-repo): add worktrunk.worktree-path git-config key (split from #2951) - #2953

Closed
worktrunk-bot wants to merge 4 commits into
mainfrom
feat/worktree-path-git-config
Closed

worktrunk-bot wants to merge 4 commits into
mainfrom
feat/worktree-path-git-config

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

The bare-repo worktree-path prompt and the user-config resolution chain treat all clones of the same repo as equivalent.

When wt switch detects a bare repo at a hidden path (.git, .bare) and the default template would produce broken paths like myproject/.git.main, accepting the prompt currently writes:

# ~/.config/worktrunk/config.toml
[projects."github.com/owner/repo"]
worktree-path = "{{ repo_path }}/../{{ branch | sanitize }}"

The [projects."..."] key is derived from the remote URL, so it matches every local clone of that repository. A user with both a bare clone (myproject/.git) and a regular clone of the same repo has the worktree-path template silently changed for the regular clone too, breaking its layout.

The correct scope for "this specific clone" is the repo's git config (.git/config), which is already used by worktrunk for other per-clone state (worktrunk.default-branch, worktrunk.skip-bare-repo-prompt, etc.).


Solution

1. Add worktrunk.worktree-path git-config key

Switch offer_bare_repo_worktree_path_fix to write worktrunk.worktree-path to the repo's git config instead of the user-config per-project stanza. Stored in .git/config, scoped to this clone only.

Make the prompt idempotent: if worktrunk.worktree-path is already set in git config, skip the prompt entirely.

The non-interactive warning and success message become:

↳ To place worktrees at camel/main, run:
  git config worktrunk.worktree-path "{{ repo_path }}/../{{ branch | sanitize }}"
…
✓ Set worktrunk.worktree-path for ~/Projects/Opensource/camel (this clone only):

2. Thread worktrunk.worktree-path into the resolution chain

format_path in UserConfig now checks sources in priority order:

Priority Source Notes
1 WORKTRUNK_WORKTREE_PATH env var Already supported; checked explicitly to win over git config
2 worktrunk.worktree-path in git config New — per-clone override
3 [projects."..."].worktree-path in user config Per remote URL
4 worktree-path in user config Global
5 Built-in default —

The env var is checked explicitly (not just via the overlay mechanism) so it correctly wins over a git config value when both are set.

3. Suppress the trailing hint when git config is set

maybe_print_worktree_path_hint (the "To customize worktree locations, run wt config create" hint) now also checks worktrunk.worktree-path in git config, so the user doesn't see contradictory advice after the prompt just set it.

4. Document the three scopes

docs/content/config.md, wt config create --help, and the dev-config example get a new "Scope options" section showing the per-clone / per-remote / global table. The tips & patterns bare-repo section is updated to lead with the git-config approach and explain why multi-clone setups prefer it.


Note on scope

This PR is the more invasive half of the original #2951. @max-sixty asked to split it out for separate review since it adds a new exception in the config-resolution chain.

Stacked on #2951 — the diff here will become smaller once #2951 lands.


Testing

  • test_bare_repo_worktree_path_prompt_skipped_when_git_config_already_set — new test confirming the prompt skips when git config already has the key.
  • test_bare_repo_worktree_path_prompt_auto_accept / ..._non_interactive_warning / the three PTY prompt snapshots — updated to show the git-config write/command.
  • test_docs_are_in_sync passes.

Co-authored with @ammachado from the original PR.

worktrunk-bot and others added 2 commits June 1, 2026 07:28
The bare-repo `worktree-path` prompt previously hardcoded `feature-auth` in
the example paths regardless of the branch being switched to, which produced
a confusing mismatch (switching to `main` showed `project/.git.feature-auth`).
Use the branch identifier instead so the example matches reality.

Symbolic identifiers (`-`, `@`, `^`, `pr:N`, `mr:N`) aren't real branch
names yet — the prompt would show the literal symbol in the example path.
Skip the prompt for those; it surfaces on the next switch with a concrete
branch.

Also fix `wt config create --project` in a bare repo with no linked
worktrees yet: instead of the bare "Cannot determine project config
location — no worktree found" error, point the user at the right next
step ("run `wt switch <branch>` to create a worktree first").

Restructure the Tips & Patterns bare-repo section to walk through the
automatic prompt, the project-scoped config it writes, and the
`wt config create --project` follow-up.

Co-Authored-By: Adriano Machado <[email protected]>
Co-Authored-By: Claude Opus 4.7 <[email protected]>
The `[projects."github.com/owner/repo"]` user-config stanza is derived
from the remote URL and applies to every local clone of that repository.
A user with both a bare clone (`myproject/.git`) and a regular clone of
the same repo cannot give them different layouts via the per-project
TOML; setting it for one clobbers the other.

Add a per-clone scope: `worktrunk.worktree-path` in the repo's
`.git/config`. The resolution chain becomes env var → git config →
user config (per-remote or global) → default, so the per-clone setting
overrides the per-remote default for that clone only.

Switch the bare-repo `worktree-path` prompt to write to git config when
accepted, since the prompt diagnoses a clone-specific problem. Skip the
prompt entirely if `worktrunk.worktree-path` is already set in git
config (idempotent on re-run). Suppress the trailing
`wt config create` hint when the prompt has already set git config, so
the user doesn't see contradictory advice.

Document the three scopes (per-clone, per-remote, global) in `wt config
create --help`, the docs site config page, and the tips & patterns
bare-repo section.

Co-Authored-By: Adriano Machado <[email protected]>
Co-Authored-By: Claude Opus 4.7 <[email protected]>
@worktrunk-bot
worktrunk-bot force-pushed the feat/worktree-path-git-config branch from b1f38ba to 32a3ac7 Compare June 1, 2026 07:30
@max-sixty

Copy link
Copy Markdown
Owner

@ammachado thanks for the other PR!

for this: what is the argument for it? that someone has multiple clones of the same repo and some are bare and some are not?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Yes — the trigger is "user has (or will have) more than one clone of this repo, with at least one of them bare". The concrete failure mode the PR description sketches:

  • A bare clone at ~/repos/foo/.git plus a regular clone of foo somewhere else (for an IDE that doesn't play nicely with bare, or a stable layout for testing).
  • Accepting the bare-repo prompt today writes [projects."github.com/owner/repo"].worktree-path = "{{ repo_path }}/../{{ branch | sanitize }}" to user config. The [projects."..."] key is keyed by remote URL, so it also applies to the regular clone, where that template is wrong.

Putting the override in .git/config keeps the fix scoped to the clone that needs it. It's also the same scope worktrunk already uses for per-clone state: worktrunk.default-branch (resolution cache) and worktrunk.skip-bare-repo-prompt (set when the user declines the prompt). Both exist for the same reason — a value that only makes sense for this checkout shouldn't be written to a scope that fans out across every clone of the repo.

A secondary motivation is that it lets a user with two bare clones at different paths choose different layouts without one stomping the other, but the multi-clone-mixed-layout case is the main one.

Resolve conflicts after #2951 landed. All conflicts kept the PR-side
(this-clone git-config) approach since that is the PR's purpose; main's
remaining changes here were the older user-config behavior that this PR
replaces.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review on the second commit (feat(bare-repo): add worktrunk.worktree-path git-config-scope key). The first commit landed via #2951 and is already on main. The git-config-scope design is clean — env > git config > user config (per-remote or global) > default, with the prompt writing into .git/config so multi-clone setups can diverge. Two findings:

1. set_project_worktree_path is now dead code. The only production caller was offer_bare_repo_worktree_path_fix in src/commands/worktree/resolve.rs, which the second commit switched to repo.set_config("worktrunk.worktree-path", …). After this PR the function in src/config/user/mutation.rs:134 is only referenced by its own tests (test_set_project_worktree_path and test_set_project_worktree_path_noop_when_unchanged in src/config/user/tests.rs). Per CLAUDE.md ("Don't suppress warnings — delete the code"), this should go in this PR — both mutation.rs:131-148 and the two test functions. Reading the user config still works via load → parse, so removing the setter doesn't affect the per-remote scope as a read source.

2. format_toml used for a shell command (src/commands/worktree/resolve.rs:276-281):

eprintln!(
    "{}",
    format_toml(&format!(
        "git config worktrunk.worktree-path \"{BARE_REPO_WORKTREE_PATH}\""
    ))
);

format_toml is the TOML syntax highlighter (synoptic with the TOML extension); git config <key> <value> is a shell command. With colour enabled, the quoted template lights up as a TOML string while the rest is dimmed as identifiers — fine in NO_COLOR snapshots but mismatched in the real terminal. The corresponding helper for this is format_bash_with_gutter, already used for shell commands in src/git/error.rs:980 and src/output/shell_integration.rs:361. Swapping it here also needs the import (src/commands/worktree/resolve.rs:14-16) updated to bring format_bash_with_gutter in (and drop format_toml if no longer used, though it still is for the accepted-message / preview blocks at lines 302 and 332 which legitimately render TOML-shaped key = "value" lines).

Marking as comment rather than approve since GitHub blocks self-approval.

…highlighting

`set_project_worktree_path` lost its sole production caller when
`offer_bare_repo_worktree_path_fix` switched to writing
`worktrunk.worktree-path` to git config — remove the setter and its
two tests. Reading the per-remote scope still works via load → parse.

Use `format_bash_with_gutter` instead of `format_toml` for the
`git config worktrunk.worktree-path "…"` snippet shown in the
non-interactive warning — it's a shell command, not a TOML fragment.
The TOML-shaped `worktrunk.worktree-path = "…"` preview/accepted
messages keep `format_toml`.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Pushed a86d15e43 addressing both findings:

  1. Dead code removed — set_project_worktree_path and its two tests deleted (src/config/user/mutation.rs, src/config/user/tests.rs). The per-remote scope still resolves as a read source via load → parse.
  2. Shell highlighting fix — line 278 in src/commands/worktree/resolve.rs switched to format_bash_with_gutter. The two TOML-shaped previews at lines 302/332 (worktrunk.worktree-path = "…") keep format_toml. The four affected snapshots passed without changes since they capture NO_COLOR output where both helpers produce identical gutter+content text.

@max-sixty

Copy link
Copy Markdown
Owner

this splits the way configs work for a benefit that seems extremely marginal, unless I'm misunderstanding

if folks can describe why they would use this, happy to reopen (hope this is reasonable)

@max-sixty max-sixty closed this Jun 3, 2026
@max-sixty
max-sixty deleted the feat/worktree-path-git-config branch June 18, 2026 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants