Commit 5f4445c3d384741c45242f077b3c0dbf76234ee8

Authored by Dmitriy Zaporozhets
1 parent 7af16bbb

store commits for MR as array of hashes

app/controllers/refs_controller.rb
... ... @@ -48,7 +48,7 @@ class RefsController < ProjectResourceController
48 48  
49 49 @repo = project.repository
50 50 @commit = @repo.commit(@ref)
51   - @tree = Tree.new(@commit.tree, @ref, params[:path])
  51 + @tree = Tree.new(@repo, @commit.id, @ref, params[:path])
52 52 @hex_path = Digest::SHA1.hexdigest(params[:path] || "")
53 53  
54 54 if params[:path]
... ...
app/models/merge_request.rb
... ... @@ -152,17 +152,7 @@ class MergeRequest < ActiveRecord::Base
152 152 end
153 153  
154 154 def commits
155   - if st_commits.present?
156   - # check if merge request commits are valid
157   - if st_commits.first.respond_to?(:short_id)
158   - st_commits
159   - else
160   - # if commits are invalid - simply reload it from repo
161   - reloaded_commits
162   - end
163   - else
164   - []
165   - end
  155 + load_commits(st_commits) || []
166 156 end
167 157  
168 158 def probably_merged?
... ... @@ -172,13 +162,7 @@ class MergeRequest < ActiveRecord::Base
172 162  
173 163 def reloaded_commits
174 164 if opened? && unmerged_commits.any?
175   - # we need to reset st_commits field first
176   - # in order to prevent internal rails comparison
177   - self.st_commits = []
178   - save
179   -
180   - # Then we can safely write unmerged commits
181   - self.st_commits = unmerged_commits
  165 + self.st_commits = dump_commits(unmerged_commits)
182 166 save
183 167 end
184 168 commits
... ... @@ -228,4 +212,14 @@ class MergeRequest < ActiveRecord::Base
228 212 def last_commit_short_sha
229 213 @last_commit_short_sha ||= last_commit.sha[0..10]
230 214 end
  215 +
  216 + private
  217 +
  218 + def dump_commits(commits)
  219 + commits.map(&:to_hash)
  220 + end
  221 +
  222 + def load_commits(array)
  223 + array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash)) }
  224 + end
231 225 end
... ...
app/models/tree.rb
1 1 class Tree
2   - include Linguist::BlobHelper
3   -
4 2 attr_accessor :path, :tree, :ref
5 3  
6   - delegate :contents, :basename, :name, :data, :mime_type,
7   - :mode, :size, :text?, :colorize, to: :tree
8   -
9   - def initialize(raw_tree, ref = nil, path = nil)
10   - @ref, @path = ref, path
11   - @tree = if path.present?
12   - raw_tree / path
13   - else
14   - raw_tree
15   - end
16   - end
17   -
18   - def is_blob?
19   - tree.is_a?(Grit::Blob)
  4 + def initialize(repository, sha, ref = nil, path = nil)
  5 + @raw = Gitlab::Git::Tree.new(repository, sha, ref, path)
20 6 end
21 7  
22 8 def invalid?
23   - tree.nil?
  9 + @raw.nil?
24 10 end
25 11  
26   - def empty?
27   - data.blank?
  12 + def method_missing(m, *args, &block)
  13 + @raw.send(m, *args, &block)
28 14 end
29 15  
30   - def up_dir?
31   - path.present?
32   - end
  16 + def respond_to?(method)
  17 + return true if @raw.respond_to?(method)
33 18  
34   - def readme
35   - @readme ||= contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
  19 + super
36 20 end
37 21 end
... ...
app/services/git_push_service.rb
... ... @@ -104,7 +104,7 @@ class GitPushService
104 104 data[:commits] << {
105 105 id: commit.id,
106 106 message: commit.safe_message,
107   - timestamp: commit.date.xmlschema,
  107 + timestamp: commit.committed_date.xmlschema,
108 108 url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/#{commit.id}",
109 109 author: {
110 110 name: commit.author_name,
... ...
app/views/commits/_diffs.html.haml
... ... @@ -16,7 +16,7 @@
16 16 - unless @suppress_diff
17 17 - diffs.each_with_index do |diff, i|
18 18 - next if diff.diff.empty?
19   - - file = (@commit.tree / diff.new_path)
  19 + - file = Tree.new(@repository, @commit.id, @ref, diff.new_path)
20 20 - file = (@commit.prev_commit.tree / diff.old_path) unless file
21 21 - next unless file
22 22 .file{id: "diff-#{i}"}
... ...
lib/extracts_path.rb
... ... @@ -102,7 +102,7 @@ module ExtractsPath
102 102 # because "@project.repository.commit(@ref)" returns wrong commit when @ref is tag name.
103 103 @commit = @project.repository.commits(@ref, @path, 1, 0).first
104 104  
105   - @tree = Tree.new(@commit.tree, @ref, @path)
  105 + @tree = Tree.new(@project.repository, @commit.id, @ref, @path)
106 106  
107 107 raise InvalidPathError if @tree.invalid?
108 108 rescue RuntimeError, NoMethodError, InvalidPathError
... ...
lib/gitlab/git/commit.rb
... ... @@ -4,13 +4,19 @@
4 4 module Gitlab
5 5 module Git
6 6 class Commit
7   - attr_accessor :raw_commit, :head, :refs
  7 + attr_accessor :raw_commit, :head, :refs,
  8 + :sha, :authored_date, :committed_date, :message,
  9 + :author_name, :author_email,
  10 + :committer_name, :committer_email
8 11  
9   - delegate :message, :authored_date, :committed_date, :parents, :sha,
10   - :date, :committer, :author, :diffs, :tree, :id, :stats, :to_patch,
  12 + delegate :parents, :diffs, :tree, :stats, :to_patch,
11 13 to: :raw_commit
12 14  
13 15 class << self
  16 + def serialize_keys
  17 + %w(sha authored_date committed_date author_name author_email committer_name committer_email message)
  18 + end
  19 +
14 20 def find_or_first(repo, commit_id = nil, root_ref)
15 21 commit = if commit_id
16 22 repo.commit(commit_id)
... ... @@ -73,10 +79,19 @@ module Gitlab
73 79 def initialize(raw_commit, head = nil)
74 80 raise "Nil as raw commit passed" unless raw_commit
75 81  
76   - @raw_commit = raw_commit
  82 + if raw_commit.is_a?(Hash)
  83 + init_from_hash(raw_commit)
  84 + else
  85 + init_from_grit(raw_commit)
  86 + end
  87 +
77 88 @head = head
78 89 end
79 90  
  91 + def id
  92 + sha
  93 + end
  94 +
80 95 def short_id(length = 10)
81 96 id.to_s[0..length]
82 97 end
... ... @@ -89,27 +104,11 @@ module Gitlab
89 104 committed_date
90 105 end
91 106  
92   - def author_email
93   - author.email
94   - end
95   -
96   - def author_name
97   - author.name
98   - end
99   -
100 107 # Was this commit committed by a different person than the original author?
101 108 def different_committer?
102 109 author_name != committer_name || author_email != committer_email
103 110 end
104 111  
105   - def committer_name
106   - committer.name
107   - end
108   -
109   - def committer_email
110   - committer.email
111   - end
112   -
113 112 def prev_commit
114 113 @prev_commit ||= if parents.present?
115 114 Commit.new(parents.first)
... ... @@ -148,6 +147,38 @@ module Gitlab
148 147 def no_commit_message
149 148 "--no commit message"
150 149 end
  150 +
  151 + def to_hash
  152 + hash = {}
  153 +
  154 + keys = Commit.serialize_keys
  155 +
  156 + keys.each do |key|
  157 + hash[key] = send(key)
  158 + end
  159 +
  160 + hash
  161 + end
  162 +
  163 + private
  164 +
  165 + def init_from_grit(grit)
  166 + @raw_commit = grit
  167 + @sha = grit.sha
  168 + @message = grit.message
  169 + @authored_date = grit.authored_date
  170 + @committed_date = grit.committed_date
  171 + @author_name = grit.author.name
  172 + @author_email = grit.author.email
  173 + @committer_name = grit.committer.name
  174 + @committer_email = grit.committer.email
  175 + end
  176 +
  177 + def init_from_hash(hash)
  178 + Commit.serialize_keys.each do |key|
  179 + send(:"#{key}=", hash[key])
  180 + end
  181 + end
151 182 end
152 183 end
153 184 end
... ...
lib/gitlab/git/tree.rb 0 → 100644
... ... @@ -0,0 +1,38 @@
  1 +module Gitlab
  2 + module Git
  3 + class Tree
  4 + include Linguist::BlobHelper
  5 +
  6 + attr_accessor :repository, :sha, :path, :ref, :raw_tree
  7 +
  8 + def initialize(repository, sha, ref = nil, path = nil)
  9 + @repository, @sha, @ref = repository, sha, ref
  10 +
  11 + # Load tree from repository
  12 + @commit = @repository.commit(sha)
  13 + @raw_tree = @repository.tree(@commit, path)
  14 + end
  15 +
  16 + def empty?
  17 + data.blank?
  18 + end
  19 +
  20 + def data
  21 + raw_tree.data
  22 + end
  23 +
  24 + def is_blob?
  25 + tree.is_a?(Grit::Blob)
  26 + end
  27 +
  28 + def up_dir?
  29 + path.present?
  30 + end
  31 +
  32 + def readme
  33 + @readme ||= contents.find { |c| c.is_a?(Grit::Blob) and c.name =~ /^readme/i }
  34 + end
  35 + end
  36 + end
  37 +end
  38 +
... ...