Skip to content

Commit f691352

Browse files
committed
Make worktree checkout idempotent
1 parent c180e0c commit f691352

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

TODO

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
Checkout worktree in tmp dir
21
Locate files from diff in worktree
32
Add full path to files in tmp dir to output annotated with change type

lib/files_in_my_diff/git/adapter.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ def diff(revision)
1414
def checkout_worktree(path, revision)
1515
@repo.add_worktree(path, revision)
1616
rescue ::Git::FailedError => e
17-
raise CheckoutError, "Could not checkout #{revision} to #{path}: #{e.message}"
17+
unless e.result.stderr.include? 'already exists'
18+
raise CheckoutError,
19+
"Could not checkout #{revision} to #{path}: #{e.message}"
20+
end
1821
end
1922

2023
private

test/git/adapter_test.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def test_that_checkout_failure_is_propagated
3232
end
3333
end
3434

35+
def test_that_checkout_failure_is_recovered_if_worktree_already_exists
36+
subject = subject(repo: repo_stub(checkout_success: MyFailedError.new('blabla already exists blabla')))
37+
subject.checkout_worktree('x', 'y')
38+
end
39+
3540
private
3641

3742
def subject(repo: repo_stub)
@@ -66,6 +71,7 @@ def initialize(checkout_success)
6671
end
6772

6873
def add_worktree(_path, _revision)
74+
raise @checkout_success if @checkout_success.is_a?(::Git::FailedError)
6975
raise MyFailedError unless @checkout_success
7076
end
7177

@@ -75,14 +81,19 @@ def object(_revision)
7581
end
7682

7783
class MyFailedError < ::Git::FailedError
78-
def initialize
79-
super(Command)
84+
def initialize(stderr = 'dummy')
85+
super(Command.new(stderr))
8086
end
8187

82-
module Command
83-
def self.git_cmd = 'dummy'
84-
def self.status = 'dummy'
85-
def self.stderr = 'dummy'
88+
class Command
89+
attr_reader :stderr
90+
91+
def initialize(stderr)
92+
@stderr = stderr
93+
end
94+
95+
def git_cmd = 'dummy'
96+
def status = 'dummy'
8697
end
8798
end
8899
end

0 commit comments

Comments
 (0)