Skip to content

Instantly share code, notes, and snippets.

@KonradAdamczyk
Forked from jmickela/gist:7c383c78af66a37a2446fe7eb733b157
Last active May 22, 2020 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KonradAdamczyk/adbdffbb8b535ccc1a7b2bd686128f5c to your computer and use it in GitHub Desktop.
Save KonradAdamczyk/adbdffbb8b535ccc1a7b2bd686128f5c to your computer and use it in GitHub Desktop.
Trying to get PhpStorm to use git in Bash on Ubuntu on Windows (Windows Subsystem for Linux)
::There's a problem that you run into right away: you can't put a command line command, with arguments,
::into the path to git executable box.
::
::So putting something like bash.exe -c "git %*" isn't going to work. I wrote a small shell script that
::fixes this, for both 32-bit and 64-bit systems.
@echo off
::replace with absoulte WSL path to git.py
SET script_path="/mnt/c/Users/Konrad/bin/git.py"
If %PROCESSOR_ARCHITECTURE% == x86 (
"C:\Windows\sysnative\bash.exe" -c "python3 %script_path% %*"
) Else (
"bash.exe" -c "python3 %script_path% %*"
)
::If you set this script as the path to the git executable you'll get the correct git version back when
::you hit the test button. You'll be able to use many of the local VCS functions, like viewing diffs or
::previous commits. It should be noted that if you're on a 64 bit system you can't simply ignore the x86
::portion of the script, even if you run the 64 bit executable of PhpStorm, the VCS handling is 32 bit.
::Or at least when this script is actually invoked it will fail if it can't run the 32 bit version.
::
::%Everything falls apart when you try to pull or push from a remote. If you watch the log the reason is
::obvious: However the handling of private keys and/or passwords works, it doesn't work here. If you
::cancel an attemp to fetch updates you'll get a few messages in the log about not being able to
::authenticate with private key or password.
::
::If I run my script in a windows command shell it works fine, it prompts me for a password and fetches
::updates. For whatever reason that request for a password isn't making it into PhpStorm.
::
::For now, I give up. Really, and I know it's unlikely, but I would like to see WSL supported because
::there's a ton of tools in PhpStorm that assume you aren't doing something stupid like running a
::different OS within your OS and trying to pipe all your commands into it while NOT doing this in a
::VM where you could just run a SSH server.
# This script calls git with paths in cmd arguments replaced to WSL paths
import re
import subprocess
import sys
if __name__ == "__main__":
args = sys.argv[1:]
for i, arg in enumerate(args):
if ":\\" in arg:
# windows style path to WSL style path
wsl_path = re.sub(r'([A-Z]):\\', lambda pat: f"/mnt/{pat.group(1).lower()}/", arg)
wsl_path = wsl_path.replace("\\", "/")
args[i] = wsl_path
args = ["git"] + args
# print(args)
# call git
subprocess.run(args, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment