From e46ddcc711c41fe96810396a9c8db032af01e154 Mon Sep 17 00:00:00 2001 From: James Couball Date: Sun, 17 Sep 2023 15:32:18 -0700 Subject: [PATCH 1/3] Add Git::Lib#compare_version_to(other_version) Signed-off-by: James Couball --- lib/git/lib.rb | 16 ++++++++++++++++ tests/units/test_lib.rb | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 27934aa3..1b586f60 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -1108,6 +1108,22 @@ def current_command_version version_parts.fill(0, version_parts.length...3) end + # Returns current_command_version <=> other_version + # + # @example + # lib.current_command_version #=> [2, 42, 0] + # + # lib.compare_version_to(2, 41, 0) #=> 1 + # lib.compare_version_to(2, 42, 0) #=> 0 + # lib.compare_version_to(2, 43, 0) #=> -1 + # + # @param other_version [Array] the other version to compare to + # @return [Integer] -1 if this version is less than other_version, 0 if equal, or 1 if greater than + # + def compare_version_to(*other_version) + current_command_version <=> other_version + end + def required_command_version [1, 6] end diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index 577d7d73..c7283d4e 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -325,4 +325,15 @@ def test_show assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n") end + def test_compare_version_to + lib = Git::Lib.new(nil, nil) + current_version = [2, 42, 0] + lib.define_singleton_method(:current_command_version) { current_version } + assert lib.compare_version_to(0, 43, 9) == 1 + assert lib.compare_version_to(2, 41, 0) == 1 + assert lib.compare_version_to(2, 42, 0) == 0 + assert lib.compare_version_to(2, 42, 1) == -1 + assert lib.compare_version_to(2, 43, 0) == -1 + assert lib.compare_version_to(3, 0, 0) == -1 + end end From 27be99ca98b6e5f2c39e5eb30917c3fbefaf547e Mon Sep 17 00:00:00 2001 From: James Couball Date: Sun, 17 Sep 2023 15:32:55 -0700 Subject: [PATCH 2/3] 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 --- tests/units/test_worktree.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/units/test_worktree.rb b/tests/units/test_worktree.rb index 021a82a3..bbe377ce 100644 --- a/tests/units/test_worktree.rb +++ b/tests/units/test_worktree.rb @@ -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 @@ -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') From b0b8ffdf81f488ca9a8a3b43ccdb8f0c0358d2ae Mon Sep 17 00:00:00 2001 From: James Couball Date: Mon, 18 Sep 2023 12:48:04 -0700 Subject: [PATCH 3/3] Rewrite test_repack so it works in Windows Signed-off-by: James Couball --- tests/units/test_repack.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/units/test_repack.rb b/tests/units/test_repack.rb index abe2442a..da7be542 100644 --- a/tests/units/test_repack.rb +++ b/tests/units/test_repack.rb @@ -3,20 +3,18 @@ require 'test_helper' class TestRepack < Test::Unit::TestCase - def test_repack + test 'should be able to call repack with the right args' do in_bare_repo_clone do |r1| new_file('new_file', 'new content') - r1.add r1.commit('my commit') - # see how big the repo is - size1 = r1.repo_size - - r1.repack + # assert_nothing_raised { r1.repack } - # see how big the repo is now, should be smaller - assert(size1 > r1.repo_size) + expected_command_line = ['repack', '-a', '-d'] + git_cmd = :repack + git_cmd_args = [] + assert_command_line(expected_command_line, git_cmd, git_cmd_args) end end end