Class: Gitlab::Git::Commit

Inherits:
Object
  • Object
show all
Includes:
EncodingHelper
Defined in:
lib/gitlab_git/commit.rb

Constant Summary collapse

SERIALIZE_KEYS =
[
  :id, :message, :parent_ids,
  :authored_date, :author_name, :author_email,
  :committed_date, :committer_name, :committer_email
].freeze

Constants included from EncodingHelper

EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EncodingHelper

#encode!, #encode_utf8

Constructor Details

#initialize(raw_commit, head = nil) ⇒ Commit

Returns a new instance of Commit.



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/gitlab_git/commit.rb', line 134

def initialize(raw_commit, head = nil)
  raise "Nil as raw commit passed" unless raw_commit

  if raw_commit.is_a?(Hash)
    init_from_hash(raw_commit)
  elsif raw_commit.is_a?(Rugged::Commit)
    init_from_rugged(raw_commit)
  else
    raise "Invalid raw commit type: #{raw_commit.class}"
  end

  @head = head
end

Instance Attribute Details

#headObject

Returns the value of attribute head.



7
8
9
# File 'lib/gitlab_git/commit.rb', line 7

def head
  @head
end

#raw_commitObject

Returns the value of attribute raw_commit.



7
8
9
# File 'lib/gitlab_git/commit.rb', line 7

def raw_commit
  @raw_commit
end

#refs(repo) ⇒ Object

Get a collection of Rugged::Reference objects for this commit.

Ex.

commit.ref(repo)


239
240
241
# File 'lib/gitlab_git/commit.rb', line 239

def refs
  @refs
end

Class Method Details

.between(repo, base, head) ⇒ Object

Get commits between two revspecs See also #repository.commits_between

Ex.

Commit.between(repo, '29eda46b', 'master')


97
98
99
100
101
102
103
# File 'lib/gitlab_git/commit.rb', line 97

def between(repo, base, head)
  repo.commits_between(base, head).map do |commit|
    decorate(commit)
  end
rescue Rugged::ReferenceError
  []
end

.decorate(commit, ref = nil) ⇒ Object



110
111
112
# File 'lib/gitlab_git/commit.rb', line 110

def decorate(commit, ref = nil)
  Gitlab::Git::Commit.new(commit, ref)
end

.diff_from_parent(rugged_commit, options = {}) ⇒ Object

Returns a diff object for the changes introduced by rugged_commit. If rugged_commit doesn’t have a parent, then the diff is between this commit and an empty repo. See Repository#diff for the keys allowed in the options hash.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gitlab_git/commit.rb', line 118

def diff_from_parent(rugged_commit, options = {})
  options ||= {}
  break_rewrites = options[:break_rewrites]
  actual_options = Diff.filter_diff_options(options)

  diff = if rugged_commit.parents.empty?
           rugged_commit.diff(actual_options.merge(reverse: true))
         else
           rugged_commit.parents[0].diff(rugged_commit, actual_options)
         end

  diff.find_similar!(break_rewrites: break_rewrites)
  diff
end

.find(repo, commit_id = "HEAD") ⇒ Object

Get single commit

Ex.

Commit.find(repo, '29eda46b')

Commit.find(repo, 'master')


55
56
57
58
59
60
61
62
63
64
# File 'lib/gitlab_git/commit.rb', line 55

def find(repo, commit_id = "HEAD")
  return decorate(commit_id) if commit_id.is_a?(Rugged::Commit)

  obj = repo.rev_parse_target(commit_id)
  return nil unless obj.is_a?(Rugged::Commit)

  decorate(obj)
rescue Rugged::ReferenceError, Rugged::InvalidError, Rugged::ObjectError
  nil
end

.find_all(repo, options = {}) ⇒ Object

Delegate Repository#find_commits



106
107
108
# File 'lib/gitlab_git/commit.rb', line 106

def find_all(repo, options = {})
  repo.find_commits(options)
end

.last(repo) ⇒ Object

Get last commit for HEAD

Ex.

Commit.last(repo)


71
72
73
# File 'lib/gitlab_git/commit.rb', line 71

def last(repo)
  find(repo)
end

.last_for_path(repo, ref, path = nil) ⇒ Object

Get last commit for specified path and ref

Ex.

Commit.last_for_path(repo, '29eda46b', 'app/models')

Commit.last_for_path(repo, 'master', 'Gemfile')


82
83
84
85
86
87
88
89
# File 'lib/gitlab_git/commit.rb', line 82

def last_for_path(repo, ref, path = nil)
  where(
    repo: repo,
    ref: ref,
    path: path,
    limit: 1
  ).first
end

.where(options) ⇒ Object

Get commits collection

Ex.

Commit.where(
  repo: repo,
  ref: 'master',
  path: 'app/models',
  limit: 10,
  offset: 5,
)


41
42
43
44
45
46
# File 'lib/gitlab_git/commit.rb', line 41

def where(options)
  repo = options.delete(:repo)
  raise 'Gitlab::Git::Repository is required' unless repo.respond_to?(:log)

  repo.log(options).map { |c| decorate(c) }
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gitlab_git/commit.rb', line 17

def ==(other)
  return false unless other.is_a?(Gitlab::Git::Commit)

  methods = [:message, :parent_ids, :authored_date, :author_name,
             :author_email, :committed_date, :committer_name,
             :committer_email]

  methods.all? do |method|
    send(method) == other.send(method)
  end
end

#author_emailObject



262
263
264
# File 'lib/gitlab_git/commit.rb', line 262

def author_email
  encode! @author_email
end

#author_nameObject



258
259
260
# File 'lib/gitlab_git/commit.rb', line 258

def author_name
  encode! @author_name
end

#committer_emailObject



270
271
272
# File 'lib/gitlab_git/commit.rb', line 270

def committer_email
  encode! @committer_email
end

#committer_nameObject



266
267
268
# File 'lib/gitlab_git/commit.rb', line 266

def committer_name
  encode! @committer_name
end

#created_atObject



160
161
162
# File 'lib/gitlab_git/commit.rb', line 160

def created_at
  committed_date
end

#dateObject



204
205
206
# File 'lib/gitlab_git/commit.rb', line 204

def date
  committed_date
end

#diff_from_parent(options = {}) ⇒ Object

Returns a diff object for the changes from this commit’s first parent. If there is no parent, then the diff is between this commit and an empty repo. See Repository#diff for keys allowed in the options hash.



184
185
186
# File 'lib/gitlab_git/commit.rb', line 184

def diff_from_parent(options = {})
  Commit.diff_from_parent(raw_commit, options)
end

#different_committer?Boolean

Was this commit committed by a different person than the original author?

Returns:

  • (Boolean)


165
166
167
# File 'lib/gitlab_git/commit.rb', line 165

def different_committer?
  author_name != committer_name || author_email != committer_email
end

#diffs(options = {}) ⇒ Object



208
209
210
# File 'lib/gitlab_git/commit.rb', line 208

def diffs(options = {})
  DiffCollection.new(diff_from_parent(options), options)
end

#has_zero_stats?Boolean

Returns:

  • (Boolean)


188
189
190
191
192
# File 'lib/gitlab_git/commit.rb', line 188

def has_zero_stats?
  stats.total.zero?
rescue
  true
end

#messageObject



254
255
256
# File 'lib/gitlab_git/commit.rb', line 254

def message
  encode! @message
end

#no_commit_messageObject



194
195
196
# File 'lib/gitlab_git/commit.rb', line 194

def no_commit_message
  "--no commit message"
end

#parent_idObject



169
170
171
# File 'lib/gitlab_git/commit.rb', line 169

def parent_id
  parent_ids.first
end

#parentsObject



212
213
214
# File 'lib/gitlab_git/commit.rb', line 212

def parents
  raw_commit.parents.map { |c| Gitlab::Git::Commit.new(c) }
end

#ref_names(repo) ⇒ Object

Get ref names collection

Ex.

commit.ref_names(repo)


248
249
250
251
252
# File 'lib/gitlab_git/commit.rb', line 248

def ref_names(repo)
  refs(repo).map do |ref|
    ref.name.sub(%r{^refs/(heads|remotes|tags)/}, "")
  end
end

#safe_messageObject



156
157
158
# File 'lib/gitlab_git/commit.rb', line 156

def safe_message
  @safe_message ||= message
end

#shaObject



148
149
150
# File 'lib/gitlab_git/commit.rb', line 148

def sha
  id
end

#short_id(length = 10) ⇒ Object



152
153
154
# File 'lib/gitlab_git/commit.rb', line 152

def short_id(length = 10)
  id.to_s[0..length]
end

#statsObject



220
221
222
# File 'lib/gitlab_git/commit.rb', line 220

def stats
  Gitlab::Git::CommitStats.new(self)
end

#to_diff(options = {}) ⇒ Object

Shows the diff between the commit’s parent and the commit.

Cuts out the header and stats from #to_patch and returns only the diff.



176
177
178
# File 'lib/gitlab_git/commit.rb', line 176

def to_diff(options = {})
  diff_from_parent(options).patch
end

#to_hashObject



198
199
200
201
202
# File 'lib/gitlab_git/commit.rb', line 198

def to_hash
  serialize_keys.map.with_object({}) do |key, hash|
    hash[key] = send(key)
  end
end

#to_patch(options = {}) ⇒ Object



224
225
226
227
228
229
230
231
232
# File 'lib/gitlab_git/commit.rb', line 224

def to_patch(options = {})
  begin
    raw_commit.to_mbox(options)
  rescue Rugged::InvalidError => ex
    if ex.message =~ /Commit \w+ is a merge commit/
      'Patch format is not currently supported for merge commits.'
    end
  end
end

#treeObject



216
217
218
# File 'lib/gitlab_git/commit.rb', line 216

def tree
  raw_commit.tree
end