andy-5 / wslgit

Use Git installed in Bash on Windows/Windows Subsystem for Linux (WSL) from Windows and Visual Studio Code (VSCode)
MIT License
1.18k stars 59 forks source link

Shell escape continued #75

Closed carlolars closed 5 years ago

carlolars commented 5 years ago

54

andy-5 commented 5 years ago

I quickly skimmed your changes. Regarding the cd to the current directory - this is due to #50. The working directory is not always set correctly by wsl.exe, so I think we should keep the cd.

carlolars commented 5 years ago

I always forget the interactive shell, but now I've added integration tests that run both interactive and non-interactive :)
Note that at least the integration tests must be run using one test thread since they modify environment variables and will hang if run in parallel.

I updated how the arguments are formatted, splitting it in two functions:

Most of changes is updated tests so the change is not that intimidating.
(Sorry if I'm breaking any essential design patterns used when programming Rust, this is the first time I program rust, usually doing low level C or asm.)

carlolars commented 5 years ago

Is there a reason to not just use wsl bash -c" for non-interactive? It would make all the escaping and quoting of arguments the same for both interactive and non-interactive.

andy-5 commented 5 years ago

Yes, the only reason for the non-interactive mode is to avoid starting a Bash process at all, because this significantly slows down wslgit. The default mode uses bash -ic because it is common to start tools like ssh-agent in Bash startup scripts like .bashrc. These tools might be required for git to work, and in some configurations they are only started for interactive shells.

carlolars commented 5 years ago

I just had to try it out...
I measured the execution time of wslgit.exe --version and wslgit.exe log -1 --pretty=format:abc using a script that looped 100 times for both the original non-interactive (wsl git ...) and wsl bash -c "git ...", and the difference was 13-17 ms.

But the code is a lot cleaner that what I have done in this branch, and there is no "add a space to quote" oddities, it's just formatting a valid command string for bash to interpret using normal quoting. I vote for taking the 15 ms penalty in favor of cleaner code 😃

I'll push my branch with the bash -c version.


Side note, I use BASH_ENV to run my .bashrc even for non-interactive shell and check if $- contains the i flag, like this:

[[ $- == *i* ]] && IS_INTERACTIVE_SHELL=1

This lets me select what parts of the .bashrc should be included or excluded depending on interactive/non-interactive.

andy-5 commented 5 years ago

Yes, you are right. Running through bash is not the slow part, but executing all of .bashrc might be slow (which is what happens in interactive mode).

Therefore, I'll go with your much simpler solution from #76 and close this.