Class: GitStore::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/git_store/diff.rb

Overview

adapted from Grit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, a_path, b_path, a_blob, b_blob, a_mode, b_mode, new_file, deleted_file, diff) ⇒ Diff

Returns a new instance of Diff.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/git_store/diff.rb', line 12

def initialize(store, a_path, b_path, a_blob, b_blob, a_mode, b_mode, new_file, deleted_file, diff)
  @store = store
  @a_path = a_path
  @b_path = b_path
  @a_blob = a_blob =~ /^0{40}$/ ? nil : store.get(a_blob)
  @b_blob = b_blob =~ /^0{40}$/ ? nil : store.get(b_blob)
  @a_mode = a_mode
  @b_mode = b_mode
  @new_file = new_file
  @deleted_file = deleted_file
  @diff = diff
end

Instance Attribute Details

#a_blobObject (readonly)

Returns the value of attribute a_blob.



7
8
9
# File 'lib/git_store/diff.rb', line 7

def a_blob
  @a_blob
end

#a_modeObject (readonly)

Returns the value of attribute a_mode.



8
9
10
# File 'lib/git_store/diff.rb', line 8

def a_mode
  @a_mode
end

#a_pathObject (readonly)

Returns the value of attribute a_path.



6
7
8
# File 'lib/git_store/diff.rb', line 6

def a_path
  @a_path
end

#b_blobObject (readonly)

Returns the value of attribute b_blob.



7
8
9
# File 'lib/git_store/diff.rb', line 7

def b_blob
  @b_blob
end

#b_modeObject (readonly)

Returns the value of attribute b_mode.



8
9
10
# File 'lib/git_store/diff.rb', line 8

def b_mode
  @b_mode
end

#b_pathObject (readonly)

Returns the value of attribute b_path.



6
7
8
# File 'lib/git_store/diff.rb', line 6

def b_path
  @b_path
end

#deleted_fileObject (readonly)

Returns the value of attribute deleted_file.



9
10
11
# File 'lib/git_store/diff.rb', line 9

def deleted_file
  @deleted_file
end

#diffObject (readonly)

Returns the value of attribute diff.



10
11
12
# File 'lib/git_store/diff.rb', line 10

def diff
  @diff
end

#new_fileObject (readonly)

Returns the value of attribute new_file.



9
10
11
# File 'lib/git_store/diff.rb', line 9

def new_file
  @new_file
end

#storeObject (readonly)

Returns the value of attribute store.



5
6
7
# File 'lib/git_store/diff.rb', line 5

def store
  @store
end

Class Method Details

.exec(store, cmd) ⇒ Object



25
26
27
# File 'lib/git_store/diff.rb', line 25

def self.exec(store, cmd)
  list(store, IO.popen(cmd) { |io| io.read })
end

.list(store, text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/git_store/diff.rb', line 29

def self.list(store, text)
  lines = text.split("\n")

  diffs = []

  while !lines.empty?
    m, a_path, b_path = *lines.shift.match(%r{^diff --git a/(.+?) b/(.+)$})

    if lines.first =~ /^old mode/
      m, a_mode = *lines.shift.match(/^old mode (\d+)/)
      m, b_mode = *lines.shift.match(/^new mode (\d+)/)
    end

    if lines.empty? || lines.first =~ /^diff --git/
      diffs << Diff.new(store, a_path, b_path, nil, nil, a_mode, b_mode, false, false, nil)
      next
    end

    new_file = false
    deleted_file = false

    if lines.first =~ /^new file/
      m, b_mode = lines.shift.match(/^new file mode (.+)$/)
      a_mode = nil
      new_file = true
    elsif lines.first =~ /^deleted file/
      m, a_mode = lines.shift.match(/^deleted file mode (.+)$/)
      b_mode = nil
      deleted_file = true
    end

    m, a_blob, b_blob, b_mode = *lines.shift.match(%r{^index ([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+) ?(.+)?$})
    b_mode.strip! if b_mode

    diff_lines = []
    while lines.first && lines.first !~ /^diff/
      diff_lines << lines.shift
    end
    diff = diff_lines.join("\n")

    diffs << Diff.new(store, a_path, b_path, a_blob, b_blob, a_mode, b_mode, new_file, deleted_file, diff)
  end

  diffs
end