Skip to content

Commit

Permalink
In ls-files do not unescape file paths with eval (#602)
Browse files Browse the repository at this point in the history
Signed-off-by: James Couball <jcouball@yahoo.com>
  • Loading branch information
jcouball committed Dec 9, 2022
1 parent 74b8e11 commit 4fe8738
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/git/lib.rb
Expand Up @@ -488,7 +488,9 @@ def ls_files(location=nil)
command_lines('ls-files', '--stage', location).each do |line|
(info, file) = line.split("\t")
(mode, sha, stage) = info.split
file = eval(file) if file =~ /^\".*\"$/ # This takes care of quoted strings returned from git
if file.start_with?('"') && file.end_with?('"')
file = Git::EscapedPath.new(file[1..-2]).unescape
end
hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
end
hsh
Expand Down
22 changes: 22 additions & 0 deletions tests/units/test_ls_files_with_escaped_path.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 TestLsFilesWithEscapedPath < Test::Unit::TestCase
def test_diff_with_non_ascii_filename
in_temp_dir do |path|
create_file('my_other_file_☠', "First Line\n")
create_file('README.md', '# My Project')
`git init`
`git add .`
`git config --local core.safecrlf false` if Gem.win_platform?
`git commit -m "First Commit"`
paths = Git.open('.').ls_files.keys.sort
assert_equal(["my_other_file_☠", 'README.md'].sort, paths)
end
end
end

0 comments on commit 4fe8738

Please sign in to comment.