@@ -1,168 +1,170 | |||
|
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 | def revision=(r) |
|
45 | 45 | write_attribute :revision, (r.nil? ? nil : r.to_s) |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | def comments=(comment) |
|
49 | 49 | write_attribute(:comments, Changeset.normalize_comments(comment)) |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def committed_on=(date) |
|
53 | 53 | self.commit_date = date |
|
54 | 54 | super |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def project |
|
58 | 58 | repository.project |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def author |
|
62 | 62 | user || committer.to_s.split('<').first |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def before_create |
|
66 | 66 | self.user = repository.find_committer_user(committer) |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def after_create |
|
70 | 70 | scan_comment_for_issue_ids |
|
71 | 71 | end |
|
72 | 72 | require 'pp' |
|
73 | 73 | |
|
74 | 74 | def scan_comment_for_issue_ids |
|
75 | 75 | return if comments.blank? |
|
76 | 76 | # keywords used to reference issues |
|
77 | 77 | ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip) |
|
78 | 78 | # keywords used to fix issues |
|
79 | 79 | fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip) |
|
80 | 80 | # status and optional done ratio applied |
|
81 | 81 | fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id) |
|
82 | 82 | done_ratio = Setting.commit_fix_done_ratio.blank? ? nil : Setting.commit_fix_done_ratio.to_i |
|
83 | 83 | |
|
84 | 84 | kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|") |
|
85 | 85 | return if kw_regexp.blank? |
|
86 | 86 | |
|
87 | 87 | referenced_issues = [] |
|
88 | 88 | |
|
89 | 89 | if ref_keywords.delete('*') |
|
90 | 90 | # find any issue ID in the comments |
|
91 | 91 | target_issue_ids = [] |
|
92 | 92 | comments.scan(%r{([\s\(,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] } |
|
93 | 93 | referenced_issues += repository.project.issues.find_all_by_id(target_issue_ids) |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match| |
|
97 | 97 | action = match[0] |
|
98 | 98 | target_issue_ids = match[1].scan(/\d+/) |
|
99 | 99 | target_issues = repository.project.issues.find_all_by_id(target_issue_ids) |
|
100 | 100 | if fix_status && fix_keywords.include?(action.downcase) |
|
101 | 101 | # update status of issues |
|
102 | 102 | logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug? |
|
103 | 103 | target_issues.each do |issue| |
|
104 | 104 | # the issue may have been updated by the closure of another one (eg. duplicate) |
|
105 | 105 | issue.reload |
|
106 | 106 | # don't change the status is the issue is closed |
|
107 | 107 | next if issue.status.is_closed? |
|
108 | 108 | csettext = "r#{self.revision}" |
|
109 | 109 | if self.scmid && (! (csettext =~ /^r[0-9]+$/)) |
|
110 | 110 | csettext = "commit:\"#{self.scmid}\"" |
|
111 | 111 | end |
|
112 | 112 | journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, csettext)) |
|
113 | 113 | issue.status = fix_status |
|
114 | 114 | issue.done_ratio = done_ratio if done_ratio |
|
115 | Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update, | |
|
116 | { :changeset => self, :issue => issue }) | |
|
115 | 117 | issue.save |
|
116 | 118 | end |
|
117 | 119 | end |
|
118 | 120 | referenced_issues += target_issues |
|
119 | 121 | end |
|
120 | 122 | |
|
121 | 123 | self.issues = referenced_issues.uniq |
|
122 | 124 | end |
|
123 | 125 | |
|
124 | 126 | def short_comments |
|
125 | 127 | @short_comments || split_comments.first |
|
126 | 128 | end |
|
127 | 129 | |
|
128 | 130 | def long_comments |
|
129 | 131 | @long_comments || split_comments.last |
|
130 | 132 | end |
|
131 | 133 | |
|
132 | 134 | # Returns the previous changeset |
|
133 | 135 | def previous |
|
134 | 136 | @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC') |
|
135 | 137 | end |
|
136 | 138 | |
|
137 | 139 | # Returns the next changeset |
|
138 | 140 | def next |
|
139 | 141 | @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC') |
|
140 | 142 | end |
|
141 | 143 | |
|
142 | 144 | # Strips and reencodes a commit log before insertion into the database |
|
143 | 145 | def self.normalize_comments(str) |
|
144 | 146 | to_utf8(str.to_s.strip) |
|
145 | 147 | end |
|
146 | 148 | |
|
147 | 149 | private |
|
148 | 150 | |
|
149 | 151 | def split_comments |
|
150 | 152 | comments =~ /\A(.+?)\r?\n(.*)$/m |
|
151 | 153 | @short_comments = $1 || comments |
|
152 | 154 | @long_comments = $2.to_s.strip |
|
153 | 155 | return @short_comments, @long_comments |
|
154 | 156 | end |
|
155 | 157 | |
|
156 | 158 | def self.to_utf8(str) |
|
157 | 159 | return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii |
|
158 | 160 | encoding = Setting.commit_logs_encoding.to_s.strip |
|
159 | 161 | unless encoding.blank? || encoding == 'UTF-8' |
|
160 | 162 | begin |
|
161 | 163 | return Iconv.conv('UTF-8', encoding, str) |
|
162 | 164 | rescue Iconv::Failure |
|
163 | 165 | # do nothing here |
|
164 | 166 | end |
|
165 | 167 | end |
|
166 | 168 | str |
|
167 | 169 | end |
|
168 | 170 | end |
General Comments 0
You need to be logged in to leave comments.
Login now