Class: Changeset

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

Overview

Redmine - project management software Copyright (C) 2006-2020 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 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 to_utf8(str, encoding)
  Redmine::CodesetUtil.to_utf8(str, encoding)
end

Instance Method Details

#authorObject


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

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

#before_create_csObject


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

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


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

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

#create_change(change) ⇒ Object

Creates a new Change from it's common parameters


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

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


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

def find_referenced_issue_by_id(id)
  return nil if id.blank?

  issue = Issue.find_by_id(id.to_i)
  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


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

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


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

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

#long_commentsObject


156
157
158
# File 'app/models/changeset.rb', line 156

def long_comments
  @long_comments || split_comments.last
end

#nextObject

Returns the next changeset


185
186
187
# File 'app/models/changeset.rb', line 185

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

#previousObject

Returns the previous changeset


180
181
182
# File 'app/models/changeset.rb', line 180

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

#projectObject


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

def project
  repository.project
end

#revision=(r) ⇒ Object


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

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

#scan_comment_for_issue_idsObject


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
149
150
# File 'app/models/changeset.rb', line 118

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


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

def scan_for_issues
  scan_comment_for_issue_ids
end

#short_commentsObject


152
153
154
# File 'app/models/changeset.rb', line 152

def short_comments
  @short_comments || split_comments.first
end

#text_tag(ref_project = nil) ⇒ Object


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

def text_tag(ref_project=nil)
  repo = ""
  if repository && repository.identifier.present?
    repo = "#{repository.identifier}|"
  end
  tag = scmid? ? "commit:#{repo}#{scmid}" : "#{repo}r#{revision}"
  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


173
174
175
176
177
# File 'app/models/changeset.rb', line 173

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