scm: git: fix fetching 7 days problem (#7146, #6013).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5788 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2011-05-13 10:38:45 +00:00
parent b0f5d3cd88
commit 7ca5a4939e
2 changed files with 51 additions and 47 deletions

View File

@ -103,39 +103,26 @@ class Repository::Git < Repository
#
# But, Git 1.7.3.4 does not support --reverse with -n or --skip.
#
# With SCM's that have a sequential commit numbering, redmine is able to be
# clever and only fetch changesets going forward from the most recent one
# it knows about.
# However, with git, you never know if people have merged
# commits into the middle of the repository history, so we should parse
# the entire log.
#
# Since it's way too slow for large repositories,
# we only parse 1 week before the last known commit.
#
# The repository can still be fully reloaded by calling #clear_changesets
# before fetching changesets (eg. for offline resync)
def fetch_changesets
c = changesets.find(:first, :order => 'committed_on DESC')
since = (c ? c.committed_on - 7.days : nil)
revisions = scm.revisions('', nil, nil, {:all => true, :since => since, :reverse => true})
return if revisions.nil? || revisions.empty?
recent_changesets = changesets.find(:all, :conditions => ['committed_on >= ?', since])
# Clean out revisions that are no longer in git
recent_changesets.each {|c| c.destroy unless revisions.detect {|r| r.scmid.to_s == c.scmid.to_s }}
# Subtract revisions that redmine already knows about
recent_revisions = recent_changesets.map{|c| c.scmid}
revisions.reject!{|r| recent_revisions.include?(r.scmid)}
# Save the remaining ones to the database
unless revisions.nil?
revisions.each do |rev|
scm_brs = branches
return if scm_brs.nil? || scm_brs.empty?
h = extra_info || {}
h["branches"] ||= {}
scm_brs.each do |br|
from_scmid = nil
from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
h["branches"][br] ||= {}
scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
db_rev = find_changeset_by_name(rev.revision)
transaction do
save_revision(rev)
if db_rev.nil?
save_revision(rev)
end
h["branches"][br]["last_scmid"] = rev.scmid
merge_extra_info(h)
self.save
end
end
end

View File

@ -55,6 +55,8 @@ class RepositoryGitTest < ActiveSupport::TestCase
end
def test_fetch_changesets_from_scratch
assert_nil @repository.extra_info
@repository.fetch_changesets
@repository.reload
@ -74,30 +76,45 @@ class RepositoryGitTest < ActiveSupport::TestCase
change = commit.changes.sort_by(&:path).first
assert_equal "README", change.path
assert_equal "A", change.action
assert_equal 4, @repository.extra_info["branches"].size
end
def test_fetch_changesets_incremental
@repository.fetch_changesets
@repository.reload
assert_equal 21, @repository.changesets.count
assert_equal 33, @repository.changes.count
extra_info_db = @repository.extra_info["branches"]
assert_equal 4, extra_info_db.size
assert_equal "1ca7f5ed374f3cb31a93ae5215c2e25cc6ec5127",
extra_info_db["latin-1-path-encoding"]["last_scmid"]
assert_equal "83ca5fd546063a3c7dc2e568ba3355661a9e2b2c",
extra_info_db["master"]["last_scmid"]
# Remove the latest changesets
@repository.changesets.find(
:all,
:order => 'committed_on DESC',
:limit => 8).each(&:destroy)
del_revs = [
"83ca5fd546063a3c7dc2e568ba3355661a9e2b2c",
"ed5bb786bbda2dee66a2d50faf51429dbc043a7b",
"4f26664364207fa8b1af9f8722647ab2d4ac5d43",
"deff712f05a90d96edbd70facc47d944be5897e3",
"32ae898b720c2f7eec2723d5bdd558b4cb2d3ddf",
"7e61ac704deecde634b51e59daa8110435dcb3da",
]
@repository.changesets.each do |rev|
rev.destroy if del_revs.detect {|r| r == rev.scmid.to_s }
end
@repository.reload
cs1 = @repository.changesets
assert_equal 13, cs1.count
rev_a_commit = @repository.changesets.find(
:first,
:order => 'committed_on DESC')
assert_equal '4f26664364207fa8b1af9f8722647ab2d4ac5d43', rev_a_commit.revision
# Mon Jul 5 22:34:26 2010 +0200
rev_a_committed_on = Time.gm(2010, 7, 5, 20, 34, 26)
assert_equal '4f26664364207fa8b1af9f8722647ab2d4ac5d43', rev_a_commit.scmid
assert_equal rev_a_committed_on, rev_a_commit.committed_on
latest_rev = @repository.latest_changeset
assert_equal rev_a_committed_on, latest_rev.committed_on
assert_equal 15, cs1.count
h = @repository.extra_info.dup
h["branches"]["master"]["last_scmid"] =
"4a07fe31bffcf2888791f3e6cbc9c4545cefe3e8"
@repository.merge_extra_info(h)
@repository.save
@repository.reload
extra_info_db_1 = @repository.extra_info["branches"]
assert_equal "4a07fe31bffcf2888791f3e6cbc9c4545cefe3e8",
extra_info_db_1["master"]["last_scmid"]
@repository.fetch_changesets
assert_equal 21, @repository.changesets.count
@ -318,7 +335,7 @@ class RepositoryGitTest < ActiveSupport::TestCase
def test_next_nil
@repository.fetch_changesets
@repository.reload
%w|1ca7f5ed374f3cb31a93ae5215c2e25cc6ec5127 1ca7f5ed|.each do |r1|
%w|67e7792ce20ccae2e4bb73eed09bb397819c8834 67e7792ce20cca|.each do |r1|
changeset = @repository.find_changeset_by_name(r1)
assert_nil changeset.next
end