Class: Gitlab::Git::Commit
- Inherits:
-
Object
- Object
- Gitlab::Git::Commit
- 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 refs collection(Grit::Head or Grit::Remote or Grit::Tag).
Class Method Summary collapse
-
.between(repo, base, head) ⇒ Object
Get commits between two refs.
- .decorate(commit, ref = nil) ⇒ Object
-
.find(repo, commit_id = nil) ⇒ 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
- #created_at ⇒ Object
- #date ⇒ Object
-
#different_committer? ⇒ Boolean
Was this commit committed by a different person than the original author?.
- #diffs ⇒ Object
- #has_zero_stats? ⇒ Boolean
-
#initialize(raw_commit, head = nil) ⇒ Commit
constructor
A new instance of Commit.
- #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 ⇒ Object
Shows the diff between the commit’s parent and the commit.
- #to_hash ⇒ Object
- #to_patch ⇒ Object
- #tree ⇒ Object
Constructor Details
#initialize(raw_commit, head = nil) ⇒ Commit
Returns a new instance of Commit.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/gitlab_git/commit.rb', line 104 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(repo) ⇒ Object
Get refs collection(Grit::Head or Grit::Remote or Grit::Tag)
Ex.
commit.ref(repo)
203 204 205 |
# File 'lib/gitlab_git/commit.rb', line 203 def refs @refs end |
Class Method Details
.between(repo, base, head) ⇒ Object
88 89 90 91 92 |
# File 'lib/gitlab_git/commit.rb', line 88 def between(repo, base, head) repo.commits_between(base, head).map do |commit| decorate(commit) end end |
.decorate(commit, ref = nil) ⇒ Object
99 100 101 |
# File 'lib/gitlab_git/commit.rb', line 99 def decorate(commit, ref = nil) Gitlab::Git::Commit.new(commit, ref) end |
.find(repo, commit_id = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gitlab_git/commit.rb', line 42 def find(repo, commit_id = nil) # Find repo.tags first, # because if commit_id is "tag name", # repo.commit(commit_id) returns wrong commit sha # that is git tag object sha. tag = repo..find {|tag| tag.name == commit_id} if commit_id commit = if tag tag.commit else repo.log(ref: commit_id).first end decorate(commit) if commit end |
.find_all(repo, options = {}) ⇒ Object
Delegate Repository#find_commits
95 96 97 |
# File 'lib/gitlab_git/commit.rb', line 95 def find_all(repo, = {}) repo.find_commits() end |
.last(repo) ⇒ Object
63 64 65 |
# File 'lib/gitlab_git/commit.rb', line 63 def last(repo) find(repo, nil) end |
.last_for_path(repo, ref, path = nil) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/gitlab_git/commit.rb', line 74 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() repo = .delete(:repo) raise 'Gitlab::Git::Repository is required' unless repo.respond_to?(:log) repo.log().map { |c| decorate(c) } end |
Instance Method Details
#created_at ⇒ Object
128 129 130 |
# File 'lib/gitlab_git/commit.rb', line 128 def created_at committed_date end |
#date ⇒ Object
174 175 176 |
# File 'lib/gitlab_git/commit.rb', line 174 def date committed_date end |
#different_committer? ⇒ Boolean
Was this commit committed by a different person than the original author?
133 134 135 |
# File 'lib/gitlab_git/commit.rb', line 133 def different_committer? != committer_name || != committer_email end |
#diffs ⇒ Object
178 179 180 |
# File 'lib/gitlab_git/commit.rb', line 178 def diffs raw_commit.diffs.map { |diff| Gitlab::Git::Diff.new(diff) } end |
#has_zero_stats? ⇒ Boolean
158 159 160 161 162 |
# File 'lib/gitlab_git/commit.rb', line 158 def has_zero_stats? stats.total.zero? rescue true end |
#no_commit_message ⇒ Object
164 165 166 |
# File 'lib/gitlab_git/commit.rb', line 164 def "--no commit message" end |
#parent_id ⇒ Object
137 138 139 |
# File 'lib/gitlab_git/commit.rb', line 137 def parent_id parent_ids.first end |
#parents ⇒ Object
182 183 184 |
# File 'lib/gitlab_git/commit.rb', line 182 def parents raw_commit.parents end |
#ref_names(repo) ⇒ Object
Get ref names collection
Ex.
commit.ref_names(repo)
212 213 214 |
# File 'lib/gitlab_git/commit.rb', line 212 def ref_names(repo) refs(repo).map(&:name) end |
#safe_message ⇒ Object
124 125 126 |
# File 'lib/gitlab_git/commit.rb', line 124 def @safe_message ||= end |
#sha ⇒ Object
116 117 118 |
# File 'lib/gitlab_git/commit.rb', line 116 def sha id end |
#short_id(length = 10) ⇒ Object
120 121 122 |
# File 'lib/gitlab_git/commit.rb', line 120 def short_id(length = 10) id.to_s[0..length] end |
#stats ⇒ Object
190 191 192 |
# File 'lib/gitlab_git/commit.rb', line 190 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.
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gitlab_git/commit.rb', line 144 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
168 169 170 171 172 |
# File 'lib/gitlab_git/commit.rb', line 168 def to_hash serialize_keys.map.with_object({}) do |key, hash| hash[key] = send(key) end end |
#to_patch ⇒ Object
194 195 196 |
# File 'lib/gitlab_git/commit.rb', line 194 def to_patch raw_commit.to_patch end |
#tree ⇒ Object
186 187 188 |
# File 'lib/gitlab_git/commit.rb', line 186 def tree raw_commit.tree end |