Skip to content

Expand ~ in the worktree creation prompt path#5597

Open
pedrampdd wants to merge 1 commit into
jesseduffield:masterfrom
pedrampdd:fix/expand-tilde-worktree-path-4708
Open

Expand ~ in the worktree creation prompt path#5597
pedrampdd wants to merge 1 commit into
jesseduffield:masterfrom
pedrampdd:fix/expand-tilde-worktree-path-4708

Conversation

@pedrampdd
Copy link
Copy Markdown

Fixes #4708.

The bug

When creating a new worktree from the worktree prompt and entering a path that begins with ~ (e.g. ~/code/feature-x), lazygit creates a directory literally called ~ inside the repo, e.g. <repo>/~/code/feature-x. The ~ is meant to expand to the user's home directory the way it does in shells.

Root cause

The user-entered path flows from the New worktree path prompt (pkg/gui/controllers/helpers/worktree_helper.go) into WorktreeCommands.New, which builds git worktree add <path> <base> and runs it via os/exec. os/exec does not invoke a shell, so ~ is taken literally. This was confirmed in the issue thread (cc @ChrisMcD1, @stefanhaller).

The fix

Adds a small helper utils.ExpandHomeDir (per @stefanhaller's suggestion to put the logic in pkg/utils/) that expands a leading ~ or ~/ to os.UserHomeDir(). Other paths — absolute paths, ../foo (which already worked because git resolves relative paths against cwd), or paths with a non-leading ~ — are returned unchanged. The ~user form is rejected explicitly so we don't silently do the wrong thing.

The helper is applied at the prompt's HandleConfirm callback so the expanded path is what flows through the rest of the worktree creation pipeline.

Tests

  • New table-driven unit test in pkg/utils/path_test.go covering: empty input, untouched relative path, untouched absolute path, ~ alone, ~/foo/bar, ~user/foo rejection, and a mid-path ~ segment.
  • All existing tests still pass (pkg/utils/..., pkg/gui/controllers/helpers/..., pkg/commands/...).
  • go build ./... succeeds.

I deliberately scoped this to home-directory expansion — env var expansion ($HOME/...) is a larger surface area and the issue thread converged on ~ specifically.

When creating a new worktree via the prompt, the user-entered path is
passed directly to "git worktree add" via os/exec. Because the command
is exec'd rather than run through a shell, "~" is taken literally and
git creates a directory called "~" containing the rest of the path,
instead of the user's home directory.

Add a small helper utils.ExpandHomeDir that turns a leading "~" or
"~/" into the user's home directory (using os.UserHomeDir) and apply
it to the path captured by the New worktree path prompt. Paths that
do not begin with "~" are returned unchanged, so absolute paths and
relative paths like "../foo" continue to work the way they did
before. The "~user" form is rejected explicitly so we don't silently
do the wrong thing.

Adds a table-driven unit test for the helper covering the empty
input, untouched paths, "~" alone, "~/foo/bar", "~user/foo", and a
mid-path "~" segment.

Fixes jesseduffield#4708
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.

Worktree path does not expand

1 participant