Class: Gitlab::Git::Commit
- Inherits:
-
Object
- Object
- Gitlab::Git::Commit
- 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 ]
Instance Attribute Summary collapse
-
#head ⇒ Object
Returns the value of attribute head.
-
#raw_commit ⇒ Object
Returns the value of attribute raw_commit.
-
#refs(repo) ⇒ Object
Get a collection of Rugged::Reference objects for this commit.
Class Method Summary collapse
-
.between(repo, base, head) ⇒ Object
Get commits between two refs.
- .decorate(commit, ref = nil) ⇒ Object
-
.diff_from_parent(rugged_commit, options = {}) ⇒ Object
Returns a diff object for the changes introduced by
rugged_commit
. -
.find(repo, commit_id = "HEAD") ⇒ Object
Get single commit.
-
.find_all(repo, options = {}) ⇒ Object
Delegate Repository#find_commits.
-
.last(repo) ⇒ Object
Get last commit for HEAD.
-
.last_for_path(repo, ref, path = nil) ⇒ Object
Get last commit for specified path and ref.
-
.where(options) ⇒ Object
Get commits collection.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #author_email ⇒ Object
- #author_name ⇒ Object
- #committer_email ⇒ Object
- #committer_name ⇒ Object
- #created_at ⇒ Object
- #date ⇒ Object
-
#diff_from_parent(options = {}) ⇒ Object
Returns a diff object for the changes from this commit’s first parent.
-
#different_committer? ⇒ Boolean
Was this commit committed by a different person than the original author?.
- #diffs(options = {}) ⇒ Object
- #has_zero_stats? ⇒ Boolean
-
#initialize(raw_commit, head = nil) ⇒ Commit
constructor
A new instance of Commit.
- #message ⇒ Object
- #no_commit_message ⇒ Object
- #parent_id ⇒ Object
- #parents ⇒ Object
-
#ref_names(repo) ⇒ Object
Get ref names collection.
- #safe_message ⇒ Object
- #sha ⇒ Object
- #short_id(length = 10) ⇒ Object
- #stats ⇒ Object
-
#to_diff(options = {}) ⇒ Object
Shows the diff between the commit’s parent and the commit.
- #to_hash ⇒ Object
- #to_patch(options = {}) ⇒ Object
- #tree ⇒ Object
Methods included from EncodingHelper
Constructor Details
#initialize(raw_commit, head = nil) ⇒ Commit
Returns a new instance of Commit.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/gitlab_git/commit.rb', line 132 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
#head ⇒ Object
Returns the value of attribute head.
7 8 9 |
# File 'lib/gitlab_git/commit.rb', line 7 def head @head end |
#raw_commit ⇒ Object
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)
237 238 239 |
# File 'lib/gitlab_git/commit.rb', line 237 def refs @refs end |
Class Method Details
.between(repo, base, head) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/gitlab_git/commit.rb', line 95 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
108 109 110 |
# File 'lib/gitlab_git/commit.rb', line 108 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.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/gitlab_git/commit.rb', line 116 def diff_from_parent(rugged_commit, = {}) ||= {} break_rewrites = [:break_rewrites] = Diff.() if rugged_commit.parents.empty? diff = rugged_commit.diff(.merge(reverse: true)) else diff = rugged_commit.parents[0].diff(rugged_commit, ) end diff.find_similar!(break_rewrites: break_rewrites) diff end |
.find(repo, commit_id = "HEAD") ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gitlab_git/commit.rb', line 54 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
104 105 106 |
# File 'lib/gitlab_git/commit.rb', line 104 def find_all(repo, = {}) repo.find_commits() end |
.last(repo) ⇒ Object
70 71 72 |
# File 'lib/gitlab_git/commit.rb', line 70 def last(repo) find(repo) end |
.last_for_path(repo, ref, path = nil) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/gitlab_git/commit.rb', line 81 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,
)
40 41 42 43 44 45 |
# File 'lib/gitlab_git/commit.rb', line 40 def where() repo = .delete(:repo) raise 'Gitlab::Git::Repository is required' unless repo.respond_to?(:log) repo.log().map { |c| decorate(c) } end |
Instance Method Details
#==(other) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gitlab_git/commit.rb', line 16 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_email ⇒ Object
260 261 262 |
# File 'lib/gitlab_git/commit.rb', line 260 def encode! @author_email end |
#author_name ⇒ Object
256 257 258 |
# File 'lib/gitlab_git/commit.rb', line 256 def encode! @author_name end |
#committer_email ⇒ Object
268 269 270 |
# File 'lib/gitlab_git/commit.rb', line 268 def committer_email encode! @committer_email end |
#committer_name ⇒ Object
264 265 266 |
# File 'lib/gitlab_git/commit.rb', line 264 def committer_name encode! @committer_name end |
#created_at ⇒ Object
158 159 160 |
# File 'lib/gitlab_git/commit.rb', line 158 def created_at committed_date end |
#date ⇒ Object
202 203 204 |
# File 'lib/gitlab_git/commit.rb', line 202 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.
182 183 184 |
# File 'lib/gitlab_git/commit.rb', line 182 def diff_from_parent( = {}) Commit.diff_from_parent(raw_commit, ) end |
#different_committer? ⇒ Boolean
Was this commit committed by a different person than the original author?
163 164 165 |
# File 'lib/gitlab_git/commit.rb', line 163 def different_committer? != committer_name || != committer_email end |
#diffs(options = {}) ⇒ Object
206 207 208 |
# File 'lib/gitlab_git/commit.rb', line 206 def diffs( = {}) diff_from_parent().map { |diff| Gitlab::Git::Diff.new(diff) } end |
#has_zero_stats? ⇒ Boolean
186 187 188 189 190 |
# File 'lib/gitlab_git/commit.rb', line 186 def has_zero_stats? stats.total.zero? rescue true end |
#message ⇒ Object
252 253 254 |
# File 'lib/gitlab_git/commit.rb', line 252 def encode! @message end |
#no_commit_message ⇒ Object
192 193 194 |
# File 'lib/gitlab_git/commit.rb', line 192 def "--no commit message" end |
#parent_id ⇒ Object
167 168 169 |
# File 'lib/gitlab_git/commit.rb', line 167 def parent_id parent_ids.first end |
#parents ⇒ Object
210 211 212 |
# File 'lib/gitlab_git/commit.rb', line 210 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)
246 247 248 249 250 |
# File 'lib/gitlab_git/commit.rb', line 246 def ref_names(repo) refs(repo).map do |ref| ref.name.sub(%r{^refs/(heads|remotes|tags)/}, "") end end |
#safe_message ⇒ Object
154 155 156 |
# File 'lib/gitlab_git/commit.rb', line 154 def @safe_message ||= end |
#sha ⇒ Object
146 147 148 |
# File 'lib/gitlab_git/commit.rb', line 146 def sha id end |
#short_id(length = 10) ⇒ Object
150 151 152 |
# File 'lib/gitlab_git/commit.rb', line 150 def short_id(length = 10) id.to_s[0..length] end |
#stats ⇒ Object
218 219 220 |
# File 'lib/gitlab_git/commit.rb', line 218 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.
174 175 176 |
# File 'lib/gitlab_git/commit.rb', line 174 def to_diff( = {}) diff_from_parent().patch end |
#to_hash ⇒ Object
196 197 198 199 200 |
# File 'lib/gitlab_git/commit.rb', line 196 def to_hash serialize_keys.map.with_object({}) do |key, hash| hash[key] = send(key) end end |
#to_patch(options = {}) ⇒ Object
222 223 224 225 226 227 228 229 230 |
# File 'lib/gitlab_git/commit.rb', line 222 def to_patch( = {}) begin raw_commit.to_mbox() rescue Rugged::InvalidError => ex if ex. =~ /Commit \w+ is a merge commit/ 'Patch format is not currently supported for merge commits.' end end end |
#tree ⇒ Object
214 215 216 |
# File 'lib/gitlab_git/commit.rb', line 214 def tree raw_commit.tree end |