You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

changeset.rb 9.4KB

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