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

ESCAPE_COMMIT_MSG Escaping Double Quotes In Merge Commit Message If It Exists #7796

Closed
wants to merge 8 commits into from
Closed

Conversation

hrai
Copy link

@hrai hrai commented Feb 22, 2020

Fixes #3954

Before

Merge commit message with quotes was failing the merge test "message text" test
image

After

The merge succeeds.
image
image

Test methodology

  • Added NUnit test

Test environment(s)

  • GIT version 2.22.0.windows.1
  • Windows 10

✒️ I contribute this code under The Developer Certificate of Origin.

@ghost ghost assigned hrai Feb 22, 2020
Copy link
Member

@mstv mstv left a comment

Choose a reason for hiding this comment

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

LGTM, have not run

@@ -1024,6 +1026,11 @@ public static ArgumentString MergeBranchCmd(string branch, bool allowFastForward
};
}

private static string EscapeDoubleQuotes(string message)
Copy link
Member

Choose a reason for hiding this comment

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

Should rather be named EscapeQuotes.
Code block can be omitted, i.e. turn into => message?.Replace("\"", "\\\"");

Copy link
Member

@RussKie RussKie left a comment

Choose a reason for hiding this comment

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

Trying to escape quotes and slashes is like opening a Pandora box of pain.
Instead I propose use --file option similar to how we run the commit command:

public ArgumentString CommitCmd(bool amend, bool signOff = false, string author = "", bool useExplicitCommitMessage = true, bool noVerify = false, bool gpgSign = false, string gpgKeyId = "")
{
return new GitArgumentBuilder("commit")
{
{ amend, "--amend" },
{ noVerify, "--no-verify" },
{ signOff, "--signoff" },
{ !string.IsNullOrEmpty(author), $"--author=\"{author?.Trim().Trim('"')}\"" },
{ gpgSign && string.IsNullOrWhiteSpace(gpgKeyId), "-S" },
{ gpgSign && !string.IsNullOrWhiteSpace(gpgKeyId), $"-S{gpgKeyId}" },
{ useExplicitCommitMessage, $"-F \"{Path.Combine(GetGitDirectory(), "COMMITMESSAGE")}\"" }
};
}

@ghost ghost added the 📭 needs: author feedback More info/confirmation awaited from OP; issues typically get closed after 30 days of inactivity label Feb 22, 2020
@hrai
Copy link
Author

hrai commented Feb 25, 2020

@RussKie GetGitDirectory() needs RepositoryPath.

Is there a method or a property that I can use to get this value?

Also the algorithm will be:

  1. Write merge message to .git/MERGE_MSG file
  2. Use git merge -F command

@ghost ghost removed the 📭 needs: author feedback More info/confirmation awaited from OP; issues typically get closed after 30 days of inactivity label Feb 25, 2020
@RussKie
Copy link
Member

RussKie commented Feb 25, 2020 via email

@hrai
Copy link
Author

hrai commented Feb 29, 2020

@RussKie now the merge happens through MERGE_MSG file. I had to shuffle some methods around. Also updated the tests.

public static string FormatCommitMessageFromTextBox(
string commitMessageText, bool usingCommitTemplate, bool ensureCommitMessageSecondLineEmpty)
{
if (commitMessageText == null)
Copy link
Member

Choose a reason for hiding this comment

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

What about commitMessageText == "" or commitMessageText == " "?

Copy link
Author

Choose a reason for hiding this comment

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

This is a pre-existing code. If replacing the above with IsNullOrWhitespace() is not going to affect the existing logic, I can push the change.

var lineNumber = 1;
foreach (var line in commitMessageText.Split('\n'))
{
string nonNullLine = line ?? string.Empty;
Copy link
Member

Choose a reason for hiding this comment

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

How can line == null?

Copy link
Author

Choose a reason for hiding this comment

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

This is a preexisting method that I pulled from GitUI/CommandsDialogs/FormCommit.cs

@@ -1007,30 +1008,23 @@ private static GitItemStatus GitItemStatusFromStatusCharacter(StagedStatus stage
};
}

public static ArgumentString MergeBranchCmd(string branch, bool allowFastForward, bool squash, bool noCommit, string strategy, bool allowUnrelatedHistories, string message, int? log)
public static ArgumentString MergeBranchCmd(string branch, bool allowFastForward, bool squash, bool noCommit, string strategy, bool allowUnrelatedHistories, string folderPath, int? log)
Copy link
Member

Choose a reason for hiding this comment

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

Please add tests for this method that at least cover the -F flag

Copy link
Author

Choose a reason for hiding this comment

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

All the test cases now accept a path instead of a message.

The response has -F flag.

@@ -102,6 +109,21 @@ private void OkClick(object sender, EventArgs e)
}
}

private void WriteMessageToMergeFile(string mergeMessage)
Copy link
Member

Choose a reason for hiding this comment

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

Please expose it via a TestAccessor and add unit tests using ReferenceRepository

Copy link
Author

Choose a reason for hiding this comment

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

This is the method in FormCommit.cs that I used to write this method.

image

This method doesn't seem to have any tests. Neither is any of the invoking code covered by tests via the TestAccessor.

I'm afraid I don't have much idea about this test that you're proposing.

@hrai hrai closed this Mar 20, 2020
@hrai hrai deleted the escape_commit_msg branch March 20, 2020 23:08
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.

Commit message field in merge dialog does not escape commit message properly
3 participants