Skip to content

Commit 4fe8738

Browse files
authored
In ls-files do not unescape file paths with eval (#602)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 74b8e11 commit 4fe8738

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/git/lib.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ def ls_files(location=nil)
488488
command_lines('ls-files', '--stage', location).each do |line|
489489
(info, file) = line.split("\t")
490490
(mode, sha, stage) = info.split
491-
file = eval(file) if file =~ /^\".*\"$/ # This takes care of quoted strings returned from git
491+
if file.start_with?('"') && file.end_with?('"')
492+
file = Git::EscapedPath.new(file[1..-2]).unescape
493+
end
492494
hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
493495
end
494496
hsh
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 TestLsFilesWithEscapedPath < Test::Unit::TestCase
10+
def test_diff_with_non_ascii_filename
11+
in_temp_dir do |path|
12+
create_file('my_other_file_☠', "First Line\n")
13+
create_file('README.md', '# My Project')
14+
`git init`
15+
`git add .`
16+
`git config --local core.safecrlf false` if Gem.win_platform?
17+
`git commit -m "First Commit"`
18+
paths = Git.open('.').ls_files.keys.sort
19+
assert_equal(["my_other_file_☠", 'README.md'].sort, paths)
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)