diff --git a/CHANGELOG.md b/CHANGELOG.md index c327e01d..f7d9bcae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ # Change Log +## v2.1.1 (2024-06-01) + +[Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.1.0..v2.1.1) + +Changes since v2.1.0: + +* 6ce3d4d Handle ignored files with quoted (non-ASCII) filenames +* dd8e8d4 Supply all of the _specific_ color options too +* 749a72d Memoize all of the significant calls in Git::Status +* 2bacccc When core.ignoreCase, check for untracked files case-insensitively +* 7758ee4 When core.ignoreCase, check for deleted files case-insensitively +* 993eb78 When core.ignoreCase, check for added files case-insensitively +* d943bf4 When core.ignoreCase, check for changed files case-insensitively + ## v2.1.0 (2024-05-31) [Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.0.1..v2.1.0) diff --git a/lib/git/base.rb b/lib/git/base.rb index 97151c20..4a04a7ec 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -309,6 +309,13 @@ def grep(string, path_limiter = nil, opts = {}) self.object('HEAD').grep(string, path_limiter, opts) end + # List the files in the worktree that are ignored by git + # @return [Array] the list of ignored files relative to teh root of the worktree + # + def ignored_files + self.lib.ignored_files + end + # removes file(s) from the git repository def rm(path = '.', opts = {}) self.lib.rm(path, opts) diff --git a/lib/git/escaped_path.rb b/lib/git/escaped_path.rb index 73e4f175..6c085e6d 100644 --- a/lib/git/escaped_path.rb +++ b/lib/git/escaped_path.rb @@ -3,7 +3,7 @@ module Git # Represents an escaped Git path string # - # Git commands that output paths (e.g. ls-files, diff), will escape usual + # Git commands that output paths (e.g. ls-files, diff), will escape unusual # characters in the path with backslashes in the same way C escapes control # characters (e.g. \t for TAB, \n for LF, \\ for backslash) or bytes with values # larger than 0x80 (e.g. octal \302\265 for "micro" in UTF-8). diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 22f474e5..1eefc70e 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -574,18 +574,52 @@ def diff_index(treeish) diff_as_hash('diff-index', treeish) end + # List all files that are in the index + # + # @param location [String] the location to list the files from + # + # @return [Hash] a hash of files in the index + # * key: file [String] the file path + # * value: file_info [Hash] the file information containing the following keys: + # * :path [String] the file path + # * :mode_index [String] the file mode + # * :sha_index [String] the file sha + # * :stage [String] the file stage + # def ls_files(location=nil) location ||= '.' - hsh = {} - command_lines('ls-files', '--stage', location).each do |line| - (info, file) = line.split("\t") - (mode, sha, stage) = info.split - if file.start_with?('"') && file.end_with?('"') - file = Git::EscapedPath.new(file[1..-2]).unescape + {}.tap do |files| + command_lines('ls-files', '--stage', location).each do |line| + (info, file) = line.split("\t") + (mode, sha, stage) = info.split + files[unescape_quoted_path(file)] = { + :path => file, :mode_index => mode, :sha_index => sha, :stage => stage + } end - hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage} end - hsh + end + + # Unescape a path if it is quoted + # + # Git commands that output paths (e.g. ls-files, diff), will escape unusual + # characters. + # + # @example + # lib.unescape_if_quoted('"quoted_file_\\342\\230\\240"') # => 'quoted_file_☠' + # lib.unescape_if_quoted('unquoted_file') # => 'unquoted_file' + # + # @param path [String] the path to unescape if quoted + # + # @return [String] the unescaped path if quoted otherwise the original path + # + # @api private + # + def unescape_quoted_path(path) + if path.start_with?('"') && path.end_with?('"') + Git::EscapedPath.new(path[1..-2]).unescape + else + path + end end def ls_remote(location=nil, opts={}) @@ -606,7 +640,7 @@ def ls_remote(location=nil, opts={}) end def ignored_files - command_lines('ls-files', '--others', '-i', '--exclude-standard') + command_lines('ls-files', '--others', '-i', '--exclude-standard').map { |f| unescape_quoted_path(f) } end def untracked_files @@ -1223,6 +1257,14 @@ def global_opts global_opts << "--work-tree=#{@git_work_dir}" if !@git_work_dir.nil? global_opts << '-c' << 'core.quotePath=true' global_opts << '-c' << 'color.ui=false' + global_opts << '-c' << 'color.advice=false' + global_opts << '-c' << 'color.diff=false' + global_opts << '-c' << 'color.grep=false' + global_opts << '-c' << 'color.push=false' + global_opts << '-c' << 'color.remote=false' + global_opts << '-c' << 'color.showBranch=false' + global_opts << '-c' << 'color.status=false' + global_opts << '-c' << 'color.transport=false' end end diff --git a/lib/git/status.rb b/lib/git/status.rb index d31dc7b4..39ceace7 100644 --- a/lib/git/status.rb +++ b/lib/git/status.rb @@ -22,7 +22,7 @@ def initialize(base) # # @return [Enumerable] def changed - @files.select { |_k, f| f.type == 'M' } + @_changed ||= @files.select { |_k, f| f.type == 'M' } end # @@ -34,7 +34,7 @@ def changed # changed?('lib/git.rb') # @return [Boolean] def changed?(file) - changed.member?(file) + case_aware_include?(:changed, :lc_changed, file) end # Returns an Enumerable containing files that have been added. @@ -42,7 +42,7 @@ def changed?(file) # # @return [Enumerable] def added - @files.select { |_k, f| f.type == 'A' } + @_added ||= @files.select { |_k, f| f.type == 'A' } end # Determines whether the given file has been added to the repository @@ -54,7 +54,7 @@ def added # added?('lib/git.rb') # @return [Boolean] def added?(file) - added.member?(file) + case_aware_include?(:added, :lc_added, file) end # @@ -63,7 +63,7 @@ def added?(file) # # @return [Enumerable] def deleted - @files.select { |_k, f| f.type == 'D' } + @_deleted ||= @files.select { |_k, f| f.type == 'D' } end # @@ -75,7 +75,7 @@ def deleted # deleted?('lib/git.rb') # @return [Boolean] def deleted?(file) - deleted.member?(file) + case_aware_include?(:deleted, :lc_deleted, file) end # @@ -84,7 +84,7 @@ def deleted?(file) # # @return [Enumerable] def untracked - @files.select { |_k, f| f.untracked } + @_untracked ||= @files.select { |_k, f| f.untracked } end # @@ -96,7 +96,7 @@ def untracked # untracked?('lib/git.rb') # @return [Boolean] def untracked?(file) - untracked.member?(file) + case_aware_include?(:untracked, :lc_untracked, file) end def pretty @@ -264,5 +264,43 @@ def fetch_added end end end + + # It's worth noting that (like git itself) this gem will not behave well if + # ignoreCase is set inconsistently with the file-system itself. For details: + # https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreignoreCase + def ignore_case? + return @_ignore_case if defined?(@_ignore_case) + @_ignore_case = @base.config('core.ignoreCase') == 'true' + rescue Git::FailedError + @_ignore_case = false + end + + def downcase_keys(hash) + hash.map { |k, v| [k.downcase, v] }.to_h + end + + def lc_changed + @_lc_changed ||= changed.transform_keys(&:downcase) + end + + def lc_added + @_lc_added ||= added.transform_keys(&:downcase) + end + + def lc_deleted + @_lc_deleted ||= deleted.transform_keys(&:downcase) + end + + def lc_untracked + @_lc_untracked ||= untracked.transform_keys(&:downcase) + end + + def case_aware_include?(cased_hash, downcased_hash, file) + if ignore_case? + send(downcased_hash).include?(file.downcase) + else + send(cased_hash).include?(file) + end + end end end diff --git a/lib/git/version.rb b/lib/git/version.rb index b88d2356..f970509b 100644 --- a/lib/git/version.rb +++ b/lib/git/version.rb @@ -1,5 +1,5 @@ module Git # The current gem version # @return [String] the current gem version. - VERSION='2.1.0' + VERSION='2.1.1' end diff --git a/tests/files/working/dot_git/config b/tests/files/working/dot_git/config index 6c545b24..50a9ab00 100644 --- a/tests/files/working/dot_git/config +++ b/tests/files/working/dot_git/config @@ -13,3 +13,12 @@ [remote "working"] url = ../working.git fetch = +refs/heads/*:refs/remotes/working/* +[color] + diff = always + showBranch = always + grep = always + advice = always + push = always + remote = always + transport = always + status = always diff --git a/tests/units/test_ignored_files_with_escaped_path.rb b/tests/units/test_ignored_files_with_escaped_path.rb new file mode 100644 index 00000000..0d40711d --- /dev/null +++ b/tests/units/test_ignored_files_with_escaped_path.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby +# encoding: utf-8 + +require '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 TestIgnoredFilesWithEscapedPath < Test::Unit::TestCase + def test_ignored_files_with_non_ascii_filename + in_temp_dir do |path| + 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"` + create_file('my_other_file_☠', "First Line\n") + create_file(".gitignore", "my_other_file_☠") + files = Git.open('.').ignored_files + assert_equal(['my_other_file_☠'].sort, files) + end + end +end diff --git a/tests/units/test_status.rb b/tests/units/test_status.rb index b7ad4888..36543bc1 100644 --- a/tests/units/test_status.rb +++ b/tests/units/test_status.rb @@ -92,6 +92,7 @@ def test_dot_files_status def test_added_boolean in_temp_dir do |path| git = Git.clone(@wdir, 'test_dot_files_status') + git.config('core.ignorecase', 'false') create_file('test_dot_files_status/test_file_1', 'content tets_file_1') create_file('test_dot_files_status/test_file_2', 'content tets_file_2') @@ -100,12 +101,17 @@ def test_added_boolean assert(git.status.added?('test_file_1')) assert(!git.status.added?('test_file_2')) + assert(!git.status.added?('TEST_FILE_1')) + + git.config('core.ignorecase', 'true') + assert(git.status.added?('TEST_FILE_1')) end end def test_changed_boolean in_temp_dir do |path| git = Git.clone(@wdir, 'test_dot_files_status') + git.config('core.ignorecase', 'false') create_file('test_dot_files_status/test_file_1', 'content tets_file_1') create_file('test_dot_files_status/test_file_2', 'content tets_file_2') @@ -117,12 +123,20 @@ def test_changed_boolean assert(git.status.changed?('test_file_1')) assert(!git.status.changed?('test_file_2')) + + update_file('test_dot_files_status/scott/text.txt', 'definitely different') + assert(git.status.changed?('scott/text.txt')) + assert(!git.status.changed?('scott/TEXT.txt')) + + git.config('core.ignorecase', 'true') + assert(git.status.changed?('scott/TEXT.txt')) end end def test_deleted_boolean in_temp_dir do |path| git = Git.clone(@wdir, 'test_dot_files_status') + git.config('core.ignorecase', 'false') create_file('test_dot_files_status/test_file_1', 'content tets_file_1') create_file('test_dot_files_status/test_file_2', 'content tets_file_2') @@ -133,6 +147,10 @@ def test_deleted_boolean assert(git.status.deleted?('test_file_1')) assert(!git.status.deleted?('test_file_2')) + assert(!git.status.deleted?('TEST_FILE_1')) + + git.config('core.ignorecase', 'true') + assert(git.status.deleted?('TEST_FILE_1')) end end @@ -189,6 +207,7 @@ def test_untracked_from_subdir def test_untracked_boolean in_temp_dir do |path| git = Git.clone(@wdir, 'test_dot_files_status') + git.config('core.ignorecase', 'false') create_file('test_dot_files_status/test_file_1', 'content tets_file_1') create_file('test_dot_files_status/test_file_2', 'content tets_file_2') @@ -196,6 +215,10 @@ def test_untracked_boolean assert(git.status.untracked?('test_file_1')) assert(!git.status.untracked?('test_file_2')) + assert(!git.status.untracked?('TEST_FILE_1')) + + git.config('core.ignorecase', 'true') + assert(git.status.untracked?('TEST_FILE_1')) end end