Class: Gitlab::Conflict::FileCollection

Inherits:
Object
  • Object
show all
Includes:
RepositoryCacheAdapter
Defined in:
lib/gitlab/conflict/file_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RepositoryCacheAdapter

#cache_method_output, #cache_method_output_as_redis_set, #cache_method_output_asymmetrically, #cached_methods, #expire_method_caches, #memoize_method_cache_value, #memoize_method_output, #no_repository_fallback, #redis_hash_cache, #redis_set_cache, #request_store_cache

Constructor Details

#initialize(merge_request, allow_tree_conflicts: false) ⇒ FileCollection

Returns a new instance of FileCollection.



10
11
12
13
14
15
16
17
18
19
# File 'lib/gitlab/conflict/file_collection.rb', line 10

def initialize(merge_request, allow_tree_conflicts: false)
  our_commit = merge_request.source_branch_head.raw
  their_commit = merge_request.target_branch_head.raw
  @target_repo = merge_request.target_project.repository
  @source_repo = merge_request.source_project.repository.raw
  @our_commit_id = our_commit.id
  @their_commit_id = their_commit.id
  @resolver = Gitlab::Git::Conflict::Resolver.new(@target_repo.raw, @our_commit_id, @their_commit_id, allow_tree_conflicts: allow_tree_conflicts)
  @merge_request = merge_request
end

Instance Attribute Details

#merge_requestObject (readonly)

Returns the value of attribute merge_request.



8
9
10
# File 'lib/gitlab/conflict/file_collection.rb', line 8

def merge_request
  @merge_request
end

#resolverObject (readonly)

Returns the value of attribute resolver.



8
9
10
# File 'lib/gitlab/conflict/file_collection.rb', line 8

def resolver
  @resolver
end

Instance Method Details

#as_json(opts = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/gitlab/conflict/file_collection.rb', line 59

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

#can_be_resolved_in_ui?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gitlab/conflict/file_collection.rb', line 41

def can_be_resolved_in_ui?
  # Try to parse each conflict. If the MR's mergeable status hasn't been
  # updated, ensure that we don't say there are conflicts to resolve
  # when there are no conflict files.
  files.each(&:lines)
  files.any?
rescue Gitlab::Git::CommandError,
       Gitlab::Git::Conflict::Parser::UnresolvableError,
       Gitlab::Git::Conflict::Resolver::ConflictSideMissing,
       Gitlab::Git::Conflict::File::UnsupportedEncoding
  false
end

#default_commit_messageObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gitlab/conflict/file_collection.rb', line 69

def default_commit_message
  conflict_filenames = files.map do |conflict|
    "#   #{conflict.our_path}"
  end

  <<EOM.chomp
Merge branch '#{merge_request.target_branch}' into '#{merge_request.source_branch}'

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

#file_for_path(old_path, new_path) ⇒ Object



55
56
57
# File 'lib/gitlab/conflict/file_collection.rb', line 55

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

#filesObject



35
36
37
38
39
# File 'lib/gitlab/conflict/file_collection.rb', line 35

def files
  @files ||= resolver.conflicts.map do |conflict_file|
    Gitlab::Conflict::File.new(conflict_file, merge_request: merge_request)
  end
end

#resolve(user, commit_message, files) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/conflict/file_collection.rb', line 21

def resolve(user, commit_message, files)
  msg = commit_message || default_commit_message
  resolution = Gitlab::Git::Conflict::Resolution.new(user, files, msg)

  resolver.resolve_conflicts(
    @source_repo,
    resolution,
    source_branch: merge_request.source_branch,
    target_branch: merge_request.target_branch
  )
ensure
  @merge_request.clear_memoized_shas
end