Methods
A
B
C
D
F
H
L
M
O
P
R
T
U
V
Included Modules
Constants
BROKEN_DIFF = "--broken-diff"
 
UNCHECKED = 1
 
CAN_BE_MERGED = 2
 
CANNOT_BE_MERGED = 3
 
Attributes
[RW] should_remove_source_branch
Class Public methods
find_all_by_branch(branch_name)
# File app/models/merge_request.rb, line 47
def self.find_all_by_branch(branch_name)
  where("source_branch LIKE :branch OR target_branch LIKE :branch", branch: branch_name)
end
find_all_by_milestone(milestone)
# File app/models/merge_request.rb, line 51
def self.find_all_by_milestone(milestone)
  where("milestone_id = :milestone_id", milestone_id: milestone)
end
Instance Public methods
automerge!(current_user)
# File app/models/merge_request.rb, line 195
def automerge!(current_user)
  if Gitlab::Satellite::MergeAction.new(current_user, self).merge! && self.unmerged_commits.empty?
    self.merge!(current_user.id)
    true
  end
rescue
  self.mark_as_unmergable
  false
end
broken_diffs?()
# File app/models/merge_request.rb, line 112
def broken_diffs?
  diffs == [BROKEN_DIFF]
end
can_be_merged?()
# File app/models/merge_request.rb, line 84
def can_be_merged?
  state == CAN_BE_MERGED
end
check_if_can_be_merged()
# File app/models/merge_request.rb, line 88
def check_if_can_be_merged
  self.state = if Gitlab::Satellite::MergeAction.new(self.author, self).can_be_merged?
                 CAN_BE_MERGED
               else
                 CANNOT_BE_MERGED
               end
  self.save
end
closed_event()
# File app/models/merge_request.rb, line 140
def closed_event
  self.project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::Closed).last
end
commits()
# File app/models/merge_request.rb, line 144
def commits
  st_commits || []
end
diffs()
# File app/models/merge_request.rb, line 97
def diffs
  st_diffs || []
end
human_state()
# File app/models/merge_request.rb, line 55
def human_state
  states = {
    CAN_BE_MERGED =>  "can_be_merged",
    CANNOT_BE_MERGED => "cannot_be_merged",
    UNCHECKED => "unchecked"
  }
  states[self.state]
end
last_commit()
# File app/models/merge_request.rb, line 128
def last_commit
  commits.first
end
last_commit_short_sha()
# File app/models/merge_request.rb, line 224
def last_commit_short_sha
  @last_commit_short_sha ||= last_commit.sha[0..10]
end
mark_as_merged!()
# File app/models/merge_request.rb, line 157
def mark_as_merged!
  self.merged = true
  self.closed = true
  save
end
mark_as_unchecked()
# File app/models/merge_request.rb, line 79
def mark_as_unchecked
  self.state = UNCHECKED
  self.save
end
mark_as_unmergable()
# File app/models/merge_request.rb, line 163
def mark_as_unmergable
  self.state = CANNOT_BE_MERGED
  self.save
end
merge!(user_id)
# File app/models/merge_request.rb, line 184
def merge!(user_id)
  self.mark_as_merged!
  Event.create(
    project: self.project,
    action: Event::Merged,
    target_id: self.id,
    target_type: "MergeRequest",
    author_id: user_id
  )
end
merge_event()
# File app/models/merge_request.rb, line 136
def merge_event
  self.project.events.where(target_id: self.id, target_type: "MergeRequest", action: Event::Merged).last
end
merged?()
# File app/models/merge_request.rb, line 132
def merged?
  merged && merge_event
end
mr_and_commit_notes()
# File app/models/merge_request.rb, line 205
def mr_and_commit_notes
  commit_ids = commits.map(&:id)
  Note.where("(noteable_type = 'MergeRequest' AND noteable_id = :mr_id) OR (noteable_type = 'Commit' AND commit_id IN (:commit_ids))", mr_id: id, commit_ids: commit_ids)
end
open?()
# File app/models/merge_request.rb, line 153
def open?
  !closed
end
probably_merged?()
# File app/models/merge_request.rb, line 148
def probably_merged?
  unmerged_commits.empty? &&
    commits.any? && open?
end
reload_code()
# File app/models/merge_request.rb, line 70
def reload_code
  self.reloaded_commits
  self.reloaded_diffs
end
reloaded_commits()
# File app/models/merge_request.rb, line 168
def reloaded_commits
  if open? && unmerged_commits.any?
    self.st_commits = unmerged_commits
    save
  end
  commits
end
reloaded_diffs()
# File app/models/merge_request.rb, line 101
def reloaded_diffs
  if open? && unmerged_diffs.any?
    self.st_diffs = unmerged_diffs
    self.save
  end

rescue Grit::Git::GitTimeout
  self.st_diffs = [BROKEN_DIFF]
  self.save
end
to_diff()

Returns the raw diff for this merge request

see “git diff”

# File app/models/merge_request.rb, line 213
def to_diff
  project.repo.git.native(:diff, {timeout: 30, raise: true}, "#{target_branch}...#{source_branch}")
end
to_patch()

Returns the commit as a series of email patches.

see “git format-patch”

# File app/models/merge_request.rb, line 220
def to_patch
  project.repo.git.format_patch({timeout: 30, raise: true, stdout: true}, "#{target_branch}..#{source_branch}")
end
unchecked?()
# File app/models/merge_request.rb, line 75
def unchecked?
  state == UNCHECKED
end
unmerged_commits()
# File app/models/merge_request.rb, line 176
def unmerged_commits
  self.project.repo.
    commits_between(self.target_branch, self.source_branch).
    map {|c| Commit.new(c)}.
    sort_by(&:created_at).
    reverse
end
unmerged_diffs()
# File app/models/merge_request.rb, line 120
def unmerged_diffs
  # Only show what is new in the source branch compared to the target branch, not the other way around.
  # The linex below with merge_base is equivalent to diff with three dots (git diff branch1...branch2)
  # From the git documentation: "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B"
  common_commit = project.repo.git.native(:merge_base, {}, [target_branch, source_branch]).strip
  diffs = project.repo.diff(common_commit, source_branch)
end
valid_diffs?()
# File app/models/merge_request.rb, line 116
def valid_diffs?
  !broken_diffs?
end
validate_branches()
# File app/models/merge_request.rb, line 64
def validate_branches
  if target_branch == source_branch
    errors.add :base, "You can not use same branch for source and target branches"
  end
end