To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / changeset.rb @ 438:34214e593c67
History | View | Annotate | Download (7.54 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2010 Jean-Philippe Lang
|
| 3 |
#
|
| 4 |
# This program is free software; you can redistribute it and/or
|
| 5 |
# modify it under the terms of the GNU General Public License
|
| 6 |
# as published by the Free Software Foundation; either version 2
|
| 7 |
# of the License, or (at your option) any later version.
|
| 8 |
#
|
| 9 |
# This program is distributed in the hope that it will be useful,
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
# GNU General Public License for more details.
|
| 13 |
#
|
| 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
|
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 17 |
|
| 18 |
require 'iconv'
|
| 19 |
|
| 20 |
class Changeset < ActiveRecord::Base |
| 21 |
belongs_to :repository
|
| 22 |
belongs_to :user
|
| 23 |
has_many :changes, :dependent => :delete_all |
| 24 |
has_and_belongs_to_many :issues
|
| 25 |
|
| 26 |
acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{RepositoriesHelper.format_revision(o)}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))}, |
| 27 |
:description => :long_comments, |
| 28 |
:datetime => :committed_on, |
| 29 |
:url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.identifier}} |
| 30 |
|
| 31 |
acts_as_searchable :columns => 'comments', |
| 32 |
:include => {:repository => :project}, |
| 33 |
:project_key => "#{Repository.table_name}.project_id", |
| 34 |
:date_column => 'committed_on' |
| 35 |
|
| 36 |
acts_as_activity_provider :timestamp => "#{table_name}.committed_on", |
| 37 |
:author_key => :user_id, |
| 38 |
:find_options => {:include => [:user, {:repository => :project}]} |
| 39 |
|
| 40 |
validates_presence_of :repository_id, :revision, :committed_on, :commit_date |
| 41 |
validates_uniqueness_of :revision, :scope => :repository_id |
| 42 |
validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true |
| 43 |
|
| 44 |
named_scope :visible, lambda {|*args| { :include => {:repository => :project}, |
| 45 |
:conditions => Project.allowed_to_condition(args.first || User.current, :view_changesets) } } |
| 46 |
|
| 47 |
def revision=(r) |
| 48 |
write_attribute :revision, (r.nil? ? nil : r.to_s) |
| 49 |
end
|
| 50 |
|
| 51 |
# Returns the identifier of this changeset.
|
| 52 |
# e.g. revision number for centralized system; hash id for DVCS
|
| 53 |
def identifier |
| 54 |
scmid || revision |
| 55 |
end
|
| 56 |
|
| 57 |
# Returns the wiki identifier, "rN" or "commit:ABCDEF"
|
| 58 |
def wiki_identifier |
| 59 |
if scmid # hash-like |
| 60 |
"commit:#{scmid}"
|
| 61 |
else # numeric |
| 62 |
"r#{revision}"
|
| 63 |
end
|
| 64 |
end
|
| 65 |
private :wiki_identifier
|
| 66 |
|
| 67 |
def comments=(comment) |
| 68 |
write_attribute(:comments, Changeset.normalize_comments(comment)) |
| 69 |
end
|
| 70 |
|
| 71 |
def committed_on=(date) |
| 72 |
self.commit_date = date
|
| 73 |
super
|
| 74 |
end
|
| 75 |
|
| 76 |
def committer=(arg) |
| 77 |
write_attribute(:committer, self.class.to_utf8(arg.to_s)) |
| 78 |
end
|
| 79 |
|
| 80 |
def project |
| 81 |
repository.project |
| 82 |
end
|
| 83 |
|
| 84 |
def author |
| 85 |
user || committer.to_s.split('<').first
|
| 86 |
end
|
| 87 |
|
| 88 |
def before_create |
| 89 |
self.user = repository.find_committer_user(committer)
|
| 90 |
end
|
| 91 |
|
| 92 |
def after_create |
| 93 |
scan_comment_for_issue_ids |
| 94 |
end
|
| 95 |
|
| 96 |
def scan_comment_for_issue_ids |
| 97 |
return if comments.blank? |
| 98 |
# keywords used to reference issues
|
| 99 |
ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip) |
| 100 |
# keywords used to fix issues
|
| 101 |
fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip) |
| 102 |
|
| 103 |
kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
|
| 104 |
return if kw_regexp.blank? |
| 105 |
|
| 106 |
referenced_issues = [] |
| 107 |
|
| 108 |
if ref_keywords.delete('*') |
| 109 |
# find any issue ID in the comments
|
| 110 |
target_issue_ids = [] |
| 111 |
comments.scan(%r{([\s\(\[,-]|^)#(\d+)(?=[[:punct:]]|\s|<|$)}).each { |m| target_issue_ids << m[1] } |
| 112 |
referenced_issues += find_referenced_issues_by_id(target_issue_ids) |
| 113 |
end
|
| 114 |
|
| 115 |
comments.scan(Regexp.new("(#{kw_regexp})[\s:]+(([\s,;&]*#?\\d+)+)", Regexp::IGNORECASE)).each do |match| |
| 116 |
action = match[0]
|
| 117 |
target_issue_ids = match[1].scan(/\d+/) |
| 118 |
target_issues = find_referenced_issues_by_id(target_issue_ids) |
| 119 |
if fix_keywords.include?(action.downcase) && fix_status = IssueStatus.find_by_id(Setting.commit_fix_status_id) |
| 120 |
# update status of issues
|
| 121 |
logger.debug "Issues fixed by changeset #{self.revision}: #{issue_ids.join(', ')}." if logger && logger.debug? |
| 122 |
target_issues.each do |issue|
|
| 123 |
# the issue may have been updated by the closure of another one (eg. duplicate)
|
| 124 |
issue.reload |
| 125 |
# don't change the status is the issue is closed
|
| 126 |
next if issue.status.is_closed? |
| 127 |
journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, wiki_identifier)) |
| 128 |
issue.status = fix_status |
| 129 |
unless Setting.commit_fix_done_ratio.blank? |
| 130 |
issue.done_ratio = Setting.commit_fix_done_ratio.to_i
|
| 131 |
end
|
| 132 |
Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update, |
| 133 |
{ :changeset => self, :issue => issue })
|
| 134 |
issue.save |
| 135 |
end
|
| 136 |
end
|
| 137 |
referenced_issues += target_issues |
| 138 |
end
|
| 139 |
|
| 140 |
referenced_issues.uniq! |
| 141 |
self.issues = referenced_issues unless referenced_issues.empty? |
| 142 |
end
|
| 143 |
|
| 144 |
def short_comments |
| 145 |
@short_comments || split_comments.first
|
| 146 |
end
|
| 147 |
|
| 148 |
def long_comments |
| 149 |
@long_comments || split_comments.last
|
| 150 |
end
|
| 151 |
|
| 152 |
# Returns the previous changeset
|
| 153 |
def previous |
| 154 |
@previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC') |
| 155 |
end
|
| 156 |
|
| 157 |
# Returns the next changeset
|
| 158 |
def next |
| 159 |
@next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC') |
| 160 |
end
|
| 161 |
|
| 162 |
# Strips and reencodes a commit log before insertion into the database
|
| 163 |
def self.normalize_comments(str) |
| 164 |
to_utf8(str.to_s.strip) |
| 165 |
end
|
| 166 |
|
| 167 |
# Creates a new Change from it's common parameters
|
| 168 |
def create_change(change) |
| 169 |
Change.create(:changeset => self, |
| 170 |
:action => change[:action], |
| 171 |
:path => change[:path], |
| 172 |
:from_path => change[:from_path], |
| 173 |
:from_revision => change[:from_revision]) |
| 174 |
end
|
| 175 |
|
| 176 |
private |
| 177 |
|
| 178 |
# Finds issues that can be referenced by the commit message
|
| 179 |
# i.e. issues that belong to the repository project, a subproject or a parent project
|
| 180 |
def find_referenced_issues_by_id(ids) |
| 181 |
return [] if ids.compact.empty? |
| 182 |
Issue.find_all_by_id(ids, :include => :project).select {|issue| |
| 183 |
project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project) |
| 184 |
} |
| 185 |
end
|
| 186 |
|
| 187 |
def split_comments |
| 188 |
comments =~ /\A(.+?)\r?\n(.*)$/m
|
| 189 |
@short_comments = $1 || comments |
| 190 |
@long_comments = $2.to_s.strip |
| 191 |
return @short_comments, @long_comments |
| 192 |
end
|
| 193 |
|
| 194 |
def self.to_utf8(str) |
| 195 |
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii |
| 196 |
encoding = Setting.commit_logs_encoding.to_s.strip
|
| 197 |
unless encoding.blank? || encoding == 'UTF-8' |
| 198 |
begin
|
| 199 |
str = Iconv.conv('UTF-8', encoding, str) |
| 200 |
rescue Iconv::Failure |
| 201 |
# do nothing here
|
| 202 |
end
|
| 203 |
end
|
| 204 |
# removes invalid UTF8 sequences
|
| 205 |
begin
|
| 206 |
Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3] |
| 207 |
rescue Iconv::InvalidEncoding |
| 208 |
# "UTF-8//IGNORE" is not supported on some OS
|
| 209 |
str |
| 210 |
end
|
| 211 |
end
|
| 212 |
end
|