comparison app/models/repository/mercurial.rb @ 3:7c48bad7d85d yuya

* Import Mercurial overhaul patches from Yuya Nishihara (see http://www.redmine.org/issues/4455)
author Chris Cannam
date Wed, 28 Jul 2010 12:40:01 +0100
parents 513646585e45
children de76cd3e8c8e b859cc0c4fa1 6056b3c5f8f2
comparison
equal deleted inserted replaced
2:b940d200fbd2 3:7c48bad7d85d
19 19
20 class Repository::Mercurial < Repository 20 class Repository::Mercurial < Repository
21 attr_protected :root_url 21 attr_protected :root_url
22 validates_presence_of :url 22 validates_presence_of :url
23 23
24 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
25
24 def scm_adapter 26 def scm_adapter
25 Redmine::Scm::Adapters::MercurialAdapter 27 Redmine::Scm::Adapters::MercurialAdapter
26 end 28 end
27 29
28 def self.scm_name 30 def self.scm_name
29 'Mercurial' 31 'Mercurial'
30 end 32 end
31 33
32 def entries(path=nil, identifier=nil) 34 def entries(path=nil, identifier=nil)
33 entries=scm.entries(path, identifier) 35 scm.entries(path, identifier)
34 if entries 36 end
35 entries.each do |entry| 37
36 next unless entry.is_file? 38 def branches
37 # Set the filesize unless browsing a specific revision 39 bras = scm.branches
38 if identifier.nil? 40 bras.sort unless bras == %w|default|
39 full_path = File.join(root_url, entry.path) 41 end
40 entry.size = File.stat(full_path).size if File.file?(full_path) 42
41 end 43 # Returns the latest changesets for +path+
42 # Search the DB for the entry's last change 44 def latest_changesets(path, rev, limit=10)
43 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC") 45 changesets.find(:all, :include => :user,
44 if change 46 :conditions => latest_changesets_cond(path, rev, limit),
45 entry.lastrev.identifier = change.changeset.revision 47 :limit => limit)
46 entry.lastrev.name = change.changeset.revision 48 end
47 entry.lastrev.author = change.changeset.committer 49
48 entry.lastrev.revision = change.revision 50 def latest_changesets_cond(path, rev, limit)
51 cond, args = [], []
52
53 if scm.branchmap.member? rev
54 # dirty hack to filter by branch. branch name should be in database.
55 cond << "#{Changeset.table_name}.scmid IN (?)"
56 args << scm.nodes_in_branch(rev, path, rev, 0, :limit => limit)
57 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
58 cond << "#{Changeset.table_name}.id <= ?"
59 args << last.id
60 end
61
62 unless path.blank?
63 # TODO: there must be a better way to build sub-query
64 cond << "EXISTS (SELECT * FROM #{Change.table_name}
65 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
66 AND (#{Change.table_name}.path = ? OR #{Change.table_name}.path LIKE ?))"
67 args << path.with_leading_slash << "#{path.with_leading_slash}/%"
68 end
69
70 [cond.join(' AND '), *args] unless cond.empty?
71 end
72 private :latest_changesets_cond
73
74 def fetch_changesets
75 scm_rev = scm.info.lastrev.revision.to_i
76 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
77 return unless db_rev < scm_rev # already up-to-date
78
79 logger.debug "Fetching changesets for repository #{url}" if logger
80 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
81 transaction do
82 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
83 cs = Changeset.create(:repository => self,
84 :revision => re.revision,
85 :scmid => re.scmid,
86 :committer => re.author,
87 :committed_on => re.time,
88 :comments => re.message)
89 re.paths.each { |e| cs.create_change(e) }
49 end 90 end
50 end 91 end
51 end 92 end
52 entries 93 self
53 end
54
55 def fetch_changesets
56 scm_info = scm.info
57 if scm_info
58 # latest revision found in database
59 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
60 # latest revision in the repository
61 latest_revision = scm_info.lastrev
62 return if latest_revision.nil?
63 scm_revision = latest_revision.identifier.to_i
64 if db_revision < scm_revision
65 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
66 identifier_from = db_revision + 1
67 while (identifier_from <= scm_revision)
68 # loads changesets by batches of 100
69 identifier_to = [identifier_from + 99, scm_revision].min
70 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
71 transaction do
72 revisions.each do |revision|
73 changeset = Changeset.create(:repository => self,
74 :revision => revision.identifier,
75 :scmid => revision.scmid,
76 :committer => revision.author,
77 :committed_on => revision.time,
78 :comments => revision.message)
79
80 revision.paths.each do |change|
81 changeset.create_change(change)
82 end
83 end
84 end unless revisions.nil?
85 identifier_from = identifier_to + 1
86 end
87 end
88 end
89 end 94 end
90 end 95 end