Class: Babygitter::RepoAnalyzer

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

Direct Known Subclasses

ReportGenerator

Defined Under Namespace

Classes: Author, Branch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}, master_branch_name = nil) ⇒ RepoAnalyzer

Returns a new instance of RepoAnalyzer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/babygitter/repo_analyzer.rb', line 9

def initialize(path, options = {}, master_branch_name=nil)
  Babygitter.repo_path = path
  repo = Grit::Repo.new(path, options)
  @path = repo.path
  @bare = options[:is_bare]
  @config = (Grit::Config.new(repo))
  @remote_url = get_remote_url
  @master_branch = nil
  @branch_names = get_branch_names(repo)
  @branches = create_branches(repo)
  @authors_names = get_authors
  @began = first_committed_commit
  @lastest_commit = last_commited_commit
  @total_commits = get_total_uniq_commits_in_repo
  @submodule_list = submodule_codes
  @project_name = get_project_name.capitalize
  @branched_commit_found_for_branches = []
  set_master_branch(master_branch_name)
  find_unique_commits_per_branch
  analyze_branches
  @branched_commit_found_for_branches = nil
end

Instance Attribute Details

#authors_namesObject (readonly)

Returns the value of attribute authors_names.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def authors_names
  @authors_names
end

#beganObject (readonly)

Returns the value of attribute began.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def began
  @began
end

#branch_namesObject (readonly)

Returns the value of attribute branch_names.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def branch_names
  @branch_names
end

#branchesObject (readonly)

Returns the value of attribute branches.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def branches
  @branches
end

#lastest_commitObject (readonly)

Returns the value of attribute lastest_commit.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def lastest_commit
  @lastest_commit
end

#master_branchObject (readonly)

Returns the value of attribute master_branch.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def master_branch
  @master_branch
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def project_name
  @project_name
end

#remote_urlObject (readonly)

Returns the value of attribute remote_url.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def remote_url
  @remote_url
end

#submodule_listObject (readonly)

Returns the value of attribute submodule_list.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def submodule_list
  @submodule_list
end

#total_commitsObject (readonly)

Returns the value of attribute total_commits.



6
7
8
# File 'lib/babygitter/repo_analyzer.rb', line 6

def total_commits
  @total_commits
end

Instance Method Details

#analyze_branchesObject



139
140
141
# File 'lib/babygitter/repo_analyzer.rb', line 139

def analyze_branches
  go_down_branch_from_head(@master_branch)
end

#create_branches(repo) ⇒ Object

Creates the Branch objects for the Repo

  • Calls collect on branch_names

  • Calls Grit::Commit with the name of the branch to get the commits associated with it

  • Sorts the commit by latest to earliest

  • Creates a branch object for each name in branch with these commits



71
72
73
74
# File 'lib/babygitter/repo_analyzer.rb', line 71

def create_branches(repo)
  @branch_names.collect {|branch_name| 
  Branch.new(branch_name ,Grit::Commit.find_all(repo, branch_name).sort_by { |k| k.authored_date}.reverse)}
end

#find_branch(name) ⇒ Object



135
136
137
# File 'lib/babygitter/repo_analyzer.rb', line 135

def find_branch(name) 
  @branches.find {|branch| branch.name == name}
end

#find_unique_commits_per_branchObject



117
118
119
120
121
122
123
124
125
# File 'lib/babygitter/repo_analyzer.rb', line 117

def find_unique_commits_per_branch
  for branch in @branches
    other_branches_commits = (@branches - [branch]).map(&:commits).flatten.map(&:id).uniq
    branch.commits.inject([]) do |result, commit|
      result << commit unless other_branches_commits.include?(commit.id)
      branch.unique_commits = result
    end
  end
end

#first_committed_commitObject

Gets the last commited commit

  • Calls get_all_commits_in_repo

  • Gets the last commit in the array



56
57
58
# File 'lib/babygitter/repo_analyzer.rb', line 56

def first_committed_commit
  get_all_commits_in_repo.last
end

#get_all_commits_in_repoObject

Gets all the commits in the repo from all branches and sorts them from newest to oldest TODO should this be a private method.



34
35
36
# File 'lib/babygitter/repo_analyzer.rb', line 34

def get_all_commits_in_repo
  @branches.collect(&:commits).flatten.sort_by { |k| k.authored_date }.reverse
end

#get_authorsObject

Collects the names of the authors in each branch, calls unique on them and sorts them by alphabetically.



77
78
79
# File 'lib/babygitter/repo_analyzer.rb', line 77

def get_authors
  @branches.collect(&:author_names).flatten.uniq.sort_by { |k| k.downcase }
end

#get_branch_names(repo) ⇒ Object

Gets the names of the branches in the repo. Uses Grit:Repo.heads



62
63
64
# File 'lib/babygitter/repo_analyzer.rb', line 62

def get_branch_names(repo)
  repo.heads.collect(&:name)
end

#get_master_branchObject



96
97
98
99
100
# File 'lib/babygitter/repo_analyzer.rb', line 96

def get_master_branch
  error = NoMasterBranchError.new("No branch with name master, set variable master branch name")
  raise error unless @branch_names.index('master') != nil
  "master"
end

#get_project_nameObject



102
103
104
# File 'lib/babygitter/repo_analyzer.rb', line 102

def get_project_name
    Babygitter.repo_path.gsub(/\/$/, "").gsub(/.*\/(?!\s)|\.git$|\/$/, "") 
end

#get_remote_urlObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/babygitter/repo_analyzer.rb', line 81

def get_remote_url
  unless @bare
    remote_url = @config.fetch('remote.origin.url').clone
    if remote_url =~ /^git:\/\/github.com/
      remote_url.gsub!(/^git/, "http").gsub!(/.git$/, "")
    elsif remote_url =~ /^[email protected]/
      remote_url.gsub!(/^[email protected]:/, "http://github.com/").gsub!(/.git$/, "")
    else
      ""
    end
  else
    ""
  end        
end

#get_total_uniq_commits_in_repoObject

Gets the amount of uniq commits in the repo

  • Calls get_all_commits_in_repo

  • collects the commits’ commit ids. We do this because each Grit::Commit is unique

  • flatten the collected array and call size on it.



42
43
44
# File 'lib/babygitter/repo_analyzer.rb', line 42

def get_total_uniq_commits_in_repo
  get_all_commits_in_repo.collect(&:id).uniq.size
end

#go_down_branch_from_head(branch) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/babygitter/repo_analyzer.rb', line 143

def go_down_branch_from_head(branch)
  @branched_commit_found_for_branches << branch.name
  for commit in branch.commits
    branches_commit_is_in = in_which_branches(commit.id)
    if (branches_commit_is_in - @branched_commit_found_for_branches).size > 0
      branched_commits = (branches_commit_is_in - @branched_commit_found_for_branches)
      for name in branched_commits
        find_branch(name).branched_at = commit if commit.date > find_branch(name).branched_at.date
        go_down_branch_from_head(find_branch(name)) 
      end
    end
  end
end

#in_which_branches(commit_id) ⇒ Object



128
129
130
131
132
133
# File 'lib/babygitter/repo_analyzer.rb', line 128

def in_which_branches(commit_id)
  @branches.inject([]) do |result, branch| 
    result << branch.name if branch.commits.map(&:id).include?(commit_id)
    result
  end
end

#inspectObject



157
158
159
# File 'lib/babygitter/repo_analyzer.rb', line 157

def inspect
  %Q{#<Babygitter::Repo #{@project_name}>}
end

#last_commited_commitObject

Gets the last commited commit

  • Calls get_all_commits_in_repo

  • Gets the first commit in the array



49
50
51
# File 'lib/babygitter/repo_analyzer.rb', line 49

def last_commited_commit
  get_all_commits_in_repo.first
end

#set_master_branch(master_branch_name) ⇒ Object



110
111
112
113
114
115
# File 'lib/babygitter/repo_analyzer.rb', line 110

def set_master_branch(master_branch_name)
  master_branch_name = get_master_branch if master_branch_name == nil
  error = NoBranchWithNameGivenError.new("No branch with name #{master_branch_name} is in the repository") 
  raise error unless  @branch_names.index(master_branch_name) != nil
  @branches.map {|branch| branch.is_master_branch, @master_branch = true, branch if branch.name == master_branch_name}
end

#submodule_codesObject



106
107
108
# File 'lib/babygitter/repo_analyzer.rb', line 106

def submodule_codes
  `cd #{@path.gsub(/\/.git$/, "")}; git submodule` unless @bare
end