Commit 49a2c203531d114059cde68488f5ef17b07c8ed4

Refactored Diff::Display classes

Commit diff

app/helpers/browse_helper.rb

 
7272 end
7373 end
7474
75 #diff = Diff::Display::Unified.new(load_diff("simple"))
76 #diff.render(Diff::Renderer::Base.new)
7577 def render_inline_diff(udiff, src_sha, dst_sha)
76 callback = Gitorious::Diff::InlineTableCallback.new
78 differ = Diff::Display::Unified.new(udiff)
7779 out = %Q{<table class="codediff inline">\n}
7880 out << "<thead>\n"
7981 out << "<tr>"
8383 out << %Q{<td class="line-numbers">#{dst_sha}</td>}
8484 out << "<td>&nbsp</td></tr>\n"
8585 out << "</thead>\n"
86 out << Diff::Display::Unified::HTMLRenderer.run(udiff, callback)
86 out << differ.render(Gitorious::Diff::InlineTableCallback.new)
8787 out << "</table>"
8888 out
8989 end
9090
9191 def render_sidebyside_diff(udiff, src_sha, dst_sha)
92 callback = Gitorious::Diff::SidebysideTableCallback.new
92 differ = Diff::Display::Unified.new(udiff)
9393 out = %Q{<table class="codediff sidebyside">\n}
9494 out << %Q{<colgroup class="left"><col class="lines"/><col class="code"/></colgroup>}
9595 out << %Q{<colgroup class="right"><col class="lines"/><col class="code"/></colgroup>}
9696 out << %Q{<thead><th colspan="2">#{src_sha}</th>}
9797 out << %Q{<th colspan="2">#{dst_sha}</th></thead>}
98 out << Diff::Display::Unified::HTMLRenderer.run(udiff, callback)
98 out << differ.render(Gitorious::Diff::SidebysideTableCallback.new)
9999 out << "</table>"
100100 out
101101 end
toggle raw diff

app/models/repository.rb

 
9797
9898 def create_delete_repos_task
9999 Task.create!(:target_class => self.class.name,
100 :command => "delete_git_repository", :arguments => [gitdir])
100 :command => "delete_git_repository", :arguments => [gitdir]) # fixme: gitdir is probably gone in after_destroy
101101 end
102102
103103 protected
toggle raw diff

config/initializers/requires.rb

 
11require "core_ext"
22require "fileutils"
33require "ruby-git/lib/git"
4require "diff-display/lib/diff/display/unified"
4require "diff-display/lib/diff-display"
toggle raw diff

lib/gitorious/diff/base_callback.rb

 
1module Gitorious
2 module Diff
3 class BaseCallback < ::Diff::Renderer::Base
4 def headerline(line); end
5 def new_line; end
6
7 protected
8 def escape(text)
9 text.gsub('&', '&amp;').
10 gsub('<', '&lt;').
11 gsub('>', '&gt;').
12 gsub('"', '&#34;')
13 end
14 end
15 end
16end
toggle raw diff

lib/gitorious/diff/inline_table_callback.rb

 
11module Gitorious
22 module Diff
3 class InlineTableCallback
4
5 # Before blocks
6 def before_addblock(block)
7 end
8
9 def before_remblock(block)
10 end
11
12 def before_modblock(block)
13 end
14
15 def before_unmodblock(block)
16 end
17
18 def before_sepblock(block)
19 end
20
21 # After blocks
22 def after_addblock(block)
23 end
24
25 def after_remblock(block)
26 end
27
28 def after_modblock(block)
29 end
30
31 def after_unmodblock(block)
32 end
33
34 def after_sepblock(block)
35 end
36
37 # Before lines
38 def before_addline(line)
3 class InlineTableCallback < BaseCallback
4 def addline(line)
395 %Q{<tr class="changes">} +
406 %Q{<td class="line-numbers">&nbsp;</td>} +
417 %Q{<td class="line-numbers">#{line.number}</td>} +
42 %Q{<td class="code ins"><ins>}
8 %Q{<td class="code ins"><ins>#{escape(line)}</ins></td></tr>}
439 end
4410
45 def before_remline(line)
11 def remline(line)
4612 %Q{<tr class="changes">} +
4713 %Q{<td class="line-numbers">#{line.number}</td>} +
4814 %Q{<td class="line-numbers">&nbsp;</td>} +
49 %Q{<td class="code del"><del>}
15 %Q{<td class="code del"><del>#{escape(line)}</del></td></tr>}
5016 end
5117
52 def before_modline(line)
18 def modline(line)
5319 %Q{<tr class="changes">} +
5420 %Q{<td class="line-numbers">&nbsp;</td>} +
5521 %Q{<td class="line-numbers">#{line.number}</td>} +
56 %Q{<td class="code unchanged mod">}
22 %Q{<td class="code unchanged mod">#{escape(line)}</td></tr>}
5723 end
5824
59 def before_unmodline(line)
25 def unmodline(line)
6026 %Q{<tr class="changes">} +
6127 %Q{<td class="line-numbers">&nbsp;</td>} +
6228 %Q{<td class="line-numbers">#{line.number}</td>} +
63 %Q{<td class="code unchanged unmod">}
29 %Q{<td class="code unchanged unmod">#{escape(line)}</td></tr>}
6430 end
6531
66 def before_sepline(line)
32 def sepline(line)
6733 %Q{<tr class="changes">} +
68 %Q{<td class="line-numbers line-num-cut">...</td>} +
69 %Q{<td class="line-numbers line-num-cut">...</td>} +
70 %Q{<td class="code cut-line">}
71 end
72
73 # After lines
74 def after_addline(line)
75 "</ins></td></tr>"
76 end
77
78 def after_remline(line)
79 "</del></td></tr>"
80 end
81
82 def after_modline(line)
83 "</td></tr>"
84 end
85
86 def after_unmodline(line)
87 "</td></tr>"
88 end
89
90 def after_sepline(line)
91 "</td></tr>"
92 end
93
94 def new_line
34 %Q{<td class="line-numbers line-num-cut">&hellip;</td>} +
35 %Q{<td class="line-numbers line-num-cut">&hellip;</td>} +
36 %Q{<td class="code cut-line"></td></tr>}
9537 end
9638 end
9739 end
toggle raw diff

lib/gitorious/diff/sidebyside_table_callback.rb

 
11module Gitorious
22 module Diff
3 class SidebysideTableCallback
3 class SidebysideTableCallback < BaseCallback
44
55 # Before blocks
66 def before_addblock(block)
4545 end
4646
4747 # Before lines
48 def before_addline(line)
48 def addline(line)
4949 # adds go on the right
5050 %Q{<th class="line-numbers">#{line.number}</th>} +
5151 %Q{<td class="code ins"></td>} +
5252 %Q{<th class="line-numbers">#{line.number}</th>} +
53 %Q{<td class="code ins"><ins>}
53 %Q{<td class="code ins"><ins>#{escape(line)}</ins></td></tr>}
5454 end
5555
56 def before_remline(line)
56 def remline(line)
5757 # rems go on the left (hide the right side)
5858 %Q{<th class="line-numbers">#{line.number}</th>} +
5959 %Q{<td class="code del"><del>#{CGI.escapeHTML(line)}</del></td>} +
6060 %Q{<th class="line-numbers">#{line.number}</th>} +
61 %Q{<td class="code del hidden"><del>}
61 %Q{<td class="code del hidden"><del>#{escape(line)}</del></td></tr>}
6262 end
6363
64 def before_modline(line)
64 def modline(line)
6565 # TODO: figure how we best display these
6666 # %Q{<th class="line-numbers">#{line.number}</th>} +
6767 # %Q{<td class="code changed mod">#{CGI.escapeHTML(line)}</td>} +
6868 # %Q{<th class="line-numbers">#{line.number}</th>} +
69 # %Q{<td class="code changed mod">}
69 # %Q{<td class="code changed mod">#{escape(line)}</td></tr>}
7070 end
7171
72 def before_unmodline(line)
72 def unmodline(line)
7373 # unmods goes on both sides
7474 %Q{<th class="line-numbers">#{line.number}</th>} +
75 %Q{<td class="code unchanged unmod">#{CGI.escapeHTML(line)}</td>} +
75 %Q{<td class="code unchanged unmod">#{escape(line)}</td>} +
7676 %Q{<th class="line-numbers">#{line.number}</th>} +
77 %Q{<td class="code unchanged unmod">}
77 %Q{<td class="code unchanged unmod">#{escape(line)}</td></tr>}
7878 end
7979
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
80 def sepline(line)
81 %Q{<th class="line-numbers line-num-cut">&hellip;</th>} +
82 %Q{<td class="code cut-line"></td>} +
83 %Q{<th class="line-numbers line-num-cut">&hellip;</th>} +
84 %Q{<td class="code cut-line"></td></tr>}
10985 end
11086 end
11187 end
toggle raw diff

vendor/diff-display/History.txt

 
1== 0.0.1 2008-01-28
2
3* 1 major enhancement:
4 * Initial release
toggle raw diff

vendor/diff-display/LICENSE

 
0Copyright (c) 2003 Marcel Molina Jr.
1
2Permission is hereby granted, free of charge, to any person obtaining
3a copy of this software and associated documentation files (the
4"Software"), to deal in the Software without restriction, including
5without limitation the rights to use, copy, modify, merge, publish,
6distribute, sublicense, and/or sell copies of the Software, and to
7permit persons to whom the Software is furnished to do so, subject to
8the following conditions:
9
10The above copyright notice and this permission notice shall be
11included in all copies or substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
toggle raw diff

vendor/diff-display/License.txt

 
1Copyright (c) 2008 Johan Sørensen
2Copyright (c) 2003 Marcel Molina Jr.
3
4Permission is hereby granted, free of charge, to any person obtaining
5a copy of this software and associated documentation files (the
6"Software"), to deal in the Software without restriction, including
7without limitation the rights to use, copy, modify, merge, publish,
8distribute, sublicense, and/or sell copies of the Software, and to
9permit persons to whom the Software is furnished to do so, subject to
10the following conditions:
11
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
toggle raw diff

vendor/diff-display/Manifest.txt

 
1History.txt
2License.txt
3Manifest.txt
4README.txt
5Rakefile
6config/hoe.rb
7config/requirements.rb
8lib/diff-display.rb
9lib/diff-display/version.rb
10log/debug.log
11script/destroy
12script/generate
13script/txt2html
14setup.rb
15spec/diff-display_spec.rb
16spec/spec.opts
17spec/spec_helper.rb
18tasks/deployment.rake
19tasks/environment.rake
20tasks/rspec.rake
21tasks/website.rake
22website/index.html
23website/index.txt
24website/javascripts/rounded_corners_lite.inc.js
25website/stylesheets/screen.css
26website/template.rhtml
toggle raw diff

vendor/diff-display/README

 
0Marcel Molina Jr. wrote this library probably back in 2004 or so.
toggle raw diff

vendor/diff-display/README.txt

 
1Rewrite of an (unreleased) library by Marcel Molina Jr., who wrote this it
2probably back in 2004 or so.
toggle raw diff

vendor/diff-display/Rakefile

 
1require 'rake'
2require 'rake/testtask'
3require 'rake/rdoctask'
4
5desc 'Default: run unit tests.'
6task :default => :test
7
8desc 'Test the resource_paths plugin.'
9Rake::TestTask.new(:test) do |t|
10 t.libs << 'lib'
11 t.pattern = 'test/**/*_test.rb'
12 t.verbose = true
13end
1require 'config/requirements'
2require 'config/hoe' # setup Hoe + all gem configuration
3
4Dir['tasks/**/*.rake'].each { |rake| load rake }
toggle raw diff

vendor/diff-display/config/hoe.rb

 
1require 'diff/display/version'
2
3AUTHOR = ['Johan Sørensen', 'Marcel Molina Jr.'] # can also be an array of Authors
4EMAIL = "johan@johansorensen.com"
5DESCRIPTION = "Displays a unified diffs in various (user-definable) ways"
6GEM_NAME = 'diff-display' # what ppl will type to install your gem
7RUBYFORGE_PROJECT = 'diff-display' # The unix name for your project
8HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
11@config_file = "~/.rubyforge/user-config.yml"
12@config = nil
13RUBYFORGE_USERNAME = "unknown"
14def rubyforge_username
15 unless @config
16 begin
17 @config = YAML.load(File.read(File.expand_path(@config_file)))
18 rescue
19 puts <<-EOS
20ERROR: No rubyforge config file found: #{@config_file}
21Run 'rubyforge setup' to prepare your env for access to Rubyforge
22 - See http://newgem.rubyforge.org/rubyforge.html for more details
23 EOS
24 exit
25 end
26 end
27 RUBYFORGE_USERNAME.replace @config["username"]
28end
29
30
31REV = nil
32# UNCOMMENT IF REQUIRED:
33# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34VERS = Diff::Display::VERSION::STRING + (REV ? ".#{REV}" : "")
35RDOC_OPTS = ['--quiet', '--title', 'diff-display documentation',
36 "--opname", "index.html",
37 "--line-numbers",
38 "--main", "README",
39 "--inline-source"]
40
41class Hoe
42 def extra_deps
43 @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44 @extra_deps
45 end
46end
47
48# Generate all the Rake tasks
49# Run 'rake -T' to see list of generated tasks (from gem root directory)
50hoe = Hoe.new(GEM_NAME, VERS) do |p|
51 p.author = AUTHOR
52 p.description = DESCRIPTION
53 p.email = EMAIL
54 p.summary = DESCRIPTION
55 p.url = HOMEPATH
56 p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57 p.test_globs = ["test/**/test_*.rb"]
58 p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
60 # == Optional
61 p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
62 #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
64 #p.spec_extras = {} # A hash of extra values to set in the gemspec.
65
66end
67
68CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
69PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
70hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
71hoe.rsync_args = '-av --delete --ignore-errors'
toggle raw diff

vendor/diff-display/config/requirements.rb

 
1require 'fileutils'
2include FileUtils
3
4require 'rubygems'
5%w[rake hoe newgem rubigen].each do |req_gem|
6 begin
7 require req_gem
8 rescue LoadError
9 puts "This Rakefile requires the '#{req_gem}' RubyGem."
10 puts "Installation: gem install #{req_gem} -y"
11 exit
12 end
13end
14
15$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
17require 'diff-display'
toggle raw diff

vendor/diff-display/doc/.gitignore

vendor/diff-display/lib/diff-display.rb

 
1$:.unshift File.dirname(__FILE__)
2
3module Diff
4 module Display
5 end
6end
7
8require "diff/display/version"
9require "diff/display/data_structure"
10require "diff/display/unified"
11require "diff/display/unified/generator"
12
13require "diff/renderer/base"
14require "diff/renderer/diff"
toggle raw diff

vendor/diff-display/lib/diff/display/data_structure.rb

 
1module Diff
2 module Display
3 class Data < Array
4 def initialize
5 super
6 end
7
8 def to_diff
9 diff = ""
10 each do |block|
11 block.each do |line|
12 case line
13 when HeaderLine
14 diff << "#{line}\n"
15 when UnModLine
16 diff << " #{line}\n"
17 when SepLine
18 diff << "\n"
19 when AddLine
20 diff << "+#{line}\n"
21 when RemLine
22 diff << "-#{line}\n"
23 end
24 end
25 end
26 diff.chomp
27 end
28 end
29
30 # Every line from the passed in diff gets transformed into an instance of
31 # one of line Line class's subclasses. One subclass exists for each line
32 # type in a diff. As such there is an AddLine class for added lines, a RemLine
33 # class for removed lines, an UnModLine class for lines which remain unchanged and
34 # a SepLine class which represents all the lines that aren't part of the diff.
35 class Line < String
36 class << self
37 def add(line, line_number)
38 AddLine.new(line, line_number)
39 end
40
41 def rem(line, line_number)
42 RemLine.new(line, line_number)
43 end
44
45 def unmod(line, line_number)
46 UnModLine.new(line, line_number)
47 end
48
49 def header(line)
50 HeaderLine.new(line)
51 end