Skip to content

Update test of 'git worktree add' with no commits #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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>
  • Loading branch information
jcouball committed Sep 17, 2023
commit 27be99ca98b6e5f2c39e5eb30917c3fbefaf547e
29 changes: 29 additions & 0 deletions tests/units/test_worktree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def setup
end

test 'adding a worktree when there are no commits should fail' do
omit('Omitted since git version is >= 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) >= 0

in_temp_dir do |path|
Dir.mkdir('main_worktree')
Dir.chdir('main_worktree') do
Expand All @@ -47,6 +49,33 @@ def setup
end
end

test 'adding a worktree when there are no commits should succeed' do
omit('Omitted since git version is < 2.42.0') if Git::Lib.new(nil, nil).compare_version_to(2, 42, 0) < 0

in_temp_dir do |path|
Dir.mkdir('main_worktree')
Dir.chdir('main_worktree') do
`git init`
# `git commit --allow-empty -m "first commit"`
end

git = Git.open('main_worktree')

assert_nothing_raised do
git.worktree('feature1').add
end

assert_equal(2, git.worktrees.size)

expected_worktree_dirs = [
File.join(path, 'main_worktree'),
File.join(path, 'feature1')
].each_with_index do |expected_worktree_dir, i|
assert_equal(expected_worktree_dir, git.worktrees.to_a[i].dir)
end
end
end

test 'adding a worktree when there is at least one commit should succeed' do
in_temp_dir do |path|
Dir.mkdir('main_worktree')
Expand Down