diff options
Diffstat (limited to 'lib/gitlab/git/commit.rb')
-rw-r--r-- | lib/gitlab/git/commit.rb | 71 |
1 files changed, 51 insertions, 20 deletions
diff --git a/lib/gitlab/git/commit.rb b/lib/gitlab/git/commit.rb index 35991a383f6..c691ea3d223 100644 --- a/lib/gitlab/git/commit.rb +++ b/lib/gitlab/git/commit.rb @@ -4,13 +4,19 @@ module Gitlab module Git class Commit - attr_accessor :raw_commit, :head, :refs + attr_accessor :raw_commit, :head, :refs, + :sha, :authored_date, :committed_date, :message, + :author_name, :author_email, + :committer_name, :committer_email - delegate :message, :authored_date, :committed_date, :parents, :sha, - :date, :committer, :author, :diffs, :tree, :id, :stats, :to_patch, + delegate :parents, :diffs, :tree, :stats, :to_patch, to: :raw_commit class << self + def serialize_keys + %w(sha authored_date committed_date author_name author_email committer_name committer_email message) + end + def find_or_first(repo, commit_id = nil, root_ref) commit = if commit_id repo.commit(commit_id) @@ -73,10 +79,19 @@ module Gitlab def initialize(raw_commit, head = nil) raise "Nil as raw commit passed" unless raw_commit - @raw_commit = raw_commit + if raw_commit.is_a?(Hash) + init_from_hash(raw_commit) + else + init_from_grit(raw_commit) + end + @head = head end + def id + sha + end + def short_id(length = 10) id.to_s[0..length] end @@ -89,27 +104,11 @@ module Gitlab committed_date end - def author_email - author.email - end - - def author_name - author.name - 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 committer_name - committer.name - end - - def committer_email - committer.email - end - def prev_commit @prev_commit ||= if parents.present? Commit.new(parents.first) @@ -148,6 +147,38 @@ module Gitlab def no_commit_message "--no commit message" end + + def to_hash + hash = {} + + keys = Commit.serialize_keys + + keys.each do |key| + hash[key] = send(key) + end + + hash + end + + private + + def init_from_grit(grit) + @raw_commit = grit + @sha = grit.sha + @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 + end + + def init_from_hash(hash) + Commit.serialize_keys.each do |key| + send(:"#{key}=", hash[key]) + end + end end end end |