##// END OF EJS Templates
scm: mercurial: refactor Repository::Mercurial#fetch_changesets (#4455)....
Toshi MARUYAMA -
r4729:5274230fda69
parent child
Show More
@@ -1,131 +1,119
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'redmine/scm/adapters/mercurial_adapter'
18 require 'redmine/scm/adapters/mercurial_adapter'
19
19
20 class Repository::Mercurial < Repository
20 class Repository::Mercurial < Repository
21 # sort changesets by revision number
21 # sort changesets by revision number
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
23
23
24 attr_protected :root_url
24 attr_protected :root_url
25 validates_presence_of :url
25 validates_presence_of :url
26
26
27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
28
27 def self.scm_adapter_class
29 def self.scm_adapter_class
28 Redmine::Scm::Adapters::MercurialAdapter
30 Redmine::Scm::Adapters::MercurialAdapter
29 end
31 end
30
32
31 def self.scm_name
33 def self.scm_name
32 'Mercurial'
34 'Mercurial'
33 end
35 end
34
36
35 # Returns the readable identifier for the given mercurial changeset
37 # Returns the readable identifier for the given mercurial changeset
36 def self.format_changeset_identifier(changeset)
38 def self.format_changeset_identifier(changeset)
37 "#{changeset.revision}:#{changeset.scmid}"
39 "#{changeset.revision}:#{changeset.scmid}"
38 end
40 end
39
41
40 # Returns the identifier for the given Mercurial changeset
42 # Returns the identifier for the given Mercurial changeset
41 def self.changeset_identifier(changeset)
43 def self.changeset_identifier(changeset)
42 changeset.scmid
44 changeset.scmid
43 end
45 end
44
46
45 def diff_format_revisions(cs, cs_to, sep=':')
47 def diff_format_revisions(cs, cs_to, sep=':')
46 super(cs, cs_to, ' ')
48 super(cs, cs_to, ' ')
47 end
49 end
48
50
49 def entries(path=nil, identifier=nil)
51 def entries(path=nil, identifier=nil)
50 entries=scm.entries(path, identifier)
52 entries=scm.entries(path, identifier)
51 if entries
53 if entries
52 entries.each do |entry|
54 entries.each do |entry|
53 next unless entry.is_file?
55 next unless entry.is_file?
54 # Set the filesize unless browsing a specific revision
56 # Set the filesize unless browsing a specific revision
55 if identifier.nil?
57 if identifier.nil?
56 full_path = File.join(root_url, entry.path)
58 full_path = File.join(root_url, entry.path)
57 entry.size = File.stat(full_path).size if File.file?(full_path)
59 entry.size = File.stat(full_path).size if File.file?(full_path)
58 end
60 end
59 # Search the DB for the entry's last change
61 # Search the DB for the entry's last change
60 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
62 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
61 if change
63 if change
62 entry.lastrev.identifier = change.changeset.revision
64 entry.lastrev.identifier = change.changeset.revision
63 entry.lastrev.name = change.changeset.revision
65 entry.lastrev.name = change.changeset.revision
64 entry.lastrev.author = change.changeset.committer
66 entry.lastrev.author = change.changeset.committer
65 entry.lastrev.revision = change.revision
67 entry.lastrev.revision = change.revision
66 end
68 end
67 end
69 end
68 end
70 end
69 entries
71 entries
70 end
72 end
71
73
72 # Finds and returns a revision with a number or the beginning of a hash
74 # Finds and returns a revision with a number or the beginning of a hash
73 def find_changeset_by_name(name)
75 def find_changeset_by_name(name)
74 return nil if name.nil? || name.empty?
76 return nil if name.nil? || name.empty?
75 if /[^\d]/ =~ name or name.to_s.size > 8
77 if /[^\d]/ =~ name or name.to_s.size > 8
76 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
78 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
77 else
79 else
78 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
80 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
79 end
81 end
80 return e if e
82 return e if e
81 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
83 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
82 end
84 end
83
85
84 # Returns the latest changesets for +path+; sorted by revision number
86 # Returns the latest changesets for +path+; sorted by revision number
85 def latest_changesets(path, rev, limit=10)
87 def latest_changesets(path, rev, limit=10)
86 if path.blank?
88 if path.blank?
87 changesets.find(:all, :include => :user, :limit => limit)
89 changesets.find(:all, :include => :user, :limit => limit)
88 else
90 else
89 changes.find(:all, :include => {:changeset => :user},
91 changes.find(:all, :include => {:changeset => :user},
90 :conditions => ["path = ?", path.with_leading_slash],
92 :conditions => ["path = ?", path.with_leading_slash],
91 :order => "#{Changeset.table_name}.id DESC",
93 :order => "#{Changeset.table_name}.id DESC",
92 :limit => limit).collect(&:changeset)
94 :limit => limit).collect(&:changeset)
93 end
95 end
94 end
96 end
95
97
96 def fetch_changesets
98 def fetch_changesets
97 scm_info = scm.info
99 scm_rev = scm.info.lastrev.revision.to_i
98 if scm_info
100 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
99 # latest revision found in database
101 return unless db_rev < scm_rev # already up-to-date
100 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
102
101 # latest revision in the repository
103 logger.debug "Fetching changesets for repository #{url}" if logger
102 latest_revision = scm_info.lastrev
104 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
103 return if latest_revision.nil?
105 transaction do
104 scm_revision = latest_revision.identifier.to_i
106 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
105 if db_revision < scm_revision
107 cs = Changeset.create(:repository => self,
106 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
108 :revision => re.revision,
107 identifier_from = db_revision + 1
109 :scmid => re.scmid,
108 while (identifier_from <= scm_revision)
110 :committer => re.author,
109 # loads changesets by batches of 100
111 :committed_on => re.time,
110 identifier_to = [identifier_from + 99, scm_revision].min
112 :comments => re.message)
111 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
113 re.paths.each { |e| cs.create_change(e) }
112 transaction do
113 revisions.each do |revision|
114 changeset = Changeset.create(:repository => self,
115 :revision => revision.revision,
116 :scmid => revision.scmid,
117 :committer => revision.author,
118 :committed_on => revision.time,
119 :comments => revision.message)
120
121 revision.paths.each do |change|
122 changeset.create_change(change)
123 end
124 end
125 end unless revisions.nil?
126 identifier_from = identifier_to + 1
127 end
114 end
128 end
115 end
129 end
116 end
117 self
130 end
118 end
131 end
119 end
General Comments 0
You need to be logged in to leave comments. Login now