class Commit

Constants

FILE_DIFF_REGEX

Attributes

author[RW]
bug_id[RW]
cached_files[RW]
cached_trees[RW]
changes[RW]
changes_total[RW]
children[RW]
commit_id[RW]
date[RW]
deletions_total[RW]
file_diffs[RW]
id[RW]
id=[RW]
insertions_total[RW]
issue_id[RW]
movements[RW]
subject[RW]
trees[RW]

Public Class Methods

from_git(commit_lines, git_client) click to toggle source
# File lib/commit.rb, line 67
def from_git(commit_lines, git_client)
  Commit.new(parse_commit(commit_lines, git_client))
end
new(attrs={}) click to toggle source
# File lib/commit.rb, line 32
def initialize(attrs={})
  attrs.each do |key, val|
    instance_variable_set("@#{key}".to_sym, val)
  end
end
parse_author(author) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

as_json() click to toggle source
# 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_value() click to toggle source
# File lib/commit.rb, line 43
def business_value
  issue = ::Store::Issue[issue_id]
  issue.present? ?
    issue[:business_value] :
    0
end
set_cached(tree) click to toggle source
# 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