Commit 99ac0935 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Introduce Repository#with_repo_branch_commit

We merge repository checks inside it so we don't have to
check it on the call site, and we could also load the commit
for the caller. This greatly reduce code duplication.

Feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7237#note_20572919
parent 05d742a0
...@@ -1063,19 +1063,26 @@ class Repository ...@@ -1063,19 +1063,26 @@ class Repository
Gitlab::Popen.popen(args, path_to_repo).first.lines.map(&:strip) Gitlab::Popen.popen(args, path_to_repo).first.lines.map(&:strip)
end end
def with_tmp_ref(source_repository, source_branch_name) def with_repo_branch_commit(source_repository, source_branch_name)
tmp_ref = "refs/tmp/#{SecureRandom.hex}/head" branch_name_or_sha =
if source_repository == self
source_branch_name
else
tmp_ref = "refs/tmp/#{SecureRandom.hex}/head"
fetch_ref( fetch_ref(
source_repository.path_to_repo, source_repository.path_to_repo,
"#{Gitlab::Git::BRANCH_REF_PREFIX}#{source_branch_name}", "#{Gitlab::Git::BRANCH_REF_PREFIX}#{source_branch_name}",
tmp_ref tmp_ref
) )
source_repository.commit(source_branch_name).sha
end
yield yield(commit(branch_name_or_sha))
ensure ensure
rugged.references.delete(tmp_ref) rugged.references.delete(tmp_ref) if tmp_ref
end end
def fetch_ref(source_path, source_ref, target_ref) def fetch_ref(source_path, source_ref, target_ref)
......
...@@ -11,19 +11,13 @@ class CompareService ...@@ -11,19 +11,13 @@ class CompareService
end end
def execute(target_project, target_branch, straight: false) def execute(target_project, target_branch, straight: false)
source_sha = source_project.repository.
commit(source_branch_name).try(:sha)
return unless source_sha
# If compare with other project we need to fetch ref first # If compare with other project we need to fetch ref first
if target_project == source_project target_project.repository.with_repo_branch_commit(
compare(source_sha, target_project, target_branch, straight) source_project.repository,
else source_branch_name) do |commit|
target_project.repository.with_tmp_ref( break unless commit
source_project.repository, source_branch_name) do
compare(source_sha, target_project, target_branch, straight) compare(commit.sha, target_project, target_branch, straight)
end
end end
end end
......
...@@ -43,23 +43,17 @@ class GitOperationService ...@@ -43,23 +43,17 @@ class GitOperationService
def with_branch( def with_branch(
branch_name, branch_name,
source_branch_name: nil, source_branch_name: nil,
source_project: repository.project) source_project: repository.project,
&block)
check_with_branch_arguments!( check_with_branch_arguments!(
branch_name, source_branch_name, source_project) branch_name, source_branch_name, source_project)
source_commit = source_project.repository.find_branch(
source_branch_name || branch_name).try(:dereferenced_target)
update_branch_with_hooks(branch_name) do update_branch_with_hooks(branch_name) do
if repository.project == source_project repository.with_repo_branch_commit(
yield(source_commit) source_project.repository,
else source_branch_name || branch_name,
repository.with_tmp_ref( &block)
source_project.repository, source_branch_name) do
yield(source_commit)
end
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment