worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

gitdir: use relative path

Open saidelike opened this issue 8 months ago • 4 comments

at the moment, the .git for git worktree is a absolute path. It means if we move the main folder repo and its worktree repo.branch1 elsewhere, they don't work anymore.

this could be fixed by changing:

repo.branch1]$ cat .git
gitdir: /absolute/path/to/repo/.git/worktrees/repo.branch1

to:

repo.branch1]$ cat .git
gitdir: ../repo/.git/worktrees/repo.branch1

saidelike avatar Mar 20 '26 16:03 saidelike

Thanks for the suggestion! I searched the codebase and worktrunk currently always passes absolute paths to git worktree add without the --relative-paths flag.

Git itself added support for this via git worktree add --relative-paths and the worktree.useRelativePaths config option (available in Git 2.48.0+). Worktrunk could potentially forward this flag or respect the git config when creating worktrees.

As a workaround, you can set worktree.useRelativePaths = true in your git config (git config worktree.useRelativePaths true), and git should use relative paths automatically — though I haven't verified whether worktrunk's path handling (which canonicalizes paths internally) interacts cleanly with that.

I'll leave it for a maintainer to evaluate and prioritize.

worktrunk-bot avatar Mar 20 '26 17:03 worktrunk-bot

As a workaround, you can set worktree.useRelativePaths = true in your git config (git config worktree.useRelativePaths true), and git should use relative paths automatically — though I haven't verified whether worktrunk's path handling (which canonicalizes paths internally) interacts cleanly with that.

let's make a test for this

max-sixty avatar Mar 20 '26 18:03 max-sixty

Added a test in #1631 — it enables worktree.useRelativePaths = true, creates a worktree via wt switch --create, asserts the .git file contains a relative gitdir: path, then verifies wt list and wt switch both work correctly.

Everything passes — worktrunk's path handling (which canonicalizes internally) is compatible with relative worktree paths out of the box.

worktrunk-bot avatar Mar 20 '26 18:03 worktrunk-bot

@saidelike is that sufficient? it seems we do the same as git and so changing git configs is the way?

max-sixty avatar Mar 20 '26 20:03 max-sixty