Commit 5f4445c3 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

store commits for MR as array of hashes

parent 7af16bbb
...@@ -48,7 +48,7 @@ class RefsController < ProjectResourceController ...@@ -48,7 +48,7 @@ class RefsController < ProjectResourceController
@repo = project.repository @repo = project.repository
@commit = @repo.commit(@ref) @commit = @repo.commit(@ref)
@tree = Tree.new(@commit.tree, @ref, params[:path]) @tree = Tree.new(@repo, @commit.id, @ref, params[:path])
@hex_path = Digest::SHA1.hexdigest(params[:path] || "") @hex_path = Digest::SHA1.hexdigest(params[:path] || "")
if params[:path] if params[:path]
......
...@@ -152,17 +152,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -152,17 +152,7 @@ class MergeRequest < ActiveRecord::Base
end end
def commits def commits
if st_commits.present? load_commits(st_commits) || []
# check if merge request commits are valid
if st_commits.first.respond_to?(:short_id)
st_commits
else
# if commits are invalid - simply reload it from repo
reloaded_commits
end
else
[]
end
end end
def probably_merged? def probably_merged?
...@@ -172,13 +162,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -172,13 +162,7 @@ class MergeRequest < ActiveRecord::Base
def reloaded_commits def reloaded_commits
if opened? && unmerged_commits.any? if opened? && unmerged_commits.any?
# we need to reset st_commits field first self.st_commits = dump_commits(unmerged_commits)
# in order to prevent internal rails comparison
self.st_commits = []
save
# Then we can safely write unmerged commits
self.st_commits = unmerged_commits
save save
end end
commits commits
...@@ -228,4 +212,14 @@ class MergeRequest < ActiveRecord::Base ...@@ -228,4 +212,14 @@ class MergeRequest < ActiveRecord::Base
def last_commit_short_sha def last_commit_short_sha
@last_commit_short_sha ||= last_commit.sha[0..10] @last_commit_short_sha ||= last_commit.sha[0..10]
end end
private
def dump_commits(commits)
commits.map(&:to_hash)
end
def load_commits(array)
array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash)) }
end
end end
class Tree class Tree
include Linguist::BlobHelper
attr_accessor :path, :tree, :ref attr_accessor :path, :tree, :ref
delegate :contents, :basename, :name, :data, :mime_type, def initialize(repository, sha, ref = nil, path = nil)
:mode, :size, :text?, :colorize, to: :tree @raw = Gitlab::Git::Tree.new(repository, sha, ref, path)
def initialize(raw_tree, ref = nil, path = nil)
@ref, @path = ref, path
@tree = if path.present?
raw_tree / path
else
raw_tree
end
end
def is_blob?
tree.is_a?(Grit::Blob)
end end
def invalid? def invalid?
tree.nil? @raw.nil?
end end
def empty? def method_missing(m, *args, &block)
data.blank? @raw.send(m, *args, &block)
end end
def up_dir? def respond_to?(method)
path.present? return true if @raw.respond_to?(method)
end
def readme super
@readme ||= contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
end end
end end
...@@ -104,7 +104,7 @@ class GitPushService ...@@ -104,7 +104,7 @@ class GitPushService
data[:commits] << { data[:commits] << {
id: commit.id, id: commit.id,
message: commit.safe_message, message: commit.safe_message,
timestamp: commit.date.xmlschema, timestamp: commit.committed_date.xmlschema,
url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/#{commit.id}", url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/#{commit.id}",
author: { author: {
name: commit.author_name, name: commit.author_name,
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
- unless @suppress_diff - unless @suppress_diff
- diffs.each_with_index do |diff, i| - diffs.each_with_index do |diff, i|
- next if diff.diff.empty? - next if diff.diff.empty?
- file = (@commit.tree / diff.new_path) - file = Tree.new(@repository, @commit.id, @ref, diff.new_path)
- file = (@commit.prev_commit.tree / diff.old_path) unless file - file = (@commit.prev_commit.tree / diff.old_path) unless file
- next unless file - next unless file
.file{id: "diff-#{i}"} .file{id: "diff-#{i}"}
......
...@@ -102,7 +102,7 @@ module ExtractsPath ...@@ -102,7 +102,7 @@ module ExtractsPath
# because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name. # because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name.
@commit = @project.repository.commits(@ref, @path, 1, 0).first @commit = @project.repository.commits(@ref, @path, 1, 0).first
@tree = Tree.new(@commit.tree, @ref, @path) @tree = Tree.new(@project.repository, @commit.id, @ref, @path)
raise InvalidPathError if @tree.invalid? raise InvalidPathError if @tree.invalid?
rescue RuntimeError, NoMethodError, InvalidPathError rescue RuntimeError, NoMethodError, InvalidPathError
......
...@@ -4,13 +4,19 @@ ...@@ -4,13 +4,19 @@
module Gitlab module Gitlab
module Git module Git
class Commit 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, delegate :parents, :diffs, :tree, :stats, :to_patch,
:date, :committer, :author, :diffs, :tree, :id, :stats, :to_patch,
to: :raw_commit to: :raw_commit
class << self 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) def find_or_first(repo, commit_id = nil, root_ref)
commit = if commit_id commit = if commit_id
repo.commit(commit_id) repo.commit(commit_id)
...@@ -73,10 +79,19 @@ module Gitlab ...@@ -73,10 +79,19 @@ module Gitlab
def initialize(raw_commit, head = nil) def initialize(raw_commit, head = nil)
raise "Nil as raw commit passed" unless raw_commit 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 @head = head
end end
def id
sha
end
def short_id(length = 10) def short_id(length = 10)
id.to_s[0..length] id.to_s[0..length]
end end
...@@ -89,27 +104,11 @@ module Gitlab ...@@ -89,27 +104,11 @@ module Gitlab
committed_date committed_date
end 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? # Was this commit committed by a different person than the original author?
def different_committer? def different_committer?
author_name != committer_name || author_email != committer_email author_name != committer_name || author_email != committer_email
end end
def committer_name
committer.name
end
def committer_email
committer.email
end
def prev_commit def prev_commit
@prev_commit ||= if parents.present? @prev_commit ||= if parents.present?
Commit.new(parents.first) Commit.new(parents.first)
...@@ -148,6 +147,38 @@ module Gitlab ...@@ -148,6 +147,38 @@ module Gitlab
def no_commit_message def no_commit_message
"--no commit message" "--no commit message"
end 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 end
end end
module Gitlab
module Git
class Tree
include Linguist::BlobHelper
attr_accessor :repository, :sha, :path, :ref, :raw_tree
def initialize(repository, sha, ref = nil, path = nil)
@repository, @sha, @ref = repository, sha, ref
# Load tree from repository
@commit = @repository.commit(sha)
@raw_tree = @repository.tree(@commit, path)
end
def empty?
data.blank?
end
def data
raw_tree.data
end
def is_blob?
tree.is_a?(Grit::Blob)
end
def up_dir?
path.present?
end
def readme
@readme ||= contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment