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
This commit is contained in:
Toshi MARUYAMA 2014-02-02 14:53:16 +00:00
parent 85f15f694a
commit b87a7b8984
2 changed files with 121 additions and 3 deletions

View File

@ -122,9 +122,15 @@ class Repository::Mercurial < Repository
all all
end 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) def scmid_for_inserting_db(scmid)
# TODO: switch short or long by existing value in DB is_short_id_in_db? ? scmid[0, 12] : scmid
scmid[0, 12]
end end
def nodes_in_branch(rev, branch_limit) def nodes_in_branch(rev, branch_limit)

View File

@ -104,12 +104,22 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
def test_entries_short_id def test_entries_short_id
assert_equal 0, @repository.changesets.count assert_equal 0, @repository.changesets.count
create_rev0_short_id
assert_equal 1, @repository.changesets.count
@repository.fetch_changesets @repository.fetch_changesets
@project.reload @project.reload
assert_equal NUM_REV, @repository.changesets.count assert_equal NUM_REV, @repository.changesets.count
assert_entries(true) assert_entries(true)
end 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 def test_entry_on_tip
entry = @repository.entry entry = @repository.entry
assert_kind_of Redmine::Scm::Adapters::Entry, entry assert_kind_of Redmine::Scm::Adapters::Entry, entry
@ -152,9 +162,16 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
private :assert_entry private :assert_entry
def test_entry_short_id def test_entry_short_id
assert_equal 0, @repository.changesets.count
create_rev0_short_id
assert_equal 1, @repository.changesets.count
assert_entry(true) assert_entry(true)
end end
def test_entry_long_id
assert_entry(false)
end
def test_fetch_changesets_from_scratch def test_fetch_changesets_from_scratch
assert_equal 0, @repository.changesets.count assert_equal 0, @repository.changesets.count
@repository.fetch_changesets @repository.fetch_changesets
@ -164,13 +181,39 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
rev0 = @repository.changesets.find_by_revision('0') rev0 = @repository.changesets.find_by_revision('0')
assert_equal "Initial import.\nThe repository contains 3 files.", assert_equal "Initial import.\nThe repository contains 3 files.",
rev0.comments rev0.comments
assert_equal "0885933ad4f6", rev0.scmid assert_equal "0885933ad4f68d77c2649cd11f8311276e7ef7ce", rev0.scmid
first_rev = @repository.changesets.first first_rev = @repository.changesets.first
last_rev = @repository.changesets.last last_rev = @repository.changesets.last
assert_equal "#{NUM_REV - 1}", first_rev.revision assert_equal "#{NUM_REV - 1}", first_rev.revision
assert_equal "0", last_rev.revision assert_equal "0", last_rev.revision
end 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 def test_fetch_changesets_incremental
assert_equal 0, @repository.changesets.count assert_equal 0, @repository.changesets.count
@repository.fetch_changesets @repository.fetch_changesets
@ -279,6 +322,16 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
assert_latest_changesets_tag assert_latest_changesets_tag
end 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 def test_latest_changesets_tag_with_path
assert_equal 0, @repository.changesets.count assert_equal 0, @repository.changesets.count
@repository.fetch_changesets @repository.fetch_changesets
@ -331,6 +384,16 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
assert_latest_changesets_default_branch assert_latest_changesets_default_branch
end 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) def assert_copied_files(is_short_scmid=true)
cs1 = @repository.changesets.find_by_revision('13') cs1 = @repository.changesets.find_by_revision('13')
assert_not_nil cs1 assert_not_nil cs1
@ -374,12 +437,22 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
def test_copied_files_short_id def test_copied_files_short_id
assert_equal 0, @repository.changesets.count assert_equal 0, @repository.changesets.count
create_rev0_short_id
assert_equal 1, @repository.changesets.count
@repository.fetch_changesets @repository.fetch_changesets
@project.reload @project.reload
assert_equal NUM_REV, @repository.changesets.count assert_equal NUM_REV, @repository.changesets.count
assert_copied_files(true) assert_copied_files(true)
end 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 def test_find_changeset_by_name
assert_equal 0, @repository.changesets.count assert_equal 0, @repository.changesets.count
@repository.fetch_changesets @repository.fetch_changesets
@ -459,12 +532,22 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
def test_parents_short_id def test_parents_short_id
assert_equal 0, @repository.changesets.count assert_equal 0, @repository.changesets.count
create_rev0_short_id
assert_equal 1, @repository.changesets.count
@repository.fetch_changesets @repository.fetch_changesets
@project.reload @project.reload
assert_equal NUM_REV, @repository.changesets.count assert_equal NUM_REV, @repository.changesets.count
assert_parents(true) assert_parents(true)
end 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 def test_activities
c = Changeset.new(:repository => @repository, c = Changeset.new(:repository => @repository,
:committed_on => Time.now, :committed_on => Time.now,
@ -523,6 +606,27 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
end end
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 def test_scmid_for_assert
rev = "0123456789012345678901234567890123456789" rev = "0123456789012345678901234567890123456789"
assert_equal rev, scmid_for_assert(rev, false) assert_equal rev, scmid_for_assert(rev, false)
@ -534,6 +638,14 @@ class RepositoryMercurialTest < ActiveSupport::TestCase
def scmid_for_assert(hex, is_short=true) def scmid_for_assert(hex, is_short=true)
is_short ? hex[0, 12] : hex is_short ? hex[0, 12] : hex
end end
def create_rev0_short_id
Changeset.create!(:repository => @repository,
:committed_on => Time.now,
:revision => '0',
:scmid => '0885933ad4f6',
:comments => 'test')
end
else else
puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!" puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
def test_fake; assert true end def test_fake; assert true end