Skip to content

Commit 27be99c

Browse files
committed
Update test of 1git worktree add1 with no commits
git-2.42.0 changes the behavior of `git worktree add` when there are no commits in the repository. Prior to 2.42.0, an error would result with creating a new worktree. Starting wtih 2.42.0, git will create a new, orphaned branch for the worktree. Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent e46ddcc commit 27be99c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/units/test_worktree.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def setup
3131
end
3232

3333
test 'adding a worktree when there are no commits should fail' do
34+
omit('Omitted since git version is >= 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) >= 0
35+
3436
in_temp_dir do |path|
3537
Dir.mkdir('main_worktree')
3638
Dir.chdir('main_worktree') do
@@ -47,6 +49,33 @@ def setup
4749
end
4850
end
4951

52+
test 'adding a worktree when there are no commits should succeed' do
53+
omit('Omitted since git version is < 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) < 0
54+
55+
in_temp_dir do |path|
56+
Dir.mkdir('main_worktree')
57+
Dir.chdir('main_worktree') do
58+
`git init`
59+
# `git commit --allow-empty -m "first commit"`
60+
end
61+
62+
git = Git.open('main_worktree')
63+
64+
assert_nothing_raised do
65+
git.worktree('feature1').add
66+
end
67+
68+
assert_equal(2, git.worktrees.size)
69+
70+
expected_worktree_dirs = [
71+
File.join(path, 'main_worktree'),
72+
File.join(path, 'feature1')
73+
].each_with_index do |expected_worktree_dir, i|
74+
assert_equal(expected_worktree_dir, git.worktrees.to_a[i].dir)
75+
end
76+
end
77+
end
78+
5079
test 'adding a worktree when there is at least one commit should succeed' do
5180
in_temp_dir do |path|
5281
Dir.mkdir('main_worktree')

0 commit comments

Comments
 (0)