Class: Gitlab::BranchPushMergeCommitAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/branch_push_merge_commit_analyzer.rb

Overview

Analyse a graph of commits from a push to a branch, for each commit, analyze that if it is the head of a merge request, then what should its merge_commit be, relative to the branch.

A—–>B—–>C—–>D target branch | ^ | | –>E—–>F– merged branch

|     ^
|     |
+->G--+

(See merge-commit-analyze-after branch in gitlab-test)

Assuming

  • A is already in remote

  • B~D are all in its own branch with its own merge request, targeting the target branch

When D is finally pushed to the target branch, what are the merge commits for all the other merge requests?

We can walk backwards from the HEAD commit D, and find status of its parents. First we determine if commit belongs to the target branch (i.e. A, B, C, D), and then determine its merge commit.

--------—————–-------------- | Commit | Direct ancestor | Merge commit | --------—————–-------------- | D | Y | D | --------—————–-------------- | C | Y | C | --------—————–-------------- | F | | C | --------—————–-------------- | B | Y | B | --------—————–-------------- | E | | C | --------—————–-------------- | G | | C | --------—————–--------------

By examining the result, it can be said that

  • If commit is direct ancestor of HEAD, its merge commit is itself.

  • Otherwise, the merge commit is the same as its child’s merge commit.

Defined Under Namespace

Classes: CommitDecorator

Instance Method Summary collapse

Constructor Details

#initialize(commits, relevant_commit_ids: nil) ⇒ BranchPushMergeCommitAnalyzer

Returns a new instance of BranchPushMergeCommitAnalyzer.

Parameters:

  • commits (Array)

    list of commits, must be ordered from the child (tip) of the graph back to the ancestors



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

def initialize(commits, relevant_commit_ids: nil)
  @commits = commits
  @id_to_commit = {}
  @commits.each do |commit|
    @id_to_commit[commit.id] = CommitDecorator.new(commit)

    if relevant_commit_ids
      relevant_commit_ids.delete(commit.id)
      break if relevant_commit_ids.empty? # Only limit the analyze up to relevant_commit_ids
    end
  end

  analyze
end

Instance Method Details

#get_merge_commit(id) ⇒ Object



83
84
85
# File 'lib/gitlab/branch_push_merge_commit_analyzer.rb', line 83

def get_merge_commit(id)
  get_commit(id).merge_commit.id
end