Class: Changeset

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

Overview

Redmine - project management software Copyright © 2006-2022 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



298
299
300
# File 'app/models/changeset.rb', line 298

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

.to_utf8(str, encoding) ⇒ Object



302
303
304
# File 'app/models/changeset.rb', line 302

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

Instance Method Details

#authorObject



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

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

#before_create_csObject



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

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



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

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

#create_change(change) ⇒ Object

Creates a new Change from it’s common parameters



203
204
205
206
207
208
209
# File 'app/models/changeset.rb', line 203

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/models/changeset.rb', line 212

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



85
86
87
88
89
90
91
# File 'app/models/changeset.rb', line 85

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



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

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

#long_commentsObject



169
170
171
# File 'app/models/changeset.rb', line 169

def long_comments
  @long_comments || split_comments.last
end

#nextObject

Returns the next changeset



198
199
200
# File 'app/models/changeset.rb', line 198

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

#previousObject

Returns the previous changeset



193
194
195
# File 'app/models/changeset.rb', line 193

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

#projectObject



93
94
95
# File 'app/models/changeset.rb', line 93

def project
  repository.project
end

#revision=(r) ⇒ Object



66
67
68
# File 'app/models/changeset.rb', line 66

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

#scan_comment_for_issue_idsObject



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
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/models/changeset.rb', line 124

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 = []
  regexp =
    %r{
      ([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?
      (\#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+\#\d+(\s+@#{TIMELOG_RE})?)*)
      (?=[[:punct:]]|\s|<|$)
    }xi
  comments.scan(regexp) do |match|
    action = match[2].to_s.downcase
    refs   = match[3]
    next unless action.present? || ref_keywords_any

    refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
      issue = find_referenced_issue_by_id(m[0].to_i)
      hours = 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



108
109
110
# File 'app/models/changeset.rb', line 108

def scan_for_issues
  scan_comment_for_issue_ids
end

#short_commentsObject



165
166
167
# File 'app/models/changeset.rb', line 165

def short_comments
  @short_comments || split_comments.first
end

#text_tag(ref_project = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/changeset.rb', line 173

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



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

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