Remove files which are newly added but to be ignored

This commit is contained in:
Nobuyoshi Nakada 2023-11-07 09:28:10 +09:00
parent a328228591
commit 304194d73e
2 changed files with 24 additions and 4 deletions

View File

@ -554,6 +554,10 @@ module SyncDefaultGems
return true return true
end end
def preexisting?(base, file)
system(*%w"git cat-file -e", "#{base}:#{file}", err: File::NULL)
end
def filter_pickup_files(changed, ignore_file_pattern, base) def filter_pickup_files(changed, ignore_file_pattern, base)
toplevels = {} toplevels = {}
remove = [] remove = []
@ -561,14 +565,13 @@ module SyncDefaultGems
changed = changed.reject do |f| changed = changed.reject do |f|
case case
when toplevels.fetch(top = f[%r[\A[^/]+(?=/|\z)]m]) { when toplevels.fetch(top = f[%r[\A[^/]+(?=/|\z)]m]) {
remove << top if toplevels[top] = remove << top if toplevels[top] = !preexisting?(base, top)
!system(*%w"git cat-file -e", "#{base}:#{top}", err: File::NULL)
} }
# Remove any new top-level directories. # Remove any new top-level directories.
true true
when ignore_file_pattern.match?(f) when ignore_file_pattern.match?(f)
# Forcibly reset any changes matching ignore_file_pattern. # Forcibly reset any changes matching ignore_file_pattern.
ignore << f (preexisting?(base, f) ? ignore : remove) << f
end end
end end
return changed, remove, ignore return changed, remove, ignore

View File

@ -246,7 +246,24 @@ module Test_SyncDefaultGems
assert_not_equal(@sha["src"], top_commit("src"), out) assert_not_equal(@sha["src"], top_commit("src"), out)
assert_equal("*~\n", File.read("src/.gitignore"), out) assert_equal("*~\n", File.read("src/.gitignore"), out)
assert_equal("#!/bin/sh\n""echo ok\n", File.read("src/tool/ok"), out) assert_equal("#!/bin/sh\n""echo ok\n", File.read("src/tool/ok"), out)
assert_not_operator(File, :exist?, "src/.github/workflows/.yml", out) assert_equal(":ok\n""Should.be_merged\n", File.read("src/lib/common.rb"), out)
assert_not_operator(File, :exist?, "src/.github/workflows/main.yml", out)
end
def test_gitignore_after_conflict
File.write("src/Gemfile", "# main\n")
git(*%W"add Gemfile", chdir: "src")
git(*%W"commit -q -m", "Add Gemfile", chdir: "src")
File.write("#@target/Gemfile", "# conflict\n", mode: "a")
File.write("#@target/lib/common.rb", "Should.be_merged\n", mode: "a")
File.write("#@target/.github/workflows/main.yml", "# Should not merge\n", mode: "a")
git(*%W"add Gemfile .github lib/common.rb", chdir: @target)
git(*%W"commit -q -m", "Should be common.rb only", chdir: @target)
out = assert_sync()
assert_not_equal(@sha["src"], top_commit("src"), out)
assert_equal("# main\n", File.read("src/Gemfile"), out)
assert_equal(":ok\n""Should.be_merged\n", File.read("src/lib/common.rb"), out)
assert_not_operator(File, :exist?, "src/.github/workflows/main.yml", out)
end end
end end
end end