Class: Commit

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

Constant Summary collapse

FILE_DIFF_REGEX =
/^diff --git a\/.* b\//

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Commit

Returns a new instance of Commit.



32
33
34
35
36
# File 'lib/commit.rb', line 32

def initialize(attrs={})
  attrs.each do |key, val|
    instance_variable_set("@#{key}".to_sym, val)
  end
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



14
15
16
# File 'lib/commit.rb', line 14

def author
  @author
end

#bug_idObject

Returns the value of attribute bug_id.



27
28
29
# File 'lib/commit.rb', line 27

def bug_id
  @bug_id
end

#cached_filesObject

Returns the value of attribute cached_files.



24
25
26
# File 'lib/commit.rb', line 24

def cached_files
  @cached_files
end

#cached_treesObject

Returns the value of attribute cached_trees.



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

def cached_trees
  @cached_trees
end

#changesObject

Returns the value of attribute changes.



20
21
22
# File 'lib/commit.rb', line 20

def changes
  @changes
end

#changes_totalObject

Returns the value of attribute changes_total.



23
24
25
# File 'lib/commit.rb', line 23

def changes_total
  @changes_total
end

#childrenObject

Returns the value of attribute children.



13
14
15
# File 'lib/commit.rb', line 13

def children
  @children
end

#commit_idObject Also known as: id

Returns the value of attribute commit_id.



12
13
14
# File 'lib/commit.rb', line 12

def commit_id
  @commit_id
end

#dateObject

Returns the value of attribute date.



15
16
17
# File 'lib/commit.rb', line 15

def date
  @date
end

#deletions_totalObject

Returns the value of attribute deletions_total.



22
23
24
# File 'lib/commit.rb', line 22

def deletions_total
  @deletions_total
end

#file_diffsObject

Returns the value of attribute file_diffs.



18
19
20
# File 'lib/commit.rb', line 18

def file_diffs
  @file_diffs
end

#insertions_totalObject

Returns the value of attribute insertions_total.



21
22
23
# File 'lib/commit.rb', line 21

def insertions_total
  @insertions_total
end

#issue_idObject

Returns the value of attribute issue_id.



26
27
28
# File 'lib/commit.rb', line 26

def issue_id
  @issue_id
end

#movementsObject

Returns the value of attribute movements.



19
20
21
# File 'lib/commit.rb', line 19

def movements
  @movements
end

#subjectObject

Returns the value of attribute subject.



16
17
18
# File 'lib/commit.rb', line 16

def subject
  @subject
end

#treesObject

Returns the value of attribute trees.



17
18
19
# File 'lib/commit.rb', line 17

def trees
  @trees
end

Class Method Details

.from_git(commit_lines, git_client) ⇒ Object



67
68
69
# File 'lib/commit.rb', line 67

def from_git(commit_lines, git_client)
  Commit.new(parse_commit(commit_lines, git_client))
end

.parse_author(author) ⇒ Object



132
133
134
135
136
137
# File 'lib/commit.rb', line 132

def parse_author(author)
  author = Iconv
    .conv("ascii//translit", "UTF-8", author)
    .tr("[]", "()")
  Mail::Address.new(author)
end

.parse_commit(commit_lines, git_client) ⇒ Object



71
72
73
74
75
76
77
78
79
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
111
112
113
114
115
116
117
118
# File 'lib/commit.rb', line 71

def parse_commit(commit_lines, git_client)
  commit_id, author, date, children, subject = parse_commit_header(commit_lines.shift())
  Loggr.instance.info("PARSE COMMIT: #{commit_id}")
  if children.length > 1
    commit_lines = git_client.diff(children.first, commit_id)
  end

  file_diffs = commit_lines
    .reduce([], &fold_reducer(FILE_DIFF_REGEX))
    .map { |file_diff_lines| FileDiff.from_git(file_diff_lines) }

  insertions = file_diffs.reduce(0) { |sum, file_diff| sum + file_diff.insertions_total }
  deletions = file_diffs.reduce(0) { |sum, file_diff| sum + file_diff.deletions_total }

  file_diffs = file_diffs.reduce({}) do |obj, file_diff|
    obj[file_diff.b_file_name] = file_diff
    obj
  end

  if children.length > 1
    diff_id = "#{children.first}..#{commit_id}"
    trees = git_client.parse_diff_tree(git_client.diff_tree(diff_id))
  elsif children.length == 1
    trees = git_client.parse_diff_tree(git_client.diff_tree(commit_id))
  else
    trees = git_client.parse_ls_tree(git_client.ls_tree(commit_id))
  end

  mutations = LineTracker.new.track_mutations!(file_diffs)

  {
    commit_id: commit_id,
    children: children,
    author: {
      name: author.display_name,
      email: author.address
    },
    date: date,
    subject: subject,
    trees: trees,
    file_diffs: file_diffs,
    movements: mutations[:movements],
    changes: mutations[:changes],
    insertions_total: insertions,
    deletions_total: deletions,
    changes_total: insertions + deletions
  }
end

.parse_commit_header(header) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/commit.rb', line 120

def parse_commit_header(header)
  commit_id, children, author, date, subject = header.split("|||")
  commit_id = commit_id.to_sym
  subject ||= ""
  children = children
    .split(" ")
    .map { |child_id| child_id.to_sym }
  author = parse_author(author)
  date = DateTime.rfc2822(date)
  [commit_id, author, date, children, subject]
end

Instance Method Details

#as_jsonObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/commit.rb', line 50

def as_json
  {
    commit_id: commit_id,
    children: children,
    author: author,
    date: date,
    subject: subject,
    insertions_total: insertions_total,
    deletions_total: deletions_total,
    changes_total: changes.count,
    movements_total: movements.count,
    issue_id: issue_id,
    bug_id: bug_id
  }
end

#business_valueObject



43
44
45
46
47
48
# File 'lib/commit.rb', line 43

def business_value
  issue = ::Store::Issue[issue_id]
  issue.present? ?
    issue[:business_value] :
    0
end

#set_cached(tree) ⇒ Object



38
39
40
41
# File 'lib/commit.rb', line 38

def set_cached(tree)
  @cached_files = tree.select { |obj| obj[:type] == :file }
  @cached_trees = tree.select { |obj| obj[:type] == :tree }
end