Skip to content

Commit ea28118

Browse files
authored
Properly escape double quotes in shell commands on Windows (#552)
* Add test for properly escaping double quote on Windows * Properly escape Windows command line arguments Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent db060fc commit ea28118

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/git/lib.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,9 @@ def escape_for_sh(s)
11911191
end
11921192

11931193
def escape_for_windows(s)
1194-
# Windows does not need single quote escaping inside double quotes
1195-
%Q{"#{s}"}
1194+
# Escape existing double quotes in s and then wrap the result with double quotes
1195+
escaped_string = s.to_s.gsub('"','\\"')
1196+
%Q{"#{escaped_string}"}
11961197
end
11971198

11981199
def windows_platform?
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env ruby
2+
# encoding: utf-8
3+
4+
require File.dirname(__FILE__) + '/../test_helper'
5+
6+
# Test diff when the file path has to be quoted according to core.quotePath
7+
# See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath
8+
#
9+
class TestWindowsCmdEscaping < Test::Unit::TestCase
10+
def test_commit_with_double_quote_in_commit_message
11+
expected_commit_message = 'Commit message with "double quotes"'
12+
in_temp_dir do |path|
13+
create_file('README.md', "# README\n")
14+
git = Git.init('.')
15+
git.add
16+
git.commit(expected_commit_message)
17+
commits = git.log(1)
18+
actual_commit_message = commits.first.message
19+
assert_equal(expected_commit_message, actual_commit_message)
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy