From ee04dd84aa0f37b0b40da2330bb04d743d6d2548 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 16 Aug 2010 07:36:32 -0700 Subject: [PATCH] normalize commit hashes so they at least show 'anonymous' if no git user is set. --- lib/gollum/frontend/app.rb | 9 +++------ lib/gollum/wiki.rb | 28 ++++++++++++++++++++++++++-- test/test_wiki.rb | 15 +++++++++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/gollum/frontend/app.rb b/lib/gollum/frontend/app.rb index 553000b7..ce5e8580 100644 --- a/lib/gollum/frontend/app.rb +++ b/lib/gollum/frontend/app.rb @@ -143,12 +143,9 @@ module Precious end def commit_message - message = params[:message] - author_name = `git config --get user.name`.strip || 'Anonymous' - author_email = `git config --get user.email`.strip || 'anon@anon.com' - { :message => message, - :name => author_name, - :email => author_email } + { :message => params[:message], + :name => `git config --get user.name `.strip, + :email => `git config --get user.email`.strip } end end end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index d4d68b6e..49820a59 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -9,6 +9,12 @@ module Gollum # Sets the file class used by all instances of this Wiki. attr_writer :file_class + # Sets the default name for commits. + attr_accessor :default_committer_name + + # Sets the default email for commits. + attr_accessor :default_committer_email + # Gets the page class used by all instances of this Wiki. # Default: Gollum::Page. def page_class @@ -32,6 +38,8 @@ module Gollum end end + self.default_committer_name = 'Anonymous' + self.default_committer_email = 'anon@anon.com' # The String base path to prefix to internal links. For example, when set # to "/wiki", the page "Hobbit" will be linked as "/wiki/Hobbit". Defaults @@ -115,7 +123,8 @@ module Gollum # # Returns the String SHA1 of the newly written version. def write_page(name, format, data, commit = {}) - map = {} + commit = normalize_commit(commit) + map = {} if pcommit = @repo.commit('master') map = tree_map(pcommit.tree) end @@ -144,6 +153,7 @@ module Gollum # # Returns the String SHA1 of the newly written version. def update_page(page, name, format, data, commit = {}) + commit = normalize_commit(commit) pcommit = @repo.commit('master') map = tree_map(pcommit.tree) name ||= page.name @@ -169,7 +179,7 @@ module Gollum # page - The Gollum::Page to delete. # commit - The commit Hash details: # :message - The String commit message. - # :author - The String author full name. + # :name - The String author full name. # :email - The String email address. # # Returns the String SHA1 of the newly written version. @@ -331,5 +341,19 @@ module Gollum (container || map).delete(name) map end + + # Ensures a commit hash has all the required fields for a commit. + # + # commit - The commit Hash details: + # :message - The String commit message. + # :name - The String author full name. + # :email - The String email address. + # + # Returns the commit Hash + def normalize_commit(commit = {}) + commit[:name] = self.class.default_committer_name if commit[:name].to_s.empty? + commit[:email] = self.class.default_committer_email if commit[:email].to_s.empty? + commit + end end end diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 086b1793..9ebb5113 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -38,6 +38,21 @@ context "Wiki" do %w(Bilbo-Baggins.md Eye-Of-Sauron.md Home.textile My-Precious.md), pages.map { |p| p.filename }.sort end + + test "normalizes commit hash" do + commit = {:message => 'abc'} + assert_equal({:message => 'abc', :name => 'Anonymous', :email => 'anon@anon.com'}, + @wiki.normalize_commit(commit.dup)) + + commit[:name] = 'bob' + commit[:email] = '' + assert_equal({:message => 'abc', :name => 'bob', :email => 'anon@anon.com'}, + @wiki.normalize_commit(commit.dup)) + + commit[:email] = 'foo@bar.com' + assert_equal({:message => 'abc', :name => 'bob', :email => 'foo@bar.com'}, + @wiki.normalize_commit(commit.dup)) + end end context "Wiki page previewing" do