##// END OF EJS Templates
scm: git: reduce saving heads times in fetching revisions (#8857, #9472)...
Toshi MARUYAMA -
r9023:bcba955456c7
parent child
Show More
@@ -1,221 +1,233
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
4 4 #
5 5 # This program is free software; you can redistribute it and/or
6 6 # modify it under the terms of the GNU General Public License
7 7 # as published by the Free Software Foundation; either version 2
8 8 # of the License, or (at your option) any later version.
9 9 #
10 10 # This program is distributed in the hope that it will be useful,
11 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 # GNU General Public License for more details.
14 14 #
15 15 # You should have received a copy of the GNU General Public License
16 16 # along with this program; if not, write to the Free Software
17 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 18
19 19 require 'redmine/scm/adapters/git_adapter'
20 20
21 21 class Repository::Git < Repository
22 22 attr_protected :root_url
23 23 validates_presence_of :url
24 24
25 25 def self.human_attribute_name(attribute_key_name, *args)
26 26 attr_name = attribute_key_name.to_s
27 27 if attr_name == "url"
28 28 attr_name = "path_to_repository"
29 29 end
30 30 super(attr_name, *args)
31 31 end
32 32
33 33 def self.scm_adapter_class
34 34 Redmine::Scm::Adapters::GitAdapter
35 35 end
36 36
37 37 def self.scm_name
38 38 'Git'
39 39 end
40 40
41 41 def report_last_commit
42 42 extra_report_last_commit
43 43 end
44 44
45 45 def extra_report_last_commit
46 46 return false if extra_info.nil?
47 47 v = extra_info["extra_report_last_commit"]
48 48 return false if v.nil?
49 49 v.to_s != '0'
50 50 end
51 51
52 52 def supports_directory_revisions?
53 53 true
54 54 end
55 55
56 56 def supports_revision_graph?
57 57 true
58 58 end
59 59
60 60 def repo_log_encoding
61 61 'UTF-8'
62 62 end
63 63
64 64 # Returns the identifier for the given git changeset
65 65 def self.changeset_identifier(changeset)
66 66 changeset.scmid
67 67 end
68 68
69 69 # Returns the readable identifier for the given git changeset
70 70 def self.format_changeset_identifier(changeset)
71 71 changeset.revision[0, 8]
72 72 end
73 73
74 74 def branches
75 75 scm.branches
76 76 end
77 77
78 78 def tags
79 79 scm.tags
80 80 end
81 81
82 82 def default_branch
83 83 scm.default_branch
84 84 rescue Exception => e
85 85 logger.error "git: error during get default branch: #{e.message}"
86 86 nil
87 87 end
88 88
89 89 def find_changeset_by_name(name)
90 90 return nil if name.nil? || name.empty?
91 91 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
92 92 return e if e
93 93 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
94 94 end
95 95
96 96 def entries(path=nil, identifier=nil)
97 97 scm.entries(path,
98 98 identifier,
99 99 options = {:report_last_commit => extra_report_last_commit})
100 100 end
101 101
102 102 # With SCMs that have a sequential commit numbering,
103 103 # such as Subversion and Mercurial,
104 104 # Redmine is able to be clever and only fetch changesets
105 105 # going forward from the most recent one it knows about.
106 106 #
107 107 # However, Git does not have a sequential commit numbering.
108 108 #
109 109 # In order to fetch only new adding revisions,
110 110 # Redmine needs to parse revisions per branch.
111 111 # Branch "last_scmid" is for this requirement.
112 112 #
113 113 # In Git and Mercurial, revisions are not in date order.
114 114 # Redmine Mercurial fixed issues.
115 115 # * Redmine Takes Too Long On Large Mercurial Repository
116 116 # http://www.redmine.org/issues/3449
117 117 # * Sorting for changesets might go wrong on Mercurial repos
118 118 # http://www.redmine.org/issues/3567
119 119 #
120 120 # Database revision column is text, so Redmine can not sort by revision.
121 121 # Mercurial has revision number, and revision number guarantees revision order.
122 122 # Redmine Mercurial model stored revisions ordered by database id to database.
123 123 # So, Redmine Mercurial model can use correct ordering revisions.
124 124 #
125 125 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
126 126 # to get limited revisions from old to new.
127 127 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
128 128 #
129 129 # The repository can still be fully reloaded by calling #clear_changesets
130 130 # before fetching changesets (eg. for offline resync)
131 131 def fetch_changesets
132 132 scm_brs = branches
133 133 return if scm_brs.nil? || scm_brs.empty?
134 134 h1 = extra_info || {}
135 135 h = h1.dup
136 136 h["branches"] ||= {}
137 137 h["db_consistent"] ||= {}
138 138 if changesets.count == 0
139 139 h["db_consistent"]["ordering"] = 1
140 140 merge_extra_info(h)
141 141 self.save
142 142 elsif ! h["db_consistent"].has_key?("ordering")
143 143 h["db_consistent"]["ordering"] = 0
144 144 merge_extra_info(h)
145 145 self.save
146 146 end
147 147 save_revisions(h, scm_brs)
148 148 end
149 149
150 150 def save_revisions(h, scm_brs)
151 151 scm_brs.each do |br1|
152 152 br = br1.to_s
153 153 from_scmid = nil
154 154 from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
155 155 h["branches"][br] ||= {}
156 156 begin
157 cnt = 0
158 last_rev_scmid = nil
157 159 scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
160 cnt += 1
158 161 db_rev = find_changeset_by_name(rev.revision)
159 transaction do
160 if db_rev.nil?
162 if db_rev.nil?
163 transaction do
161 164 db_saved_rev = save_revision(rev)
162 165 parents = {}
163 166 parents[db_saved_rev] = rev.parents unless rev.parents.nil?
164 167 parents.each do |ch, chparents|
165 168 ch.parents = chparents.collect{|rp| find_changeset_by_name(rp)}.compact
166 169 end
167 170 end
168 h["branches"][br]["last_scmid"] = rev.scmid
171 end
172 last_rev_scmid = rev.scmid
173 if cnt > 100
174 cnt = 0
175 h["branches"][br]["last_scmid"] = last_rev_scmid
169 176 merge_extra_info(h)
170 177 self.save
171 178 end
172 179 end
180 unless last_rev_scmid.nil?
181 h["branches"][br]["last_scmid"] = last_rev_scmid
182 merge_extra_info(h)
183 self.save
184 end
173 185 rescue Redmine::Scm::Adapters::CommandFailed => e
174 186 logger.error("save revisions error: #{e.message}")
175 187 end
176 188 end
177 189 end
178 190 private :save_revisions
179 191
180 192 def save_revision(rev)
181 193 changeset = Changeset.new(
182 194 :repository => self,
183 195 :revision => rev.identifier,
184 196 :scmid => rev.scmid,
185 197 :committer => rev.author,
186 198 :committed_on => rev.time,
187 199 :comments => rev.message
188 200 )
189 201 if changeset.save
190 202 rev.paths.each do |file|
191 203 Change.create(
192 204 :changeset => changeset,
193 205 :action => file[:action],
194 206 :path => file[:path])
195 207 end
196 208 end
197 209 changeset
198 210 end
199 211 private :save_revision
200 212
201 213 def heads_from_branches_hash
202 214 h1 = extra_info || {}
203 215 h = h1.dup
204 216 h["branches"] ||= {}
205 217 h['branches'].map{|br, hs| hs['last_scmid']}
206 218 end
207 219
208 220 def latest_changesets(path,rev,limit=10)
209 221 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
210 222 return [] if revisions.nil? || revisions.empty?
211 223
212 224 changesets.find(
213 225 :all,
214 226 :conditions => [
215 227 "scmid IN (?)",
216 228 revisions.map!{|c| c.scmid}
217 229 ],
218 230 :order => 'committed_on DESC'
219 231 )
220 232 end
221 233 end
General Comments 0
You need to be logged in to leave comments. Login now