From b87a7b8984b38b0e6f6b7106a508c07d3bf4db51 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Sun, 2 Feb 2014 14:53:16 +0000 Subject: [PATCH] scm: mercurial: insert long id to DB unless existing id in DB is not short id (#14361) git-svn-id: http://svn.redmine.org/redmine/trunk@12777 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/repository/mercurial.rb | 10 ++- test/unit/repository_mercurial_test.rb | 114 ++++++++++++++++++++++++- 2 files changed, 121 insertions(+), 3 deletions(-) diff --git a/app/models/repository/mercurial.rb b/app/models/repository/mercurial.rb index 36b73e920..f9339d800 100644 --- a/app/models/repository/mercurial.rb +++ b/app/models/repository/mercurial.rb @@ -122,9 +122,15 @@ class Repository::Mercurial < Repository all end + def is_short_id_in_db? + return @is_short_id_in_db unless @is_short_id_in_db.nil? + cs = changesets.first + @is_short_id_in_db = (!cs.nil? && cs.scmid.length != 40) + end + private :is_short_id_in_db? + def scmid_for_inserting_db(scmid) - # TODO: switch short or long by existing value in DB - scmid[0, 12] + is_short_id_in_db? ? scmid[0, 12] : scmid end def nodes_in_branch(rev, branch_limit) diff --git a/test/unit/repository_mercurial_test.rb b/test/unit/repository_mercurial_test.rb index 5dd4b7a0a..953986edb 100644 --- a/test/unit/repository_mercurial_test.rb +++ b/test/unit/repository_mercurial_test.rb @@ -104,12 +104,22 @@ class RepositoryMercurialTest < ActiveSupport::TestCase def test_entries_short_id assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count @repository.fetch_changesets @project.reload assert_equal NUM_REV, @repository.changesets.count assert_entries(true) end + def test_entries_long_id + assert_equal 0, @repository.changesets.count + @repository.fetch_changesets + @project.reload + assert_equal NUM_REV, @repository.changesets.count + assert_entries(false) + end + def test_entry_on_tip entry = @repository.entry assert_kind_of Redmine::Scm::Adapters::Entry, entry @@ -152,9 +162,16 @@ class RepositoryMercurialTest < ActiveSupport::TestCase private :assert_entry def test_entry_short_id + assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count assert_entry(true) end + def test_entry_long_id + assert_entry(false) + end + def test_fetch_changesets_from_scratch assert_equal 0, @repository.changesets.count @repository.fetch_changesets @@ -164,13 +181,39 @@ class RepositoryMercurialTest < ActiveSupport::TestCase rev0 = @repository.changesets.find_by_revision('0') assert_equal "Initial import.\nThe repository contains 3 files.", rev0.comments - assert_equal "0885933ad4f6", rev0.scmid + assert_equal "0885933ad4f68d77c2649cd11f8311276e7ef7ce", rev0.scmid first_rev = @repository.changesets.first last_rev = @repository.changesets.last assert_equal "#{NUM_REV - 1}", first_rev.revision assert_equal "0", last_rev.revision end + def test_fetch_changesets_keep_short_id + assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count + @repository.fetch_changesets + @project.reload + assert_equal NUM_REV, @repository.changesets.count + rev1 = @repository.changesets.find_by_revision('1') + assert_equal "9d5b5b004199", rev1.scmid + end + + def test_fetch_changesets_keep_long_id + assert_equal 0, @repository.changesets.count + Changeset.create!(:repository => @repository, + :committed_on => Time.now, + :revision => '0', + :scmid => '0885933ad4f68d77c2649cd11f8311276e7ef7ce', + :comments => 'test') + assert_equal 1, @repository.changesets.count + @repository.fetch_changesets + @project.reload + assert_equal NUM_REV, @repository.changesets.count + rev1 = @repository.changesets.find_by_revision('1') + assert_equal "9d5b5b00419901478496242e0768deba1ce8c51e", rev1.scmid + end + def test_fetch_changesets_incremental assert_equal 0, @repository.changesets.count @repository.fetch_changesets @@ -279,6 +322,16 @@ class RepositoryMercurialTest < ActiveSupport::TestCase assert_latest_changesets_tag end + def test_latest_changesets_tag_short_id + assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count + @repository.fetch_changesets + @project.reload + assert_equal NUM_REV, @repository.changesets.count + assert_latest_changesets_tag + end + def test_latest_changesets_tag_with_path assert_equal 0, @repository.changesets.count @repository.fetch_changesets @@ -331,6 +384,16 @@ class RepositoryMercurialTest < ActiveSupport::TestCase assert_latest_changesets_default_branch end + def test_latest_changesets_default_branch_short_id + assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count + @repository.fetch_changesets + @project.reload + assert_equal NUM_REV, @repository.changesets.count + assert_latest_changesets_default_branch + end + def assert_copied_files(is_short_scmid=true) cs1 = @repository.changesets.find_by_revision('13') assert_not_nil cs1 @@ -374,12 +437,22 @@ class RepositoryMercurialTest < ActiveSupport::TestCase def test_copied_files_short_id assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count @repository.fetch_changesets @project.reload assert_equal NUM_REV, @repository.changesets.count assert_copied_files(true) end + def test_copied_files_long_id + assert_equal 0, @repository.changesets.count + @repository.fetch_changesets + @project.reload + assert_equal NUM_REV, @repository.changesets.count + assert_copied_files(false) + end + def test_find_changeset_by_name assert_equal 0, @repository.changesets.count @repository.fetch_changesets @@ -459,12 +532,22 @@ class RepositoryMercurialTest < ActiveSupport::TestCase def test_parents_short_id assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count @repository.fetch_changesets @project.reload assert_equal NUM_REV, @repository.changesets.count assert_parents(true) end + def test_parents_long_id + assert_equal 0, @repository.changesets.count + @repository.fetch_changesets + @project.reload + assert_equal NUM_REV, @repository.changesets.count + assert_parents(false) + end + def test_activities c = Changeset.new(:repository => @repository, :committed_on => Time.now, @@ -523,6 +606,27 @@ class RepositoryMercurialTest < ActiveSupport::TestCase end end + def test_scmid_for_inserting_db_short_id + assert_equal 0, @repository.changesets.count + create_rev0_short_id + assert_equal 1, @repository.changesets.count + rev = "0123456789012345678901234567890123456789" + assert_equal 12, @repository.scmid_for_inserting_db(rev).length + end + + def test_scmid_for_inserting_db_long_id + rev = "0123456789012345678901234567890123456789" + assert_equal 0, @repository.changesets.count + assert_equal 40, @repository.scmid_for_inserting_db(rev).length + Changeset.create!(:repository => @repository, + :committed_on => Time.now, + :revision => '0', + :scmid => rev, + :comments => 'test') + assert_equal 1, @repository.changesets.count + assert_equal 40, @repository.scmid_for_inserting_db(rev).length + end + def test_scmid_for_assert rev = "0123456789012345678901234567890123456789" assert_equal rev, scmid_for_assert(rev, false) @@ -534,6 +638,14 @@ class RepositoryMercurialTest < ActiveSupport::TestCase def scmid_for_assert(hex, is_short=true) is_short ? hex[0, 12] : hex end + + def create_rev0_short_id + Changeset.create!(:repository => @repository, + :committed_on => Time.now, + :revision => '0', + :scmid => '0885933ad4f6', + :comments => 'test') + end else puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!" def test_fake; assert true end -- 2.39.5