Chris@909
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@909
|
3 #
|
Chris@909
|
4 # This program is free software; you can redistribute it and/or
|
Chris@909
|
5 # modify it under the terms of the GNU General Public License
|
Chris@909
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@909
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@909
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@909
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@909
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@909
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@909
|
14 # You should have received a copy of the GNU General Public License
|
Chris@909
|
15 # along with this program; if not, write to the Free Software
|
Chris@909
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@909
|
17
|
Chris@909
|
18 require 'iconv'
|
Chris@909
|
19
|
Chris@909
|
20 class Changeset < ActiveRecord::Base
|
Chris@909
|
21 belongs_to :repository
|
Chris@909
|
22 belongs_to :user
|
Chris@909
|
23 has_many :changes, :dependent => :delete_all
|
Chris@909
|
24 has_and_belongs_to_many :issues
|
Chris@909
|
25 has_and_belongs_to_many :parents,
|
Chris@909
|
26 :class_name => "Changeset",
|
Chris@909
|
27 :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}",
|
Chris@909
|
28 :association_foreign_key => 'parent_id', :foreign_key => 'changeset_id'
|
Chris@909
|
29 has_and_belongs_to_many :children,
|
Chris@909
|
30 :class_name => "Changeset",
|
Chris@909
|
31 :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}",
|
Chris@909
|
32 :association_foreign_key => 'changeset_id', :foreign_key => 'parent_id'
|
Chris@909
|
33
|
Chris@909
|
34 acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.format_identifier}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))},
|
Chris@909
|
35 :description => :long_comments,
|
Chris@909
|
36 :datetime => :committed_on,
|
Chris@909
|
37 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.identifier}}
|
Chris@909
|
38
|
Chris@909
|
39 acts_as_searchable :columns => 'comments',
|
Chris@909
|
40 :include => {:repository => :project},
|
Chris@909
|
41 :project_key => "#{Repository.table_name}.project_id",
|
Chris@909
|
42 :date_column => 'committed_on'
|
Chris@909
|
43
|
Chris@909
|
44 acts_as_activity_provider :timestamp => "#{table_name}.committed_on",
|
Chris@909
|
45 :author_key => :user_id,
|
Chris@909
|
46 :find_options => {:include => [:user, {:repository => :project}]}
|
Chris@909
|
47
|
Chris@909
|
48 validates_presence_of :repository_id, :revision, :committed_on, :commit_date
|
Chris@909
|
49 validates_uniqueness_of :revision, :scope => :repository_id
|
Chris@909
|
50 validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
|
Chris@909
|
51
|
Chris@909
|
52 named_scope :visible, lambda {|*args| { :include => {:repository => :project},
|
Chris@909
|
53 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args) } }
|
Chris@909
|
54
|
Chris@909
|
55 after_create :scan_for_issues
|
Chris@909
|
56 before_create :before_create_cs
|
Chris@909
|
57
|
Chris@909
|
58 def revision=(r)
|
Chris@909
|
59 write_attribute :revision, (r.nil? ? nil : r.to_s)
|
Chris@909
|
60 end
|
Chris@909
|
61
|
Chris@909
|
62 # Returns the identifier of this changeset; depending on repository backends
|
Chris@909
|
63 def identifier
|
Chris@909
|
64 if repository.class.respond_to? :changeset_identifier
|
Chris@909
|
65 repository.class.changeset_identifier self
|
Chris@909
|
66 else
|
Chris@909
|
67 revision.to_s
|
Chris@909
|
68 end
|
Chris@909
|
69 end
|
Chris@909
|
70
|
Chris@909
|
71 def committed_on=(date)
|
Chris@909
|
72 self.commit_date = date
|
Chris@909
|
73 super
|
Chris@909
|
74 end
|
Chris@909
|
75
|
Chris@909
|
76 # Returns the readable identifier
|
Chris@909
|
77 def format_identifier
|
Chris@909
|
78 if repository.class.respond_to? :format_changeset_identifier
|
Chris@909
|
79 repository.class.format_changeset_identifier self
|
Chris@909
|
80 else
|
Chris@909
|
81 identifier
|
Chris@909
|
82 end
|
Chris@909
|
83 end
|
Chris@909
|
84
|
Chris@909
|
85 def project
|
Chris@909
|
86 repository.project
|
Chris@909
|
87 end
|
Chris@909
|
88
|
Chris@909
|
89 def author
|
Chris@909
|
90 user || committer.to_s.split('<').first
|
Chris@909
|
91 end
|
Chris@909
|
92
|
Chris@909
|
93 def before_create_cs
|
Chris@909
|
94 self.committer = self.class.to_utf8(self.committer, repository.repo_log_encoding)
|
Chris@909
|
95 self.comments = self.class.normalize_comments(
|
Chris@909
|
96 self.comments, repository.repo_log_encoding)
|
Chris@909
|
97 self.user = repository.find_committer_user(self.committer)
|
Chris@909
|
98 end
|
Chris@909
|
99
|
Chris@909
|
100 def scan_for_issues
|
Chris@909
|
101 scan_comment_for_issue_ids
|
Chris@909
|
102 end
|
Chris@909
|
103
|
Chris@909
|
104 TIMELOG_RE = /
|
Chris@909
|
105 (
|
Chris@909
|
106 ((\d+)(h|hours?))((\d+)(m|min)?)?
|
Chris@909
|
107 |
|
Chris@909
|
108 ((\d+)(h|hours?|m|min))
|
Chris@909
|
109 |
|
Chris@909
|
110 (\d+):(\d+)
|
Chris@909
|
111 |
|
Chris@909
|
112 (\d+([\.,]\d+)?)h?
|
Chris@909
|
113 )
|
Chris@909
|
114 /x
|
Chris@909
|
115
|
Chris@909
|
116 def scan_comment_for_issue_ids
|
Chris@909
|
117 return if comments.blank?
|
Chris@909
|
118 # keywords used to reference issues
|
Chris@909
|
119 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
|
Chris@909
|
120 ref_keywords_any = ref_keywords.delete('*')
|
Chris@909
|
121 # keywords used to fix issues
|
Chris@909
|
122 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
|
Chris@909
|
123
|
Chris@909
|
124 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
|
Chris@909
|
125
|
Chris@909
|
126 referenced_issues = []
|
Chris@909
|
127
|
Chris@909
|
128 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
|
Chris@909
|
129 action, refs = match[2], match[3]
|
Chris@909
|
130 next unless action.present? || ref_keywords_any
|
Chris@909
|
131
|
Chris@909
|
132 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
|
Chris@909
|
133 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
|
Chris@909
|
134 if issue
|
Chris@909
|
135 referenced_issues << issue
|
Chris@909
|
136 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
|
Chris@909
|
137 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
|
Chris@909
|
138 end
|
Chris@909
|
139 end
|
Chris@909
|
140 end
|
Chris@909
|
141
|
Chris@909
|
142 referenced_issues.uniq!
|
Chris@909
|
143 self.issues = referenced_issues unless referenced_issues.empty?
|
Chris@909
|
144 end
|
Chris@909
|
145
|
Chris@909
|
146 def short_comments
|
Chris@909
|
147 @short_comments || split_comments.first
|
Chris@909
|
148 end
|
Chris@909
|
149
|
Chris@909
|
150 def long_comments
|
Chris@909
|
151 @long_comments || split_comments.last
|
Chris@909
|
152 end
|
Chris@909
|
153
|
Chris@909
|
154 def text_tag
|
Chris@909
|
155 if scmid?
|
Chris@909
|
156 "commit:#{scmid}"
|
Chris@909
|
157 else
|
Chris@909
|
158 "r#{revision}"
|
Chris@909
|
159 end
|
Chris@909
|
160 end
|
Chris@909
|
161
|
Chris@909
|
162 # Returns the previous changeset
|
Chris@909
|
163 def previous
|
Chris@909
|
164 @previous ||= Changeset.find(:first,
|
Chris@909
|
165 :conditions => ['id < ? AND repository_id = ?',
|
Chris@909
|
166 self.id, self.repository_id],
|
Chris@909
|
167 :order => 'id DESC')
|
Chris@909
|
168 end
|
Chris@909
|
169
|
Chris@909
|
170 # Returns the next changeset
|
Chris@909
|
171 def next
|
Chris@909
|
172 @next ||= Changeset.find(:first,
|
Chris@909
|
173 :conditions => ['id > ? AND repository_id = ?',
|
Chris@909
|
174 self.id, self.repository_id],
|
Chris@909
|
175 :order => 'id ASC')
|
Chris@909
|
176 end
|
Chris@909
|
177
|
Chris@909
|
178 # Creates a new Change from it's common parameters
|
Chris@909
|
179 def create_change(change)
|
Chris@909
|
180 Change.create(:changeset => self,
|
Chris@909
|
181 :action => change[:action],
|
Chris@909
|
182 :path => change[:path],
|
Chris@909
|
183 :from_path => change[:from_path],
|
Chris@909
|
184 :from_revision => change[:from_revision])
|
Chris@909
|
185 end
|
Chris@909
|
186
|
Chris@909
|
187 private
|
Chris@909
|
188
|
Chris@909
|
189 # Finds an issue that can be referenced by the commit message
|
Chris@909
|
190 # i.e. an issue that belong to the repository project, a subproject or a parent project
|
Chris@909
|
191 def find_referenced_issue_by_id(id)
|
Chris@909
|
192 return nil if id.blank?
|
Chris@909
|
193 issue = Issue.find_by_id(id.to_i, :include => :project)
|
Chris@909
|
194 if issue
|
Chris@909
|
195 unless issue.project &&
|
Chris@909
|
196 (project == issue.project || project.is_ancestor_of?(issue.project) ||
|
Chris@909
|
197 project.is_descendant_of?(issue.project))
|
Chris@909
|
198 issue = nil
|
Chris@909
|
199 end
|
Chris@909
|
200 end
|
Chris@909
|
201 issue
|
Chris@909
|
202 end
|
Chris@909
|
203
|
Chris@909
|
204 def fix_issue(issue)
|
Chris@909
|
205 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
|
Chris@909
|
206 if status.nil?
|
Chris@909
|
207 logger.warn("No status matches commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
|
Chris@909
|
208 return issue
|
Chris@909
|
209 end
|
Chris@909
|
210
|
Chris@909
|
211 # the issue may have been updated by the closure of another one (eg. duplicate)
|
Chris@909
|
212 issue.reload
|
Chris@909
|
213 # don't change the status is the issue is closed
|
Chris@909
|
214 return if issue.status && issue.status.is_closed?
|
Chris@909
|
215
|
Chris@909
|
216 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
|
Chris@909
|
217 issue.status = status
|
Chris@909
|
218 unless Setting.commit_fix_done_ratio.blank?
|
Chris@909
|
219 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
|
Chris@909
|
220 end
|
Chris@909
|
221 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
|
Chris@909
|
222 { :changeset => self, :issue => issue })
|
Chris@909
|
223 unless issue.save
|
Chris@909
|
224 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
|
Chris@909
|
225 end
|
Chris@909
|
226 issue
|
Chris@909
|
227 end
|
Chris@909
|
228
|
Chris@909
|
229 def log_time(issue, hours)
|
Chris@909
|
230 time_entry = TimeEntry.new(
|
Chris@909
|
231 :user => user,
|
Chris@909
|
232 :hours => hours,
|
Chris@909
|
233 :issue => issue,
|
Chris@909
|
234 :spent_on => commit_date,
|
Chris@909
|
235 :comments => l(:text_time_logged_by_changeset, :value => text_tag,
|
Chris@909
|
236 :locale => Setting.default_language)
|
Chris@909
|
237 )
|
Chris@909
|
238 time_entry.activity = log_time_activity unless log_time_activity.nil?
|
Chris@909
|
239
|
Chris@909
|
240 unless time_entry.save
|
Chris@909
|
241 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
|
Chris@909
|
242 end
|
Chris@909
|
243 time_entry
|
Chris@909
|
244 end
|
Chris@909
|
245
|
Chris@909
|
246 def log_time_activity
|
Chris@909
|
247 if Setting.commit_logtime_activity_id.to_i > 0
|
Chris@909
|
248 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
|
Chris@909
|
249 end
|
Chris@909
|
250 end
|
Chris@909
|
251
|
Chris@909
|
252 def split_comments
|
Chris@909
|
253 comments =~ /\A(.+?)\r?\n(.*)$/m
|
Chris@909
|
254 @short_comments = $1 || comments
|
Chris@909
|
255 @long_comments = $2.to_s.strip
|
Chris@909
|
256 return @short_comments, @long_comments
|
Chris@909
|
257 end
|
Chris@909
|
258
|
Chris@909
|
259 public
|
Chris@909
|
260
|
Chris@909
|
261 # Strips and reencodes a commit log before insertion into the database
|
Chris@909
|
262 def self.normalize_comments(str, encoding)
|
Chris@909
|
263 Changeset.to_utf8(str.to_s.strip, encoding)
|
Chris@909
|
264 end
|
Chris@909
|
265
|
Chris@909
|
266 def self.to_utf8(str, encoding)
|
Chris@909
|
267 Redmine::CodesetUtil.to_utf8(str, encoding)
|
Chris@909
|
268 end
|
Chris@909
|
269 end
|