Commit 6a4a17f3399904b43275c4a2d53322cb2b011aca

Authored by Dmitriy Zaporozhets
1 parent 5f8f685b

Remove native git calls from MR. Dump diff as array, not class

Showing 1 changed file with 12 additions and 14 deletions   Show diff stats
app/models/merge_request.rb
... ... @@ -24,8 +24,6 @@ require Rails.root.join("lib/static_model")
24 24 class MergeRequest < ActiveRecord::Base
25 25 include Issuable
26 26  
27   - BROKEN_DIFF = "--broken-diff"
28   -
29 27 attr_accessible :title, :assignee_id, :target_branch, :source_branch, :milestone_id,
30 28 :author_id_of_changes, :state_event
31 29  
... ... @@ -109,22 +107,18 @@ class MergeRequest &lt; ActiveRecord::Base
109 107 end
110 108  
111 109 def diffs
112   - st_diffs || []
  110 + load_diffs(st_diffs) || []
113 111 end
114 112  
115 113 def reloaded_diffs
116 114 if opened? && unmerged_diffs.any?
117   - self.st_diffs = unmerged_diffs
  115 + self.st_diffs = dump_diffs(unmerged_diffs)
118 116 self.save
119 117 end
120   -
121   - rescue Grit::Git::GitTimeout
122   - self.st_diffs = [BROKEN_DIFF]
123   - self.save
124 118 end
125 119  
126 120 def broken_diffs?
127   - diffs == [BROKEN_DIFF]
  121 + diffs == [Gitlab::Git::Diff::BROKEN_DIFF]
128 122 end
129 123  
130 124 def valid_diffs?
... ... @@ -132,11 +126,7 @@ class MergeRequest &lt; ActiveRecord::Base
132 126 end
133 127  
134 128 def unmerged_diffs
135   - # Only show what is new in the source branch compared to the target branch, not the other way around.
136   - # The linex below with merge_base is equivalent to diff with three dots (git diff branch1...branch2)
137   - # From the git documentation: "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B"
138   - common_commit = project.repo.git.native(:merge_base, {}, [target_branch, source_branch]).strip
139   - diffs = project.repo.diff(common_commit, source_branch)
  129 + project.repository.diffs_between(source_branch, target_branch)
140 130 end
141 131  
142 132 def last_commit
... ... @@ -222,4 +212,12 @@ class MergeRequest &lt; ActiveRecord::Base
222 212 def load_commits(array)
223 213 array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash)) }
224 214 end
  215 +
  216 + def dump_diffs(diffs)
  217 + diffs.map(&:to_hash)
  218 + end
  219 +
  220 + def load_diffs(array)
  221 + array.map { |hash| Gitlab::Git::Diff.new(hash) }
  222 + end
225 223 end
... ...