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