From e4da6057a50e613db24ca53c3dba52b5e33d3f77 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Mon, 13 Feb 2012 22:28:43 +0400 Subject: [PATCH 1/2] [issue #64] Changed authorization filters --- app/controllers/wiki_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 65b4077d0..66d9c3442 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -257,20 +257,19 @@ class WikiController < ApplicationController elsif file = @wiki.file(@name) render :text => file.raw_data, :content_type => file.mime_type elsif can? :write, @project -# @name = CGI.escape(@name) @new = true render :new else - redirect_to forbidden_path + redirect_to :action => :index #forbidden_path end end def authorize_read_actions - redirect_to forbidden_path and return if cannot? :read, @project + authorize! :read, @project end def authorize_write_actions - redirect_to forbidden_path and return if cannot? :write, @project + authorize! :write, @project end end From 99ee490e94fd7a4f1198a1ae84a4467e0a2161b1 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Tue, 14 Feb 2012 03:29:33 +0400 Subject: [PATCH 2/2] [issue #64] Added callback for notifications + some minor changes --- app/controllers/wiki_controller.rb | 34 +++++------ app/helpers/diff_helper.rb | 2 +- app/helpers/wiki_helper.rb | 2 +- app/views/wiki/_history.html.haml | 6 +- lib/gollum/page.rb | 8 +-- lib/gollum/wiki.rb | 90 +++++++++++++++++++++++++----- lib/grit/repo.rb | 9 +-- 7 files changed, 107 insertions(+), 44 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 3b34f8afd..b334262e3 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -48,12 +48,10 @@ class WikiController < ApplicationController @name = CGI.unescape(params[:id]) @page = @wiki.page(@name) name = params[:rename] || @name - committer = Gollum::Committer.new(@wiki, commit) - commit_arg = {:committer => committer} - update_wiki_page(@wiki, @page, params[:content], commit_arg, name, params[:format]) - update_wiki_page(@wiki, @page.footer, params[:footer], commit_arg) if params[:footer] - update_wiki_page(@wiki, @page.sidebar, params[:sidebar], commit_arg) if params[:sidebar] + update_wiki_page(@wiki, @page, params[:content], {:committer => committer}, name, params[:format]) + update_wiki_page(@wiki, @page.footer, params[:footer], {:committer => committer}) if params[:footer] + update_wiki_page(@wiki, @page.sidebar, params[:sidebar], {:committer => committer}) if params[:sidebar] committer.commit @@ -68,9 +66,8 @@ class WikiController < ApplicationController def create @name = CGI.unescape(params['page']) format = params['format'].intern - begin - @wiki.write_page(@name, format, params['content'] || '', commit) + @wiki.write_page(@name, format, params['content'] || '', {:committer => committer}).commit redirect_to project_wiki_path(@project, CGI.escape(@name)) rescue Gollum::DuplicatePageError => e flash[:error] = t("flash.wiki.duplicate_page", :name => @name) @@ -82,7 +79,7 @@ class WikiController < ApplicationController @name = CGI.unescape(params[:id]) page = @wiki.page(@name) if page - @wiki.delete_page(page, commit.merge(:message => 'Page removed')) + @wiki.delete_page(page, {:committer => committer}).commit flash[:notice] = t("flash.wiki.page_successfully_removed") else flash[:notice] = t("flash.wiki.page_not_found", :name => params[:id]) @@ -145,7 +142,7 @@ class WikiController < ApplicationController sha1 = params[:sha1] sha2 = params[:sha2] - if @wiki.revert_page(@page, sha1, sha2, commit) + if @wiki.revert_page(@page, sha1, sha2, {:committer => committer}).commit flash[:notice] = t("flash.wiki.revert_success") redirect_to project_wiki_path(@project, CGI.escape(@name)) else @@ -165,7 +162,7 @@ class WikiController < ApplicationController def revert_wiki sha1 = params[:sha1] sha2 = params[:sha2] - if @wiki.revert_commit(sha1, sha2, commit) + if @wiki.revert_commit(sha1, sha2, {:committer => committer}).commit flash[:notice] = t("flash.wiki.revert_success") redirect_to project_wiki_index_path(@project) else @@ -233,11 +230,11 @@ class WikiController < ApplicationController if params['message'] and !params['message'].empty? msg = params['message'] else -# msg = "#{!!@wiki.page(@name) ? 'Updated page' : 'Created page'} #{@name}" msg = case action_name.to_s - when 'create' then "Created page #{@name.to_s}" - when 'update' then "Updated page #{@name.to_s}" - when 'revert' then "Reverted page #{@name.to_s}" + when 'create' then "Created page #{@name.to_s}" + when 'update' then "Updated page #{@name.to_s}" + when 'destroy' then "Removed page #{@name.to_s}" + when 'revert' then "Reverted page #{@name.to_s}" when 'revert_wiki' then "Reverted wiki" end msg += " (#{params['format']})" if params['format'] @@ -246,8 +243,13 @@ class WikiController < ApplicationController { :message => msg } end - def commit - commit_message.merge({:name => current_user.uname, :email => current_user.email}) + def committer + p = commit_message.merge({:name => current_user.uname, :email => current_user.email}) + @committer ||= Gollum::Committer.new(@wiki, p) + # @committer.after_commit do |committer, sha1| + # here goes callback for notification + # end + @committer end def show_or_create_page diff --git a/app/helpers/diff_helper.rb b/app/helpers/diff_helper.rb index a7f1163cf..0addc764b 100644 --- a/app/helpers/diff_helper.rb +++ b/app/helpers/diff_helper.rb @@ -12,7 +12,7 @@ module DiffHelper res += "" res += "" - res.html_safe.force_encoding(Encoding.default_internal || Encoding::UTF_8) + res.html_safe.encode_to_default end end diff --git a/app/helpers/wiki_helper.rb b/app/helpers/wiki_helper.rb index 9b406a88b..05ac815ab 100644 --- a/app/helpers/wiki_helper.rb +++ b/app/helpers/wiki_helper.rb @@ -86,7 +86,7 @@ module WikiHelper end def author - @page.version.author.name.force_encoding(Encoding.default_internal || Encoding::UTF_8) + @page.version.author.name.encode_to_default end def author_email diff --git a/app/views/wiki/_history.html.haml b/app/views/wiki/_history.html.haml index a1f66a2a8..bb14ce8e2 100644 --- a/app/views/wiki/_history.html.haml +++ b/app/views/wiki/_history.html.haml @@ -16,13 +16,13 @@ - user = User.where(:email => v.author.email).first = link_to user_path_by_user(user) do %img{:src => gravatar_url(v.author.email), - :alt => "avatar: #{v.author.name.force_encoding(Encoding.default_internal || Encoding::UTF_8)}", + :alt => "avatar: #{v.author.name.encode_to_default}", :class => "mini-gravatar"} - %span.username= user.present? ? user.uname : v.author.name + %span.username= user.present? ? user.uname : v.author.name.encode_to_default %td.commit-name %span.time-elapsed= "#{l v.committed_date.to_date, :format => :long}:"   - = v.message.force_encoding(Encoding.default_internal) + = v.message.encode_to_default - if @name = raw "[#{link_to v.id[0..6], versioned_project_wiki_path(@project, escaped_name, v.id), :title => t("layout.wiki.view_commit")}]" - else diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 5850f0b3b..9fb1a31f9 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -1,11 +1,11 @@ # -*- encoding : utf-8 -*- module Gollum class Page - alias_method :native_gollum_name, :name - - def name - native_gollum_name.force_encoding(Encoding.default_internal || Encoding::UTF_8) + def name_with_encoding + name_without_encoding.encode_to_default end + alias_method_chain :name, :encoding + end end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 9916eea08..14e8ae614 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -2,26 +2,90 @@ module Gollum class Wiki - alias_method :native_gollum_page, :page - alias_method :native_gollum_file, :file - alias_method :native_gollum_write_page, :write_page - alias_method :native_gollum_update_page, :update_page - - def page(name, version = @ref) - native_gollum_page(force_grit_encoding(name), version) + def page_with_forced_encoding(name, version = @ref) + page_without_forced_encoding(force_grit_encoding(name), version) end + alias_method_chain :page, :forced_encoding - def file(name, version = @ref) - native_gollum_file(force_grit_encoding(name), version) + def file_with_forced_encoding(name, version = @ref) + file_without_forced_encoding(force_grit_encoding(name), version) end + alias_method_chain :file, :forced_encoding - def write_page(name, format, data, commit = {}) - native_gollum_write_page(force_grit_encoding(name), format, data, commit) + def write_page_with_forced_encoding(name, format, data, commit = {}) + write_page_without_forced_encoding(force_grit_encoding(name), format, data, commit) end + alias_method_chain :write_page, :forced_encoding - def update_page(page, name, format, data, commit = {}) - native_gollum_update_page(page, force_grit_encoding(name), format, data, commit) + def update_page_with_forced_encoding(page, name, format, data, commit = {}) + update_page_without_forced_encoding(page, force_grit_encoding(name), format, data, commit) end + alias_method_chain :update_page, :forced_encoding + + # Public: Applies a reverse diff for a given page. If only 1 SHA is given, + # the reverse diff will be taken from its parent (^SHA...SHA). If two SHAs + # are given, the reverse diff is taken from SHA1...SHA2. + # + # page - The Gollum::Page to delete. + # sha1 - String SHA1 of the earlier parent if two SHAs are given, + # or the child. + # sha2 - Optional String SHA1 of the child. + # commit - The commit Hash details: + # :message - The String commit message. + # :name - The String author full name. + # :email - The String email address. + # :parent - Optional Grit::Commit parent to this update. + # + # Returns a String SHA1 of the new commit, or nil if the reverse diff does + # not apply. + def revert_page_with_committer(page, sha1, sha2 = nil, commit = {}) + if sha2.is_a?(Hash) + commit = sha2 + sha2 = nil + end + + multi_commit = false + + patch = full_reverse_diff_for(page, sha1, sha2) + committer = if obj = commit[:committer] + multi_commit = true + obj + else + Committer.new(self, commit) + end + parent = committer.parents[0] + committer.options[:tree] = @repo.git.apply_patch(parent.sha, patch) + return false unless committer.options[:tree] + committer.after_commit do |index, sha| + @access.refresh + + files = [] + if page + files << [page.path, page.name, page.format] + else + # Grit::Diff can't parse reverse diffs.... yet + patch.each_line do |line| + if line =~ %r{^diff --git b/.+? a/(.+)$} + path = $1 + ext = ::File.extname(path) + name = ::File.basename(path, ext) + if format = ::Gollum::Page.format_for(ext) + files << [path, name, format] + end + end + end + end + + files.each do |(path, name, format)| + dir = ::File.dirname(path) + dir = '' if dir == '.' + index.update_working_dir(dir, name, format) + end + end + + multi_commit ? committer : committer.commit + end + alias_method_chain :revert_page, :committer private diff --git a/lib/grit/repo.rb b/lib/grit/repo.rb index d24bb6ff3..998ff7b30 100644 --- a/lib/grit/repo.rb +++ b/lib/grit/repo.rb @@ -2,12 +2,8 @@ module Grit class Repo - alias_method :native_grit_diff, :diff - - def diff(a, b, *paths) - diff = self.git.native('diff', {}, a, b, '--', *paths).force_encoding(Encoding.default_internal || Encoding::UTF_8) - Grit.log 'in grit' - Grit.log diff + def diff_with_encoding(a, b, *paths) + diff = self.git.native('diff', {}, a, b, '--', *paths).encode_to_default if diff =~ /diff --git "{0,1}a/ diff = diff.sub(/.*?(diff --git "{0,1}a)/m, '\1') else @@ -15,6 +11,7 @@ module Grit end Diff.list_from_string(self, diff) end + alias_method_chain :diff, :encoding end end