Skip to content

Commit 631e488

Browse files
committed
[GitRepository] #clone_repo_if_missing with #git_transaction
Changed `#clone_repo` to `#clone_repo_if_missing`, and encapsulated the logic that existed in `#worktree` (dir check) in it as well. Also wrapped the method in a `#git_transaction`, which makes sure only one process handles this at a time.
1 parent 9ed8cce commit 631e488

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

app/models/git_repository.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def refresh_tags
156156

157157
def worktree
158158
@worktree ||= begin
159-
clone_repo unless Dir.exist?(directory_name)
159+
clone_repo_if_missing
160160
fetch_worktree
161161
end
162162
end
@@ -165,13 +165,15 @@ def fetch_worktree
165165
GitWorktree.new(worktree_params)
166166
end
167167

168-
def clone_repo
169-
handling_worktree_errors do
170-
message = "Cloning #{url} to #{directory_name}..."
171-
_log.info(message)
172-
GitWorktree.new(worktree_params.merge(:clone => true, :url => url))
173-
@updated_repo = true
174-
_log.info("#{message}...Complete")
168+
def clone_repo_if_missing
169+
git_transaction do
170+
handling_worktree_errors do
171+
message = "Cloning #{url} to #{directory_name}..."
172+
_log.info(message)
173+
GitWorktree.new(worktree_params.merge(:clone => true, :url => url))
174+
@updated_repo = true
175+
_log.info("#{message}...Complete")
176+
end unless Dir.exist?(directory_name)
175177
end
176178
end
177179

spec/models/git_repository_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
tag_info_hash[name]
9898
end
9999

100-
expect(repo).to receive(:clone_repo).once.with(no_args).and_call_original
100+
expect(repo).to receive(:clone_repo_if_missing).once.with(no_args).and_call_original
101101
expect(GitWorktree).to receive(:new).with(anything).and_return(gwt)
102102
expect(gwt).to receive(:fetch_and_merge).with(no_args)
103103

0 commit comments

Comments
 (0)