lib/gitlab_git/diff.rb in gitlab_git-6.3.0 vs lib/gitlab_git/diff.rb in gitlab_git-7.0.0.rc1

- old
+ new

@@ -1,12 +1,11 @@ -# Gitlab::Git::Diff is a wrapper around native Grit::Diff object -# We dont want to use grit objects inside app/ -# It helps us easily migrate to rugged in future +# Gitlab::Git::Diff is a wrapper around native Rugged::Diff object module Gitlab module Git class Diff class TimeoutError < StandardError; end + include EncodingHelper attr_accessor :raw_diff # Diff properties attr_accessor :old_path, :new_path, :a_mode, :b_mode, :diff @@ -19,25 +18,23 @@ # Only show what is new in the source branch compared to the target branch, not the other way around. # The linex below with merge_base is equivalent to diff with three dots (git diff branch1...branch2) # From the git documentation: "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B" common_commit = repo.merge_base_commit(head, base) - repo.diff(common_commit, head, *paths).map do |diff| - Gitlab::Git::Diff.new(diff) - end - rescue Grit::Git::GitTimeout - raise TimeoutError.new("Diff.between exited with timeout") + repo.diff(common_commit, head, *paths) end end def initialize(raw_diff) raise "Nil as raw diff passed" unless raw_diff if raw_diff.is_a?(Hash) init_from_hash(raw_diff) + elsif raw_diff.is_a?(Rugged::Patch) + init_from_rugged(raw_diff) else - init_from_grit(raw_diff) + raise "Invalid raw diff type: #{raw_diff.class}" end end def serialize_keys @serialize_keys ||= %w(diff new_path old_path a_mode b_mode new_file renamed_file deleted_file).map(&:to_sym) @@ -55,23 +52,38 @@ hash end private - def init_from_grit(grit) - @raw_diff = grit + def init_from_rugged(rugged) + @raw_diff = rugged - serialize_keys.each do |key| - send(:"#{key}=", grit.send(key)) - end + @diff = encode!(strip_diff_headers(rugged.to_s)) + + d = rugged.delta + @new_path = encode!(d.new_file[:path]) + @old_path = encode!(d.old_file[:path]) + @a_mode = d.old_file[:mode].to_s(8) + @b_mode = d.new_file[:mode].to_s(8) + @new_file = d.added? + @renamed_file = d.renamed? + @deleted_file = d.deleted? end def init_from_hash(hash) raw_diff = hash.symbolize_keys serialize_keys.each do |key| send(:"#{key}=", raw_diff[key.to_sym]) end + end + + # Strip out the information at the beginning of the patch's text to match + # Grit's output + def strip_diff_headers(diff_text) + lines = diff_text.split("\n") + lines.shift until lines.empty? || lines.first.match("^(---|Binary)") + lines.join("\n") end end end end