##// END OF EJS Templates
remove unneeded Relation#all from Repository::Git model...
Toshi MARUYAMA -
r12468:619857e1a4ac
parent child
Show More
@@ -1,258 +1,258
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 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 if name.present?
91 91 changesets.where(:revision => name.to_s).first ||
92 92 changesets.where('scmid LIKE ?', "#{name}%").first
93 93 end
94 94 end
95 95
96 96 def entries(path=nil, identifier=nil)
97 97 entries = scm.entries(path, identifier, :report_last_commit => extra_report_last_commit)
98 98 load_entries_changesets(entries)
99 99 entries
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 save "heads".
111 111 #
112 112 # In Git and Mercurial, revisions are not in date order.
113 113 # Redmine Mercurial fixed issues.
114 114 # * Redmine Takes Too Long On Large Mercurial Repository
115 115 # http://www.redmine.org/issues/3449
116 116 # * Sorting for changesets might go wrong on Mercurial repos
117 117 # http://www.redmine.org/issues/3567
118 118 #
119 119 # Database revision column is text, so Redmine can not sort by revision.
120 120 # Mercurial has revision number, and revision number guarantees revision order.
121 121 # Redmine Mercurial model stored revisions ordered by database id to database.
122 122 # So, Redmine Mercurial model can use correct ordering revisions.
123 123 #
124 124 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
125 125 # to get limited revisions from old to new.
126 126 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
127 127 #
128 128 # The repository can still be fully reloaded by calling #clear_changesets
129 129 # before fetching changesets (eg. for offline resync)
130 130 def fetch_changesets
131 131 scm_brs = branches
132 132 return if scm_brs.nil? || scm_brs.empty?
133 133
134 134 h1 = extra_info || {}
135 135 h = h1.dup
136 136 repo_heads = scm_brs.map{ |br| br.scmid }
137 137 h["heads"] ||= []
138 138 prev_db_heads = h["heads"].dup
139 139 if prev_db_heads.empty?
140 140 prev_db_heads += heads_from_branches_hash
141 141 end
142 142 return if prev_db_heads.sort == repo_heads.sort
143 143
144 144 h["db_consistent"] ||= {}
145 145 if changesets.count == 0
146 146 h["db_consistent"]["ordering"] = 1
147 147 merge_extra_info(h)
148 148 self.save
149 149 elsif ! h["db_consistent"].has_key?("ordering")
150 150 h["db_consistent"]["ordering"] = 0
151 151 merge_extra_info(h)
152 152 self.save
153 153 end
154 154 save_revisions(prev_db_heads, repo_heads)
155 155 end
156 156
157 157 def save_revisions(prev_db_heads, repo_heads)
158 158 h = {}
159 159 opts = {}
160 160 opts[:reverse] = true
161 161 opts[:excludes] = prev_db_heads
162 162 opts[:includes] = repo_heads
163 163
164 164 revisions = scm.revisions('', nil, nil, opts)
165 165 return if revisions.blank?
166 166
167 167 # Make the search for existing revisions in the database in a more sufficient manner
168 168 #
169 169 # Git branch is the reference to the specific revision.
170 170 # Git can *delete* remote branch and *re-push* branch.
171 171 #
172 172 # $ git push remote :branch
173 173 # $ git push remote branch
174 174 #
175 175 # After deleting branch, revisions remain in repository until "git gc".
176 176 # On git 1.7.2.3, default pruning date is 2 weeks.
177 177 # So, "git log --not deleted_branch_head_revision" return code is 0.
178 178 #
179 179 # After re-pushing branch, "git log" returns revisions which are saved in database.
180 180 # So, Redmine needs to scan revisions and database every time.
181 181 #
182 182 # This is replacing the one-after-one queries.
183 183 # Find all revisions, that are in the database, and then remove them
184 184 # from the revision array.
185 185 # Then later we won't need any conditions for db existence.
186 186 # Query for several revisions at once, and remove them
187 187 # from the revisions array, if they are there.
188 188 # Do this in chunks, to avoid eventual memory problems
189 189 # (in case of tens of thousands of commits).
190 190 # If there are no revisions (because the original code's algorithm filtered them),
191 191 # then this part will be stepped over.
192 192 # We make queries, just if there is any revision.
193 193 limit = 100
194 194 offset = 0
195 195 revisions_copy = revisions.clone # revisions will change
196 196 while offset < revisions_copy.size
197 197 scmids = revisions_copy.slice(offset, limit).map{|x| x.scmid}
198 recent_changesets_slice = changesets.where(:scmid => scmids).all
198 recent_changesets_slice = changesets.where(:scmid => scmids)
199 199 # Subtract revisions that redmine already knows about
200 200 recent_revisions = recent_changesets_slice.map{|c| c.scmid}
201 201 revisions.reject!{|r| recent_revisions.include?(r.scmid)}
202 202 offset += limit
203 203 end
204 204 revisions.each do |rev|
205 205 transaction do
206 206 # There is no search in the db for this revision, because above we ensured,
207 207 # that it's not in the db.
208 208 save_revision(rev)
209 209 end
210 210 end
211 211 h["heads"] = repo_heads.dup
212 212 merge_extra_info(h)
213 213 self.save
214 214 end
215 215 private :save_revisions
216 216
217 217 def save_revision(rev)
218 218 parents = (rev.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
219 219 changeset = Changeset.create(
220 220 :repository => self,
221 221 :revision => rev.identifier,
222 222 :scmid => rev.scmid,
223 223 :committer => rev.author,
224 224 :committed_on => rev.time,
225 225 :comments => rev.message,
226 226 :parents => parents
227 227 )
228 228 unless changeset.new_record?
229 229 rev.paths.each { |change| changeset.create_change(change) }
230 230 end
231 231 changeset
232 232 end
233 233 private :save_revision
234 234
235 235 def heads_from_branches_hash
236 236 h1 = extra_info || {}
237 237 h = h1.dup
238 238 h["branches"] ||= {}
239 239 h['branches'].map{|br, hs| hs['last_scmid']}
240 240 end
241 241
242 242 def latest_changesets(path,rev,limit=10)
243 243 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
244 244 return [] if revisions.nil? || revisions.empty?
245 245 changesets.where(:scmid => revisions.map {|c| c.scmid}).all
246 246 end
247 247
248 248 def clear_extra_info_of_changesets
249 249 return if extra_info.nil?
250 250 v = extra_info["extra_report_last_commit"]
251 251 write_attribute(:extra_info, nil)
252 252 h = {}
253 253 h["extra_report_last_commit"] = v
254 254 merge_extra_info(h)
255 255 self.save
256 256 end
257 257 private :clear_extra_info_of_changesets
258 258 end
General Comments 0
You need to be logged in to leave comments. Login now