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