Skip to content

Commit 5e6f3ea

Browse files
committed
Fix diffs of files that have quoted paths
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 12f908c commit 5e6f3ea

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-14
lines changed

lib/git/diff.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def process_full_diff
129129
final = {}
130130
current_file = nil
131131
@full_diff.split("\n").each do |line|
132-
if m = /^diff --git a\/(.*?) b\/(.*?)/.match(line)
133-
current_file = m[1]
132+
if m = %r{\Adiff --git ("?)a/(.+?)\1 \1b/(.+?)\1\z}.match(line)
133+
current_file = m[2]
134134
final[current_file] = defaults.merge({:patch => line, :path => current_file})
135135
else
136136
if m = /^index ([0-9a-f]{4,40})\.\.([0-9a-f]{4,40})( ......)*/.match(line)

lib/git/lib.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,8 @@ def command(cmd, *opts, &block)
10671067
global_opts = []
10681068
global_opts << "--git-dir=#{@git_dir}" if !@git_dir.nil?
10691069
global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil?
1070-
global_opts << ["-c", "color.ui=false"]
1070+
global_opts << %w[-c core.quotePath=false]
1071+
global_opts << %w[-c color.ui=false]
10711072

10721073
opts = [opts].flatten.map {|s| escape(s) }.join(' ')
10731074

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.dirname(__FILE__) + '/../test_helper'
4+
5+
# Test diff when the file path has to be quoted according to core.quotePath
6+
# See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath
7+
#
8+
class TestDiffWithQuotedPath < Test::Unit::TestCase
9+
def test_diff_with_non_ascii_filename
10+
in_temp_dir do |path|
11+
create_file('my_other_file_☠', "First Line\n")
12+
`git init`
13+
`git add .`
14+
`git commit -m 'First Commit'`
15+
update_file('my_other_file_☠', "Second Line\n")
16+
diff_paths = Git.open('.').diff.map(&:path)
17+
assert_equal(diff_paths, ['my_other_file_☠'])
18+
end
19+
end
20+
end

tests/units/test_logger.rb

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,49 @@ class TestLogger < Test::Unit::TestCase
77
def setup
88
set_file_paths
99
end
10-
10+
11+
def missing_log_entry
12+
'Did not find expected log entry.'
13+
end
14+
15+
def unexpected_log_entry
16+
'Unexpected log entry found'
17+
end
18+
1119
def test_logger
1220
log = Tempfile.new('logfile')
1321
log.close
14-
22+
1523
logger = Logger.new(log.path)
1624
logger.level = Logger::DEBUG
17-
25+
1826
@git = Git.open(@wdir, :log => logger)
1927
@git.branches.size
20-
28+
2129
logc = File.read(log.path)
22-
assert(/INFO -- : git ['"]--git-dir=[^'"]+['"] ['"]--work-tree=[^'"]+['"] ['"]-c['"] ['"]color.ui=false['"] branch ['"]-a['"]/.match(logc))
23-
assert(/DEBUG -- : cherry\n diff_over_patches\n\* git_grep/m.match(logc))
2430

31+
expected_log_entry = /INFO -- : git (?<global_options>.*?) branch '-a'/
32+
assert_match(expected_log_entry, logc, missing_log_entry)
33+
34+
expected_log_entry = /DEBUG -- : cherry/
35+
assert_match(expected_log_entry, logc, missing_log_entry)
36+
end
37+
38+
def test_logging_at_info_level_should_not_show_debug_messages
2539
log = Tempfile.new('logfile')
2640
log.close
2741
logger = Logger.new(log.path)
2842
logger.level = Logger::INFO
29-
43+
3044
@git = Git.open(@wdir, :log => logger)
3145
@git.branches.size
32-
46+
3347
logc = File.read(log.path)
34-
assert(/INFO -- : git ['"]--git-dir=[^'"]+['"] ['"]--work-tree=[^'"]+['"] ['"]-c['"] ['"]color.ui=false['"] branch ['"]-a['"]/.match(logc))
35-
assert(!/DEBUG -- : cherry\n diff_over_patches\n\* git_grep/m.match(logc))
48+
49+
expected_log_entry = /INFO -- : git (?<global_options>.*?) branch '-a'/
50+
assert_match(expected_log_entry, logc, missing_log_entry)
51+
52+
expected_log_entry = /DEBUG -- : cherry/
53+
assert_not_match(expected_log_entry, logc, unexpected_log_entry)
3654
end
37-
3855
end

0 commit comments

Comments
 (0)