Skip to content

Fix repo name resolving to .git for bare-style worktree setups - #1281

Closed
seakayone wants to merge 11 commits into
max-sixty:mainfrom
seakayone:fix/bare-repo-name-dotgit
Closed

seakayone wants to merge 11 commits into
max-sixty:mainfrom
seakayone:fix/bare-repo-name-dotgit

Conversation

@seakayone

Copy link
Copy Markdown

Summary

  • Fixes {{ repo }} template variable resolving to .git instead of the project name when using bare-style worktree setups (e.g. /project/.git as the common dir with sibling worktrees)
  • When repo_path() detects a bare repo whose path ends in .git, it now returns the parent directory instead

Fixes #1279

Test plan

  • Verify wt -vv list shows correct repo= value in a bare-style worktree setup
  • Verify hook templates using {{ repo }} produce the correct project name
  • Verify actual bare repos (not ending in .git) still work correctly

🤖 Generated with Claude Code

When a worktree-based setup uses `.git` as a bare-style common dir
(e.g. /project/.git with sibling worktrees), `repo_path()` returned
the `.git` directory itself, causing `{{ repo }}` to resolve to ".git"
instead of the project name.

Now detects when a bare repo path ends in `.git` and returns the parent
directory, so the repo name correctly reflects the project folder.

Fixes max-sixty#1279

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@seakayone
seakayone force-pushed the fix/bare-repo-name-dotgit branch from d1bd286 to 518acf1 Compare March 5, 2026 07:15
Christian Kleinbölting and others added 2 commits March 5, 2026 08:19
Verifies that {{ repo }} resolves to the project directory name
("project") and not ".git" when using a nested bare repo setup
(project/.git pattern).

Relates to max-sixty#1279

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

this might be a bit purist — but I'm a bit skeptical of choosing different paths based on the name. What if someone names it .bare? then we pick a different path?

I previously had avoided trying to identify the parent because I'm not sure there is a reliable way of doing that. I agree .git is a reasonable heuristic, but it also seems like it would be quite confusing when the heuristic fails. Instead, the docs point to using a different worktree default path: https://worktrunk.dev/tips-patterns/#bare-repository-layout

Is there a better way?

(and a disclaimer that I haven't got onto using bare repos, so I might be misunderstand)

@seakayone

seakayone commented Mar 5, 2026 •

Copy link
Copy Markdown
Author

Unfortunately git does not itself have the concept of a repo name.
wt uses the folder name as a heuristic for determining it's repo.

The problem arises when someone checks out the repo in a folder which is named quite generic, like .bare, .git as described in the wt tipps. And this is most probable if one is using the bare approach where a parent folder contains the bare repo in a hidden folder. Theoratically you can checkout the bare repo in a properly named folder myproject which would then contain the git internals and the worktree folders, this case is not a problem.

I think assuming that when the repo was checked out in a hidden folder starting with . might be better heuristic than only matching .git. If the folder is hidden, then choose the parent folder name.

Another approach might be to also look at the remotes and use their url for crafting a repo name. IMHO this is even more tricky as remotes are optional, there might be more than one and parsing the url is a challenge for itself.

One more option could be to make this configurable in the project's wt.toml.

@max-sixty

max-sixty commented Mar 5, 2026 •

Copy link
Copy Markdown
Owner

should we be relying on {{ repo }} in this case? in the docs [linked above] we suggest, for bare repos:

Configure worktrunk to create worktrees as subdirectories:

# ~/.config/worktrunk/config.toml
worktree-path = "../{{ branch | sanitize }}"

I'd be -1 on adding heuristics for paths. So I think that leaves two options:

  • find a better default worktree path, that works with both bare and normal repos
  • do it through config (current solution)

any ideas for the former? I agree we also can't use the remote for this, unfortunately. Is there any git command that can resolve repo for both bare and normal repos?

@worktrunk-bot any ideas??

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Good question — I don't think there's a git command that reliably resolves a human-meaningful "repo name" for both bare and normal repos. Git simply doesn't have that concept.

Here are the options I see, roughly in order of robustness:

1. Config override (repo-name in wt.toml) — Most reliable, zero heuristics. Users who hit this problem can set repo-name = "myproject" and it Just Works. Downside: it's opt-in, so the default experience for bare repos stays broken.

2. Remote URL fallback — When repo_path().file_name() looks unhelpful (hidden dir like .git, .bare), parse origin's URL for the repo name. git remote get-url origin is local-only (no network), and URL parsing is well-understood (foo/bar.git → bar). Downside: not all repos have remotes, though in practice the intersection of "bare repo + worktrees + no remote" is tiny.

3. Improve the default worktree-path to not need {{ repo }} — The current default is {{ repo_path }}/../{{ repo }}.{{ branch | sanitize }}. For bare repos this produces nonsense regardless of heuristics. A bare-repo-aware default (or just documenting the override, which the docs already do) sidesteps the problem.

I'd lean toward option 1 (config override) as the simplest reliable fix — it's a small addition and gives users full control. Could combine with option 2 as a best-effort default so most bare-repo users get a reasonable name without config. That avoids the fragility concern with this PR's path-name matching while still improving the out-of-box experience.

@max-sixty

Copy link
Copy Markdown
Owner
  1. Improve the default worktree-path to not need {{ repo }} — The current default is {{ repo_path }}/../{{ repo }}.{{ branch | sanitize }}. For bare repos this produces nonsense regardless of heuristics. A bare-repo-aware default (or just documenting the override, which the docs already do) sidesteps the problem.

right, but are there viable alternatives that would produce the same result for normal repos and reasonable results for bare repos?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

The fundamental challenge is that normal repos and bare repos have different ideal worktree layouts:

Normal: worktrees as siblings of the repo directory

/projects/myrepo/           ← main worktree (repo_path)
/projects/myrepo.feature/   ← linked worktree

Bare: worktrees as children of the parent directory (siblings of .git/.bare)

/projects/myrepo/
  .git/                     ← bare repo (repo_path = here, or parent with this PR)
  main/                     ← worktree
  feature/                  ← worktree

A single template can't naturally produce both patterns. The current default ({{ repo_path }}/../{{ repo }}.{{ branch | sanitize }}) is designed for the normal layout — it creates <parent>/<repo>.<branch> siblings.

Two approaches that could work without heuristics:

  1. Expose is_bare as a template variable and make the default conditional:

    {% if is_bare %}{{ repo_path }}/../{{ branch | sanitize }}{% else %}{{ repo_path }}/../{{ repo }}.{{ branch | sanitize }}{% endif %}
    

    For bare repos this puts worktrees as siblings of the git dir (e.g., /projects/myrepo/feature-foo). For normal repos the behavior is unchanged. Downside: ugly default, and bare repo users who want the repo.branch naming pattern would need to override.

  2. Just use repo_path as the anchor with a relative template:

    {{ repo_path }}/../{{ branch | sanitize }}
    

    This works for bare repos (produces /projects/myrepo/feature-foo) but changes the normal-repo behavior — worktrees become /projects/feature-foo instead of /projects/myrepo.feature-foo. Probably worse.

Neither is a clean drop-in improvement. I think the most practical path forward is:

  • Keep the current default (works great for normal repos, which are the majority)
  • Improve {{ repo }} resolution for bare repos so the default does produce something reasonable — either through this PR's approach (path-name matching) or the remote URL fallback
  • Document the worktree-path config override for users who want a different layout

The remote URL fallback feels like the most robust way to improve {{ repo }} — git remote get-url origin is local-only, and most bare-repo-with-worktrees setups have a remote. It avoids the naming heuristic concern while covering the common case.

@max-sixty

Copy link
Copy Markdown
Owner

if it's driven by just "is it bare", then we could just have repo to mean the parent in the bare case

I thought that the underlying problem here was that there are two common bare layouts, is that not right?

could you lay out all the common layouts and how our variables map against them?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Here are the common layouts and how the current variables resolve for each:

1. Normal repo (non-bare)

/projects/myrepo/          ← main worktree
  .git/                    ← git_common_dir
Variable Value Source
repo_path /projects/myrepo parent(git_common_dir)
repo myrepo file_name(repo_path)

Default template → /projects/myrepo.feature-foo ✅


2. Bare repo in named directory (e.g. git clone --bare)

/projects/myrepo.git/      ← bare git dir = git_common_dir
Variable Value Source
repo_path /projects/myrepo.git git_common_dir (bare)
repo myrepo.git file_name(repo_path)

Default template → /projects/myrepo.git.feature-foo — ugly but functional. Only one common layout here.


3. Bare repo in hidden .git subdirectory (the issue)

This is the layout from popular guides (e.g. Atlassian's bare repo worktree pattern):

/projects/myrepo/
  .git/                    ← bare git dir = git_common_dir
  main/                    ← linked worktree
  feature/                 ← linked worktree
Variable Value (current main) Value (this PR)
repo_path /projects/myrepo/.git /projects/myrepo
repo .git ❌ myrepo ✅

Default template (current) → /projects/myrepo/.git..feature-foo — broken.


4. Bare repo in hidden .bare subdirectory

Another common pattern (used in some dotfiles setups):

/projects/myrepo/
  .bare/                   ← bare git dir = git_common_dir
  main/                    ← linked worktree
  feature/                 ← linked worktree
Variable Value (current main) Value (this PR)
repo_path /projects/myrepo/.bare /projects/myrepo/.bare (unchanged)
repo .bare ❌ .bare ❌

This PR doesn't fix .bare — only .git. Same problem with any hidden directory name.


5. Bare repo at top level (no parent wrapper)

/projects/myrepo/          ← bare git dir = git_common_dir, worktrees elsewhere
Variable Value
repo_path /projects/myrepo
repo myrepo

Works fine — the directory is meaningfully named. This is effectively the same as layout 2 without the .git suffix.


Summary

Layouts 3 and 4 are the two common "bare repo with sibling worktrees" patterns, and they share the same underlying problem: git_common_dir is a hidden subdirectory whose name is meaningless.

@max-sixty — to your point about is_bare driving the logic: for bare repos, repo_path could simply always return parent(git_common_dir) when the directory name starts with . (hidden). That would fix both .git and .bare layouts without relying on exact name matching. And for layout 2 (myrepo.git) and layout 5 (myrepo), the directory isn't hidden, so they'd be unaffected.

The one edge case would be someone who intentionally puts their bare repo in a hidden directory without a meaningful parent (e.g. /projects/.git/ where there's nothing at /projects/ except the git dir). But that seems unlikely in practice — users creating hidden subdirs are almost always using the "wrapper parent" pattern.

@max-sixty

Copy link
Copy Markdown
Owner

ok, so, my current thinking:

  • defining repo as repo_path | parent_if_hidden | name is possible...
    • or even repo_path | parent_if_hidden_and_bare | name
  • but it's going to be extremely confusing if anyone ever has a repo named .foo, which is not impossible (a dotfiles repo?)
  • adding a name config to the project is not ideal, because these are all user configs, and then projects need a new config to fully support worktrunk, which is only useful in a tiny number of cases

is there any synthesis of these problems that solves it?!

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Here's a synthesis I think works:

Use the remote URL as the primary source for bare repos, directory name as fallback.

The resolution order for {{ repo }} would be:

  1. Not bare → file_name(parent(git_common_dir)) (current behavior, works perfectly)
  2. Bare + has origin remote → parse repo name from git remote get-url origin (e.g. github.com/user/myproject.git → myproject)
  3. Bare + no remote → file_name(git_common_dir) (current fallback)

Why this works:

  • No path heuristics — we never guess based on directory names, so .foo repos are fine
  • No config needed — the vast majority of bare-repo-with-worktrees setups have a remote (that's the whole point — you cloned it bare to work on multiple branches)
  • Covers the actual problem — layouts 3 (.git) and 4 (.bare) both have remotes in practice, so they get the right name automatically
  • Zero behavior change for normal repos — step 1 is identical to today
  • The bare+no-remote case is essentially nonexistent — who creates a bare repo with worktrees purely locally? And if they do, they can set worktree-path in their user config as the docs already suggest

git remote get-url origin is local-only (no network), cached via RepoCache, and already called elsewhere in the codebase for forge detection. The only new logic is a URL → repo name parser, which is straightforward (path.split('/').last().trim_end_matches(".git")).

This sidesteps the "confusing when the heuristic fails" concern entirely — there's no heuristic to fail, just a data source that's either present or not.

@seakayone

Copy link
Copy Markdown
Author

@max-sixty I really appreciate your interest in solving this problem and completely understand that a heuristic taking "hidden" files into account does not work for you.

The real problem is trying to have a repo name when git itself does not have the concept of such a name. So any heuristic is potentially inaccurate. Mostly people check out the repo in the same folder as in the url, that is why using the url for bare repos is a good candidate.

Using the remote url of origin would also work for me and the origin remote is the git standard when checking out a new repo so it is available in most cases and would also solve my problem.

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Thanks @seakayone — glad the remote URL approach works for you too.

@max-sixty, it sounds like there's agreement on the direction: use git remote get-url origin for bare repos to resolve {{ repo }}, with the current directory name as fallback. Want me to rework this PR with that approach? The implementation would be:

  1. Not bare → current behavior (unchanged)
  2. Bare + has origin remote → parse repo name from the remote URL
  3. Bare + no remote → file_name(git_common_dir) (current fallback)

No path-name heuristics, no new config — just a data source that's either present or not.

Christian Kleinbölting and others added 8 commits March 9, 2026 14:35
Replace the path-name heuristic (.git parent detection) with remote URL
parsing for bare repos. This avoids fragile directory name matching and
covers both .git and .bare layouts.

Resolution order for repo_name():
1. Not bare → directory name of repo_path (unchanged)
2. Bare + has primary remote → parse repo name from remote URL
3. Bare + no remote → directory name of git_common_dir (fallback)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Resolve conflicts with repo_path() returning Result<&Path>.
Adapt repo_name() to handle the new fallible signature.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Propagate errors from is_bare()/repo_path() instead of silently
falling back to "unknown". All callers already return Result.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The LLM commit prompt used wt.root().file_name() for repo context,
not repo_path().file_name(). Restore the original behavior — this PR
should only change {{ repo }} template variable resolution.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Cover all branches: non-bare directory name, bare with remote URL,
bare without remote fallback, and error propagation from is_bare().

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@seakayone seakayone closed this Mar 10, 2026
@max-sixty

Copy link
Copy Markdown
Owner

coming back to this given the comments on #1279, even though I see it's been closed — not sure if that was deliberate?

thanks @seakayone, appreciate your understanding. I know my concerns are with edge cases (but one hundred 1% edge cases means something is getting hit a lot of the time...)

I'm also skeptical of using the remote; in particular this often hits when first using a repo, when a default remote may not have been set up; and then we get different behavior on something as fundamental as "where worktrees go" as soon as we set up a remote?

I think we may need to escalate this to the user; I can see a couple options:

  • Print a warning when repo resolves to a hidden dir
  • Offer a "seems like this is a bare repo, do you want to set the worktree-path to [.git/..] for this repo" (will think of the exact phrase)
  • Any others?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

You're right to push back on both the path heuristic and remote URL approaches — the remote concern is a good one I hadn't considered. Behavior changing when you add a remote is surprising for something that controls where worktrees go.

The warning + prompt approach feels like the right call. Here's how I'd see it working:

  1. When {{ repo }} resolves to a hidden directory name (starts with .), print a warning the first time it's used in a template expansion:

    warning: {{ repo }} resolved to ".git" — this is a bare repo directory name, not a project name
    hint: set worktree-path in your project config to override:
    hint:   wt.toml → worktree-path = "../{{ branch | sanitize }}"
    
  2. On wt switch (worktree creation), if the resolved path looks wrong because of this, prompt interactively: "This bare repo's {{ repo }} is .git — would you like to set worktree-path for this project?" and write worktree-path = "../{{ branch | sanitize }}" to the project's wt.toml.

The second option is nicer UX — users hit the fix path naturally the first time they create a worktree. And once worktree-path is set in the project config, the warning never fires again.

I'll close #1544 since it uses the path-name heuristic approach. Happy to implement whichever direction you prefer here.

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Good points — the remote URL concern is real. Behavior shifting when you git remote add origin would be surprising, especially for something as fundamental as worktree placement.

I think the "escalate to the user" direction is the right one. The codebase already has the pieces for this:

  • Per-project config overrides — worktree_path_for_project() in src/config/user/accessors.rs already supports project-level worktree-path overrides
  • Interactive prompts — prompt_yes_no_preview() in src/output/prompt.rs
  • Config mutation — with_locked_mutation() pattern in src/config/user/mutation.rs

So a concrete approach:

  1. Detect: when repo resolves to a hidden directory name (starts with .) on a bare repo
  2. Warn + offer: "This bare repo's directory name is {name} — worktree paths using {{ repo }} may not work as expected. Set worktree-path for this project? [y/N]"
  3. If accepted: write a project-level override like worktree-path = "../{{ branch | sanitize }}" to the user's config, keyed by the project identifier

This avoids all the heuristic problems — no guessing repo names, no remote dependency, no behavior that changes over time. The user makes an explicit choice once, and it's persisted in their config. The warning surfaces the problem at the right moment (first wt switch or wt list in a bare repo with a hidden dir name), and the offer provides a one-step fix.

The only question is when to trigger it — probably on any command that would use {{ repo }} in path resolution (worktree creation via wt switch), not on read-only commands like wt list.

worktrunk-bot added a commit that referenced this pull request Mar 16, 2026
Two behavioral issues from this hour's runs:

1. Bot used Edit to replace 3 lines with 1 when a GitHub suggestion only
   asked to change one word on one line (run 23129611560 on PR #1548).
   Add guidance to apply suggestions with exact scope.

2. Two concurrent claude-mention runs both commented on PR #1281 within
   17 seconds (runs 23129315322, 23129320855). Add a dedup check before
   posting comments.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
max-sixty pushed a commit that referenced this pull request Mar 21, 2026
Two behavioral issues from this hour's runs:

1. Bot used Edit to replace 3 lines with 1 when a GitHub suggestion only
   asked to change one word on one line (run 23129611560 on PR #1548).
   Add guidance to apply suggestions with exact scope.

2. Two concurrent claude-mention runs both commented on PR #1281 within
   17 seconds (runs 23129315322, 23129320855). Add a dedup check before
   posting comments.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@Xilis

Xilis commented May 4, 2026 •

Copy link
Copy Markdown

Hey!

I'm not fully sure if this is considered finished, please let me know if I should instead continue in #1279 or if you are not considering further changes here.

Currently, using a bare repo setup like

project/
  .git

you get prompted if you want

worktree-path = "{{ repo_path }}/../{{ branch | sanitize }}"

which works if you want the folder structure of

project/
  .git
  something
  somethingelse

However, if you want

project/
  .git
  project.something
  project.somethingelse

(or something similar to that) I think that is currently not possible, since {{ repo }} resolves to .git.

Based on the discussion so far, would something like just exposing the parent folder (project in this case) as a template variable ({{ parent_dir }} or whatever) be a reasonable solution?

max-sixty added a commit that referenced this pull request May 4, 2026
)

Two new filters expose `Path::parent` and `Path::file_name` to
templates, enabling path traversal that previous filters couldn't
express. They unblock the bare-repo-in-hidden-directory layout
(`myproject/.git`), where `{{ repo }}` resolves to `.git`: users who
want a richer worktree-path naming scheme can now write `{{ repo_path |
parent | name }}` to recover `myproject`.

The interactive bare-repo prompt still suggests the simpler `{{
repo_path }}/../{{ branch | sanitize }}` template, since the bare layout
already nests worktrees inside a repo-specific wrapper directory — no
prefix needed for disambiguation. The filters are there for users with
different layout preferences (#1281 discussion).

Thanks to @seakayone for reporting #1279 and @Xilis for raising the
`parent_dir` question that prompted this approach.

> _This was written by Claude Code on behalf of @max-sixty_

Co-authored-by: Claude <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

great point @Xilis ! this should be fixed in #2592 (review)

max-sixty pushed a commit that referenced this pull request Nov 28, 2026
…#4908)

`{{ repo }}` in commit and squash prompt templates now renders the
repository's name in every worktree. Before, it rendered the current
worktree's folder name, so a custom template run from a linked worktree
at `~/code/myrepo.feature` got `myrepo.feature`. A hook in the same
worktree gets `myrepo`, and [the LLM commits
docs](https://worktrunk.dev/llm-commits/) describe the variable as
"Repository name".

The three prompt sites (`build_commit_prompt`, the squash preview, the
squash run) each derived the name from `wt.root()`. They now share
`llm::prompt_repo_name`, which reads `Repository::repo_path()`, the same
source `build_hook_context` and the worktree-path template use. The
default templates don't reference `{{ repo }}`, so only custom templates
are affected.

One effect to know about: in a nested bare layout (`project/.git` with
worktrees beside it), the prompt's `repo` changes from the worktree's
folder name (e.g. `main`) to `.git`. That's the value hooks already get
there. Naming bare repos better was discussed in #1281, #1544 and #1545,
and this PR doesn't change that.

The new test
`test_prompt_repo_names_the_repository_from_a_linked_worktree` covers
`commit --show-prompt`, `squash --show-prompt`, `squash --dry-run`, and
a real squash, all from a linked worktree. On `main` it fails with
`repo=repo.feature`; with the fix it passes. The `merge::` and
`bare_repository::` integration tests also pass locally, except
`test_merge_no_ff_signs_merge_commit`, which fails the same way on
`main` in this sandbox (ssh refuses the test's signing key because its
file permissions are too open).

Co-authored-by: worktrunk-bot <[email protected]>
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.

In a bare repo layout the template var repo resolves to .git

4 participants