BigW Consortium Gitlab

file_collection.rb 1.71 KB
Newer Older
1 2 3
module Gitlab
  module Conflict
    class FileCollection
4 5 6
      class ConflictSideMissing < StandardError
      end

7 8 9 10
      attr_reader :merge_request, :our_commit, :their_commit

      def initialize(merge_request)
        @merge_request = merge_request
11
        @our_commit = merge_request.source_branch_head.raw.raw_commit
12 13 14 15 16 17 18 19 20
        @their_commit = merge_request.target_branch_head.raw.raw_commit
      end

      def repository
        merge_request.project.repository
      end

      def merge_index
        @merge_index ||= repository.rugged.merge_commits(our_commit, their_commit)
21 22
      end

23 24
      def files
        @files ||= merge_index.conflicts.map do |conflict|
25
          raise ConflictSideMissing unless conflict[:theirs] && conflict[:ours]
26

27
          Gitlab::Conflict::File.new(merge_index.merge_file(conflict[:ours][:path]),
28
                                     conflict,
29
                                     merge_request: merge_request)
30 31 32
        end
      end

33 34 35 36
      def file_for_path(old_path, new_path)
        files.find { |file| file.their_path == old_path && file.our_path == new_path }
      end

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
      def as_json(opts = nil)
        {
          target_branch: merge_request.target_branch,
          source_branch: merge_request.source_branch,
          commit_sha: merge_request.diff_head_sha,
          commit_message: default_commit_message,
          files: files
        }
      end

      def default_commit_message
        conflict_filenames = merge_index.conflicts.map do |conflict|
          "#   #{conflict[:ours][:path]}"
        end

        <<EOM.chomp
53
Merge branch '#{merge_request.target_branch}' into '#{merge_request.source_branch}'
54 55 56 57 58 59 60 61

# Conflicts:
#{conflict_filenames.join("\n")}
EOM
      end
    end
  end
end