carapace-sh / carapace-bin

multi-shell multi-command argument completer
https://carapace.sh
MIT License
860 stars 47 forks source link

git: --work-tree not being handled properly #2480

Closed wthueb closed 1 month ago

wthueb commented 1 month ago

Current Behavior

With a bare repo at ~/.cfg with a worktree of ~ (happens to be my dotfiles repo), the following command gives an error:

~ > carapace git nushell "git" "--git-dir" "~/.cfg" "--work-tree" "~" "add" ""
[{"value":"ERR","display":"ERR","description":"stat /Users/wil/~: no such file or directory","style":{"fg":"red","attr":"b"}},{"value":"_","display":"_"}]

Changing the command to carapace git nushell "git" "--git-dir" "/Users/wil/.cfg" "--work-tree" "/Users/wil" "add" "" seems to avoid that error (this solution is not ideal, as I'd rather use ~ expansion for home instead of getting weird with environment variables, especially considering the nushell completer setup doesn't seem to expand environment variables/interpolate strings?):

~ > carapace git nushell "git" "--git-dir" "/Users/wil/.cfg" "--work-tree" "/Users/wil" "add" ""
[{"value":".config/nushell/config.nu ","display":".config/nushell/config.nu","description":"M"}]

However, it still doesn't quite work how it should. In my instance, if I cd to ~/.config/nushell and run the command again, it still uses paths relative to the worktree even though they should be relative to the CWD:

~/.config/nushell > carapace git nushell "git" "--git-dir" "/Users/wil/.cfg" "--work-tree" "/Users/wil" "add" ""
[{"value":".config/nushell/config.nu ","display":".config/nushell/config.nu","description":"M"}]

Expected Behavior

In the above instance, ~/.config/nushell > carapace git nushell "git" "--git-dir" "~/.cfg" "--work-tree" "~" "add" "" to output config.nu.

Steps To Reproduce

With carapace 1.0.5, run carapace git nushell "git" "--git-dir" "~/.cfg" "--work-tree" "~" "add" "" with the CWD being anything other than the worktree dir, in this case ~. Don't think this is isolated to nushell, seems to happen in bash as well.

Version

1.0.5

OS

Shell

Anything else?

No response

Polar

Fund with Polar

rsteube commented 1 month ago

Homedir expansion is one of the tough topics. There's a difference between unquoted ~ (expanded) and quoted "~" (not exanded) tilde. But the information of it being quoted or not gets lost along the way.

I think it's safe for now to simply assume expansion here if one passes the ~ prefix as argument. But be aware that while it shows completions using carapace the git command will likely fail if quoted:

git --git-dir ~/.cfg --work-tree ~ log # works
git --git-dir "~/.cfg" --work-tree "~" log # fails