Commit 5f8f685bd45b65f0b8506988b7ad9447f2026abb

Authored by Dmitriy Zaporozhets
1 parent a45e4721

Init Gitlab::Git::Diff class

Showing 1 changed file with 63 additions and 0 deletions   Show diff stats
lib/gitlab/git/diff.rb 0 → 100644
... ... @@ -0,0 +1,63 @@
  1 +# Gitlab::Git::Diff is a wrapper around native Grit::Diff object
  2 +# We dont want to use grit objects inside app/
  3 +# It helps us easily migrate to rugged in future
  4 +module Gitlab
  5 + module Git
  6 + class Diff
  7 + BROKEN_DIFF = "--broken-diff"
  8 +
  9 + attr_accessor :raw_diff
  10 +
  11 + # Diff properties
  12 + attr_accessor :old_path, :new_path, :a_mode, :b_mode, :diff
  13 +
  14 + # Stats properties
  15 + attr_accessor :new_file, :renamed_file, :deleted_file
  16 +
  17 + def initialize(raw_diff, head = nil)
  18 + raise "Nil as raw diff passed" unless raw_diff
  19 +
  20 + if raw_diff.is_a?(Hash)
  21 + init_from_hash(raw_diff)
  22 + else
  23 + init_from_grit(raw_diff)
  24 + end
  25 +
  26 + @head = head
  27 + end
  28 +
  29 + def serialize_keys
  30 + %w(diff new_path old_path a_mode b_mode new_file renamed_file deleted_file)
  31 + end
  32 +
  33 + def to_hash
  34 + hash = {}
  35 +
  36 + keys = serialize_keys
  37 +
  38 + keys.each do |key|
  39 + hash[key] = send(key)
  40 + end
  41 +
  42 + hash
  43 + end
  44 +
  45 + private
  46 +
  47 + def init_from_grit(grit)
  48 + @raw_diff = grit
  49 +
  50 + serialize_keys.each do |key|
  51 + send(:"#{key}=", grit.send(key))
  52 + end
  53 + end
  54 +
  55 + def init_from_hash(hash)
  56 + serialize_keys.each do |key|
  57 + send(:"#{key}=", hash[key])
  58 + end
  59 + end
  60 + end
  61 + end
  62 +end
  63 +
... ...