From 934d17da36db2fabdfed384936ce2dfdd52abfc9 Mon Sep 17 00:00:00 2001 From: James Couball Date: Mon, 3 Jan 2022 08:35:25 -0800 Subject: [PATCH 1/2] Add test for properly escaping double quote on Windows Signed-off-by: James Couball --- tests/units/test_windows_cmd_escaping.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/units/test_windows_cmd_escaping.rb diff --git a/tests/units/test_windows_cmd_escaping.rb b/tests/units/test_windows_cmd_escaping.rb new file mode 100644 index 00000000..a5d994d9 --- /dev/null +++ b/tests/units/test_windows_cmd_escaping.rb @@ -0,0 +1,22 @@ +#!/usr/bin/env ruby +# encoding: utf-8 + +require File.dirname(__FILE__) + '/../test_helper' + +# Test diff when the file path has to be quoted according to core.quotePath +# See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath +# +class TestWindowsCmdEscaping < Test::Unit::TestCase + def test_commit_with_double_quote_in_commit_message + expected_commit_message = 'Commit message with "double quotes"' + in_temp_dir do |path| + create_file('README.md', "# README\n") + git = Git.init('.') + git.add + git.commit(expected_commit_message) + commits = git.log(1) + actual_commit_message = commits.first.message + assert_equal(expected_commit_message, actual_commit_message) + end + end +end From cca7109273858b78411431e775e8d9365a6ef41c Mon Sep 17 00:00:00 2001 From: James Couball Date: Mon, 3 Jan 2022 09:08:07 -0800 Subject: [PATCH 2/2] Properly escape Windows command line arguments Signed-off-by: James Couball --- lib/git/lib.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index d892462a..2d6c129d 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -1191,8 +1191,9 @@ def escape_for_sh(s) end def escape_for_windows(s) - # Windows does not need single quote escaping inside double quotes - %Q{"#{s}"} + # Escape existing double quotes in s and then wrap the result with double quotes + escaped_string = s.to_s.gsub('"','\\"') + %Q{"#{escaped_string}"} end def windows_platform?