Skip to content
This repository has been archived by the owner on Apr 19, 2019. It is now read-only.

Commit

Permalink
normalize commit hashes so they at least show 'anonymous' if no git u…
Browse files Browse the repository at this point in the history
…ser is set.
  • Loading branch information
technoweenie committed Aug 16, 2010
1 parent 7633879 commit ee04dd8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
9 changes: 3 additions & 6 deletions lib/gollum/frontend/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,9 @@ def show_page_or_file(name)
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
28 changes: 26 additions & 2 deletions lib/gollum/wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class << self
# 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
Expand All @@ -32,6 +38,8 @@ def file_class
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
Expand Down Expand Up @@ -115,7 +123,8 @@ def preview_page(name, data, format)
#
# 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
Expand Down Expand Up @@ -144,6 +153,7 @@ def write_page(name, format, data, commit = {})
#
# 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
Expand All @@ -169,7 +179,7 @@ def update_page(page, name, format, data, commit = {})
# 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.
Expand Down Expand Up @@ -331,5 +341,19 @@ def delete_from_tree_map(map, path)
(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
15 changes: 15 additions & 0 deletions test/test_wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@
%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
Expand Down

0 comments on commit ee04dd8

Please sign in to comment.