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

Instance Method Summary collapse

Constructor Details

#initialize(raw_commit, head = nil) ⇒ Commit

Returns a new instance of Commit.



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

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

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 ⇒ Object

Returns the value of attribute refs.



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

def refs
  @refs
end

Instance Method Details

#created_at ⇒ Object



40
41
42
# File 'lib/gitlab_git/commit.rb', line 40

def created_at
  committed_date
end

#date ⇒ Object



86
87
88
# File 'lib/gitlab_git/commit.rb', line 86

def date
  committed_date
end

#different_committer? ⇒ Boolean

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/gitlab_git/commit.rb', line 45

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

#diffs ⇒ Object



90
91
92
# File 'lib/gitlab_git/commit.rb', line 90

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

#has_zero_stats? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#no_commit_message ⇒ Object



76
77
78
# File 'lib/gitlab_git/commit.rb', line 76

def no_commit_message
  "--no commit message"
end

#parent_id ⇒ Object



49
50
51
# File 'lib/gitlab_git/commit.rb', line 49

def parent_id
  parent_ids.first
end

#parents ⇒ Object



94
95
96
# File 'lib/gitlab_git/commit.rb', line 94

def parents
  raw_commit.parents
end

#safe_message ⇒ Object



36
37
38
# File 'lib/gitlab_git/commit.rb', line 36

def safe_message
  @safe_message ||= message
end

#sha ⇒ Object



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

def sha
  id
end

#short_id(length = 10) ⇒ Object



32
33
34
# File 'lib/gitlab_git/commit.rb', line 32

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

#stats ⇒ Object



102
103
104
# File 'lib/gitlab_git/commit.rb', line 102

def stats
  raw_commit.stats
end

#to_diff ⇒ 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.



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

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_hash ⇒ Object



80
81
82
83
84
# File 'lib/gitlab_git/commit.rb', line 80

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

#to_patch ⇒ Object



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

def to_patch
  raw_commit.to_patch
end

#tree ⇒ Object



98
99
100
# File 'lib/gitlab_git/commit.rb', line 98

def tree
  raw_commit.tree
end