Class: GitReviewer::DiffFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gitreviewer/analyze/diff_tree.rb

Constant Summary collapse

UNKNOWN =

未知情况,理论上不存在

0
DELETE =

删除文件

1
ADD =

新增文件

2
MODIFY =

修改文件

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, diff_lines, operation, binary) ⇒ DiffFile



23
24
25
26
27
28
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 23

def initialize(file_name, diff_lines, operation, binary)
  @file_name = file_name
  @diff_lines = diff_lines
  @operation = operation
  @binary = binary
end

Instance Attribute Details

#binary=(value) ⇒ Object (writeonly)

Sets the attribute binary



17
18
19
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 17

def binary=(value)
  @binary = value
end

#diff_linesObject

Returns the value of attribute diff_lines.



15
16
17
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 15

def diff_lines
  @diff_lines
end

#file_nameObject

Returns the value of attribute file_name.



14
15
16
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 14

def file_name
  @file_name
end

#operationObject

Returns the value of attribute operation.



13
14
15
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 13

def operation
  @operation
end

Instance Method Details

#binary?Boolean



19
20
21
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 19

def binary?
  @binary
end

#format_file_nameObject



76
77
78
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 76

def format_file_name
   return "filename: #{file_name}\n"
end

#format_line_diffObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 80

def format_line_diff
  name_max_length = 0
  result = []

  diff_lines.each_with_index do |line, index|
    name_max_length = [name_max_length, line.format_user.length].max

    if line.is_unchange
      if result.size == 0 || !result.last.is_unchange
        result.append(line)
      end
    else
      result.append(line)
    end
  end

  name_max_length += 2

  format_content = ""
  result.each do |line|
    if line.operation == DiffLine::DELETE
      format_content += "\033[0;31m#{line.source_line.user.rjust(name_max_length)} #{line.source_line.format_line} - #{line.source_line.description}\033[0m\n"
    elsif line.operation == DiffLine::ADD
      format_content += "\033[0;32m#{line.target_line.user.rjust(name_max_length)} #{line.target_line.format_line} + #{line.target_line.description}\033[0m\n"
    else
      format_content += "...\n"
    end
  end

  return format_content
end

#format_operationObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 63

def format_operation
  case @operation
  when DiffFile::UNKNOWN
    return "UNKNOWN"
  when DiffFile::DELETE
    return "DELETE"
  when DiffFile::ADD
    return "ADD"
  when DiffFile::MODIFY
    return "MODIFY"
  end
end

#format_propertyObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 42

def format_property
  result = ""
  case @operation
  when DiffFile::UNKNOWN
    result += "operation: UNKNOWN \n"
  when DiffFile::DELETE
    result += "operation: DELETE \n"
  when DiffFile::ADD
    result += "operation: ADD \n"
  when DiffFile::MODIFY
    result += "operation: MODIFY \n"
  end

  if binary?
    result += "binary: true \n"
  else
    result += "binary: false \n"
  end
  return result
end


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitreviewer/analyze/diff_tree.rb', line 30

def print_meta_info
  rows = [
    ["filename", file_name],
    ["operation", format_operation],
    ["binary", binary?]
  ]
  table = Terminal::Table.new do |t|
    t.rows = rows
  end
  Printer.verbose_put table
end