Repository navigation
Conversation
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]>
d1bd286 to
518acf1
Compare
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]>
|
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 I previously had avoided trying to identify the parent because I'm not sure there is a reliable way of doing that. I agree Is there a better way? (and a disclaimer that I haven't got onto using bare repos, so I might be misunderstand) |
|
Unfortunately git does not itself have the concept of a repo name. The problem arises when someone checks out the repo in a folder which is named quite generic, like I think assuming that when the repo was checked out in a hidden folder starting with 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 |
|
should we be relying on I'd be -1 on adding heuristics for paths. So I think that leaves two options:
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 @worktrunk-bot any ideas?? |
|
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 ( 2. Remote URL fallback — When 3. Improve the default 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. |
right, but are there viable alternatives that would produce the same result for normal repos and reasonable results for bare repos? |
|
The fundamental challenge is that normal repos and bare repos have different ideal worktree layouts: Normal: worktrees as siblings of the repo directory Bare: worktrees as children of the parent directory (siblings of A single template can't naturally produce both patterns. The current default ( Two approaches that could work without heuristics:
Neither is a clean drop-in improvement. I think the most practical path forward is:
The remote URL fallback feels like the most robust way to improve |
|
if it's driven by just "is it bare", then we could just have 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? |
|
Here are the common layouts and how the current variables resolve for each: 1. Normal repo (non-bare)
Default template → 2. Bare repo in named directory (e.g.
|
| 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.
|
ok, so, my current thinking:
is there any synthesis of these problems that solves it?! |
|
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
Why this works:
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. |
|
@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. |
|
Thanks @seakayone — glad the remote URL approach works for you too. @max-sixty, it sounds like there's agreement on the direction: use
No path-name heuristics, no new config — just a data source that's either present or not. |
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]>
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]>
…llback 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]>
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:
|
|
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:
The second option is nicer UX — users hit the fix path naturally the first time they create a worktree. And once I'll close #1544 since it uses the path-name heuristic approach. Happy to implement whichever direction you prefer here. |
|
Good points — the remote URL concern is real. Behavior shifting when you I think the "escalate to the user" direction is the right one. The codebase already has the pieces for this:
So a concrete approach:
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 The only question is when to trigger it — probably on any command that would use |
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]>
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]>
|
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 you get prompted if you want which works if you want the folder structure of However, if you want (or something similar to that) I think that is currently not possible, since Based on the discussion so far, would something like just exposing the parent folder ( |
) 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]>
|
great point @Xilis ! this should be fixed in #2592 (review) |
…#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]>
Summary
{{ repo }}template variable resolving to.gitinstead of the project name when using bare-style worktree setups (e.g./project/.gitas the common dir with sibling worktrees)repo_path()detects a bare repo whose path ends in.git, it now returns the parent directory insteadFixes #1279
Test plan
wt -vv listshows correctrepo=value in a bare-style worktree setup{{ repo }}produce the correct project name.git) still work correctly🤖 Generated with Claude Code