##// END OF EJS Templates
fixed bug 9537 fetch_changesets broken...
Jean-Philippe Lang -
r379:1f7863e32391
parent child
Show More
@@ -1,96 +1,88
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 class Repository < ActiveRecord::Base
18 class Repository < ActiveRecord::Base
19 belongs_to :project
19 belongs_to :project
20 has_many :changesets, :dependent => :destroy, :order => 'revision DESC'
20 has_many :changesets, :dependent => :destroy, :order => 'revision DESC'
21 has_many :changes, :through => :changesets
21 has_many :changes, :through => :changesets
22 has_one :latest_changeset, :class_name => 'Changeset', :foreign_key => :repository_id, :order => 'revision DESC'
22 has_one :latest_changeset, :class_name => 'Changeset', :foreign_key => :repository_id, :order => 'revision DESC'
23
23
24 attr_protected :root_url
24 attr_protected :root_url
25
25
26 validates_presence_of :url
26 validates_presence_of :url
27 validates_format_of :url, :with => /^(http|https|svn|file):\/\/.+/i
27 validates_format_of :url, :with => /^(http|https|svn|file):\/\/.+/i
28
28
29 def scm
29 def scm
30 @scm ||= SvnRepos::Base.new url, root_url, login, password
30 @scm ||= SvnRepos::Base.new url, root_url, login, password
31 update_attribute(:root_url, @scm.root_url) if root_url.blank?
31 update_attribute(:root_url, @scm.root_url) if root_url.blank?
32 @scm
32 @scm
33 end
33 end
34
34
35 def url=(str)
35 def url=(str)
36 super if root_url.blank?
36 super if root_url.blank?
37 end
37 end
38
38
39 def changesets_with_path(path="")
39 def changesets_with_path(path="")
40 path = "/#{path}%"
40 path = "/#{path}%"
41 path = url.gsub(/^#{root_url}/, '') + path if root_url && root_url != url
41 path = url.gsub(/^#{root_url}/, '') + path if root_url && root_url != url
42 path.squeeze!("/")
42 path.squeeze!("/")
43 Changeset.with_scope(:find => { :include => :changes, :conditions => ["#{Change.table_name}.path LIKE ?", path] }) do
43 Changeset.with_scope(:find => { :include => :changes, :conditions => ["#{Change.table_name}.path LIKE ?", path] }) do
44 yield
44 yield
45 end
45 end
46 end
46 end
47
47
48 def changesets_for_path(path="")
49 path = "/#{path}%"
50 path = url.gsub(/^#{root_url}/, '') + path if root_url && root_url != url
51 path.squeeze!("/")
52 changesets.find(:all, :include => :changes,
53 :conditions => ["#{Change.table_name}.path LIKE ?", path])
54 end
55
56 def fetch_changesets
48 def fetch_changesets
57 scm_info = scm.info
49 scm_info = scm.info
58 if scm_info
50 if scm_info
59 lastrev_identifier = scm_info.lastrev.identifier.to_i
51 lastrev_identifier = scm_info.lastrev.identifier.to_i
60 if latest_changeset.nil? || latest_changeset.revision < lastrev_identifier
52 if latest_changeset.nil? || latest_changeset.revision < lastrev_identifier
61 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
53 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
62 identifier_from = latest_changeset ? latest_changeset.revision + 1 : 1
54 identifier_from = latest_changeset ? latest_changeset.revision + 1 : 1
63 while (identifier_from <= lastrev_identifier)
55 while (identifier_from <= lastrev_identifier)
64 # loads changesets by batches of 200
56 # loads changesets by batches of 200
65 identifier_to = [identifier_from + 199, lastrev_identifier].min
57 identifier_to = [identifier_from + 199, lastrev_identifier].min
66 revisions = scm.revisions('', identifier_to, identifier_from, :with_paths => true)
58 revisions = scm.revisions('', identifier_to, identifier_from, :with_paths => true)
67 transaction do
59 transaction do
68 revisions.reverse_each do |revision|
60 revisions.reverse_each do |revision|
69 changeset = Changeset.create(:repository => self,
61 changeset = Changeset.create(:repository => self,
70 :revision => revision.identifier,
62 :revision => revision.identifier,
71 :committer => revision.author,
63 :committer => revision.author,
72 :committed_on => revision.time,
64 :committed_on => revision.time,
73 :comment => revision.message)
65 :comment => revision.message)
74
66
75 revision.paths.each do |change|
67 revision.paths.each do |change|
76 Change.create(:changeset => changeset,
68 Change.create(:changeset => changeset,
77 :action => change[:action],
69 :action => change[:action],
78 :path => change[:path],
70 :path => change[:path],
79 :from_path => change[:from_path],
71 :from_path => change[:from_path],
80 :from_revision => change[:from_revision])
72 :from_revision => change[:from_revision])
81 end
73 end
82 end
74 end
83 end
75 end unless revisions.nil?
84 identifier_from = identifier_to + 1
76 identifier_from = identifier_to + 1
85 end
77 end
86 end
78 end
87 end
79 end
88 end
80 end
89
81
90 # fetch new changesets for all repositories
82 # fetch new changesets for all repositories
91 # can be called periodically by an external script
83 # can be called periodically by an external script
92 # eg. ruby script/runner "Repository.fetch_changesets"
84 # eg. ruby script/runner "Repository.fetch_changesets"
93 def self.fetch_changesets
85 def self.fetch_changesets
94 find(:all).each(&:fetch_changesets)
86 find(:all).each(&:fetch_changesets)
95 end
87 end
96 end
88 end
General Comments 0
You need to be logged in to leave comments. Login now