Class: Gitlab::Git::Commit

Inherits:
Object
  • Object
show all
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
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_commit, head = nil) ⇒ Commit

Returns a new instance of Commit.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitlab_git/commit.rb', line 93

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
    init_from_grit(raw_commit)
  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 refs collection(Grit::Head or Grit::Remote or Grit::Tag)

Ex.

commit.ref(repo)


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

def refs
  @refs
end

Class Method Details

.between(repo, base, head) ⇒ Object

Get commits between two refs

Ex.

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


77
78
79
80
81
# File 'lib/gitlab_git/commit.rb', line 77

def between(repo, base, head)
  repo.commits_between(base, head).map do |commit|
    decorate(commit)
  end
end

.decorate(commit, ref = nil) ⇒ Object



88
89
90
# File 'lib/gitlab_git/commit.rb', line 88

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

.find(repo, commit_id = nil) ⇒ Object

Get single commit

Ex.

Commit.find(repo, '29eda46b')

Commit.find(repo, 'master')


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

def find(repo, commit_id = nil)
  commit = repo.log(ref: commit_id, limit: 1).first
  decorate(commit) if commit
end

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

Delegate Repository#find_commits



84
85
86
# File 'lib/gitlab_git/commit.rb', line 84

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

.last(repo) ⇒ Object

Get last commit for HEAD

Ex.

Commit.last(repo)


52
53
54
# File 'lib/gitlab_git/commit.rb', line 52

def last(repo)
  find(repo, nil)
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')


63
64
65
66
67
68
69
70
# File 'lib/gitlab_git/commit.rb', line 63

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,
)


28
29
30
31
32
33
# File 'lib/gitlab_git/commit.rb', line 28

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

#created_atObject



119
120
121
# File 'lib/gitlab_git/commit.rb', line 119

def created_at
  committed_date
end

#dateObject



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

def date
  committed_date
end

#different_committer?Boolean

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

Returns:

  • (Boolean)


124
125
126
# File 'lib/gitlab_git/commit.rb', line 124

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

#diffsObject



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

def diffs
  raw_commit.diffs.map { |diff| Gitlab::Git::Diff.new(diff) }
end

#has_zero_stats?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/gitlab_git/commit.rb', line 149

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

#no_commit_messageObject



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

def no_commit_message
  "--no commit message"
end

#parent_idObject



128
129
130
# File 'lib/gitlab_git/commit.rb', line 128

def parent_id
  parent_ids.first
end

#parentsObject



173
174
175
# File 'lib/gitlab_git/commit.rb', line 173

def parents
  raw_commit.parents
end

#ref_names(repo) ⇒ Object

Get ref names collection

Ex.

commit.ref_names(repo)


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

def ref_names(repo)
  refs(repo).map(&:name)
end

#safe_messageObject



115
116
117
# File 'lib/gitlab_git/commit.rb', line 115

def safe_message
  @safe_message ||= message
end

#shaObject



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

def sha
  id
end

#short_id(length = 10) ⇒ Object



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

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

#statsObject



181
182
183
# File 'lib/gitlab_git/commit.rb', line 181

def stats
  raw_commit.stats
end

#to_diffObject

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.



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

def to_diff
  # see Grit::Commit#show
  patch = to_patch

  # discard lines before the diff
  lines = patch.split("\n")
  while !lines.first.start_with?("diff --git") do
    lines.shift
  end
  lines.pop if lines.last =~ /^[\d.]+$/ # Git version
  lines.pop if lines.last == "-- "      # end of diff
  lines.join("\n")
end

#to_hashObject



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

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

#to_patchObject



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

def to_patch
  raw_commit.to_patch
end

#treeObject



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

def tree
  raw_commit.tree
end