Skip to content

Commit a9fa45f

Browse files
committed
Represent DiffRefs as proper class instead of tuple array
1 parent 6ce25e7 commit a9fa45f

20 files changed

+111
-57
lines changed

app/controllers/projects/blob_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def preview
5757
diffy = Diffy::Diff.new(@blob.data, @content, diff: '-U 3', include_diff_info: true)
5858
diff_lines = diffy.diff.scan(/.*\n/)[2..-1]
5959
diff_lines = Gitlab::Diff::Parser.new.parse(diff_lines)
60-
@diff_lines = Gitlab::Diff::Highlight.new(diff_lines).highlight
60+
@diff_lines = Gitlab::Diff::Highlight.new(diff_lines, repository: @repository).highlight
6161

6262
render layout: false
6363
end

app/controllers/projects/commit_controller.rb

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ def define_show_vars
121121
opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
122122

123123
@diffs = commit.diffs(opts)
124-
@diff_refs = [commit.parent || commit, commit]
125124
@notes_count = commit.notes.count
126125

127126
@statuses = CommitStatus.where(pipeline: pipelines)

app/controllers/projects/compare_controller.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ def index
1414

1515
def show
1616
compare = CompareService.new.
17-
execute(@project, @head_ref, @project, @base_ref, diff_options)
17+
execute(@project, @head_ref, @project, @start_ref, diff_options)
1818

1919
if compare
2020
@commits = Commit.decorate(compare.commits, @project)
21+
22+
@start_commit = @project.commit(@start_ref)
2123
@commit = @project.commit(@head_ref)
22-
@base_commit = @project.merge_base_commit(@base_ref, @head_ref)
24+
@base_commit = @project.merge_base_commit(@start_ref, @head_ref)
25+
2326
@diffs = compare.diffs(diff_options)
24-
@diff_refs = [@base_commit, @commit]
27+
@diff_refs = Gitlab::Diff::DiffRefs.new(
28+
base_sha: @base_commit.try(:sha),
29+
start_sha: @start_commit.try(:sha),
30+
head_sha: @commit.try(:sha)
31+
)
32+
2533
@diff_notes_disabled = true
2634
@grouped_diff_notes = {}
2735
end
@@ -35,12 +43,12 @@ def create
3543
private
3644

3745
def assign_ref_vars
38-
@base_ref = Addressable::URI.unescape(params[:from])
46+
@start_ref = Addressable::URI.unescape(params[:from])
3947
@ref = @head_ref = Addressable::URI.unescape(params[:to])
4048
end
4149

4250
def merge_request
4351
@merge_request ||= @project.merge_requests.opened.
44-
find_by(source_project: @project, source_branch: @head_ref, target_branch: @base_ref)
52+
find_by(source_project: @project, source_branch: @head_ref, target_branch: @start_ref)
4553
end
4654
end

app/helpers/diff_helper.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def diff_options
3030
options
3131
end
3232

33-
def safe_diff_files(diffs, diff_refs)
34-
diffs.decorate! { |diff| Gitlab::Diff::File.new(diff, diff_refs) }
33+
def safe_diff_files(diffs, diff_refs: nil, repository: nil)
34+
diffs.decorate! { |diff| Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
3535
end
3636

3737
def generate_line_code(file_path, line)

app/models/commit.rb

+7
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ def short_id
214214
@raw.short_id(7)
215215
end
216216

217+
def diff_refs
218+
Gitlab::Diff::DiffRefs.new(
219+
base_sha: self.parent_id || self.sha,
220+
head_sha: self.sha
221+
)
222+
end
223+
217224
def pipelines
218225
@pipeline ||= project.pipelines.where(sha: sha)
219226
end

app/models/merge_request.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ def source_branch_sha
249249
source_branch_head.try(:sha)
250250
end
251251

252+
def diff_refs
253+
return nil unless diff_start_commit || diff_base_commit
254+
255+
Gitlab::Diff::DiffRefs.new(
256+
base_sha: diff_base_sha,
257+
start_sha: diff_start_sha,
258+
head_sha: diff_head_sha
259+
)
260+
end
261+
252262
def validate_branches
253263
if target_project == source_project && target_branch == source_branch
254264
errors.add :branch_conflict, "You can not use same project/branch for source and target"
@@ -622,12 +632,6 @@ def diverged_from_target_branch?
622632
end
623633

624634
def pipeline
625-
end
626-
627-
def diff_refs
628-
return nil unless diff_base_commit
629-
630-
[diff_base_commit, last_commit]
631635
@pipeline ||= source_project.pipeline(diff_head_sha, source_branch) if diff_head_sha && source_project
632636
end
633637

app/views/projects/commit/show.html.haml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
= render "ci_menu"
88
- else
99
%div.block-connector
10-
= render "projects/diffs/diffs", diffs: @diffs, project: @project,
11-
diff_refs: @diff_refs
10+
= render "projects/diffs/diffs", diffs: @diffs, project: @project, diff_refs: @commit.diff_refs
1211
= render "projects/notes/notes_with_form"
1312
- if can_collaborate_with_project?
1413
- %w(revert cherry-pick).each do |type|

app/views/projects/diffs/_diffs.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
- if diff_view == 'parallel'
33
- fluid_layout true
44

5-
- diff_files = safe_diff_files(diffs, diff_refs)
5+
- diff_files = safe_diff_files(diffs, diff_refs: diff_refs, repository: project.repository)
66

77
.content-block.oneline-block.files-changed
88
.inline-parallel-buttons

app/views/projects/diffs/_image.html.haml

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
- diff = diff_file.diff
2-
- file_raw_path = namespace_project_raw_path(@project.namespace, @project, tree_join(@commit.id, diff.new_path))
2+
- file_raw_path = namespace_project_raw_path(@project.namespace, @project, tree_join(diff_file.new_ref, diff.new_path))
33
// diff_refs will be nil for orphaned commits (e.g. first commit in repo)
4-
- if diff_refs
5-
- old_commit_id = diff_refs.first.id
6-
- old_file_raw_path = namespace_project_raw_path(@project.namespace, @project, tree_join(old_commit_id, diff.old_path))
4+
- if diff_file.old_ref
5+
- old_file_raw_path = namespace_project_raw_path(@project.namespace, @project, tree_join(diff_file.old_ref, diff.old_path))
76

87
- if diff.renamed_file || diff.new_file || diff.deleted_file
98
.image
@@ -16,7 +15,7 @@
1615
%div.two-up.view
1716
%span.wrap
1817
.frame.deleted
19-
%a{href: namespace_project_blob_path(@project.namespace, @project, tree_join(old_commit_id, diff.old_path))}
18+
%a{href: namespace_project_blob_path(@project.namespace, @project, tree_join(diff_file.old_ref, diff.old_path))}
2019
%img{src: old_file_raw_path}
2120
%p.image-info.hide
2221
%span.meta-filesize= "#{number_to_human_size old_file.size}"
@@ -28,7 +27,7 @@
2827
%span.meta-height
2928
%span.wrap
3029
.frame.added
31-
%a{href: namespace_project_blob_path(@project.namespace, @project, tree_join(@commit.id, diff.new_path))}
30+
%a{href: namespace_project_blob_path(@project.namespace, @project, tree_join(diff_file.new_ref, diff.new_path))}
3231
%img{src: file_raw_path}
3332
%p.image-info.hide
3433
%span.meta-filesize= "#{number_to_human_size file.size}"

app/workers/emails_on_push_worker.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,30 @@ def perform(project_id, recipients, push_data, options = {})
2828
:push
2929
end
3030

31+
merge_base_sha = project.merge_base_commit(before_sha, after_sha).try(:sha)
32+
3133
diff_refs = nil
3234
compare = nil
3335
reverse_compare = false
3436
if action == :push
3537
compare = Gitlab::Git::Compare.new(project.repository.raw_repository, before_sha, after_sha)
36-
diff_refs = [project.merge_base_commit(before_sha, after_sha), project.commit(after_sha)]
38+
39+
diff_refs = Gitlab::Diff::DiffRefs.new(
40+
base_sha: merge_base_sha,
41+
start_sha: before_sha,
42+
head_sha: after_sha
43+
)
3744

3845
return false if compare.same
3946

4047
if compare.commits.empty?
4148
compare = Gitlab::Git::Compare.new(project.repository.raw_repository, after_sha, before_sha)
42-
diff_refs = [project.merge_base_commit(after_sha, before_sha), project.commit(before_sha)]
49+
50+
diff_refs = Gitlab::Diff::DiffRefs.new(
51+
base_sha: merge_base_sha,
52+
start_sha: after_sha,
53+
head_sha: before_sha
54+
)
4355

4456
reverse_compare = true
4557

lib/gitlab/diff/diff_refs.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Gitlab
2+
module Diff
3+
class DiffRefs
4+
attr_reader :base_sha
5+
attr_reader :start_sha
6+
attr_reader :head_sha
7+
8+
def initialize(base_sha:, start_sha: base_sha, head_sha:)
9+
@base_sha = base_sha
10+
@start_sha = start_sha
11+
@head_sha = head_sha
12+
end
13+
14+
def ==(other)
15+
other.is_a?(self.class) &&
16+
base_sha == other.base_sha &&
17+
start_sha == other.start_sha &&
18+
head_sha == other.head_sha
19+
end
20+
21+
def complete?
22+
start_sha && head_sha
23+
end
24+
end
25+
end
26+
end

lib/gitlab/diff/file.rb

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
module Gitlab
22
module Diff
33
class File
4-
attr_reader :diff, :diff_refs
4+
attr_reader :diff, :repository, :diff_refs
55

66
delegate :new_file, :deleted_file, :renamed_file,
77
:old_path, :new_path, to: :diff, prefix: false
88

9-
def initialize(diff, diff_refs)
9+
def initialize(diff, repository:, diff_refs: nil)
1010
@diff = diff
11+
@repository = repository
1112
@diff_refs = diff_refs
1213
end
1314

1415
def old_ref
15-
diff_refs[0] if diff_refs
16+
diff_refs.try(:base_sha)
1617
end
1718

1819
def new_ref
19-
diff_refs[1] if diff_refs
20+
diff_refs.try(:head_sha)
2021
end
2122

22-
# Array of Gitlab::DIff::Line objects
23+
# Array of Gitlab::Diff::Line objects
2324
def diff_lines
24-
@lines ||= parser.parse(raw_diff.each_line).to_a
25-
end
26-
27-
def too_large?
28-
diff.too_large?
25+
@lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
2926
end
3027

3128
def highlighted_diff_lines
32-
Gitlab::Diff::Highlight.new(self).highlight
29+
@highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
3330
end
3431

3532
def parallel_diff_lines
36-
Gitlab::Diff::ParallelDiff.new(self).parallelize
33+
@parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
3734
end
3835

3936
def mode_changed?

lib/gitlab/diff/highlight.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Gitlab
22
module Diff
33
class Highlight
4-
attr_reader :diff_file, :diff_lines, :raw_lines
4+
attr_reader :diff_file, :diff_lines, :raw_lines, :repository
55

66
delegate :old_path, :new_path, :old_ref, :new_ref, to: :diff_file, prefix: :diff
77

8-
def initialize(diff_lines)
8+
def initialize(diff_lines, repository: nil)
9+
@repository = repository
10+
911
if diff_lines.is_a?(Gitlab::Diff::File)
1012
@diff_file = diff_lines
1113
@diff_lines = @diff_file.diff_lines
@@ -19,7 +21,7 @@ def highlight
1921
@diff_lines.map.with_index do |diff_line, i|
2022
diff_line = diff_line.dup
2123
# ignore highlighting for "match" lines
22-
next diff_line if diff_line.type == 'match' || diff_line.type == 'nonewline'
24+
next diff_line if diff_line.meta?
2325

2426
rich_line = highlight_line(diff_line) || diff_line.text
2527

@@ -70,7 +72,7 @@ def processing_args(version)
7072
ref = send("diff_#{version}_ref")
7173
path = send("diff_#{version}_path")
7274

73-
[ref.project.repository, ref.id, path]
75+
[self.repository, ref, path]
7476
end
7577
end
7678
end

lib/gitlab/email/message/repository_push.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ def author
3333
end
3434

3535
def commits
36-
@commits ||= (Commit.decorate(compare.commits, project) if compare)
36+
return unless compare
37+
38+
@commits ||= Commit.decorate(compare.commits, project)
3739
end
3840

3941
def diffs
40-
@diffs ||= (safe_diff_files(compare.diffs(max_files: 30), diff_refs) if compare)
42+
return unless compare
43+
44+
@diffs ||= safe_diff_files(compare.diffs(max_files: 30), diff_refs: diff_refs, repository: project.repository)
4145
end
4246

4347
def diffs_count

lib/gitlab/workhorse.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ def send_git_archive(repository, ref:, format:)
3838
end
3939

4040
def send_git_diff(repository, diff_refs)
41-
from, to = diff_refs
42-
4341
params = {
4442
'RepoPath' => repository.path_to_repo,
45-
'ShaFrom' => from.sha,
46-
'ShaTo' => to.sha
43+
'ShaFrom' => diff_refs.start_sha,
44+
'ShaTo' => diff_refs.head_sha
4745
}
4846

4947
[

spec/helpers/diff_helper_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
let(:diffs) { commit.diffs }
1010
let(:diff) { diffs.first }
1111
let(:diff_refs) { [commit.parent, commit] }
12-
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs) }
12+
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
1313

1414
describe 'diff_view' do
1515
it 'returns a valid value when cookie is set' do

spec/lib/gitlab/diff/file_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
let(:project) { create(:project) }
77
let(:commit) { project.commit(sample_commit.id) }
88
let(:diff) { commit.diffs.first }
9-
let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent, commit]) }
9+
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) }
1010

1111
describe :diff_lines do
1212
let(:diff_lines) { diff_file.diff_lines }

spec/lib/gitlab/diff/highlight_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
let(:project) { create(:project) }
77
let(:commit) { project.commit(sample_commit.id) }
88
let(:diff) { commit.diffs.first }
9-
let(:diff_file) { Gitlab::Diff::File.new(diff, [commit.parent, commit]) }
9+
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) }
1010

1111
describe '#highlight' do
1212
context "with a diff file" do
13-
let(:subject) { Gitlab::Diff::Highlight.new(diff_file).highlight }
13+
let(:subject) { Gitlab::Diff::Highlight.new(diff_file, repository: project.repository).highlight }
1414

1515
it 'should return Gitlab::Diff::Line elements' do
1616
expect(subject.first).to be_an_instance_of(Gitlab::Diff::Line)
@@ -41,7 +41,7 @@
4141
end
4242

4343
context "with diff lines" do
44-
let(:subject) { Gitlab::Diff::Highlight.new(diff_file.diff_lines).highlight }
44+
let(:subject) { Gitlab::Diff::Highlight.new(diff_file.diff_lines, repository: project.repository).highlight }
4545

4646
it 'should return Gitlab::Diff::Line elements' do
4747
expect(subject.first).to be_an_instance_of(Gitlab::Diff::Line)

spec/lib/gitlab/diff/parallel_diff_spec.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
let(:commit) { project.commit(sample_commit.id) }
99
let(:diffs) { commit.diffs }
1010
let(:diff) { diffs.first }
11-
let(:diff_refs) { [commit.parent, commit] }
12-
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs) }
11+
let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: repository) }
1312
subject { described_class.new(diff_file) }
1413

1514
let(:parallel_diff_result_array) { YAML.load_file("#{Rails.root}/spec/fixtures/parallel_diff_result.yml") }

0 commit comments

Comments
 (0)