dugite icon indicating copy to clipboard operation
dugite copied to clipboard

execute git in WSL for WSL paths

Open schummar opened this issue 2 years ago • 11 comments

Motivation

Executing git commands from Windows on WSL (Windows Subsystem for Linux) paths has some problems. The major ones (that I'm aware of) are

  • The performance is bad because cross file system file access is slooow
  • By default just checking out a branch will create changes from the WSL point of view because file endings are "wrong". I know line ending behavior can be configured, but out-of-the box this creates chaos.
  • Git hooks don't work

My use case is, I would like to use GitHub Desktop to manage my git repos both in windows and in WSL, where I do most of my programming. Commonly recommended solutions like running GitHub Desktop in WSL (WSL GUIs are not stable enough for my tastes) or just opening the WSL path in GitHub Desktop on Windows (suffers from the above mentioned issues) are not working for me.

There are a number of issues that are related to this, e.g. https://github.com/desktop/desktop/issues/13953

Solution

So my suggestion is this: When working in repositories that have a WSL path (e.g. \\wsl$\home\user\src\whatever or \\wsl.localhost\\Ubuntu\home\user\src\whatever), execute git commands within WSL: wsl.exe -e git .... This PR is a POC and probably needs more work, so I am grateful for any feedback. It works very smoothly in my locally built GitHub Desktop though.

My considerations so far:

Performance

The approach works a lot faster on my machine. I can't say I have done bullet proof benchmarking yet, but some of my tests show significant improvements:

Opening a repository - old:

Executing getBranches: git for-each-ref --format=%00%(refname)%00%(refname:short)%00%(upstream:short)%00%(objectname)%00%(author)%00%(symref)%00 refs/heads refs/remotes (took 1.495s)
Executing getCommits: git log HEAD --date=raw --max-count=100 -z --format=%H%x00%h%x00%s%x00%b%x00%an <%ae> %ad%x00%cn <%ce> %cd%x00%P%x00%(trailers:unfold,only)%x00%D --no-show-signature --no-color --not --remotes -- (took 2.632s)
Executing getAllTags: git show-ref --tags -d (took 1.890s)

Opening a repository - new:

Executing getBranches: git for-each-ref --format=%00%(refname)%00%(refname:short)%00%(upstream:short)%00%(objectname)%00%(author)%00%(symref)%00 refs/heads refs/remotes (took 0.155s)
Executing getCommits: git log HEAD --date=raw --max-count=100 -z --format=%H%x00%h%x00%s%x00%b%x00%an <%ae> %ad%x00%cn <%ce> %cd%x00%P%x00%(trailers:unfold,only)%x00%D --no-show-signature --no-color --not --remotes -- (took 0.203s)
Executing getAllTags: git show-ref --tags -d (took 0.140s)

Checking out a branch - old:

Executing checkoutBranch: git -c credential.helper= checkout --progress master --recurse-submodules -- (took 1.679s)
[Timing] Action 'checkout branch from list' for 'schummar/schummar-state' took 3.284s

Checking out a branch - new:

Executing checkoutBranch: git -c credential.helper= checkout --progress master --recurse-submodules -- (took 0.133s)
[Timing] Action 'checkout branch from list' for 'schummar/schummar-state' took 1.094s

Authentication

WSL has a neat mechanism to forward environment variables between WSL and Windows processes. It even can convert paths while doing it. By forwarding certain env vars, authentication seem to work nicely for private repositories.

Multiple WSL distributions

Since the distribution name is part of the WLS repo path, it can be specified in the command, so working across multiple distributions should work, though I didn't test that case yet.

schummar avatar Mar 21 '23 23:03 schummar

@schummar Thank you for looking into this. I would love for this feature to get merged.

refringe avatar Mar 24 '23 19:03 refringe

Thanks for putting this together. I tried to get this working with my local GitHub Desktop but was unsuccessful. The error I'm seeing is "Git could not be found at the expected path"

SynthLuvr avatar Apr 26 '23 07:04 SynthLuvr

You need to use the modified GitHub Desktop version from this PR: https://github.com/desktop/desktop/pull/16378

schummar avatar Apr 26 '23 07:04 schummar

A little script for anyone who wants to try it out:

git clone https://github.com/schummar/dugite.git
git clone https://github.com/schummar/desktop.git

cd dugite
git checkout origin/feature/wslGit
yarn
yarn build
yarn link

cd ../desktop
git checkout origin/feature/wslCompatibility
yarn
cd app
yarn link dugite
cd ..
yarn build:prod

schummar avatar Apr 26 '23 07:04 schummar

Thanks @schummar, that helps a lot. I'm still getting errors when running GitHub Desktop though:

  • error launching git: The system cannot find the path specified.
  • Failed to execute getStatus: ENOENT
  • Git returned an unexpected exit code '1' which should be handled by the caller (getStatus).'

It also fails to load any repository from WSL, keeps show "Can't find [repo]"

SynthLuvr avatar Apr 26 '23 07:04 SynthLuvr

Ah my bad. I forgot that you have to link dugite in the app folder as well. I have updated the script.

Edit: Actually, only in the app folder. Root was just plain wrong 😬

schummar avatar Apr 26 '23 08:04 schummar

It's working for me, thanks!

SynthLuvr avatar Apr 26 '23 08:04 SynthLuvr

Bumping this PR! WSL-based git would be fantastic.

CaseyLabs avatar Oct 30 '23 17:10 CaseyLabs

I've been running this since April. I'm too afraid to update to the latest version in case I can't get this feature working again. So I'm sticking with this PR and forgoing updates.

SynthLuvr avatar Oct 30 '23 17:10 SynthLuvr

I've updated the branches once again. You can create a current build if you want. 😉

schummar avatar Oct 31 '23 17:10 schummar

@sergiou87 any chance this can be looked at? cheers

Stanzilla avatar May 05 '24 03:05 Stanzilla