From 0c527af291fc3e7bb4bfde6f57c84ec71feeb073 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Thu, 19 Jan 2012 17:49:32 +0400 Subject: [PATCH] [issue #64] Added revisions comparsion. --- app/controllers/wiki_controller.rb | 4 +- app/helpers/wiki_helper.rb | 86 ++++++++++++++++++++++++++++ app/views/wiki/_compare.html.haml | 24 ++++++++ app/views/wiki/compare.html.haml | 29 ++++++++++ config/locales/ru.yml | 3 + config/routes.rb | 2 +- public/stylesheets/gollum/gollum.css | 7 +++ 7 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 app/views/wiki/_compare.html.haml create mode 100644 app/views/wiki/compare.html.haml diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 94f66e983..342132834 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -117,6 +117,7 @@ class WikiController < ApplicationController @page = @wiki.page(@name) diffs = @wiki.repo.diff(@versions.first, @versions.last, @page.path) @diff = diffs.first + @helper = WikiHelper::CompareHelper.new(@diff, @versions) render :compare else redirect_to project_wiki_path(@project, CGI.escape(@name)) @@ -137,6 +138,7 @@ class WikiController < ApplicationController @versions = [sha1, sha2] diffs = @wiki.repo.diff(@versions.first, @versions.last, @page.path) @diff = diffs.first + @helper = WikiHelper::CompareHelper.new(@diff, @versions) flash[:error] = t("flash.wiki.patch_does_not_apply") render :compare end @@ -158,7 +160,7 @@ class WikiController < ApplicationController def history @name = params['id'] if @page = @wiki.page(@name) - @versions = @page.versions(:page => params['page'], :per_page => 25)#.paginate :page => params[:page] #try to use will_paginate + @versions = @page.versions#(:page => params['page'], :per_page => 25)#.paginate :page => params[:page] #try to use will_paginate else redirect_to :back end diff --git a/app/helpers/wiki_helper.rb b/app/helpers/wiki_helper.rb index a44e40ac8..b28a37396 100644 --- a/app/helpers/wiki_helper.rb +++ b/app/helpers/wiki_helper.rb @@ -1,4 +1,90 @@ module WikiHelper + class CompareHelper + + def initialize(diff, versions) + @diff = diff + @versions = versions + end + + def before + @versions[0][0..6] + end + + def after + @versions[1][0..6] + end + + def lines + lines = [] + @diff.diff.split("\n")[2..-1].each do |line| + lines << { :line => line, + :class => line_class(line), + :ldln => left_diff_line_number(line), + :rdln => right_diff_line_number(line) } + end if @diff + lines + end + + def show_revert + !@message + end + + private + + def line_class(line) + if line =~ /^@@/ + 'gc' + elsif line =~ /^\+/ + 'gi' + elsif line =~ /^\-/ + 'gd' + else + '' + end + end + + @left_diff_line_number = nil + def left_diff_line_number(line) + if line =~ /^@@/ + m, li = *line.match(/\-(\d+)/) + @left_diff_line_number = li.to_i + @current_line_number = @left_diff_line_number + ret = '...' + elsif line[0] == ?- + ret = @left_diff_line_number.to_s + @left_diff_line_number += 1 + @current_line_number = @left_diff_line_number - 1 + elsif line[0] == ?+ + ret = ' ' + else + ret = @left_diff_line_number.to_s + @left_diff_line_number += 1 + @current_line_number = @left_diff_line_number - 1 + end + ret + end + + @right_diff_line_number = nil + def right_diff_line_number(line) + if line =~ /^@@/ + m, ri = *line.match(/\+(\d+)/) + @right_diff_line_number = ri.to_i + @current_line_number = @right_diff_line_number + ret = '...' + elsif line[0] == ?- + ret = ' ' + elsif line[0] == ?+ + ret = @right_diff_line_number.to_s + @right_diff_line_number += 1 + @current_line_number = @right_diff_line_number - 1 + else + ret = @right_diff_line_number.to_s + @right_diff_line_number += 1 + @current_line_number = @right_diff_line_number - 1 + end + ret + end + end def gravatar_url(email) "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.downcase)}?s=16&r=pg" diff --git a/app/views/wiki/_compare.html.haml b/app/views/wiki/_compare.html.haml new file mode 100644 index 000000000..b7e76862c --- /dev/null +++ b/app/views/wiki/_compare.html.haml @@ -0,0 +1,24 @@ +#compare-content + - if action_name != 'revert' + %ul.actions + %li.minibutton + = form_tag revert_page_project_wiki_path(@project, escaped_name, @helper.before, @helper.after), + :name => "gollum-revert", :id => "gollum-revert-form" do + = link_to t("layout.wiki.revert_changes"), '#', :class => "gollum-revert-button" + + .data.highlight + %table{:cellpadding => "0", :cellspacing => "0"} + - @helper.lines.each do |line| + %tr + %td.line_numbers= line[:ldln] + %td.line_numbers= line[:rdln] + %td + %pre + %div{:class => line[:class]}= line[:line] + +#gollum-footer + %ul.actions + - if action_name != 'revert' + %li.minibutton + = link_to t("layout.wiki.revert_changes"), '#', :class => "gollum-revert-button" + %li.minibutton= link_to t("layout.wiki.back_to_top"), '#wiki' diff --git a/app/views/wiki/compare.html.haml b/app/views/wiki/compare.html.haml new file mode 100644 index 000000000..e48f43563 --- /dev/null +++ b/app/views/wiki/compare.html.haml @@ -0,0 +1,29 @@ += render :partial => 'gollum_includes' += render :partial => 'project_short' + +%a{ :name => "wiki"} +.block + .secondary-navigation + %ul.wat-cf + %li.first= link_to t("layout.wiki.home"), project_wiki_index_path(@project) + %li= link_to t("layout.wiki.pages"), pages_project_wiki_index_path(@project) + %li= link_to t("layout.wiki.wiki_history"), '#' + %li= link_to t("layout.wiki.git_access"), '#' + + .content + #wiki-wrapper.inner.compare + #head + %h1 + = t("layout.wiki.history_for") + %strong= @name + + %ul.actions + %li.minibutton= link_to t("layout.wiki.back_to_history"), history_project_wiki_path(@project, escaped_name) + = render :partial => 'searchbar' + + #wiki-content + = render :partial => "compare" + +- content_for :sidebar do + - render :partial => 'projects/sidebar' + diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 29f54903f..2602dce68 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -187,6 +187,9 @@ ru: no_pages_in: В %{ref} нет страниц для отображения. no_results_for_search: По запросу %{query} ничего не найдено. matches: Совпадений + back_to_history: Назад к истории + history_for: История для + revert_changes: Откатить изменения roles: confirm_delete: Вы уверены, что хотите удалить эту роль? diff --git a/config/routes.rb b/config/routes.rb index 4ebd0017f..80f4472a3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -91,7 +91,7 @@ Rosa::Application.routes.draw do member do get :history get :edit - match 'revert/:sha1/:sha2' => 'wiki#revert', :as => :revert_page, :via => :get + match 'revert/:sha1/:sha2' => 'wiki#revert', :as => :revert_page, :via => [:get, :post] match ':ref' => 'wiki#show', :as => :versioned, :via => :get post :compare diff --git a/public/stylesheets/gollum/gollum.css b/public/stylesheets/gollum/gollum.css index 2ca8af025..1233a8198 100755 --- a/public/stylesheets/gollum/gollum.css +++ b/public/stylesheets/gollum/gollum.css @@ -274,6 +274,7 @@ #wiki-history table tr td.checkbox { min-width: 2em; padding: 0.3em; + width: 24px; } #wiki-history table tr td.checkbox input { @@ -415,8 +416,13 @@ .compare .data { border: 1px solid #ddd; margin-top: 1em; + margin-bottom: 1em; overflow: auto; } + + .compare .data table { + width: 100%; + } .compare .data pre { margin: 0; @@ -440,6 +446,7 @@ border-right: 1px solid #999; color: #999; padding: 0 0 0 0.5em; + width: 1%; } .compare #compare-content ul.actions li,