選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

changeset.rb 9.6KB

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