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

Shell escape continued #75

Closed
wants to merge 7 commits into from
Closed

Shell escape continued #75

wants to merge 7 commits into from

Conversation

carlolars
Copy link
Contributor

  • Improved shell_escape():
    • Arguments with invalid characters and spaces should not be quoted since the space cause them to be quoted elsewhere.
    • Long arguments (--argname) and flag arguments (-f) that have invalid characters are not quoted, instead a space i appended so they also get quoted elsewhere.
    • Other arguments are enclosed in single-quotes.
  • Added integration tests using assert_cli.

#54

…invoked by wsl.exe run in the same working dir.
… characters but not a space.

The space character is appended to long arguments (--argname=value) and flag arguments (-Xvalue),
while other arguments are quoted.
@andy-5
Copy link
Owner

andy-5 commented Jun 17, 2019

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.

…ommands invoked by wsl.exe run in the same working dir."

This reverts commit 3877221.
…teractive_shell() and format_argument_for_non_interactive_shell().

Only the arguments to wslgit are formatted.
@carlolars
Copy link
Contributor Author

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:

  • format_argument_for_interactive_shell():
    • Quote entire argument if:
      • Not already quoted, assumes correctly quoted then.
      • Contains invalid characters (including space).
  • format_argument_for_non_interactive_shell():
    • Strip all existing quotes since they would be inside any outer quote.
    • Append space character to '--arg' and '-X' arguments if they contain invalid characters but no space.
    • Quote any other argument that contain invalid characters bu no space.

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
Copy link
Contributor Author

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
Copy link
Owner

andy-5 commented Jun 19, 2019

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
Copy link
Contributor Author

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.

@carlolars carlolars mentioned this pull request Jun 19, 2019
@andy-5
Copy link
Owner

andy-5 commented Jul 3, 2019

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.

@andy-5 andy-5 closed this Jul 3, 2019
@carlolars carlolars deleted the shell_escape_continued branch July 4, 2019 16:47
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

Successfully merging this pull request may close these issues.

2 participants