Class: Grit::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/diff.rb,
lib/grit_ext/diff.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, a_path, b_path, a_blob, b_blob, a_mode, b_mode, new_file, deleted_file, diff, renamed_file = false, similarity_index = 0) ⇒ Diff

Returns a new instance of Diff.



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

def initialize(repo, a_path, b_path, a_blob, b_blob, a_mode, b_mode, new_file, deleted_file, diff, renamed_file = false, similarity_index = 0)
  @repo   = repo
  @a_path = a_path
  @b_path = b_path
  @a_blob = a_blob =~ /^0{40}$/ ? nil : Blob.create(repo, :id => a_blob)
  @b_blob = b_blob =~ /^0{40}$/ ? nil : Blob.create(repo, :id => b_blob)
  @a_mode = a_mode
  @b_mode = b_mode
  @new_file         = new_file     || @a_blob.nil?
  @deleted_file     = deleted_file || @b_blob.nil?
  @renamed_file     = renamed_file
  @similarity_index = similarity_index.to_i
  @diff             = diff
end

Instance Attribute Details

#a_blobObject (readonly)

Returns the value of attribute a_blob.



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

def a_blob
  @a_blob
end

#a_modeObject (readonly)

Returns the value of attribute a_mode.



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

def a_mode
  @a_mode
end

#a_pathObject (readonly)

Returns the value of attribute a_path.



4
5
6
# File 'lib/grit/diff.rb', line 4

def a_path
  @a_path
end

#b_blobObject (readonly)

Returns the value of attribute b_blob.



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

def b_blob
  @b_blob
end

#b_modeObject (readonly)

Returns the value of attribute b_mode.



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

def b_mode
  @b_mode
end

#b_pathObject (readonly)

Returns the value of attribute b_path.



4
5
6
# File 'lib/grit/diff.rb', line 4

def b_path
  @b_path
end

#deleted_fileObject (readonly)

Returns the value of attribute deleted_file.



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

def deleted_file
  @deleted_file
end

#diffObject

Returns the value of attribute diff.



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

def diff
  @diff
end

#new_fileObject (readonly)

Returns the value of attribute new_file.



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

def new_file
  @new_file
end

#renamed_fileObject (readonly)

Returns the value of attribute renamed_file.



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

def renamed_file
  @renamed_file
end

#similarity_indexObject (readonly)

Returns the value of attribute similarity_index.



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

def similarity_index
  @similarity_index
end

Class Method Details

.list_from_string(repo, text) ⇒ Object



26
27
28
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
74
75
76
# File 'lib/grit/diff.rb', line 26

def self.list_from_string(repo, 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(repo, a_path, b_path, nil, nil, a_mode, b_mode, false, false, nil)
      next
    end

    sim_index    = 0
    new_file     = false
    deleted_file = false
    renamed_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
    elsif lines.first =~ /^similarity index (\d+)\%/
      sim_index    = $1.to_i
      renamed_file = true
      2.times { lines.shift } # shift away the 2 `rename from/to ...` lines
    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(repo, a_path, b_path, a_blob, b_blob, a_mode, b_mode, new_file, deleted_file, diff, renamed_file, sim_index)
  end

  diffs
end

Instance Method Details

#new_pathObject



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

def new_path
  GritExt.encode! @b_path
end

#old_pathObject



4
5
6
# File 'lib/grit_ext/diff.rb', line 4

def old_path
  GritExt.encode! @a_path
end