Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: custom Git binary path using wsl doesn't work #141

Closed
229c9cf0 opened this issue Nov 24, 2021 · 16 comments
Closed

[Bug]: custom Git binary path using wsl doesn't work #141

229c9cf0 opened this issue Nov 24, 2021 · 16 comments

Comments

@229c9cf0
Copy link

[note: suspected cause at the very end; skip ahead and come back if that's not it]

I have Git installed in WSL and added a shell script in my home that will take care of connecting to the ssh-agent etc. followed by actually running the requested git command. Despite the command working in both PowerShell and plain CMD, Obsidian Git reports "cannot run git command".

  • Are you sure that the custom git binary path code actually works?
  • If yes, what's the required format?

I have tried both C:\Windows\System32\wsl.exe /home/REDACTED/bin/ogit as well as just wsl.exe /home/REDACTED/bin/ogit or wsl /home/REDACTED/bin/ogit (in case there's a problem with handling backslashes...)

In CMD and PowerShell, both work:

C:\Users\REDACTED\Desktop\maps\obsidian>wsl /home/REDACTED/bin/ogit status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .obsidian/plugins/obsidian-git/data.json


C:\Users\REDACTED\Desktop\maps\obsidian>C:\Windows\System32\wsl.exe /home/REDACTED/bin/ogit status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .obsidian/plugins/obsidian-git/data.json

C:\Users\REDACTED\Desktop\maps\obsidian>C:\Windows\System32\wsl.exe /home/REDACTED/bin/ogit push
Host key fingerprint is SHA256:<redacted>
Enumerating objects: 38, done.
Counting objects: 100% (38/38), done.
Delta compression using up to 8 threads
Compressing objects: 100% (27/27), done.
Writing objects: 100% (29/29), 6.13 KiB | 184.00 KiB/s, done.
Total 29 (delta 4), reused 0 (delta 0)
To <redacted>:repos/obsidian-vault.git
   <redacted>..<redacted>  master -> master

In Obsidian, all variants fail.

So there seems to be some special formatting / adjustment to the command required to make it work when run from Obsidian. (Or maybe it just doesn't work at all?)


While I don't really understand JS & related languages, from a quick scan of the code in src/simpleGit.ts, the following seem strange:

  • isGitInstalled, commitAll, and push all appear to hard-code the git command; if isGitInstalled gatekeeps the running of all other commands, then it's not surprising that nothing works
@Vinzent03
Copy link
Owner

I haven't tested it with wsl. I guess the problem is that you first run wsl and then the git path for wsl, but that's caused by the underlying git library. Seems to be the same issue as in this issue
Regarding your guess why it doesn't work, the command use the git object, which is created here with the path from the settings.

@229c9cf0
Copy link
Author

But isGitInstalled just runs
spawnSync('git', ['--version'], …) and doesn't use the indirection, right? (The code you linked is only reached if isGitInstalled returns success, so it can't have been reached yet.)

And since the indirection is required to find git (as it's not just git), of course it can't work.

@Vinzent03
Copy link
Owner

Oh ** you are right. Definitely need to fix that. But I still doubt that it works, because the user in the other issue had a local git installation and just wanted to use the wsl installation, which didn't work. Maybe your setup is different and it works nevertheless.

@Vinzent03
Copy link
Owner

Released a new version, please try it out.

@229c9cf0
Copy link
Author

229c9cf0 commented Nov 26, 2021

Progress! After the update, I'm getting /bin/bash: status: command not found which is exactly the error message that wsl status generates.

So it looks like something drops the second part of wsl /home/REDACTED/bin/ogit, and I confirmed that with an experiment. (Details below.)

For now, I added an extra git.cmd wrapper somewhere in the %PATH% and then wondered why it doesn't work until I found out that apparently I need to add the full C:\... path in spite of it being in %PATH% (where both cmd and powershell find & run it without problems). So now things mostly work for me!

I'd love to see the argument-dropping problem fixed eventually (so I no longer need the second wrapper) but it's not urgent.


Experiment: To test the argument dropping, I added symlinks named status, branch, ... in /usr/local/bin to another wrapper that calls /home/REDACTED/bin/ogit "$(basename "$0")" "$@" (i.e. reroutes those program names to become a git command), such that wsl status, wsl branch, ... will work like wsl /home/REDACTED/bin/ogit status, .... and that confirms this guess: With that in place, I can select the branch from the drop-down in the obsidian-git config and I get a "git: ready" in the status bar, i.e. basic communication with git works. (Adding lots of other wrappers for add/commit/push/... means I can use the source control view to add, commit, push/pull and it all works, as verified externally.)


Just for reference, in case others want my full solution for getting WSL's git working: There's a git.cmd

@echo off
C:\Windows\System32\wsl.exe /home/REDACTED/bin/obsidian-git %*

and since %PATH% search doesn't work anyway, just place that anywhere and specify the full path to it as the custom command. On the Linux side, there's the obsidian-git wrapper that connects to the running ssh-agent and then calls git:

#!/bin/sh
source ${HOME}/.profile > /dev/null 2>&1 # this is where my config does the ssh-agent connecting
git "$@"

and in case you don't have an automated ssh-agent setup yet, eval "$(keychain --eval --agents ssh)" in e.g. your .profile with keychain installed gets that done without lots of duplicate ssh-agents. (Just ssh-add the key once after boot to unlock/cache it and you're done.)

@229c9cf0
Copy link
Author

(Also I guess @maximberezin97 might want to know that there was indeed a problem and it mostly works now. Oh and thanks for the quick help too, Vinzent!)

@Vinzent03
Copy link
Owner

Sorry to hear that it still doesn't work. I'm just providing the path to the simple-git library, so it has to be an issue on their side.

I'm really thankful for your workarounds for both Windows and Linux! Should help other people facing the same issue.

@Vinzent03 Vinzent03 changed the title [Bug]: custom Git binary path apparently doesn't work or needs special formatting? [Bug]: custom Git binary path using wsl doesn't work Nov 26, 2021
@maximberezin97
Copy link

Progress! After the update, I'm getting /bin/bash: status: command not found which is exactly the error message that wsl status generates.

So it looks like something drops the second part of wsl /home/REDACTED/bin/ogit, and I confirmed that with an experiment. (Details below.)

For now, I added an extra git.cmd wrapper somewhere in the %PATH% and then wondered why it doesn't work until I found out that apparently I need to add the full C:\... path in spite of it being in %PATH% (where both cmd and powershell find & run it without problems). So now things mostly work for me!

I'd love to see the argument-dropping problem fixed eventually (so I no longer need the second wrapper) but it's not urgent.

Experiment: To test the argument dropping, I added symlinks named status, branch, ... in /usr/local/bin to another wrapper that calls /home/REDACTED/bin/ogit "$(basename "$0")" "$@" (i.e. reroutes those program names to become a git command), such that wsl status, wsl branch, ... will work like wsl /home/REDACTED/bin/ogit status, .... and that confirms this guess: With that in place, I can select the branch from the drop-down in the obsidian-git config and I get a "git: ready" in the status bar, i.e. basic communication with git works. (Adding lots of other wrappers for add/commit/push/... means I can use the source control view to add, commit, push/pull and it all works, as verified externally.)

Just for reference, in case others want my full solution for getting WSL's git working: There's a git.cmd

@echo off
C:\Windows\System32\wsl.exe /home/REDACTED/bin/obsidian-git %*

and since %PATH% search doesn't work anyway, just place that anywhere and specify the full path to it as the custom command. On the Linux side, there's the obsidian-git wrapper that connects to the running ssh-agent and then calls git:

#!/bin/sh
source ${HOME}/.profile > /dev/null 2>&1 # this is where my config does the ssh-agent connecting
git "$@"

and in case you don't have an automated ssh-agent setup yet, eval "$(keychain --eval --agents ssh)" in e.g. your .profile with keychain installed gets that done without lots of duplicate ssh-agents. (Just ssh-add the key once after boot to unlock/cache it and you're done.)

Thank you so much @229c9cf0 , this solution worked for me! I created the two helper files git.cmd and the wrapper script obsidian-git and set my custom Git path to C:\Users\MyUser\git.cmd, and had finally been able to push and pull from my Obsidian repository!

Screenshot 2021-11-27 204449
Screenshot 2021-11-27 204525

The hardest part was getting my ssh-agent to run automatically on login, but the ssh-agent plugin for Oh My Zsh takes care of this in one line change in my .zshrc. While this solution does appear quite fragile, it is working for the time being. I really appreciate both of your help.

@raulvasquez
Copy link

raulvasquez commented Sep 6, 2023

I am trying to get git in WSL working but I keep getting an error

plugin:obsidian-git:26622 Uncaught (in promise) Error: Error: Git.cwd: cannot change to non-directory "/mnt/c/Users/FirstName LastName/obsidian-vault"
    at GitExecutorChain.onFatalException (plugin:obsidian-git:26622:79)
    at GitExecutorChain.eval (plugin:obsidian-git:26614:24)
    at Generator.throw (<anonymous>)
    at rejected (plugin:obsidian-git:25483:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:96:5).

I setup a git.cmd and wrapper as git-wrapper. Both of these files are located in my vault.

The contents git.cmd is:

@echo off
C:\Windows\System32\wsl.exe /mnt/c/Users/FirstName\ LastName/obsidian-vault/git-wrapper %*

and git-wrapper contains

#!/bin/sh
source ${HOME}/.profile > /dev/null 2>&1
git "$@"

From the command prompt in windows I can pass git commands through git.cmd and the same with the WSL ubuntu terminal using git-wrapper

I have the "Custom Git binary path" set to:

C:\Users\FirstName LastName\obsidian-vault\git.cmd

In the dev-console I have run app.vault.adapter.basePath which returns `'C:\Users\FirstName LastName\obsidian-vault'

Does anyone have any ideas? The only thing I can think is the space between first and lastname in my windows Users directory is causing issues.


EDIT: I applied the changes from #587 and I was able to make progress, but I got another error:

plugin:obsidian-git:29119 Uncaught (in promise) Error: 'C:\Users\FirstName' is not recognized as an internal or external command, operable program or batch file.

I moved git.cmd to c:\tools\git.cmd and everything is working. Is there a proper way to format paths for the git binary if a space is part of the name?

@Vinzent03
Copy link
Owner

@raulvasquez You are using the git.cmd as Git Binary Path, right? Can you wrap it in ' in the settings? That should fix the whitespace in the path issue.

@raulvasquez
Copy link

It cannot run the git command when I surround it in '. I've also tried double quotes " and escaping the space.

@Vinzent03
Copy link
Owner

Hmm I can't get it to work as well. Seems like a bug in simpel-git to me. You'll have to use no space then.

@229c9cf0
Copy link
Author

Haven't used Obsidian in a while, but I've looked into the current iteration of problems as well now.

When checking validity of git settings, the plugin currently runs git -c core.quotepath=off rev-parse --show-toplevel, which results in a path of the shape /mnt/c/<…normal windows path…>. It doesn't log what it's trying to do next, but whatever that is results in an Error: Git.cwd: cannot change to non-directory /mnt/c/… (because the /mnt/c/ prefix is doesn't make sense on the Windows side.) This does not require whitespace or other special characters in the path components.

Apparently at some point the behavior of the plugin changed from just working with relative paths or passing paths as arguments (which works fine if Obsidian is in "Windows-land" and git lives in "WSL-/Linux-land") to trying to do stuff at the location of the git repository (which breaks since the absolute paths aren't the same.)

I don't have time to dig into the code, and the logs don't contain more information, so that's all I can figure out for now… But maybe that helps someone else.

@229c9cf0
Copy link
Author

229c9cf0 commented Oct 2, 2023

Found a silly/stupid workaround: (@raulvasquez)

In PowerShell, cd /, mkdir mnt, cd mnt, junction c ...

That creates a folder C:\mnt containing a junction link back up to to C:\, which makes paths of the form /mnt/c/… work. At that point, the current behavior of using absolute paths doesn't hurt anymore.

@H3mul
Copy link
Contributor

H3mul commented Mar 3, 2024

Just noticed activity on this issue - wanted to put in my 2 cents since I have a working solution that satisfies me.

My motivation: I want Windows Obsidian to use WSL's git for obsidian-git, because my WSL has my ssh keys in the agent and also my git repo has git-crypt set up.

  1. I have the latest obsidian-git patched with Adjust git.cwd to use a relative path to git root #587 (just updated to latest in my fork)
  2. I have the Custom Git binary path setting point to C:\Users\<user>\AppData\Local\scripts\wsl_git.cmd
    I believe I didnt run into the issue described above simply because my Windows user has no spaces. I'd suggest attempting to escape them with \ , but can't test that theory.
  3. Contents of wsl_git.cmd, inspired by this thread:
@echo off
wsl.exe /home/<wsl user>/bin/obsidian-git %*
  1. Contents of ~/bin/obsidian-git, which ensures my env is properly set up (most importantly, the correct SSH_AUTH_SOCK):
#!/bin/bash
source ${HOME}/.profile > /dev/null 2>&1
git "$@"

The last one is optional, if you don't need your .profile you can just have wsl_git.cmd pointing to the wsl git binary instead.

With this setup, obsidian-git pulls and pushes to my remote seamlessly using my WSL auth configuration.

Hope this helps!

@Vinzent03
Copy link
Owner

Some base path and git binary path issues have been fixed in the last weeks. E.g. #733. Please open a new issue if you still experience issues.

@Vinzent03 Vinzent03 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
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

No branches or pull requests

5 participants