From 3c44205be7e7144538c786513cbcd6b2186fc4f4 Mon Sep 17 00:00:00 2001 From: Randy Merrill Date: Thu, 27 Jan 2011 11:24:15 -0800 Subject: [PATCH 1/8] Adding the logic to accept a command line flag for the branch to use to retrieve pages. --- bin/gollum | 4 ++++ lib/gollum/committer.rb | 2 +- lib/gollum/page.rb | 4 ++-- lib/gollum/wiki.rb | 35 ++++++++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/bin/gollum b/bin/gollum index 5ce8ce7e..7756831e 100755 --- a/bin/gollum +++ b/bin/gollum @@ -48,6 +48,10 @@ opts = OptionParser.new do |opts| opts.on("--page-file-dir [PATH]", "Specify the sub directory for all page files (default: repository root).") do |path| wiki_options[:page_file_dir] = path end + + opts.on("--branch [BRANCH]", "Specify the repository branch to use (default: working branch).") do |branch| + wiki_options[:branch] = branch + end end # Read command line options into `options` hash diff --git a/lib/gollum/committer.rb b/lib/gollum/committer.rb index a8b73673..947aab5f 100644 --- a/lib/gollum/committer.rb +++ b/lib/gollum/committer.rb @@ -62,7 +62,7 @@ module Gollum # Returns an array of Grit::Commit instances. def parents @parents ||= begin - arr = [@options[:parent] || @wiki.repo.commit('master')] + arr = [@options[:parent] || @wiki.repo.commit(@wiki.branch)] arr.flatten! arr.compact! arr diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 487700e2..3a52a0e0 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -199,10 +199,10 @@ module Gollum options[:pretty] = 'raw' options.delete :max_count options.delete :skip - log = @wiki.repo.git.native "log", options, "master", "--", @path + log = @wiki.repo.git.native "log", options, @wiki.branch, "--", @path Grit::Commit.list_from_string(@wiki.repo, log) else - @wiki.repo.log('master', @path, log_pagination_options(options)) + @wiki.repo.log(@wiki.branch, @path, log_pagination_options(options)) end end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index dd362572..6beb65c9 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -12,6 +12,9 @@ module Gollum # Sets the markup class used by all instances of this Wiki. attr_writer :markup_class + # Sets the default branch for the wiki. + attr_accessor :default_branch + # Sets the default name for commits. attr_accessor :default_committer_name @@ -80,6 +83,7 @@ module Gollum end end + self.default_branch = 'master' self.default_committer_name = 'Anonymous' self.default_committer_email = 'anon@anon.com' @@ -94,6 +98,9 @@ module Gollum # Gets the sanitization options for older page revisions used by this Wiki. attr_reader :history_sanitization + # Gets the String branch in which all page files reside. + attr_reader :branch + # Gets the String directory in which all page files reside. attr_reader :page_file_dir @@ -109,6 +116,7 @@ module Gollum # :markup_class - The markup Class. Default: Gollum::Markup # :sanitization - An instance of Sanitization. # :page_file_dir - String the directory in which all page files reside + # :branch - String the repository branch to retrieve pages from # # Returns a fresh Gollum::Repo. def initialize(path, options = {}) @@ -124,6 +132,7 @@ module Gollum @file_class = options[:file_class] || self.class.file_class @markup_class = options[:markup_class] || self.class.markup_class @repo = @access.repo + @branch = options[:branch] || self.class.default_branch @sanitization = options[:sanitization] || self.class.sanitization @history_sanitization = options[:history_sanitization] || self.class.history_sanitization @@ -139,20 +148,20 @@ module Gollum # Public: Get the formatted page for a given page name. # # name - The human or canonical String page name of the wiki page. - # version - The String version ID to find (default: "master"). + # version - The String version ID to find (default: @branch). # # Returns a Gollum::Page or nil if no matching page was found. - def page(name, version = 'master') + def page(name, version = @branch) @page_class.new(self).find(name, version) end # Public: Get the static file for a given name. # # name - The full String pathname to the file. - # version - The String version ID to find (default: "master"). + # version - The String version ID to find (default: @branch). # # Returns a Gollum::File or nil if no matching file was found. - def file(name, version = 'master') + def file(name, version = @branch) @file_class.new(self).find(name, version) end @@ -381,11 +390,11 @@ module Gollum # Public: Lists all pages for this wiki. # - # treeish - The String commit ID or ref to find (default: master) + # treeish - The String commit ID or ref to find (default: @branch) # # Returns an Array of Gollum::Page instances. def pages(treeish = nil) - tree_list(treeish || 'master') + tree_list(treeish || @branch) end # Public: Returns the number of pages accessible from a commit @@ -394,7 +403,7 @@ module Gollum # # Returns a Fixnum def size(ref = nil) - tree_map_for(ref || 'master').inject(0) do |num, entry| + tree_map_for(ref || @branch).inject(0) do |num, entry| num + (@page_class.valid_page_name?(entry.name) ? 1 : 0) end rescue Grit::GitRuby::Repository::NoSuchShaFound @@ -407,7 +416,7 @@ module Gollum # # Returns an Array with Objects of page name and count of matches def search(query) - args = [{:c => query}, 'master', '--'] + args = [{:c => query}, @branch, '--'] args << '--' << @page_file_dir if @page_file_dir @repo.git.grep(*args).split("\n").map! do |line| @@ -429,7 +438,7 @@ module Gollum # # Returns an Array of Grit::Commit. def log(options = {}) - @repo.log('master', nil, log_pagination_options(options)) + @repo.log(@branch, nil, log_pagination_options(options)) end # Public: Refreshes just the cached Git reference data. This should @@ -547,6 +556,14 @@ module Gollum full_reverse_diff_for(nil, sha1, sha2) end + # Gets the default branch for the wiki. + # + # Returns the String name. + def default_branch + @default_branch ||= \ + self.class.default_branch + end + # Gets the default name for commits. # # Returns the String name. From 2bb1937fa903e30dbc66af89e7cf530251ffbf95 Mon Sep 17 00:00:00 2001 From: Randy Merrill Date: Thu, 27 Jan 2011 11:29:21 -0800 Subject: [PATCH 2/8] Fixing the docs for the default branch. --- bin/gollum | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/gollum b/bin/gollum index 7756831e..efacca54 100755 --- a/bin/gollum +++ b/bin/gollum @@ -49,7 +49,7 @@ opts = OptionParser.new do |opts| wiki_options[:page_file_dir] = path end - opts.on("--branch [BRANCH]", "Specify the repository branch to use (default: working branch).") do |branch| + opts.on("--branch [BRANCH]", "Specify the repository branch to use (default: master).") do |branch| wiki_options[:branch] = branch end end From a71ab7c418ef8708bb3d2945287c662dbdeffd54 Mon Sep 17 00:00:00 2001 From: Randy Merrill Date: Thu, 27 Jan 2011 11:38:48 -0800 Subject: [PATCH 3/8] Changing from using `branch` as the variable to using `ref`. --- bin/gollum | 4 ++-- lib/gollum/committer.rb | 2 +- lib/gollum/page.rb | 4 ++-- lib/gollum/wiki.rb | 40 ++++++++++++++++++++-------------------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/bin/gollum b/bin/gollum index efacca54..e45deff7 100755 --- a/bin/gollum +++ b/bin/gollum @@ -49,8 +49,8 @@ opts = OptionParser.new do |opts| wiki_options[:page_file_dir] = path end - opts.on("--branch [BRANCH]", "Specify the repository branch to use (default: master).") do |branch| - wiki_options[:branch] = branch + opts.on("--ref [REF]", "Specify the repository ref to use (default: master).") do |ref| + wiki_options[:ref] = ref end end diff --git a/lib/gollum/committer.rb b/lib/gollum/committer.rb index 947aab5f..fcf2a159 100644 --- a/lib/gollum/committer.rb +++ b/lib/gollum/committer.rb @@ -62,7 +62,7 @@ module Gollum # Returns an array of Grit::Commit instances. def parents @parents ||= begin - arr = [@options[:parent] || @wiki.repo.commit(@wiki.branch)] + arr = [@options[:parent] || @wiki.repo.commit(@wiki.ref)] arr.flatten! arr.compact! arr diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 3a52a0e0..ad95e4da 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -199,10 +199,10 @@ module Gollum options[:pretty] = 'raw' options.delete :max_count options.delete :skip - log = @wiki.repo.git.native "log", options, @wiki.branch, "--", @path + log = @wiki.repo.git.native "log", options, @wiki.ref, "--", @path Grit::Commit.list_from_string(@wiki.repo, log) else - @wiki.repo.log(@wiki.branch, @path, log_pagination_options(options)) + @wiki.repo.log(@wiki.ref, @path, log_pagination_options(options)) end end diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 6beb65c9..75941f7c 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -12,8 +12,8 @@ module Gollum # Sets the markup class used by all instances of this Wiki. attr_writer :markup_class - # Sets the default branch for the wiki. - attr_accessor :default_branch + # Sets the default ref for the wiki. + attr_accessor :default_ref # Sets the default name for commits. attr_accessor :default_committer_name @@ -83,7 +83,7 @@ module Gollum end end - self.default_branch = 'master' + self.default_ref = 'master' self.default_committer_name = 'Anonymous' self.default_committer_email = 'anon@anon.com' @@ -98,8 +98,8 @@ module Gollum # Gets the sanitization options for older page revisions used by this Wiki. attr_reader :history_sanitization - # Gets the String branch in which all page files reside. - attr_reader :branch + # Gets the String ref in which all page files reside. + attr_reader :ref # Gets the String directory in which all page files reside. attr_reader :page_file_dir @@ -116,7 +116,7 @@ module Gollum # :markup_class - The markup Class. Default: Gollum::Markup # :sanitization - An instance of Sanitization. # :page_file_dir - String the directory in which all page files reside - # :branch - String the repository branch to retrieve pages from + # :ref - String the repository ref to retrieve pages from # # Returns a fresh Gollum::Repo. def initialize(path, options = {}) @@ -132,7 +132,7 @@ module Gollum @file_class = options[:file_class] || self.class.file_class @markup_class = options[:markup_class] || self.class.markup_class @repo = @access.repo - @branch = options[:branch] || self.class.default_branch + @ref = options[:ref] || self.class.default_ref @sanitization = options[:sanitization] || self.class.sanitization @history_sanitization = options[:history_sanitization] || self.class.history_sanitization @@ -148,20 +148,20 @@ module Gollum # Public: Get the formatted page for a given page name. # # name - The human or canonical String page name of the wiki page. - # version - The String version ID to find (default: @branch). + # version - The String version ID to find (default: @ref). # # Returns a Gollum::Page or nil if no matching page was found. - def page(name, version = @branch) + def page(name, version = @ref) @page_class.new(self).find(name, version) end # Public: Get the static file for a given name. # # name - The full String pathname to the file. - # version - The String version ID to find (default: @branch). + # version - The String version ID to find (default: @ref). # # Returns a Gollum::File or nil if no matching file was found. - def file(name, version = @branch) + def file(name, version = @ref) @file_class.new(self).find(name, version) end @@ -390,11 +390,11 @@ module Gollum # Public: Lists all pages for this wiki. # - # treeish - The String commit ID or ref to find (default: @branch) + # treeish - The String commit ID or ref to find (default: @ref) # # Returns an Array of Gollum::Page instances. def pages(treeish = nil) - tree_list(treeish || @branch) + tree_list(treeish || @ref) end # Public: Returns the number of pages accessible from a commit @@ -403,7 +403,7 @@ module Gollum # # Returns a Fixnum def size(ref = nil) - tree_map_for(ref || @branch).inject(0) do |num, entry| + tree_map_for(ref || @ref).inject(0) do |num, entry| num + (@page_class.valid_page_name?(entry.name) ? 1 : 0) end rescue Grit::GitRuby::Repository::NoSuchShaFound @@ -416,7 +416,7 @@ module Gollum # # Returns an Array with Objects of page name and count of matches def search(query) - args = [{:c => query}, @branch, '--'] + args = [{:c => query}, @ref, '--'] args << '--' << @page_file_dir if @page_file_dir @repo.git.grep(*args).split("\n").map! do |line| @@ -438,7 +438,7 @@ module Gollum # # Returns an Array of Grit::Commit. def log(options = {}) - @repo.log(@branch, nil, log_pagination_options(options)) + @repo.log(@ref, nil, log_pagination_options(options)) end # Public: Refreshes just the cached Git reference data. This should @@ -556,12 +556,12 @@ module Gollum full_reverse_diff_for(nil, sha1, sha2) end - # Gets the default branch for the wiki. + # Gets the default ref for the wiki. # # Returns the String name. - def default_branch - @default_branch ||= \ - self.class.default_branch + def default_ref + @default_ref ||= \ + self.class.default_ref end # Gets the default name for commits. From 364b2b85aeb8980bd12daa601b088603866f09b5 Mon Sep 17 00:00:00 2001 From: Randy Merrill Date: Thu, 27 Jan 2011 12:46:29 -0800 Subject: [PATCH 4/8] Moving the `ref` to be an argument to the parents that defaults to the `@wiki.ref` --- lib/gollum/committer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gollum/committer.rb b/lib/gollum/committer.rb index fcf2a159..3f7df6d6 100644 --- a/lib/gollum/committer.rb +++ b/lib/gollum/committer.rb @@ -60,9 +60,9 @@ module Gollum # Public: The parent commits to this pending commit. # # Returns an array of Grit::Commit instances. - def parents + def parents(ref = @wiki.ref) @parents ||= begin - arr = [@options[:parent] || @wiki.repo.commit(@wiki.ref)] + arr = [@options[:parent] || @wiki.repo.commit(ref)] arr.flatten! arr.compact! arr From 7427ddd900db312d88a43ba4a94ebaefd4aa6f58 Mon Sep 17 00:00:00 2001 From: Randy Merrill Date: Thu, 27 Jan 2011 13:00:54 -0800 Subject: [PATCH 5/8] Adding some tests to test that the commiter can retrieve parents that are not the same as the wiki ref. Also testing that the default wiki ref works without passing a ref. --- test/test_committer.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test_committer.rb b/test/test_committer.rb index 68157844..bbfc4617 100644 --- a/test/test_committer.rb +++ b/test/test_committer.rb @@ -47,4 +47,16 @@ context "Wiki" do FileUtils.rm_rf(@path) end end + + test "parents with default wiki ref" do + ref = 'a8ad3c09dd842a3517085bfadd37718856dee813' + committer = Gollum::Committer.new(@wiki) + assert_equal ref, committer.parents.first.sha + end + + test "parents with custom ref" do + ref = '60f12f4254f58801b9ee7db7bca5fa8aeefaa56b' + committer = Gollum::Committer.new(@wiki) + assert_equal ref, committer.parents(ref).first.sha + end end \ No newline at end of file From e3950d52c1edca9815b51567dba95499b17be797 Mon Sep 17 00:00:00 2001 From: Randy Merrill Date: Thu, 27 Jan 2011 13:59:39 -0800 Subject: [PATCH 6/8] Removing the `ref` as an argument to the `Committer#parents` and just using the `@wiki.ref`. Updated the test to reflect the change and to test creating a wiki with a custom ref returns the correct parent for the `Committer#parents`. --- lib/gollum/committer.rb | 4 ++-- test/test_committer.rb | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/gollum/committer.rb b/lib/gollum/committer.rb index 3f7df6d6..68161f83 100644 --- a/lib/gollum/committer.rb +++ b/lib/gollum/committer.rb @@ -60,9 +60,9 @@ module Gollum # Public: The parent commits to this pending commit. # # Returns an array of Grit::Commit instances. - def parents(ref = @wiki.ref) + def parents() @parents ||= begin - arr = [@options[:parent] || @wiki.repo.commit(ref)] + arr = [@options[:parent] || @wiki.repo.commit(@wiki.ref)] arr.flatten! arr.compact! arr diff --git a/test/test_committer.rb b/test/test_committer.rb index bbfc4617..45842a01 100644 --- a/test/test_committer.rb +++ b/test/test_committer.rb @@ -48,7 +48,7 @@ context "Wiki" do end end - test "parents with default wiki ref" do + test "parents with default master ref" do ref = 'a8ad3c09dd842a3517085bfadd37718856dee813' committer = Gollum::Committer.new(@wiki) assert_equal ref, committer.parents.first.sha @@ -56,7 +56,8 @@ context "Wiki" do test "parents with custom ref" do ref = '60f12f4254f58801b9ee7db7bca5fa8aeefaa56b' + @wiki = Gollum::Wiki.new(testpath("examples/lotr.git"), :ref => ref) committer = Gollum::Committer.new(@wiki) - assert_equal ref, committer.parents(ref).first.sha + assert_equal ref, committer.parents.first.sha end end \ No newline at end of file From a1d760df1e0ac59a9303799115661ba59a1721be Mon Sep 17 00:00:00 2001 From: Tom Clark Date: Mon, 21 Feb 2011 23:32:27 -0500 Subject: [PATCH 7/8] Write pages to the proper branch --- lib/gollum/committer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gollum/committer.rb b/lib/gollum/committer.rb index 68161f83..2b40cd27 100644 --- a/lib/gollum/committer.rb +++ b/lib/gollum/committer.rb @@ -142,7 +142,7 @@ module Gollum # # Returns the String SHA1 of the new commit. def commit - sha1 = index.commit(@options[:message], parents, actor) + sha1 = index.commit(@options[:message], parents, actor, nil, @wiki.ref) @callbacks.each do |cb| cb.call(self, sha1) end From 8403868ac38a37e6a3451b2eb2c9952c7e66ae83 Mon Sep 17 00:00:00 2001 From: Tom Clark Date: Tue, 22 Feb 2011 00:12:32 -0500 Subject: [PATCH 8/8] Test that wikis can write to different branches in isolation --- test/test_wiki.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/test_wiki.rb b/test/test_wiki.rb index 4ac9aa4e..0227b6db 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -301,3 +301,34 @@ context "page_file_dir option" do FileUtils.rm_r(@path) end end + +context "Wiki page writing with different branch" do + setup do + @path = testpath("examples/test.git") + FileUtils.rm_rf(@path) + @repo = Grit::Repo.init_bare(@path) + @wiki = Gollum::Wiki.new(@path) + + # We need an initial commit to create the master branch + # before we can create new branches + cd = commit_details + @wiki.write_page("Gollum", :markdown, "# Gollum", cd) + + # Create our test branch and check it out + @repo.update_ref("test", @repo.commits.first.id) + @branch = Gollum::Wiki.new(@path, :ref => "test") + end + + test "write_page" do + cd = commit_details + + @branch.write_page("Bilbo", :markdown, "# Bilbo", commit_details) + assert @branch.page("Bilbo") + assert @wiki.page("Gollum") + + assert_equal 1, @wiki.repo.commits.size + assert_equal 1, @branch.repo.commits.size + + assert_equal nil, @wiki.page("Bilbo") + end +end \ No newline at end of file