Class: GitReviewer::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_branch, target_branch) ⇒ Builder

Returns a new instance of Builder.



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

def initialize(source_branch, target_branch)
  @source_branch = source_branch
  @target_branch = target_branch
end

Instance Attribute Details

#diff_filesObject

Array<DiffFiles>



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

def diff_files
  @diff_files
end

#source_blameObject

BlameBranch



11
12
13
# File 'lib/gitreviewer/analyze/builder.rb', line 11

def source_blame
  @source_blame
end

#source_branchObject

source branch



9
10
11
# File 'lib/gitreviewer/analyze/builder.rb', line 9

def source_branch
  @source_branch
end

#target_blameObject

BlameBranch



12
13
14
# File 'lib/gitreviewer/analyze/builder.rb', line 12

def target_blame
  @target_blame
end

#target_branchObject

Returns the value of attribute target_branch.



10
11
12
# File 'lib/gitreviewer/analyze/builder.rb', line 10

def target_branch
  @target_branch
end

Instance Method Details

#blame_branch(branch, files) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitreviewer/analyze/builder.rb', line 82

def blame_branch(branch, files)
  blame_files = []
  files.each do |file_name|
    bf = BlameFile.new("", [], false, false)

    if Checker.is_file_exist?(branch, file_name) then
      if Checker.is_file_binary?(branch, file_name)
        bf = BlameFile.new(file_name, [], true, true)
      else
        bf = blame_file(branch, file_name)
      end
    else
      bf = BlameFile.new(file_name, [], false, false)
    end
    blame_files.append(bf)
  end
  result = BlameBranch.new(branch, blame_files)
  return result
end

#blame_file(branch, file_name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gitreviewer/analyze/builder.rb', line 102

def blame_file(branch, file_name)
  blame_lines = []
  content = Checker.snapshot_of_blame_file(branch, file_name)
  # 遍历文件的每一行,得到 BlameLine 数组
  content.lines do |line|
    result = blame_line(line)
    blame_lines.append(result)
  end

  result = BlameFile.new(file_name, blame_lines, true, false)
  return result
end

#blame_line(text) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gitreviewer/analyze/builder.rb', line 115

def blame_line(text)
  # 获取哈希值
  hash = text.slice!(0, 40)
  rest = text.strip
  # 移除 (
  rest = rest[1..-1]
  # 根据时间格式进行拆分
  pattern = /\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [+-]\d{4}\b/
  pattern_index = rest.index(pattern)
  pattern_index = pattern_index + 25      # 2022-04-08 06:19:37 -0200 长度为 25
  # 根据特定位置进行拆分
  user_date = rest.slice(0, pattern_index)
  rest = rest.slice(pattern_index , rest.length)
  # user_date, rest = rest.split("+", 2)
  user_date = user_date.strip
  # 提取作者,日期。日期:提取倒数 19 个字符
  date = user_date.slice!(-25, 25)
  date = date.strip
  user = user_date.strip
  # # 提取行号,代码
  line, code = rest.split(")", 2)
  line = line.strip
  # 结果
  result = BlameLine.new(hash, user, date, line, code)
  return result
end

#buildObject



21
22
23
24
25
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
77
78
79
80
# File 'lib/gitreviewer/analyze/builder.rb', line 21

def build
  # 遍历分支改动的每个文件,得到 BlameFile 数组
  files = Checker.diff_files(@source_branch, @target_branch)
  header_length = 0

  if files == nil || files.count == 0
    Printer.warning "Warning: there are no analyzable differences between the target branch and source branch."
    exit 0
  else
    header = "============ Diff files between source<#{@source_branch}> and target<#{@target_branch}> ============"
    header_length = header.length
    footer = "=" * header_length

    Printer.verbose_put header
    Printer.verbose_put files
    Printer.verbose_put footer
    Printer.verbose_put "\n"
  end

  # 构建 source branch 的 BlameBranch
  source_header_title = " Source Blame Files "
  target_header_title = " Target Blame Files "
  source_prefix_length = (header_length - source_header_title.length) / 2
  target_prefix_length = (header_length - target_header_title.length) / 2
  source_header = "=" * source_prefix_length + source_header_title + "=" * (header_length - source_prefix_length - source_header_title.length)
  target_header = "=" * target_prefix_length + target_header_title + "=" * (header_length - target_prefix_length - target_header_title.length)
  footer = "=" * header_length

  # 打印 source branch Log
  Printer.verbose_put source_header
  files.each do |file_name|
    Printer.verbose_put "#{file_name}"
  end
  Printer.verbose_put footer
  Printer.verbose_put "\n"

  # 打印 target branch Log
  Printer.verbose_put target_header
  files.each do |file_name|
    Printer.verbose_put "#{file_name}"
  end
  Printer.verbose_put footer
  Printer.verbose_put "\n"

  # 构建 source branch & target branch
  @source_blame = blame_branch(@source_branch, files)
  @target_blame = blame_branch(@target_branch, files)

  # 构建 diff_files
  @diff_files = []
  @source_blame.blame_files.each_with_index do |sfile, index|
    tfile = @target_blame.blame_files[index]
    # Diff 时需要交换 tfile 和 sfile
    myers = Myers.new(tfile, sfile)
    @diff_files.append(myers.resolve)
  end

  # 打印 Code Diff

end