diff options
author | Douwe Maan <douwe@selenight.nl> | 2016-08-03 15:32:00 -0700 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2016-08-03 15:32:00 -0700 |
commit | 538e66d71c0f7125cc62ea51480668ba8b342544 (patch) | |
tree | d27f667dbe404ce406e2ac6b3a901b87ff93da47 /app/models/compare.rb | |
parent | 3ccb27c0c79ef92585a901de32339948319cf068 (diff) | |
parent | 8890376f0f72f713a7530bd7989e71442c69dc91 (diff) | |
download | gitlab-ce-538e66d71c0f7125cc62ea51480668ba8b342544.tar.gz |
Merge branch 'master' into diff-line-comment-vuejs
# Conflicts:
# app/models/discussion.rb
# db/schema.rb
Diffstat (limited to 'app/models/compare.rb')
-rw-r--r-- | app/models/compare.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/app/models/compare.rb b/app/models/compare.rb new file mode 100644 index 00000000000..4856510f526 --- /dev/null +++ b/app/models/compare.rb @@ -0,0 +1,66 @@ +class Compare + delegate :same, :head, :base, to: :@compare + + attr_reader :project + + def self.decorate(compare, project) + if compare.is_a?(Compare) + compare + else + self.new(compare, project) + end + end + + def initialize(compare, project) + @compare = compare + @project = project + end + + def commits + @commits ||= Commit.decorate(@compare.commits, project) + end + + def start_commit + return @start_commit if defined?(@start_commit) + + commit = @compare.base + @start_commit = commit ? ::Commit.new(commit, project) : nil + end + + def head_commit + return @head_commit if defined?(@head_commit) + + commit = @compare.head + @head_commit = commit ? ::Commit.new(commit, project) : nil + end + alias_method :commit, :head_commit + + def base_commit + return @base_commit if defined?(@base_commit) + + @base_commit = if start_commit && head_commit + project.merge_base_commit(start_commit.id, head_commit.id) + else + nil + end + end + + def raw_diffs(*args) + @compare.diffs(*args) + end + + def diffs(diff_options = nil) + Gitlab::Diff::FileCollection::Compare.new(self, + project: project, + diff_options: diff_options, + diff_refs: diff_refs) + end + + def diff_refs + Gitlab::Diff::DiffRefs.new( + base_sha: base_commit.try(:sha), + start_sha: start_commit.try(:sha), + head_sha: commit.try(:sha) + ) + end +end |