Class: Changeset

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

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


254
255
256
# File 'app/models/changeset.rb', line 254

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

Instance Method Details

#after_createObject


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

def after_create
  scan_comment_for_issue_ids
end

#authorObject


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

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

#before_createObject


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

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

#committed_on=(date) ⇒ Object


56
57
58
59
# File 'app/models/changeset.rb', line 56

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

#committerObject

Committer of the Changeset

Attribute reader for committer that encodes the committer string to the repository log encoding (e.g. UTF-8)


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

def committer
  self.class.to_utf8(read_attribute(:committer), repository_encoding)
end

#create_change(change) ⇒ Object

Creates a new Change from it's common parameters


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

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

#format_identifierObject

Returns the readable identifier


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

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


48
49
50
51
52
53
54
# File 'app/models/changeset.rb', line 48

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

#long_commentsObject


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

def long_comments
  @long_comments || split_comments.last
end

#nextObject

Returns the next changeset


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

def next
  @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
end

#previousObject

Returns the previous changeset


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

def previous
  @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
end

#projectObject


70
71
72
# File 'app/models/changeset.rb', line 70

def project
  repository.project
end

#repository_encodingObject

Delegate to a Repository's log encoding


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

def repository_encoding
  if repository.present?
    repository.repo_log_encoding
  else
    nil
  end
end

#revision=(r) ⇒ Object


43
44
45
# File 'app/models/changeset.rb', line 43

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
# 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_fix_keywords.downcase.split(",").collect(&:strip)

  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], 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
        referenced_issues << issue
        fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
        log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
      end
    end
  end

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

#short_commentsObject


147
148
149
# File 'app/models/changeset.rb', line 147

def short_comments
  @short_comments || split_comments.first
end

#text_tagObject


155
156
157
158
159
160
161
# File 'app/models/changeset.rb', line 155

def text_tag
  if scmid?
    "commit:#{scmid}"
  else
    "r#{revision}"
  end
end