compare_service.rb 930 字节
Newer Older
1 2
require 'securerandom'

3 4 5
# Compare 2 branches for one repo or between repositories
# and return Gitlab::CompareResult object that responds to commits and diffs
class CompareService
6
  def execute(source_project, source_branch, target_project, target_branch, diff_options = {})
7 8 9 10
    source_commit = source_project.commit(source_branch)
    return unless source_commit

    source_sha = source_commit.sha
11 12 13 14 15 16 17 18 19

    # If compare with other project we need to fetch ref first
    unless target_project == source_project
      random_string = SecureRandom.hex

      target_project.repository.fetch_ref(
        source_project.repository.path_to_repo,
        "refs/heads/#{source_branch}",
        "refs/tmp/#{random_string}/head"
20
      )
21
    end
22 23 24 25 26 27

    Gitlab::CompareResult.new(
      Gitlab::Git::Compare.new(
        target_project.repository.raw_repository,
        target_branch,
        source_sha,
28
      ), diff_options
29
    )
30 31
  end
end