Adjust git.cwd to use a relative path to git root #587
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a simple adjustment for the git cwd command to use a relative path to the git root instead of an absolute path. It should keep the behavior identical, but it solves an issue I ran into in my setup (described below).
More details on
--show-cdup
vs--show-toplevel
here:https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt---show-cdup
Motivation:
I ran into this issue during my esoteric obsidian-git setup using WSL2 (following the solution in #141). Basically, I want to use WSL's git instead of native windows
git.exe
, since I set it up withgit-crypt
and it has my ssh credentials. I ran into the issue that in this case,git.revparse("--show-toplevel")
will return the root as an absolute WSL path (eg,/mnt/c/Users/...
).git.cwd()
checks path existence prior to actually switching the working directory, but does so usingfs
- which will be using the windows filesystem instead, and, of course, not find the root directory, and throw an error.A quick solution is to pass a relative path to
git.cwd()
instead of an absolute path, as those will work the same way in WSL and Windows filesystems for a given repo. This is exactly what the--show-cdup
flag returns. Note that it will be an empty string if we are already at the repo root, hence the additional existence check.Some sources for my deep dive:
https://github.com/steveukx/git-js/blob/d64b31ca8670edd7af5a7fe5658516f5717c79a8/simple-git/src/lib/tasks/change-working-directory.ts#L7
https://github.com/steveukx/git-js/blob/d64b31ca8670edd7af5a7fe5658516f5717c79a8/simple-git/src/lib/utils/util.ts#L74
https://github.com/kwsites/file-exists/blob/13f0789cbd8df52588acdd185cd33de3b77fb342/src/index.ts#L10C13-L10C18