1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Gitlab::Git::Commit is a wrapper around native Grit::Commit object
# We dont want to use grit objects inside app/
# It helps us easily migrate to rugged in future
module Gitlab
module Git
class Commit
attr_accessor :raw_commit, :head, :refs,
:id, :authored_date, :committed_date, :message,
:author_name, :author_email, :parent_ids,
:committer_name, :committer_email
delegate :parents, :diffs, :tree, :stats, :to_patch,
to: :raw_commit
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)
else
init_from_grit(raw_commit)
end
@head = head
end
def serialize_keys
%w(id authored_date committed_date author_name author_email committer_name committer_email message parent_ids)
end
def sha
id
end
def short_id(length = 10)
id.to_s[0..length]
end
def safe_message
@safe_message ||= message
end
def created_at
committed_date
end
# Was this commit committed by a different person than the original author?
def different_committer?
author_name != committer_name || author_email != committer_email
end
def parent_id
parent_ids.first
end
# 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.
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
def has_zero_stats?
stats.total.zero?
rescue
true
end
def no_commit_message
"--no commit message"
end
def to_hash
hash = {}
keys = serialize_keys
keys.each do |key|
hash[key] = send(key)
end
hash
end
def date
committed_date
end
private
def init_from_grit(grit)
@raw_commit = grit
@id = grit.id
@message = grit.message
@authored_date = grit.authored_date
@committed_date = grit.committed_date
@author_name = grit.author.name
@author_email = grit.author.email
@committer_name = grit.committer.name
@committer_email = grit.committer.email
@parent_ids = grit.parents.map(&:id)
end
def init_from_hash(hash)
serialize_keys.each do |key|
send(:"#{key}=", hash[key])
end
end
end
end
end