Commit b8786588f608be72f41e7ba5739d91d4f4319b50

sidebyside diffing of commits

Commit diff

app/controllers/browse_controller.rb

 
2525 end
2626
2727 def commit
28 @diffmode = params[:diffmode] == "sidebyside" ? "sidebyside" : "inline"
2829 @git = Git.bare(@repository.full_repository_path)
2930 @commit = @git.gcommit(params[:sha])
3031 if @commit.parent
toggle raw diff

app/helpers/browse_helper.rb

 
3737 end
3838
3939 # Takes a unified diff as input and renders it as html
40 def render_diff(udiff, src_sha, dst_sha)
40 def render_diff(udiff, src_sha, dst_sha, display_mode = "inline")
4141 return if udiff.blank?
42
42
43 case display_mode
44 when "sidebyside"
45 render_sidebyside_diff(udiff, src_sha, dst_sha)
46 else
47 render_inline_diff(udiff, src_sha, dst_sha)
48 end
49 end
50
51 def render_inline_diff(udiff, src_sha, dst_sha)
4352 callback = Gitorious::Diff::InlineTableCallback.new
44
45 out = %Q{<table class="codediff">\n}
53 out = %Q{<table class="codediff inline">\n}
4654 out << "<thead>\n"
4755 out << "<tr>"
4856 out << %Q{<td class="line-numbers">#{src_sha}</td>}
6262 out
6363 end
6464
65 def render_sidebyside_diff(udiff, src_sha, dst_sha)
66 callback = Gitorious::Diff::SidebysideTableCallback.new
67 out = %Q{<table class="codediff sidebyside">\n}
68 out << %Q{<colgroup class="left"><col class="lines"/><col class="code"/></colgroup>}
69 out << %Q{<colgroup class="right"><col class="lines"/><col class="code"/></colgroup>}
70 out << %Q{<thead><th colspan="2">#{src_sha}</th>}
71 out << %Q{<th colspan="2">#{dst_sha}</th></thead>}
72 out << Diff::Display::Unified::Renderer.run(udiff, callback)
73 out << "</table>"
74 out
75 end
76
6577end
toggle raw diff

app/views/browse/commit.html.erb

 
1313<% #TODO: commit diff stats (as sparklines?) -%>
1414
1515<h2>Commit diff</h2>
16<% #TODO diffmode selector -%>
1617
1718<% @diff.each do |file| -%>
1819 <h4><%= h(file.path) -%><%#=link_to h(file.path), blob_path(file.sha, file.path) -%></h4>
19 <%= render_diff(file.patch, file.src, file.dst) -%>
20 <%= render_diff(file.patch, file.src, file.dst, @diffmode) -%>
2021 <small><%= link_to_function "toggle raw diff", "$('#{file.id}').toggle()" -%></small>
2122 <div class="toggle_diff" style="display:none" id="<%= file.id -%>">
2223 <p><pre><%= h(file.patch) -%></pre></p>
toggle raw diff

db/schema.rb

 
4848 t.string "bugtracker_url"
4949 end
5050
51 add_index "projects", ["slug"], :name => "index_projects_on_slug", :unique => true
5152 add_index "projects", ["title"], :name => "index_projects_on_name"
5253 add_index "projects", ["user_id"], :name => "index_projects_on_user_id"
53 add_index "projects", ["slug"], :name => "index_projects_on_slug"
5454
5555 create_table "repositories", :force => true do |t|
5656 t.string "name"
9393 t.string "name"
9494 end
9595
96 add_index "tags", ["name"], :name => "index_tags_on_name", :unique => true
97
9698 create_table "tasks", :force => true do |t|
9799 t.string "target_class"
98100 t.string "command"
toggle raw diff

lib/gitorious/diff/sidebyside_table_callback.rb

 
1module Gitorious
2 module Diff
3 class SidebysideTableCallback
4
5 # Before blocks
6 def before_addblock(block)
7 "<tbody><tr>"
8 end
9
10 def before_remblock(block)
11 "<tbody><tr>"
12 end
13
14 def before_modblock(block)
15 "<tbody><tr>"
16 end
17
18 def before_unmodblock(block)
19 "<tbody><tr>"
20 end
21
22 def before_sepblock(block)
23 "<tbody><tr>"
24 end
25
26 # After blocks
27 def after_addblock(block)
28 "</tbody>"
29 end
30
31 def after_remblock(block)
32 "</tbody>"
33 end
34
35 def after_modblock(block)
36 # "</tbody>"
37 end
38
39 def after_unmodblock(block)
40 "</tbody>"
41 end
42
43 def after_sepblock(block)
44 "</body>"
45 end
46
47 # Before lines
48 def before_addline(line)
49 # adds go on the right
50 %Q{<th class="line-numbers">#{line.number}</th>} +
51 %Q{<td class="code ins"></td>} +
52 %Q{<th class="line-numbers">#{line.number}</th>} +
53 %Q{<td class="code ins"><ins>}
54 end
55
56 def before_remline(line)
57 # rems go on the left (hide the right side)
58 %Q{<th class="line-numbers">#{line.number}</th>} +
59 %Q{<td class="code del"><del>#{line}</del></td>} +
60 %Q{<th class="line-numbers">#{line.number}</th>} +
61 %Q{<td class="code del hidden"><del>}
62 end
63
64 def before_modline(line)
65 # TODO: figure how we best display these
66 # %Q{<th class="line-numbers">#{line.number}</th>} +
67 # %Q{<td class="code changed mod">#{line}</td>} +
68 # %Q{<th class="line-numbers">#{line.number}</th>} +
69 # %Q{<td class="code changed mod">}
70 end
71
72 def before_unmodline(line)
73 # unmods goes on both sides
74 %Q{<th class="line-numbers">#{line.number}</th>} +
75 %Q{<td class="code unchanged unmod">#{line}</td>} +
76 %Q{<th class="line-numbers">#{line.number}</th>} +
77 %Q{<td class="code unchanged unmod">}
78 end
79
80 def before_sepline(line)
81 %Q{<th class="line-numbers line-num-cut">...</th>} +
82 %Q{<td class="code cut-line">...</td>} +
83 %Q{<th class="line-numbers line-num-cut">...</th>} +
84 %Q{<td class="code cut-line">}
85 end
86
87 # After lines
88 def after_addline(line)
89 "</ins></td></tr>"
90 end
91
92 def after_remline(line)
93 "</del></td></tr>"
94 end
95
96 def after_modline(line)
97 # "</td></tr>"
98 end
99
100 def after_unmodline(line)
101 "</td></tr>"
102 end
103
104 def after_sepline(line)
105 "</td></tr>"
106 end
107
108 def new_line
109 end
110 end
111 end
112end
toggle raw diff

public/stylesheets/base.css

 
553553
554554}
555555
556table tr td.line-numbers {
556table tr .line-numbers {
557557 text-align: right;
558558 vertical-align: top;
559559 background: #eee;
572572 font: 100%/110% "Courier New", Courier, monaco, monospace;
573573}
574574
575table.sidebyside {
576 empty-cells: show;
577 table-layout: fixed;
578}
579table.sidebyside tr td.code {
580 white-space: normal;
581}
575582
576583table.codediff {
577584 border: 1px solid #ccc;
587587 font-family: monospace;
588588 line-height: 90%;
589589}
590table.codediff td.line-num-cut {
590table.codediff .line-num-cut {
591591 background: #ccc;
592592 border-top: 1px dashed #ccc;
593593 border-bottom: 1px dashed #ccc;
594594}
595table.codediff thead td.line-numbers, table.codediff thead { background: #ddd; }
595
596table.codediff thead .line-numbers, table.codediff thead { background: #ddd; }
596597table.codediff td.code { padding-left: 2px; }
597table.codediff td.cut-line {
598table.codediff .cut-line {
598599 background: #eee;
599600 border-top: 1px dashed #aaa;
600601 border-bottom: 1px dashed #aaa;
601602}
603table.codediff td.hidden del { display:none; }
602604table.codediff td.del { background: #fdd; }
603605table.codediff td.ins { background: #dfd; }
604606table.codediff td.del del { /*background: #fbb;*/ text-decoration: none; }
605607table.codediff td.ins ins { /*background: #afa;*/ text-decoration: none; }
608table.codediff col.lines {
609 width: 3em;
610}
toggle raw diff