Class: Gitlab::Git::Commit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_commit, head = nil) ⇒ Commit

Returns a new instance of Commit.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitlab_git/commit.rb', line 12

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

#author_emailObject

Returns the value of attribute author_email.



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

def author_email
  @author_email
end

#author_nameObject

Returns the value of attribute author_name.



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

def author_name
  @author_name
end

#authored_dateObject

Returns the value of attribute authored_date.



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

def authored_date
  @authored_date
end

#committed_dateObject

Returns the value of attribute committed_date.



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

def committed_date
  @committed_date
end

#committer_emailObject

Returns the value of attribute committer_email.



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

def committer_email
  @committer_email
end

#committer_nameObject

Returns the value of attribute committer_name.



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

def committer_name
  @committer_name
end

#headObject

Returns the value of attribute head.



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

def head
  @head
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#parent_idsObject

Returns the value of attribute parent_ids.



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

def parent_ids
  @parent_ids
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

#refsObject

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_atObject



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

def created_at
  committed_date
end

#dateObject



92
93
94
# File 'lib/gitlab_git/commit.rb', line 92

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

#diffsObject



96
97
98
# File 'lib/gitlab_git/commit.rb', line 96

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_messageObject



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

def no_commit_message
  "--no commit message"
end

#parent_idObject



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

def parent_id
  parent_ids.first
end

#parentsObject



100
101
102
# File 'lib/gitlab_git/commit.rb', line 100

def parents
  raw_commit.parents
end

#safe_messageObject



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

def safe_message
  @safe_message ||= message
end

#serialize_keysObject



24
25
26
# File 'lib/gitlab_git/commit.rb', line 24

def serialize_keys
  @serialize_keys ||= %w(id authored_date committed_date author_name author_email committer_name committer_email message parent_ids).map(&:to_sym)
end

#shaObject



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

#statsObject



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

def stats
  raw_commit.tree
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.



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_hashObject



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

def to_hash
  hash = {}

  keys = serialize_keys

  keys.each do |key|
    hash[key] = send(key)
  end

  hash
end

#to_patchObject



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

def to_patch
  raw_commit.to_patch
end

#treeObject



104
105
106
# File 'lib/gitlab_git/commit.rb', line 104

def tree
  raw_commit.tree
end