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

[#1657] Escape special chars for bash commands #1662

Merged

Conversation

chan-j-d
Copy link
Contributor

@chan-j-d chan-j-d commented Feb 13, 2022

Fixes #1657

Currently, the file path arguments are added to git commands using normal double quotes. However, for Bash terminals, double quotes still allow for special symbols like $ to perform a special function like referencing a variable name. There is a need to use single quotes for Bash terminals. Single quotes ' themselves are escaped as such '"'"' by breaking up the string into various quoted segments where a single quote is left in a double quoted segment as per this stackoverflow post.

The test case specifically tests against special bash characters according to this unix stackexchange post.

Unfortunately, Windows CMD does not work with single quotes and only accepts double quotes. As such, there needs to be a distinction in the way this is done for different OS.

A small note was added to the DG of the importance in using this additional method.

Commit message:

Special characters in file names are not properly translated into git
commands on bash for non-Windows OS. Usage of single quotes on bash is
only necessary to escape such special characters.

Let's fix the escaping of special characters for bash commands.

@dcshzj dcshzj requested a review from a team February 13, 2022 11:02
@chan-j-d chan-j-d requested a review from a team February 13, 2022 11:12
@dcshzj dcshzj removed the request for review from a team February 13, 2022 12:27
Copy link
Contributor

@zhoukerrr zhoukerrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is trivial but maybe we can cover the case on that line. Otherwise LGTM.

*/
public static String addQuotesForFilePath(String filePath) {
if (SystemUtil.isWindows()) {
return "\"" + filePath + "\"";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to cover this line in the test as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I don't think there is a good way to test the escaping specifically. We can add a test case just to check if quotation marks are added which does not seem very useful. For this case, it was to avoid mishandling of spaces in the repo directory and the test cases are located here.

For testing the escaping specifically, I don't think there's a good way to test this for Windows filesystems. The CMD's echo command echoes the quotation marks compared to bash's echo command which strips the quotation marks and does symbol substitution where appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this line missed just because the coverage report wasn't generated on Windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more that there isn't a specific test case that runs this line of code at all since the test case I added only runs on non-Windows systems.

/**
* Adds the appropriate quotation marks for a file path depending on the OS.
*/
public static String addQuotesForFilePath(String filePath) {
Copy link
Contributor

@gok99 gok99 Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be possible that developers forget to use these if they weren't sure how or when to use them. Do you think it would be a good idea now to create a CommandBuilder to take care of these things?

This could be tricky though, given that the construction of commands can be non-trivial so this might be out of scope for this PR.

Copy link
Contributor Author

@chan-j-d chan-j-d Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I actually voiced a similar idea here and you can read the response from dcshzj but besides adding paths, most other argument types are quite tailored to the specific git command. So this is a specific standard for file paths only.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the link! It's interesting seeing similar discussions and considerations springing up independently at different points in this project :)

@chan-j-d chan-j-d requested a review from gok99 February 13, 2022 14:15
Copy link
Contributor

@gok99 gok99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@gok99 gok99 requested a review from a team February 13, 2022 17:24
@chan-j-d chan-j-d requested a review from dcshzj February 14, 2022 06:15
Copy link
Member

@dcshzj dcshzj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dcshzj dcshzj requested a review from a team February 14, 2022 08:13
@chan-j-d
Copy link
Contributor Author

@dcshzj @fzdy1914 would it be possible to get a final review sooner or merge it earlier? I think the StringsUtil::addQuotesForFilePath method is necessary for issue #1682 which is further exacerbated in PR #1644 when the output directory now retains information of the entire path that might have more spaces.

@dcshzj
Copy link
Member

dcshzj commented Feb 21, 2022

I will move this PR along since it is necessary for some of the other PRs.

@dcshzj dcshzj changed the title [#1657] Escape special chars for Bash commands [#1657] Escape special chars for bash commands Feb 24, 2022
@dcshzj dcshzj merged commit 4923449 into reposense:master Feb 24, 2022
@github-actions
Copy link
Contributor

The following links are for previewing this pull request:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Special characters such as $ not properly escaped in BASH commands
5 participants