Welcome to mirror list, hosted at ThFree Co, Russian Federation.

compare_service.rb « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c8b35edff8bbd2c4a7fa3b640dd33e3d8864cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'securerandom'

# Compare 2 branches for one repo or between repositories
# and return Gitlab::CompareResult object that responds to commits and diffs
class CompareService
  def execute(current_user, source_project, source_branch, target_project, target_branch)
    # Try to compare branches to get commits list and diffs
    if target_project == source_project
      Gitlab::CompareResult.new(
        Gitlab::Git::Compare.new(
          target_project.repository.raw_repository,
          target_branch,
          source_branch,
        )
      )
    else
      random_string = SecureRandom.hex
      target_project.repository.fetch_ref(
        source_project.repository.path_to_repo,
        "refs/heads/#{source_branch}",
        "refs/tmp/#{random_string}/head"
      )

      source_sha = source_project.commit(source_branch).sha

      Gitlab::CompareResult.new(
        Gitlab::Git::Compare.new(
          target_project.repository.raw_repository,
          target_branch,
          source_sha,
        )
      )
    end
  end
end