##// END OF EJS Templates
Fixes Repository#clear_changesets....
Jean-Philippe Lang -
r1782:4b9df2eac774
parent child
Show More
@@ -1,139 +1,140
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, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
20 has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
21 has_many :changes, :through => :changesets
21 has_many :changes, :through => :changesets
22
22
23 # Raw SQL to delete changesets and changes in the database
23 # Raw SQL to delete changesets and changes in the database
24 # has_many :changesets, :dependent => :destroy is too slow for big repositories
24 # has_many :changesets, :dependent => :destroy is too slow for big repositories
25 before_destroy :clear_changesets
25 before_destroy :clear_changesets
26
26
27 # Checks if the SCM is enabled when creating a repository
27 # Checks if the SCM is enabled when creating a repository
28 validate_on_create { |r| r.errors.add(:type, :activerecord_error_invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) }
28 validate_on_create { |r| r.errors.add(:type, :activerecord_error_invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) }
29
29
30 # Removes leading and trailing whitespace
30 # Removes leading and trailing whitespace
31 def url=(arg)
31 def url=(arg)
32 write_attribute(:url, arg ? arg.to_s.strip : nil)
32 write_attribute(:url, arg ? arg.to_s.strip : nil)
33 end
33 end
34
34
35 # Removes leading and trailing whitespace
35 # Removes leading and trailing whitespace
36 def root_url=(arg)
36 def root_url=(arg)
37 write_attribute(:root_url, arg ? arg.to_s.strip : nil)
37 write_attribute(:root_url, arg ? arg.to_s.strip : nil)
38 end
38 end
39
39
40 def scm
40 def scm
41 @scm ||= self.scm_adapter.new url, root_url, login, password
41 @scm ||= self.scm_adapter.new url, root_url, login, password
42 update_attribute(:root_url, @scm.root_url) if root_url.blank?
42 update_attribute(:root_url, @scm.root_url) if root_url.blank?
43 @scm
43 @scm
44 end
44 end
45
45
46 def scm_name
46 def scm_name
47 self.class.scm_name
47 self.class.scm_name
48 end
48 end
49
49
50 def supports_cat?
50 def supports_cat?
51 scm.supports_cat?
51 scm.supports_cat?
52 end
52 end
53
53
54 def supports_annotate?
54 def supports_annotate?
55 scm.supports_annotate?
55 scm.supports_annotate?
56 end
56 end
57
57
58 def entry(path=nil, identifier=nil)
58 def entry(path=nil, identifier=nil)
59 scm.entry(path, identifier)
59 scm.entry(path, identifier)
60 end
60 end
61
61
62 def entries(path=nil, identifier=nil)
62 def entries(path=nil, identifier=nil)
63 scm.entries(path, identifier)
63 scm.entries(path, identifier)
64 end
64 end
65
65
66 def properties(path, identifier=nil)
66 def properties(path, identifier=nil)
67 scm.properties(path, identifier)
67 scm.properties(path, identifier)
68 end
68 end
69
69
70 def cat(path, identifier=nil)
70 def cat(path, identifier=nil)
71 scm.cat(path, identifier)
71 scm.cat(path, identifier)
72 end
72 end
73
73
74 def diff(path, rev, rev_to)
74 def diff(path, rev, rev_to)
75 scm.diff(path, rev, rev_to)
75 scm.diff(path, rev, rev_to)
76 end
76 end
77
77
78 # Default behaviour: we search in cached changesets
78 # Default behaviour: we search in cached changesets
79 def changesets_for_path(path)
79 def changesets_for_path(path)
80 path = "/#{path}" unless path.starts_with?('/')
80 path = "/#{path}" unless path.starts_with?('/')
81 Change.find(:all, :include => :changeset,
81 Change.find(:all, :include => :changeset,
82 :conditions => ["repository_id = ? AND path = ?", id, path],
82 :conditions => ["repository_id = ? AND path = ?", id, path],
83 :order => "committed_on DESC, #{Changeset.table_name}.id DESC").collect(&:changeset)
83 :order => "committed_on DESC, #{Changeset.table_name}.id DESC").collect(&:changeset)
84 end
84 end
85
85
86 # Returns a path relative to the url of the repository
86 # Returns a path relative to the url of the repository
87 def relative_path(path)
87 def relative_path(path)
88 path
88 path
89 end
89 end
90
90
91 def latest_changeset
91 def latest_changeset
92 @latest_changeset ||= changesets.find(:first)
92 @latest_changeset ||= changesets.find(:first)
93 end
93 end
94
94
95 def scan_changesets_for_issue_ids
95 def scan_changesets_for_issue_ids
96 self.changesets.each(&:scan_comment_for_issue_ids)
96 self.changesets.each(&:scan_comment_for_issue_ids)
97 end
97 end
98
98
99 # fetch new changesets for all repositories
99 # fetch new changesets for all repositories
100 # can be called periodically by an external script
100 # can be called periodically by an external script
101 # eg. ruby script/runner "Repository.fetch_changesets"
101 # eg. ruby script/runner "Repository.fetch_changesets"
102 def self.fetch_changesets
102 def self.fetch_changesets
103 find(:all).each(&:fetch_changesets)
103 find(:all).each(&:fetch_changesets)
104 end
104 end
105
105
106 # scan changeset comments to find related and fixed issues for all repositories
106 # scan changeset comments to find related and fixed issues for all repositories
107 def self.scan_changesets_for_issue_ids
107 def self.scan_changesets_for_issue_ids
108 find(:all).each(&:scan_changesets_for_issue_ids)
108 find(:all).each(&:scan_changesets_for_issue_ids)
109 end
109 end
110
110
111 def self.scm_name
111 def self.scm_name
112 'Abstract'
112 'Abstract'
113 end
113 end
114
114
115 def self.available_scm
115 def self.available_scm
116 subclasses.collect {|klass| [klass.scm_name, klass.name]}
116 subclasses.collect {|klass| [klass.scm_name, klass.name]}
117 end
117 end
118
118
119 def self.factory(klass_name, *args)
119 def self.factory(klass_name, *args)
120 klass = "Repository::#{klass_name}".constantize
120 klass = "Repository::#{klass_name}".constantize
121 klass.new(*args)
121 klass.new(*args)
122 rescue
122 rescue
123 nil
123 nil
124 end
124 end
125
125
126 private
126 private
127
127
128 def before_save
128 def before_save
129 # Strips url and root_url
129 # Strips url and root_url
130 url.strip!
130 url.strip!
131 root_url.strip!
131 root_url.strip!
132 true
132 true
133 end
133 end
134
134
135 def clear_changesets
135 def clear_changesets
136 connection.delete("DELETE FROM changes WHERE changes.changeset_id IN (SELECT changesets.id FROM changesets WHERE changesets.repository_id = #{id})")
136 connection.delete("DELETE FROM changes WHERE changes.changeset_id IN (SELECT changesets.id FROM changesets WHERE changesets.repository_id = #{id})")
137 connection.delete("DELETE FROM changesets_issues WHERE changesets_issues.changeset_id IN (SELECT changesets.id FROM changesets WHERE changesets.repository_id = #{id})")
137 connection.delete("DELETE FROM changesets WHERE changesets.repository_id = #{id}")
138 connection.delete("DELETE FROM changesets WHERE changesets.repository_id = #{id}")
138 end
139 end
139 end
140 end
General Comments 0
You need to be logged in to leave comments. Login now