From 88c624877e83d619d92956cb1d865d2e8c6515d1 Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Wed, 18 Sep 2019 18:10:09 +0200 Subject: [PATCH] Extract page dir from latest changes. Fix #1388 (#1395) * Strip off page_dir prefix from latest changes urls * Rename spurious next and previous_link * New format for LatestChanges#versions: detect renames --- lib/gollum/helpers.rb | 1 - lib/gollum/templates/latest_changes.mustache | 4 +- lib/gollum/views/helpers.rb | 13 ++++ lib/gollum/views/latest_changes.rb | 18 ++---- lib/gollum/views/layout.rb | 1 + test/helper.rb | 6 +- test/test_latest_changes_view.rb | 67 ++++++++++++++++---- 7 files changed, 81 insertions(+), 29 deletions(-) diff --git a/lib/gollum/helpers.rb b/lib/gollum/helpers.rb index 316ecd87..d0dad08e 100644 --- a/lib/gollum/helpers.rb +++ b/lib/gollum/helpers.rb @@ -41,6 +41,5 @@ module Precious fail ArgumentError, "emoji `#{name}' not found" end end - end end diff --git a/lib/gollum/templates/latest_changes.mustache b/lib/gollum/templates/latest_changes.mustache index 0946c18c..79bee321 100644 --- a/lib/gollum/templates/latest_changes.mustache +++ b/lib/gollum/templates/latest_changes.mustache @@ -15,8 +15,8 @@ {{date}} {{message}}
{{#files}} - {{file}}
- {{/files}} + {{#renamed}}{{renamed}} -> {{/renamed}}{{file}}
+ {{/files}}
[{{id7}}] diff --git a/lib/gollum/views/helpers.rb b/lib/gollum/views/helpers.rb index 854fe8d4..29c44ba4 100644 --- a/lib/gollum/views/helpers.rb +++ b/lib/gollum/views/helpers.rb @@ -2,6 +2,15 @@ require 'json' module Precious module Views + + module AppHelpers + def extract_page_dir(path) + return path unless @page_dir + @path_to_extract ||= "#{Pathname.new(@page_dir).cleanpath}/" + path.start_with?(@path_to_extract) ? path.slice(@path_to_extract.length, path.length) : path + end + end + module RouteHelpers ROUTES = { 'gollum' => { @@ -43,6 +52,10 @@ module Precious @@route_methods.to_json end end + + def page_route(page) + "#{base_url}/#{page}".gsub(/\/{2,}/, '/') # remove double slashes + end end module OcticonHelpers diff --git a/lib/gollum/views/latest_changes.rb b/lib/gollum/views/latest_changes.rb index ec92bf6a..170097ce 100644 --- a/lib/gollum/views/latest_changes.rb +++ b/lib/gollum/views/latest_changes.rb @@ -22,25 +22,17 @@ module Precious :date => v.authored_date.strftime("%B %d, %Y"), :user_icon => self.user_icon_code(v.author.email), :date_full => v.authored_date, - :files => v.stats.files.map { |f,*rest| - page_path = extract_renamed_path_destination(f) - { :file => f, - :link => "#{page_path}/#{v.id}" + :files => v.stats.files.map { |f| + new_path = extract_page_dir(f[:new_file]) + { :file => new_path, + :link => "#{page_route(new_path)}/#{v.id}", + :renamed => f[:old_file] ? extract_page_dir(f[:old_file]) : nil } } } end end - def extract_renamed_path_destination(file) - return file.gsub(/{.* => (.*)}/, '\1').gsub(/.* => (.*)/, '\1') - end - - def previous_link - end - - def next_link - end end end end diff --git a/lib/gollum/views/layout.rb b/lib/gollum/views/layout.rb index cfc63188..718c9172 100644 --- a/lib/gollum/views/layout.rb +++ b/lib/gollum/views/layout.rb @@ -5,6 +5,7 @@ module Precious class Layout < Mustache include Rack::Utils include Sprockets::Helpers + include Precious::Views::AppHelpers include Precious::Views::SprocketsHelpers include Precious::Views::RouteHelpers include Precious::Views::OcticonHelpers diff --git a/test/helper.rb b/test/helper.rb index 228f7369..556faac4 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -19,7 +19,11 @@ $LOAD_PATH.unshift(dir) module Gollum end -Gollum::GIT_ADAPTER = ENV['GIT_ADAPTER'] if ENV['GIT_ADAPTER'] +if ENV['GIT_ADAPTER'] + Gollum::GIT_ADAPTER = ENV['GIT_ADAPTER'] +else + Gollum::GIT_ADAPTER = RUBY_PLATFORM == 'java' ? 'rjgit' : 'rugged' +end ENV['RACK_ENV'] = 'test' require 'gollum' diff --git a/test/test_latest_changes_view.rb b/test/test_latest_changes_view.rb index bf606714..4704c793 100644 --- a/test/test_latest_changes_view.rb +++ b/test/test_latest_changes_view.rb @@ -2,7 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) require File.expand_path '../../lib/gollum/views/latest_changes', __FILE__ -context "Precious::Views::LatestChanges" do +context 'Precious::Views::LatestChanges' do include Rack::Test::Methods def app @@ -10,13 +10,27 @@ context "Precious::Views::LatestChanges" do end setup do - @path = cloned_testpath("examples/lotr.git") + @path = cloned_testpath('examples/lotr.git') @wiki = Gollum::Wiki.new(@path) @url = '/gollum/latest_changes' Precious::App.set(:gollum_path, @path) Precious::App.set(:wiki_options, {:pagination_count => 10}) end + test 'rename detection' do + versions = [OpenStruct.new( + :id => 'fake', + :author => OpenStruct.new(:email => 'fake', :name => 'fake'), + :message => 'fake', + :authored_date => Time.now, + :stats => OpenStruct.new(:files => [{:new_file => 'new_path', :old_file => 'old_path', :new_additions => 1, :new_deletions => 1, :changes => 1}]) + )] + view = Precious::Views::LatestChanges.new + view.instance_variable_set(:@versions, versions) + view.instance_variable_set(:@request, OpenStruct.new(:host => 'fake')) # dummy to generate gravatar icon + assert_equal view.versions[0][:files][0][:renamed], 'old_path' + end + test "displays_latest_changes" do get(@url) body = last_response.body @@ -24,10 +38,10 @@ context "Precious::Views::LatestChanges" do assert body.include?("Charles Pence"), "/latest_changes should include Author Charles Pence" assert body.include?('1db89eb'), "/latest_changes should include the :pagination_count commit" assert !body.include?('a8ad3c0'), "/latest_changes should not include more than :pagination_count commits" - assert body.include?('Data-Two.csv'), "/latest_changes include links to modified files in #{body}" - assert body.include?('Hobbit.md'), "/latest_changes should include links to modified pages in #{body}" + assert body.include?('Data-Two.csv'), "/latest_changes include links to modified files in #{body}" + assert body.include?('Hobbit.md'), "/latest_changes should include links to modified pages in #{body}" end - + test 'gravatar' do Precious::App.set(:wiki_options, {:user_icons => 'gravatar'}) get @url @@ -42,13 +56,42 @@ context "Precious::Views::LatestChanges" do Precious::App.set(:wiki_options, {:user_icons => 'none'}) end - test "extract destination file name in case of path renaming" do - view = Precious::Views::LatestChanges.new - assert_equal "newname.md", view.extract_renamed_path_destination("oldname.md => newname.md") - assert_equal "newDirectoryName/fileName.md", view.extract_renamed_path_destination("{oldDirectoryName => newDirectoryName}/fileName.md") - end - teardown do FileUtils.rm_rf(@path) end -end \ No newline at end of file +end + +context 'Latest changes with page-file-dir' do + include Rack::Test::Methods + + def app + Precious::App + end + + setup do + @path = cloned_testpath('examples/lotr.git') + @wiki = Gollum::Wiki.new(@path) + Precious::App.set(:gollum_path, @path) + Precious::App.set(:wiki_options, { :page_file_dir => 'Rivendell'}) + end + + test 'latest changes respects page_file_dir' do + get('/gollum/latest_changes') + body = last_response.body + + assert !body.include?('Hobbit.md'), 'latest changes with page_file_dir should not log changes to files in root' + assert body.include?('Elrond.md'), 'latest changes should log changes in page_file_dir' + end + + test 'latest chages should strip off page_file_dir' do + get('/gollum/latest_changes') + body = last_response.body + assert_equal body.include?(' nil}) + end +end