Skip to content

Commit 3651f98

Browse files
committed
Exclude files added to the toplevel
1 parent af13b03 commit 3651f98

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

tool/sync_default_gems.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ def sync_default_gems(gem)
436436
|\.git.*
437437
|[A-Z]\w+file
438438
|COPYING
439-
|\Arakelib\/.*
440-
|\Atest\/lib\/.*
439+
|rakelib\/.*
440+
|test\/lib\/.*
441441
)\z/mx
442442

443443
def message_filter(repo, sha, input: ARGF)
@@ -586,16 +586,32 @@ def sync_default_gems_with_commits(gem, ranges, edit: nil)
586586
next
587587
end
588588

589-
tools = pipe_readlines(%W"git diff --name-only -z HEAD~..HEAD -- test/lib/ tool/ rakelib/")
589+
changed = pipe_readlines(%W"git diff --name-only -z HEAD~..HEAD --")
590+
toplevels = changed.map {|f| f[%r[\A(?!tool/)[^/]+/?]]}.compact
591+
toplevels.delete_if do |top|
592+
if system(*%w"git checkout -f HEAD~ --", top, err: File::NULL)
593+
# previously existent path
594+
system(*%w"git checkout -f HEAD --", top, out: File::NULL)
595+
true
596+
end
597+
end
598+
unless toplevels.empty?
599+
puts "Remove files added to toplevel: #{toplevels.join(', ')}"
600+
system(*%w"git rm -r --", *toplevels)
601+
end
602+
tools = changed.select {|f|f.start_with?("test/lib/", "tool/")}
590603
unless tools.empty?
591-
system(*%W"git rm --", *tools)
604+
system(*%W"git rm -r --", *tools)
592605
system(*%W"git checkout HEAD~ --", *tools)
606+
end
607+
unless toplevels.empty? and tools.empty?
608+
clean = toplevels + tools
593609
if system(*%W"git diff --quiet HEAD~")
594610
`git reset HEAD~ --` && `git checkout .` && `git clean -fd`
595-
puts "Skip commit #{sha} only for tools"
611+
puts "Skip commit #{sha} only for tools or toplevel"
596612
next
597613
end
598-
unless system(*%W"git commit --amend --no-edit --", *tools)
614+
unless system(*%W"git commit --amend --no-edit --", *clean)
599615
failed_commits << sha
600616
`git reset HEAD~ --` && `git checkout .` && `git clean -fd`
601617
puts "Failed to pick #{sha}"

tool/test/test_sync_default_gems.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ def setup
9393
Dir.chdir(@testdir)
9494
["src", @target].each do |dir|
9595
system(*%W"git init -q #{dir}", exception: true)
96+
if dir == "src"
97+
Dir.mkdir("#{dir}/lib")
98+
File.write("#{dir}/lib/fine.rb", "return\n")
99+
system(*%W"git add lib/fine.rb", exception: true, chdir: dir)
100+
system(*%W"git commit -q -m", "Looks fine", exception: true, chdir: dir)
101+
end
96102
Dir.mkdir("#{dir}/tool")
97103
File.write("#{dir}/tool/ok", "#!/bin/sh\n""echo ok\n")
98104
system(*%W"git add tool/ok", exception: true, chdir: dir)
@@ -147,5 +153,36 @@ def test_skip_tool
147153
end
148154
assert_equal(@sha["src"], IO.popen(%W[git log --format=%H -1], chdir: "src", &:read).chomp, out)
149155
end
156+
157+
def test_skip_toplevel
158+
Dir.mkdir("#@target/docs")
159+
File.write("#@target/docs/NEWS.md", "= NEWS!!!\n")
160+
system(*%W"git add --", "docs/NEWS.md", exception: true, chdir: @target)
161+
system(*%W"git commit -q -m", "It's a news", exception: true, chdir: @target)
162+
out = capture_process_output_to([STDOUT, STDERR]) do
163+
Dir.chdir("src") do
164+
SyncDefaultGems.sync_default_gems_with_commits(@target, true)
165+
end
166+
end
167+
assert_equal(@sha["src"], IO.popen(%W[git log --format=%H -1], chdir: "src", &:read).chomp, out)
168+
end
169+
170+
def test_adding_toplevel
171+
Dir.mkdir("#@target/docs")
172+
File.write("#@target/docs/NEWS.md", "= New library\n")
173+
Dir.mkdir("#@target/lib")
174+
File.write("#@target/lib/news.rb", "return\n")
175+
system(*%W"git add --", "docs/NEWS.md", "lib/news.rb", exception: true, chdir: @target)
176+
system(*%W"git commit -q -m", "New lib", exception: true, chdir: @target)
177+
out = capture_process_output_to([STDOUT, STDERR]) do
178+
Dir.chdir("src") do
179+
SyncDefaultGems.sync_default_gems_with_commits(@target, true)
180+
end
181+
end
182+
assert_not_equal(@sha["src"], IO.popen(%W[git log --format=%H -1], chdir: "src", &:read).chomp, out)
183+
assert_equal "return\n", File.read("src/lib/news.rb")
184+
assert_include IO.popen(%W[git log -1 --oneline], chdir: "src", &:read), "[ruby/#{@target}] New lib"
185+
assert_not_operator File, :exist?, "src/docs"
186+
end
150187
end
151188
end

0 commit comments

Comments
 (0)