Class: Changeset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/changeset.rb

Overview

Redmine - project management software Copyright (C) 2006-2014 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Constant Summary collapse

TIMELOG_RE =
/
(
((\d+)(h|hours?))((\d+)(m|min)?)?
|
((\d+)(h|hours?|m|min))
|
(\d+):(\d+)
|
(\d+([\.,]\d+)?)h?
)
/x

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize_comments(str, encoding) ⇒ Object

Strips and reencodes a commit log before insertion into the database


284
285
286
# File 'app/models/changeset.rb', line 284

def self.normalize_comments(str, encoding)
  Changeset.to_utf8(str.to_s.strip, encoding)
end

.to_utf8(str, encoding) ⇒ Object


288
289
290
# File 'app/models/changeset.rb', line 288

def self.to_utf8(str, encoding)
  Redmine::CodesetUtil.to_utf8(str, encoding)
end

Instance Method Details

#authorObject


90
91
92
# File 'app/models/changeset.rb', line 90

def author
  user || committer.to_s.split('<').first
end

#before_create_csObject


94
95
96
97
98
99
# File 'app/models/changeset.rb', line 94

def before_create_cs
  self.committer = self.class.to_utf8(self.committer, repository.repo_log_encoding)
  self.comments  = self.class.normalize_comments(
                     self.comments, repository.repo_log_encoding)
  self.user = repository.find_committer_user(self.committer)
end

#committed_on=(date) ⇒ Object


72
73
74
75
# File 'app/models/changeset.rb', line 72

def committed_on=(date)
  self.commit_date = date
  super
end

#create_change(change) ⇒ Object

Creates a new Change from it's common parameters


192
193
194
195
196
197
198
# File 'app/models/changeset.rb', line 192

def create_change(change)
  Change.create(:changeset     => self,
                :action        => change[:action],
                :path          => change[:path],
                :from_path     => change[:from_path],
                :from_revision => change[:from_revision])
end

#find_referenced_issue_by_id(id) ⇒ Object

Finds an issue that can be referenced by the commit message


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/changeset.rb', line 201

def find_referenced_issue_by_id(id)
  return nil if id.blank?
  issue = Issue.includes(:project).where(:id => id.to_i).first
  if Setting.commit_cross_project_ref?
    # all issues can be referenced/fixed
  elsif issue
    # issue that belong to the repository project, a subproject or a parent project only
    unless issue.project &&
              (project == issue.project || project.is_ancestor_of?(issue.project) ||
               project.is_descendant_of?(issue.project))
      issue = nil
    end
  end
  issue
end

#format_identifierObject

Returns the readable identifier


78
79
80
81
82
83
84
# File 'app/models/changeset.rb', line 78

def format_identifier
  if repository.class.respond_to? :format_changeset_identifier
    repository.class.format_changeset_identifier self
  else
    identifier
  end
end

#identifierObject

Returns the identifier of this changeset; depending on repository backends


64
65
66
67
68
69
70
# File 'app/models/changeset.rb', line 64

def identifier
  if repository.class.respond_to? :changeset_identifier
    repository.class.changeset_identifier self
  else
    revision.to_s
  end
end

#long_commentsObject


154
155
156
# File 'app/models/changeset.rb', line 154

def long_comments
  @long_comments || split_comments.last
end

#nextObject

Returns the next changeset


187
188
189
# File 'app/models/changeset.rb', line 187

def next
  @next ||= Changeset.where(["id > ? AND repository_id = ?", id, repository_id]).order('id ASC').first
end

#previousObject

Returns the previous changeset


182
183
184
# File 'app/models/changeset.rb', line 182

def previous
  @previous ||= Changeset.where(["id < ? AND repository_id = ?", id, repository_id]).order('id DESC').first
end

#projectObject


86
87
88
# File 'app/models/changeset.rb', line 86

def project
  repository.project
end

#revision=(r) ⇒ Object


59
60
61
# File 'app/models/changeset.rb', line 59

def revision=(r)
  write_attribute :revision, (r.nil? ? nil : r.to_s)
end

#scan_comment_for_issue_idsObject


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/changeset.rb', line 117

def scan_comment_for_issue_ids
  return if comments.blank?
  # keywords used to reference issues
  ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
  ref_keywords_any = ref_keywords.delete('*')
  # keywords used to fix issues
  fix_keywords = Setting.commit_update_keywords_array.map {|r| r['keywords']}.flatten.compact

  kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")

  referenced_issues = []

  comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\[email protected]#{TIMELOG_RE})?([\s,;&]+#\d+(\[email protected]#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
    action, refs = match[2].to_s.downcase, match[3]
    next unless action.present? || ref_keywords_any

    refs.scan(/#(\d+)(\[email protected]#{TIMELOG_RE})?/).each do |m|
      issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
      if issue && !issue_linked_to_same_commit?(issue)
        referenced_issues << issue
        # Don't update issues or log time when importing old commits
        unless repository.created_on && committed_on && committed_on < repository.created_on
          fix_issue(issue, action) if fix_keywords.include?(action)
          log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
        end
      end
    end
  end

  referenced_issues.uniq!
  self.issues = referenced_issues unless referenced_issues.empty?
end

#scan_for_issuesObject


101
102
103
# File 'app/models/changeset.rb', line 101

def scan_for_issues
  scan_comment_for_issue_ids
end

#short_commentsObject


150
151
152
# File 'app/models/changeset.rb', line 150

def short_comments
  @short_comments || split_comments.first
end

#text_tag(ref_project = nil) ⇒ Object


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/changeset.rb', line 158

def text_tag(ref_project=nil)
  repo = ""
  if repository && repository.identifier.present?
    repo = "#{repository.identifier}|"
  end
  tag = if scmid?
    "commit:#{repo}#{scmid}"
  else
    "#{repo}r#{revision}"
  end
  if ref_project && project && ref_project != project
    tag = "#{project.identifier}:#{tag}"
  end
  tag
end

#titleObject

Returns the title used for the changeset in the activity/search results


175
176
177
178
179
# File 'app/models/changeset.rb', line 175

def title
  repo = (repository && repository.identifier.present?) ? " (#{repository.identifier})" : ''
  comm = short_comments.blank? ? '' : (': ' + short_comments)
  "#{l(:label_revision)} #{format_identifier}#{repo}#{comm}"
end