##// END OF EJS Templates
Refactor: Extract method to create a Change from a Changeset....
Eric Davis -
r3246:39c585740d45
parent child
Show More
@@ -1,181 +1,190
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'iconv'
19 19
20 20 class Changeset < ActiveRecord::Base
21 21 belongs_to :repository
22 22 belongs_to :user
23 23 has_many :changes, :dependent => :delete_all
24 24 has_and_belongs_to_many :issues
25 25
26 26 acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.revision}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))},
27 27 :description => :long_comments,
28 28 :datetime => :committed_on,
29 29 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.revision}}
30 30
31 31 acts_as_searchable :columns => 'comments',
32 32 :include => {:repository => :project},
33 33 :project_key => "#{Repository.table_name}.project_id",
34 34 :date_column => 'committed_on'
35 35
36 36 acts_as_activity_provider :timestamp => "#{table_name}.committed_on",
37 37 :author_key => :user_id,
38 38 :find_options => {:include => [:user, {:repository => :project}]}
39 39
40 40 validates_presence_of :repository_id, :revision, :committed_on, :commit_date
41 41 validates_uniqueness_of :revision, :scope => :repository_id
42 42 validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
43 43
44 44 named_scope :visible, lambda {|*args| { :include => {:repository => :project},
45 45 :conditions => Project.allowed_to_condition(args.first || User.current, :view_changesets) } }
46 46
47 47 def revision=(r)
48 48 write_attribute :revision, (r.nil? ? nil : r.to_s)
49 49 end
50 50
51 51 def comments=(comment)
52 52 write_attribute(:comments, Changeset.normalize_comments(comment))
53 53 end
54 54
55 55 def committed_on=(date)
56 56 self.commit_date = date
57 57 super
58 58 end
59 59
60 60 def project
61 61 repository.project
62 62 end
63 63
64 64 def author
65 65 user || committer.to_s.split('<').first
66 66 end
67 67
68 68 def before_create
69 69 self.user = repository.find_committer_user(committer)
70 70 end
71 71
72 72 def after_create
73 73 scan_comment_for_issue_ids
74 74 end
75 75 require 'pp'
76 76
77 77 def scan_comment_for_issue_ids
78 78 return if comments.blank?
79 79 # keywords used to reference issues
80 80 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
81 81 # keywords used to fix issues
82 82 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
83 83 # status and optional done ratio applied
84 84 fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id)
85 85 done_ratio = Setting.commit_fix_done_ratio.blank? ? nil : Setting.commit_fix_done_ratio.to_i
86 86
87 87 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
88 88 return if kw_regexp.blank?
89 89
90 90 referenced_issues = []
91 91
92 92 if ref_keywords.delete('*')
93 93 # find any issue ID in the comments
94 94 target_issue_ids = []
95 95 comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] }
96 96 referenced_issues += find_referenced_issues_by_id(target_issue_ids)
97 97 end
98 98
99 99 comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match|
100 100 action = match[0]
101 101 target_issue_ids = match[1].scan(/\d+/)
102 102 target_issues = find_referenced_issues_by_id(target_issue_ids)
103 103 if fix_status && fix_keywords.include?(action.downcase)
104 104 # update status of issues
105 105 logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug?
106 106 target_issues.each do |issue|
107 107 # the issue may have been updated by the closure of another one (eg. duplicate)
108 108 issue.reload
109 109 # don't change the status is the issue is closed
110 110 next if issue.status.is_closed?
111 111 csettext = "r#{self.revision}"
112 112 if self.scmid && (! (csettext =~ /^r[0-9]+$/))
113 113 csettext = "commit:\"#{self.scmid}\""
114 114 end
115 115 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, csettext))
116 116 issue.status = fix_status
117 117 issue.done_ratio = done_ratio if done_ratio
118 118 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
119 119 { :changeset => self, :issue => issue })
120 120 issue.save
121 121 end
122 122 end
123 123 referenced_issues += target_issues
124 124 end
125 125
126 126 self.issues = referenced_issues.uniq
127 127 end
128 128
129 129 def short_comments
130 130 @short_comments || split_comments.first
131 131 end
132 132
133 133 def long_comments
134 134 @long_comments || split_comments.last
135 135 end
136 136
137 137 # Returns the previous changeset
138 138 def previous
139 139 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
140 140 end
141 141
142 142 # Returns the next changeset
143 143 def next
144 144 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
145 145 end
146 146
147 147 # Strips and reencodes a commit log before insertion into the database
148 148 def self.normalize_comments(str)
149 149 to_utf8(str.to_s.strip)
150 150 end
151 151
152 # Creates a new Change from it's common parameters
153 def create_change(change)
154 Change.create(:changeset => self,
155 :action => change[:action],
156 :path => change[:path],
157 :from_path => change[:from_path],
158 :from_revision => change[:from_revision])
159 end
160
152 161 private
153 162
154 163 # Finds issues that can be referenced by the commit message
155 164 # i.e. issues that belong to the repository project, a subproject or a parent project
156 165 def find_referenced_issues_by_id(ids)
157 166 Issue.find_all_by_id(ids, :include => :project).select {|issue|
158 167 project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
159 168 }
160 169 end
161 170
162 171 def split_comments
163 172 comments =~ /\A(.+?)\r?\n(.*)$/m
164 173 @short_comments = $1 || comments
165 174 @long_comments = $2.to_s.strip
166 175 return @short_comments, @long_comments
167 176 end
168 177
169 178 def self.to_utf8(str)
170 179 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
171 180 encoding = Setting.commit_logs_encoding.to_s.strip
172 181 unless encoding.blank? || encoding == 'UTF-8'
173 182 begin
174 183 return Iconv.conv('UTF-8', encoding, str)
175 184 rescue Iconv::Failure
176 185 # do nothing here
177 186 end
178 187 end
179 188 str
180 189 end
181 190 end
@@ -1,100 +1,96
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/darcs_adapter'
19 19
20 20 class Repository::Darcs < Repository
21 21 validates_presence_of :url
22 22
23 23 def scm_adapter
24 24 Redmine::Scm::Adapters::DarcsAdapter
25 25 end
26 26
27 27 def self.scm_name
28 28 'Darcs'
29 29 end
30 30
31 31 def entry(path=nil, identifier=nil)
32 32 patch = identifier.nil? ? nil : changesets.find_by_revision(identifier)
33 33 scm.entry(path, patch.nil? ? nil : patch.scmid)
34 34 end
35 35
36 36 def entries(path=nil, identifier=nil)
37 37 patch = identifier.nil? ? nil : changesets.find_by_revision(identifier)
38 38 entries = scm.entries(path, patch.nil? ? nil : patch.scmid)
39 39 if entries
40 40 entries.each do |entry|
41 41 # Search the DB for the entry's last change
42 42 changeset = changesets.find_by_scmid(entry.lastrev.scmid) if entry.lastrev && !entry.lastrev.scmid.blank?
43 43 if changeset
44 44 entry.lastrev.identifier = changeset.revision
45 45 entry.lastrev.name = changeset.revision
46 46 entry.lastrev.time = changeset.committed_on
47 47 entry.lastrev.author = changeset.committer
48 48 end
49 49 end
50 50 end
51 51 entries
52 52 end
53 53
54 54 def cat(path, identifier=nil)
55 55 patch = identifier.nil? ? nil : changesets.find_by_revision(identifier.to_s)
56 56 scm.cat(path, patch.nil? ? nil : patch.scmid)
57 57 end
58 58
59 59 def diff(path, rev, rev_to)
60 60 patch_from = changesets.find_by_revision(rev)
61 61 return nil if patch_from.nil?
62 62 patch_to = changesets.find_by_revision(rev_to) if rev_to
63 63 if path.blank?
64 64 path = patch_from.changes.collect{|change| change.path}.join(' ')
65 65 end
66 66 patch_from ? scm.diff(path, patch_from.scmid, patch_to ? patch_to.scmid : nil) : nil
67 67 end
68 68
69 69 def fetch_changesets
70 70 scm_info = scm.info
71 71 if scm_info
72 72 db_last_id = latest_changeset ? latest_changeset.scmid : nil
73 73 next_rev = latest_changeset ? latest_changeset.revision.to_i + 1 : 1
74 74 # latest revision in the repository
75 75 scm_revision = scm_info.lastrev.scmid
76 76 unless changesets.find_by_scmid(scm_revision)
77 77 revisions = scm.revisions('', db_last_id, nil, :with_path => true)
78 78 transaction do
79 79 revisions.reverse_each do |revision|
80 80 changeset = Changeset.create(:repository => self,
81 81 :revision => next_rev,
82 82 :scmid => revision.scmid,
83 83 :committer => revision.author,
84 84 :committed_on => revision.time,
85 85 :comments => revision.message)
86 86
87 87 revision.paths.each do |change|
88 Change.create(:changeset => changeset,
89 :action => change[:action],
90 :path => change[:path],
91 :from_path => change[:from_path],
92 :from_revision => change[:from_revision])
88 changeset.create_change(change)
93 89 end
94 90 next_rev += 1
95 91 end if revisions
96 92 end
97 93 end
98 94 end
99 95 end
100 96 end
@@ -1,94 +1,90
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/mercurial_adapter'
19 19
20 20 class Repository::Mercurial < Repository
21 21 attr_protected :root_url
22 22 validates_presence_of :url
23 23
24 24 def scm_adapter
25 25 Redmine::Scm::Adapters::MercurialAdapter
26 26 end
27 27
28 28 def self.scm_name
29 29 'Mercurial'
30 30 end
31 31
32 32 def entries(path=nil, identifier=nil)
33 33 entries=scm.entries(path, identifier)
34 34 if entries
35 35 entries.each do |entry|
36 36 next unless entry.is_file?
37 37 # Set the filesize unless browsing a specific revision
38 38 if identifier.nil?
39 39 full_path = File.join(root_url, entry.path)
40 40 entry.size = File.stat(full_path).size if File.file?(full_path)
41 41 end
42 42 # Search the DB for the entry's last change
43 43 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
44 44 if change
45 45 entry.lastrev.identifier = change.changeset.revision
46 46 entry.lastrev.name = change.changeset.revision
47 47 entry.lastrev.author = change.changeset.committer
48 48 entry.lastrev.revision = change.revision
49 49 end
50 50 end
51 51 end
52 52 entries
53 53 end
54 54
55 55 def fetch_changesets
56 56 scm_info = scm.info
57 57 if scm_info
58 58 # latest revision found in database
59 59 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
60 60 # latest revision in the repository
61 61 latest_revision = scm_info.lastrev
62 62 return if latest_revision.nil?
63 63 scm_revision = latest_revision.identifier.to_i
64 64 if db_revision < scm_revision
65 65 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
66 66 identifier_from = db_revision + 1
67 67 while (identifier_from <= scm_revision)
68 68 # loads changesets by batches of 100
69 69 identifier_to = [identifier_from + 99, scm_revision].min
70 70 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
71 71 transaction do
72 72 revisions.each do |revision|
73 73 changeset = Changeset.create(:repository => self,
74 74 :revision => revision.identifier,
75 75 :scmid => revision.scmid,
76 76 :committer => revision.author,
77 77 :committed_on => revision.time,
78 78 :comments => revision.message)
79 79
80 80 revision.paths.each do |change|
81 Change.create(:changeset => changeset,
82 :action => change[:action],
83 :path => change[:path],
84 :from_path => change[:from_path],
85 :from_revision => change[:from_revision])
81 changeset.create_change(change)
86 82 end
87 83 end
88 84 end unless revisions.nil?
89 85 identifier_from = identifier_to + 1
90 86 end
91 87 end
92 88 end
93 89 end
94 90 end
@@ -1,89 +1,85
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/subversion_adapter'
19 19
20 20 class Repository::Subversion < Repository
21 21 attr_protected :root_url
22 22 validates_presence_of :url
23 23 validates_format_of :url, :with => /^(http|https|svn(\+[^\s:\/\\]+)?|file):\/\/.+/i
24 24
25 25 def scm_adapter
26 26 Redmine::Scm::Adapters::SubversionAdapter
27 27 end
28 28
29 29 def self.scm_name
30 30 'Subversion'
31 31 end
32 32
33 33 def latest_changesets(path, rev, limit=10)
34 34 revisions = scm.revisions(path, rev, nil, :limit => limit)
35 35 revisions ? changesets.find_all_by_revision(revisions.collect(&:identifier), :order => "committed_on DESC", :include => :user) : []
36 36 end
37 37
38 38 # Returns a path relative to the url of the repository
39 39 def relative_path(path)
40 40 path.gsub(Regexp.new("^\/?#{Regexp.escape(relative_url)}"), '')
41 41 end
42 42
43 43 def fetch_changesets
44 44 scm_info = scm.info
45 45 if scm_info
46 46 # latest revision found in database
47 47 db_revision = latest_changeset ? latest_changeset.revision.to_i : 0
48 48 # latest revision in the repository
49 49 scm_revision = scm_info.lastrev.identifier.to_i
50 50 if db_revision < scm_revision
51 51 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
52 52 identifier_from = db_revision + 1
53 53 while (identifier_from <= scm_revision)
54 54 # loads changesets by batches of 200
55 55 identifier_to = [identifier_from + 199, scm_revision].min
56 56 revisions = scm.revisions('', identifier_to, identifier_from, :with_paths => true)
57 57 revisions.reverse_each do |revision|
58 58 transaction do
59 59 changeset = Changeset.create(:repository => self,
60 60 :revision => revision.identifier,
61 61 :committer => revision.author,
62 62 :committed_on => revision.time,
63 63 :comments => revision.message)
64 64
65 65 revision.paths.each do |change|
66 Change.create(:changeset => changeset,
67 :action => change[:action],
68 :path => change[:path],
69 :from_path => change[:from_path],
70 :from_revision => change[:from_revision])
66 changeset.create_change(change)
71 67 end unless changeset.new_record?
72 68 end
73 69 end unless revisions.nil?
74 70 identifier_from = identifier_to + 1
75 71 end
76 72 end
77 73 end
78 74 end
79 75
80 76 private
81 77
82 78 # Returns the relative url of the repository
83 79 # Eg: root_url = file:///var/svn/foo
84 80 # url = file:///var/svn/foo/bar
85 81 # => returns /bar
86 82 def relative_url
87 83 @relative_url ||= url.gsub(Regexp.new("^#{Regexp.escape(root_url || scm.root_url)}", Regexp::IGNORECASE), '')
88 84 end
89 85 end
General Comments 0
You need to be logged in to leave comments. Login now