Skip to content

Commit 52f5e9c

Browse files
Merge pull request opf#6994 from opf/fix/diff
Diff fix [ci skip]
2 parents 561400b + 3c29f90 commit 52f5e9c

12 files changed

Lines changed: 93 additions & 68 deletions

File tree

app/views/repositories/_revisions.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ See docs/COPYRIGHT.rdoc for more details.
2727
2828
++#%>
2929

30-
<%= form_tag({controller: '/repositories', action: 'diff', project_id: @project, path: to_path_param(path)}, method: :get) do %>
30+
<%= form_tag({controller: '/repositories', action: 'diff', project_id: @project, repo_path: to_path_param(path)}, method: :get) do %>
3131
<div class="generic-table--container">
3232
<div class="generic-table--results-container">
3333
<table class="generic-table changesets">

app/views/repositories/diff.html.erb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ See docs/COPYRIGHT.rdoc for more details.
2929

3030
<%= toolbar title: "#{l(:label_revision)} #{@diff_format_revisions} #{@path}" do %>
3131
<li class="toolbar-item">
32-
<%= form_tag({path: to_path_param(@path)}, method: :get) do %>
32+
<%= form_tag({repo_path: to_path_param(@path)}, method: :get) do %>
3333
<%= hidden_field_tag('rev', params[:rev]) if params[:rev] %>
3434
<%= hidden_field_tag('rev_to', params[:rev_to]) if params[:rev_to] %>
3535
<%= styled_select_tag 'type', options_for_select([[l(:label_diff_inline), "inline"], [l(:label_diff_side_by_side), "sbs"]], @diff_type), id: "repository-diff-type-select" %>
@@ -48,7 +48,14 @@ See docs/COPYRIGHT.rdoc for more details.
4848
<%= render partial: 'common/diff', locals: { diff: @diff, diff_type: @diff_type } %>
4949
<% end -%>
5050
<%= other_formats_links do |f| %>
51-
<%= f.link_to 'Diff', url: permitted_params.repository_diff.to_h, caption: 'Unified diff' %>
51+
<% unidiff_link = f.link_to 'Diff', url: permitted_params.repository_diff.to_h, caption: 'Unified diff' %>
52+
<% if !@path.blank? %>
53+
<% unidiff_link.gsub!('?', '&amp;') %>
54+
<% end %>
55+
<% wrong_url = CGI::escapeHTML(URI.encode(with_leading_slash(@path))).concat('.diff') %>
56+
<% good_url = '.diff'.concat('?repo_path=', URI.encode(without_leading_slash(@path)).gsub('&', '%26')) %>
57+
<% unidiff_link.gsub!(wrong_url, good_url) %>
58+
<%= unidiff_link.html_safe %>
5259
<% end %>
5360

5461
<% html_title(h(with_leading_slash(@path)), 'Diff') -%>

config/initializers/scm.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@
304304
get '(/revisions/:rev)/diff.:format', action: :diff
305305
get '(/revisions/:rev)/diff(/*repo_path)',
306306
action: :diff,
307-
format: false
307+
format: 'html',
308+
constraints: { rev: /[\w0-9\.\-_]+/, repo_path: /.*/ }
308309

309310
get '(/revisions/:rev)/:format/*repo_path',
310311
action: :entry,

lib/open_project/scm/adapters/git.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def diff(path, identifier_from, identifier_to = nil)
408408
args << 'show' << '--no-color' << identifier_from
409409
end
410410
args << '--' << scm_encode(@path_encoding, 'UTF-8', path) unless path.empty?
411-
capture_git(args).lines.map(&:chomp)
411+
capture_git(args).lines
412412
rescue Exceptions::CommandFailed
413413
nil
414414
end

lib/open_project/scm/adapters/subversion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def diff(path, identifier_from, identifier_to = nil, _type = 'inline')
185185

186186
cmd = ['diff', '-r', "#{identifier_to}:#{identifier_from}",
187187
"#{target(path)}@#{identifier_from}"]
188-
capture_svn(cmd).lines.map(&:chomp)
188+
capture_svn(cmd).lines
189189
end
190190

191191
def numeric_identifier(identifier, default = '')

lib/open_project/scm/manager.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ module Scm
3333
class Manager
3434
class << self
3535
def registered
36-
@scms ||= {}
36+
@scms ||= {
37+
subversion: ::Repository::Subversion,
38+
git: ::Repository::Git
39+
}
3740
end
3841

3942
##

lib/redmine/unified_diff.rb

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def offsets(line_left, line_right)
197197
starting += 1
198198
end
199199
ending = -1
200-
while ending >= -(max - starting) && line_left[ending] == line_right[ending]
200+
while ending >= -(max - starting) && (line_left[ending] == line_right[ending])
201201
ending -= 1
202202
end
203203
unless starting == 0 && ending == -1
@@ -209,6 +209,8 @@ def offsets(line_left, line_right)
209209

210210
# A line of diff
211211
class Diff
212+
include ActionView::Helpers::TagHelper
213+
212214
attr_accessor :nb_line_left
213215
attr_accessor :line_left
214216
attr_accessor :nb_line_right
@@ -235,27 +237,15 @@ def line
235237
end
236238

237239
def html_line_left
238-
if offsets
239-
line_left.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>').html_safe
240-
else
241-
line_left
242-
end
240+
line_to_html(line_left, offsets)
243241
end
244242

245243
def html_line_right
246-
if offsets
247-
line_right.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>').html_safe
248-
else
249-
line_right
250-
end
244+
line_to_html(line_right, offsets)
251245
end
252246

253247
def html_line
254-
if offsets
255-
line.dup.insert(offsets.first, '<span>').insert(offsets.last, '</span>').html_safe
256-
else
257-
line
258-
end
248+
line_to_html(line, offsets)
259249
end
260250

261251
def inspect
@@ -265,5 +255,29 @@ def inspect
265255
puts nb_line_right
266256
puts line_right
267257
end
258+
259+
private
260+
261+
def line_to_html(line, offsets)
262+
line_to_html_raw(line, offsets).tap do |html_str|
263+
html_str.force_encoding('UTF-8')
264+
end
265+
end
266+
267+
def line_to_html_raw(line, offsets)
268+
return line unless offsets
269+
270+
ActiveSupport::SafeBuffer.new.tap do |output|
271+
if offsets.first != 0
272+
output << line[0..offsets.first-1]
273+
end
274+
275+
output << content_tag(:span, line[offsets.first..offsets.last])
276+
277+
unless offsets.last == -1
278+
output << line[offsets.last+1..-1]
279+
end
280+
end.to_s
281+
end
268282
end
269283
end

spec/lib/open_project/scm/adapters/git_adapter_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@
490490

491491
describe '.diff' do
492492
it 'provides a full diff of the last commit by default' do
493-
diff = adapter.diff('', 'HEAD')
493+
diff = adapter.diff('', 'HEAD').map(&:chomp)
494494
expect(diff[0]).to eq('commit 71e5c1d3dca6304805b143b9d0e6695fb3895ea4')
495495

496496
bare = "Author: Oliver G\xFCnther <mail@oliverguenther.de>"
@@ -503,18 +503,18 @@
503503
end
504504

505505
it 'provides a negative diff' do
506-
diff = adapter.diff('', 'HEAD~2', 'HEAD~1')
506+
diff = adapter.diff('', 'HEAD~2', 'HEAD~1').map(&:chomp)
507507
expect(diff.join("\n")).to include('-And this is a file')
508508
end
509509

510510
it 'provides the complete for the given range' do
511-
diff = adapter.diff('', '61b685f', '2f9c009')
511+
diff = adapter.diff('', '61b685f', '2f9c009').map(&:chomp)
512512
expect(diff[1]).to eq('index 6cbd30c..b94e68e 100644')
513513
expect(diff[10]).to eq('index 4eca635..9a541fe 100644')
514514
end
515515

516516
it 'provides the selected diff for the given range' do
517-
diff = adapter.diff('README', '61b685f', '2f9c009')
517+
diff = adapter.diff('README', '61b685f', '2f9c009').map(&:chomp)
518518
expect(diff).to eq(<<-DIFF.strip_heredoc.split("\n"))
519519
diff --git a/README b/README
520520
index 6cbd30c..b94e68e 100644

spec/lib/open_project/scm/adapters/subversion_adapter_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,23 +369,23 @@
369369

370370
describe '.diff' do
371371
it 'provides a full diff against the last revision' do
372-
diff = adapter.diff('', 12)
372+
diff = adapter.diff('', 12).map(&:chomp)
373373
expect(diff.join("\n")).to include('Added: svn:ignore')
374374
end
375375

376376
it 'provides a negative diff' do
377-
diff = adapter.diff('', 11, 12)
377+
diff = adapter.diff('', 11, 12).map(&:chomp)
378378
expect(diff.join("\n")).to include('Deleted: svn:ignore')
379379
end
380380

381381
it 'provides the complete for the given range' do
382-
diff = adapter.diff('', 8, 6).join("\n")
382+
diff = adapter.diff('', 8, 6).map(&:chomp).join("\n")
383383
expect(diff).to include('Index: subversion_test/folder/greeter.rb')
384384
expect(diff).to include('Index: subversion_test/helloworld.c')
385385
end
386386

387387
it 'provides the selected diff for the given range' do
388-
diff = adapter.diff('subversion_test/helloworld.c', 8, 6)
388+
diff = adapter.diff('subversion_test/helloworld.c', 8, 6).map(&:chomp)
389389
expect(diff).to eq(<<-DIFF.strip_heredoc.split("\n"))
390390
Index: helloworld.c
391391
===================================================================

0 commit comments

Comments
 (0)