Class: GitReviewer::Analyzer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_branch, target_branch) ⇒ Analyzer

Returns a new instance of Analyzer.



21
22
23
24
25
26
# File 'lib/gitreviewer/analyze/analyzer.rb', line 21

def initialize(source_branch, target_branch)
  @source_branch = source_branch
  @target_branch = target_branch
  @author_results = Hash.new
  @reviewer_results = Hash.new
end

Instance Attribute Details

#author_resultsObject

Returns the value of attribute author_results.



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

def author_results
  @author_results
end

#builderObject

blame builder



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

def builder
  @builder
end

#configurationObject

配置信息 Configuration



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

def configuration
  @configuration
end

#reviewer_resultsObject

Returns the value of attribute reviewer_results.



18
19
20
# File 'lib/gitreviewer/analyze/analyzer.rb', line 18

def reviewer_results
  @reviewer_results
end

#source_branchObject

source branch



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

def source_branch
  @source_branch
end

#target_branchObject

target branch



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

def target_branch
  @target_branch
end

Instance Method Details

#analyze_authorObject



77
78
79
80
81
82
83
# File 'lib/gitreviewer/analyze/analyzer.rb', line 77

def analyze_author
  @builder.diff_files.each do |fdiff|
    fdiff.diff_lines.each_with_index do |ldiff, index|
    record_author(fdiff, ldiff)
    end
  end
end

#analyze_reviewerObject



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
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gitreviewer/analyze/analyzer.rb', line 85

def analyze_reviewer
  @builder.diff_files.each do |fdiff|
    reviewer = nil
    lines = 0

    # 其他操作按行计算权重
    fdiff.diff_lines.each_with_index do |ldiff, index|
      if ldiff.operation == DiffLine::DELETE
        # 删除行: 由原作者 review
        reviewer = ldiff.source_line.user
        record_reviewer(fdiff, reviewer, 1)
      elsif ldiff.operation == DiffLine::ADD
        # 新增行
        if reviewer != nil
          # 紧随删除,由删除行的原作者 review
          lines += 1
        else
          # 非紧随删除,由 configuration 决定谁来 review
          reviewer = @configuration.reviewer_of_file(fdiff.file_name)
        record_reviewer(fdiff, reviewer, 1)
        end
      else
        # 未变化
        if reviewer != nil
          # 处理编辑类型
          record_reviewer(fdiff, reviewer, lines)
          reviewer = nil
          lines = 0
        end
      end

      # 最后一行
      if index == fdiff.diff_lines.count - 1 && reviewer != nil
        record_reviewer(fdiff, reviewer, lines)
      end
    end
  end
end

#executeObject



70
71
72
73
74
75
# File 'lib/gitreviewer/analyze/analyzer.rb', line 70

def execute
  setup_builder
  setup_configuration
  analyze_author
  analyze_reviewer
end


170
171
172
# File 'lib/gitreviewer/analyze/analyzer.rb', line 170

def print_author_result
  print @author_results
end


174
175
176
# File 'lib/gitreviewer/analyze/analyzer.rb', line 174

def print_reviewer_result
  print @reviewer_results
end

#record_author(fdiff, ldiff) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gitreviewer/analyze/analyzer.rb', line 124

def record_author(fdiff, ldiff)
  file_name = fdiff.file_name
  if @configuration.ignore?(file_name)
    return
  end

  author = ""
  if ldiff.operation == DiffLine::DELETE
    # 删除类型,记录为原始作者
    author = ldiff.source_line.user
  elsif ldiff.operation == DiffLine::ADD
    # 新增类型,记录为最新作者
    author = ldiff.target_line.user
  end

  if author.empty?
    return
  end

  item = @author_results[author]
  if item == nil
    item = ResultItem.new(author)
  end
  item.add_file_name(file_name)
  item.add_line_count(1)
  @author_results[author] = item
end

#record_reviewer(fdiff, reviewer, lines) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/gitreviewer/analyze/analyzer.rb', line 152

def record_reviewer(fdiff, reviewer, lines)
  if reviewer == nil
    return
  end
  file_name = fdiff.file_name
  if @configuration.ignore?(file_name)
    return
  end

  item = @reviewer_results[reviewer]
  if item == nil
    item = ResultItem.new(reviewer)
  end
  item.add_file_name(file_name)
  item.add_line_count(lines)
  @reviewer_results[reviewer] = item
end

#setup_builderObject



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
# File 'lib/gitreviewer/analyze/analyzer.rb', line 29

def setup_builder
  # 构建 BlameTree
  @builder = Builder.new(@source_branch, @target_branch)
  @builder.build

  if @builder.source_blame.blame_files.count == 0
    Printer.warning "Warning: no blame file for source blame branch<#{@source_branch}>"
    exit 1
  end

  if @builder.target_blame.blame_files.count == 0
    Printer.warning "Warning: no blame file for target blame branch<#{@target_branch}>"
    exit 1
  end

  if @builder.source_blame.blame_files.count != @builder.target_blame.blame_files.count
    Printer.red "Error: internal error. The number of files is not equal."
    exit 1
  end

  if @builder.diff_files == nil
    Printer.red "Error: internal error. The diff files of builder is nil."
    exit 1
  end
end

#setup_configurationObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gitreviewer/analyze/analyzer.rb', line 55

def setup_configuration
  file_name = ".gitreviewer.yml"
  file_exist = File.exist?(file_name)
  # 检测配置文件
  if !file_exist
    Printer.red "Error: `.gitreviewer.yml` not exist in current directory. Please execute `git reviewer --init` first."
    exit 1
  end
  # 解析配置文件
  data = YAML.load_file(file_name)
  folder_owner = data['folder_owner'].map { |hash| FolderOwner.new(hash["path"], hash["owner"]) }
  file_owner = data['file_owner'].map { |hash| FileOwner.new(hash["path"], hash["owner"]) }
  @configuration = Configuration.new(data['project_owner'], folder_owner, file_owner, data['ignore_files'], data['ignore_folders'])
end