Skip to content

Commit 3a5d375

Browse files
committed
Fix Diff::Position#diff_file for positions on straight diffs
1 parent 4ab1e07 commit 3a5d375

File tree

8 files changed

+128
-23
lines changed

8 files changed

+128
-23
lines changed

app/services/compare_service.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ def execute(target_project, target_branch, straight: false)
1717
start_branch_name) do |commit|
1818
break unless commit
1919

20-
compare(commit.sha, target_project, target_branch, straight)
20+
compare(commit.sha, target_project, target_branch, straight: straight)
2121
end
2222
end
2323

2424
private
2525

26-
def compare(source_sha, target_project, target_branch, straight)
26+
def compare(source_sha, target_project, target_branch, straight:)
2727
raw_compare = Gitlab::Git::Compare.new(
2828
target_project.repository.raw_repository,
2929
target_branch,
3030
source_sha,
31-
straight
31+
straight: straight
3232
)
3333

3434
Compare.new(raw_compare, target_project, straight: straight)

lib/gitlab/diff/diff_refs.rb

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def hash
3737
def complete?
3838
start_sha && head_sha
3939
end
40+
41+
def compare_in(project)
42+
# We're at the initial commit, so just get that as we can't compare to anything.
43+
if Gitlab::Git.blank_ref?(start_sha)
44+
project.commit(head_sha)
45+
else
46+
straight = start_sha == base_sha
47+
CompareService.new(project, head_sha).execute(project, start_sha, straight: straight)
48+
end
49+
end
4050
end
4151
end
4252
end

lib/gitlab/diff/position.rb

+2-16
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,9 @@ def line_code(repository)
145145
private
146146

147147
def find_diff_file(repository)
148-
# We're at the initial commit, so just get that as we can't compare to anything.
149-
compare =
150-
if Gitlab::Git.blank_ref?(start_sha)
151-
Gitlab::Git::Commit.find(repository.raw_repository, head_sha)
152-
else
153-
Gitlab::Git::Compare.new(
154-
repository.raw_repository,
155-
start_sha,
156-
head_sha
157-
)
158-
end
159-
160-
diff = compare.diffs(paths: paths).first
161-
162-
return unless diff
148+
return unless diff_refs.complete?
163149

164-
Gitlab::Diff::File.new(diff, repository: repository, diff_refs: diff_refs)
150+
diff_refs.compare_in(repository.project).diffs(paths: paths, expanded: true).diff_files.first
165151
end
166152
end
167153
end

lib/gitlab/diff/position_tracer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def cd_diffs
216216

217217
def compare(start_sha, head_sha, straight: false)
218218
compare = CompareService.new(project, head_sha).execute(project, start_sha, straight: straight)
219-
compare.diffs(paths: paths)
219+
compare.diffs(paths: paths, expanded: true)
220220
end
221221

222222
def position(diff_file, old_line, new_line)

lib/gitlab/git/compare.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Git
33
class Compare
44
attr_reader :head, :base, :straight
55

6-
def initialize(repository, base, head, straight = false)
6+
def initialize(repository, base, head, straight: false)
77
@repository = repository
88
@straight = straight
99

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'spec_helper'
2+
3+
describe Gitlab::Diff::DiffRefs, lib: true do
4+
let(:project) { create(:project, :repository) }
5+
6+
describe '#compare_in' do
7+
context 'with diff refs for the initial commit' do
8+
let(:commit) { project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863') }
9+
subject { commit.diff_refs }
10+
11+
it 'returns an appropriate comparison' do
12+
compare = subject.compare_in(project)
13+
14+
expect(compare.diff_refs).to eq(subject)
15+
end
16+
end
17+
18+
context 'with diff refs for a commit' do
19+
let(:commit) { project.commit('6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9') }
20+
subject { commit.diff_refs }
21+
22+
it 'returns an appropriate comparison' do
23+
compare = subject.compare_in(project)
24+
25+
expect(compare.diff_refs).to eq(subject)
26+
end
27+
end
28+
29+
context 'with diff refs for a comparison through the base' do
30+
subject do
31+
described_class.new(
32+
start_sha: '0b4bc9a49b562e85de7cc9e834518ea6828729b9', # feature
33+
base_sha: 'ae73cb07c9eeaf35924a10f713b364d32b2dd34f',
34+
head_sha: 'e63f41fe459e62e1228fcef60d7189127aeba95a' # master
35+
)
36+
end
37+
38+
it 'returns an appropriate comparison' do
39+
compare = subject.compare_in(project)
40+
41+
expect(compare.diff_refs).to eq(subject)
42+
end
43+
end
44+
45+
context 'with diff refs for a straight comparison' do
46+
subject do
47+
described_class.new(
48+
start_sha: '0b4bc9a49b562e85de7cc9e834518ea6828729b9', # feature
49+
base_sha: '0b4bc9a49b562e85de7cc9e834518ea6828729b9',
50+
head_sha: 'e63f41fe459e62e1228fcef60d7189127aeba95a' # master
51+
)
52+
end
53+
54+
it 'returns an appropriate comparison' do
55+
compare = subject.compare_in(project)
56+
57+
expect(compare.diff_refs).to eq(subject)
58+
end
59+
end
60+
end
61+
end

spec/lib/gitlab/diff/position_spec.rb

+48
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,54 @@
381381
end
382382
end
383383

384+
describe "position for a file in a straight comparison" do
385+
let(:diff_refs) do
386+
Gitlab::Diff::DiffRefs.new(
387+
start_sha: '0b4bc9a49b562e85de7cc9e834518ea6828729b9', # feature
388+
base_sha: '0b4bc9a49b562e85de7cc9e834518ea6828729b9',
389+
head_sha: 'e63f41fe459e62e1228fcef60d7189127aeba95a' # master
390+
)
391+
end
392+
393+
subject do
394+
described_class.new(
395+
old_path: "files/ruby/feature.rb",
396+
new_path: "files/ruby/feature.rb",
397+
old_line: 3,
398+
new_line: nil,
399+
diff_refs: diff_refs
400+
)
401+
end
402+
403+
describe "#diff_file" do
404+
it "returns the correct diff file" do
405+
diff_file = subject.diff_file(project.repository)
406+
407+
expect(diff_file.deleted_file?).to be true
408+
expect(diff_file.old_path).to eq(subject.old_path)
409+
expect(diff_file.diff_refs).to eq(subject.diff_refs)
410+
end
411+
end
412+
413+
describe "#diff_line" do
414+
it "returns the correct diff line" do
415+
diff_line = subject.diff_line(project.repository)
416+
417+
expect(diff_line.removed?).to be true
418+
expect(diff_line.old_line).to eq(subject.old_line)
419+
expect(diff_line.text).to eq("- puts 'bar'")
420+
end
421+
end
422+
423+
describe "#line_code" do
424+
it "returns the correct line code" do
425+
line_code = Gitlab::Diff::LineCode.generate(subject.file_path, 0, subject.old_line)
426+
427+
expect(subject.line_code(project.repository)).to eq(line_code)
428+
end
429+
end
430+
end
431+
384432
describe "#to_json" do
385433
let(:hash) do
386434
{

spec/lib/gitlab/git/compare_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
describe Gitlab::Git::Compare, seed_helper: true do
44
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) }
5-
let(:compare) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, false) }
6-
let(:compare_straight) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, true) }
5+
let(:compare) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, straight: false) }
6+
let(:compare_straight) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, straight: true) }
77

88
describe '#commits' do
99
subject do

0 commit comments

Comments
 (0)