From 4fe8738e8348567255ab4be25867684b5d0d282d Mon Sep 17 00:00:00 2001 From: James Couball Date: Fri, 9 Dec 2022 14:17:01 -0800 Subject: [PATCH] In ls-files do not unescape file paths with eval (#602) Signed-off-by: James Couball --- lib/git/lib.rb | 4 +++- .../units/test_ls_files_with_escaped_path.rb | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/units/test_ls_files_with_escaped_path.rb diff --git a/lib/git/lib.rb b/lib/git/lib.rb index e898ebc3..293f2878 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -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 diff --git a/tests/units/test_ls_files_with_escaped_path.rb b/tests/units/test_ls_files_with_escaped_path.rb new file mode 100644 index 00000000..47607dd3 --- /dev/null +++ b/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