From e2df831b537c6a038a799e236cc497a59d3a79e7 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Sat, 8 Jan 2011 06:24:27 +0000 Subject: [PATCH] scm: mercurial: accept both of revision and nodeid as changeset id (#3724). Contributed by Yuya Nishihara. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4654 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/repository/mercurial.rb | 11 +++++++++++ .../scm/adapters/mercurial_adapter_test.rb | 11 +++++++++++ test/unit/repository_mercurial_test.rb | 19 ++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/app/models/repository/mercurial.rb b/app/models/repository/mercurial.rb index 9ce3b63eb..8e2ed4517 100644 --- a/app/models/repository/mercurial.rb +++ b/app/models/repository/mercurial.rb @@ -55,6 +55,17 @@ class Repository::Mercurial < Repository entries end + # Finds and returns a revision with a number or the beginning of a hash + def find_changeset_by_name(name) + if /[^\d]/ =~ name or name.to_s.size > 8 + e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s]) + else + e = changesets.find(:first, :conditions => ['revision = ?', name.to_s]) + end + return e if e + changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch + end + # Returns the latest changesets for +path+; sorted by revision number def latest_changesets(path, rev, limit=10) if path.blank? diff --git a/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb b/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb index 642415812..db050c836 100644 --- a/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb +++ b/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb @@ -47,6 +47,17 @@ begin assert_nil @adapter.cat("sources/welcome_controller.rb") end + def test_access_by_nodeid + path = 'sources/welcome_controller.rb' + assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400bb8672109') + end + + def test_access_by_fuzzy_nodeid + path = 'sources/welcome_controller.rb' + # falls back to nodeid + assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400') + end + private def test_hgversion_for(hgversion, version) diff --git a/test/unit/repository_mercurial_test.rb b/test/unit/repository_mercurial_test.rb index d8daf0f9a..37ef1a7ed 100644 --- a/test/unit/repository_mercurial_test.rb +++ b/test/unit/repository_mercurial_test.rb @@ -35,7 +35,8 @@ class RepositoryMercurialTest < ActiveSupport::TestCase assert_equal 17, @repository.changesets.count assert_equal 25, @repository.changes.count - assert_equal "Initial import.\nThe repository contains 3 files.", @repository.changesets.find_by_revision('0').comments + assert_equal "Initial import.\nThe repository contains 3 files.", + @repository.changesets.find_by_revision('0').comments end def test_fetch_changesets_incremental @@ -51,7 +52,9 @@ class RepositoryMercurialTest < ActiveSupport::TestCase def test_entries assert_equal 2, @repository.entries("sources", 2).size + assert_equal 2, @repository.entries("sources", '400bb8672109').size assert_equal 1, @repository.entries("sources", 3).size + assert_equal 1, @repository.entries("sources", 'b3a615152df8').size end def test_locate_on_outdated_repository @@ -122,6 +125,20 @@ class RepositoryMercurialTest < ActiveSupport::TestCase assert_equal '/README (1)[2]&,%.-3_4', c2[0].path assert_equal '/README', c2[0].from_path end + + def test_find_changeset_by_name + @repository.fetch_changesets + @repository.reload + %w|2 400bb8672109 400|.each do |r| + assert_equal @repository.find_changeset_by_name(r).revision, '2' + end + end + + def test_find_changeset_by_invalid_name + @repository.fetch_changesets + @repository.reload + assert_nil @repository.find_changeset_by_name('100000') + end else puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!" def test_fake; assert true end -- 2.39.5