Class: Babygitter::RepoAnalyzer::Branch

Inherits:
Object
  • Object
show all
Includes:
DateTimeArrays, FolderAnalysisMethods
Defined in:
lib/babygitter/repo_analyzer/branch.rb

Overview

A Branch contains the basic informtion of what makes up the git branch

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FolderAnalysisMethods

#build_regexp, #create_hash_map, #create_stable_hash, #find_key, #folder_hash_for_level, #get_array_of_mapped_folder_names, #get_folder_commits_by_week_and_level, #plot_folder_points

Methods included from DateTimeArrays

#create_52_week_map, #create_active_date_array

Constructor Details

#initialize(name, commits) ⇒ Branch

Returns a new instance of Branch.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/babygitter/repo_analyzer/branch.rb', line 11

def initialize(name, commits)
  @name = name
  @commits = commits
  @total_commits = commits.size
  @author_names = get_author_names
  @authors = create_authors
  @began = commits.last 
  @branched_at = commits.last 
  @latest_commit = commits.first
  @is_master_branch = false
  @unique_commits = nil
  @mapped_diffs = map_commits_to_diff_state_by_week
  @folder_array = get_array_of_mapped_folder_names
end

Instance Attribute Details

#author_namesObject (readonly)

Returns the value of attribute author_names.



9
10
11
# File 'lib/babygitter/repo_analyzer/branch.rb', line 9

def author_names
  @author_names
end

#authorsObject (readonly)

Returns the value of attribute authors.



9
10
11
# File 'lib/babygitter/repo_analyzer/branch.rb', line 9

def authors
  @authors
end

#beganObject (readonly)

Returns the value of attribute began.



9
10
11
# File 'lib/babygitter/repo_analyzer/branch.rb', line 9

def began
  @began
end

#branched_atObject

Returns the value of attribute branched_at.



8
9
10
# File 'lib/babygitter/repo_analyzer/branch.rb', line 8

def branched_at
  @branched_at
end

#commitsObject (readonly)

Returns the value of attribute commits.



9
10
11
# File 'lib/babygitter/repo_analyzer/branch.rb', line 9

def commits
  @commits
end

#is_master_branchObject

Returns the value of attribute is_master_branch.



8
9
10
# File 'lib/babygitter/repo_analyzer/branch.rb', line 8

def is_master_branch
  @is_master_branch
end

#latest_commitObject (readonly)

Returns the value of attribute latest_commit.



9
10
11
# File 'lib/babygitter/repo_analyzer/branch.rb', line 9

def latest_commit
  @latest_commit
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/babygitter/repo_analyzer/branch.rb', line 9

def name
  @name
end

#total_commitsObject (readonly)

Returns the value of attribute total_commits.



9
10
11
# File 'lib/babygitter/repo_analyzer/branch.rb', line 9

def total_commits
  @total_commits
end

#unique_commitsObject

Returns the value of attribute unique_commits.



8
9
10
# File 'lib/babygitter/repo_analyzer/branch.rb', line 8

def unique_commits
  @unique_commits
end

Instance Method Details

#create_authorsObject

Create the authors objects for the branch

  • creates an grouped_commits_array of arrays the size of the author_names array

  • sorts the commits by date and reveres this so the dates go from newest to olders

  • goes through the sorted commits and finds the index of that commit’s author in author_names array

  • adds that commit to the group_commits_array

  • takes each array in grouped_commits_array and creates an author object



48
49
50
51
52
53
54
55
# File 'lib/babygitter/repo_analyzer/branch.rb', line 48

def create_authors
  grouped_commits_array = Array.new(@author_names.size).map {|a| a = []}
  sorted_commits = @commits.sort_by {|k| k.date}.reverse
  sorted_commits.map {|commit|
    index = @author_names.index(commit.author.name)
    grouped_commits_array[index] << commit }
  grouped_commits_array.map {|grouped_commits| Author.new(grouped_commits)}
end

#get_author_namesObject

Gets a list authors who have committed to this branch

  • sort by authors name

  • remove the duplicates wih uniq



29
30
31
# File 'lib/babygitter/repo_analyzer/branch.rb', line 29

def get_author_names
  @commits.sort_by {|k| k.author.name.downcase}.collect {|commit| commit.author.name}.uniq
end

#get_total_lines_added_by_weekObject

Gets the total line added by week to the git repository TODO use this in an actual graph



70
71
72
# File 'lib/babygitter/repo_analyzer/branch.rb', line 70

def get_total_lines_added_by_week
  @mapped_diffs.map {|week| week.map {|c| c.additions - c.deletions}}.map {|a| a.inject(0) {|result, number| result += number}}
end

#inspectObject



74
75
76
# File 'lib/babygitter/repo_analyzer/branch.rb', line 74

def inspect
  %Q{#<Babygitter::Branch #{@total_commits} #{@name}>}
end

#map_commits_to_diff_state_by_weekObject

Conver to diff state through gruff and map those by week

  • call sorted_commits_by_week to sort commits by week into arrays

  • map those arrays of commits and convert them to diff states

  • flatten array of diff states so they are contained by week and not by commit.

TODO this method is incredibly slow due to going through the command line. Improve!



38
39
40
# File 'lib/babygitter/repo_analyzer/branch.rb', line 38

def map_commits_to_diff_state_by_week
  sorted_commits_by_week.map {|array_of_commits| array_of_commits.map(&:stats).map(&:to_diffstat).flatten }
end

#sorted_commits_by_weekObject

Sorts the commits by week into an array of arrays to plot out with gruff



58
59
60
61
62
63
64
65
66
# File 'lib/babygitter/repo_analyzer/branch.rb', line 58

def sorted_commits_by_week
  sort_array = create_active_date_array(@began.date, @latest_commit.date)
  mapped_commits_array = Array.new(sort_array.size).map {|a| a = []}
  for commit in @commits.reverse
    index = sort_array.index(commit.date.strftime("%U %Y"))
    mapped_commits_array[index] << commit
  end
  mapped_commits_array
end