Class: Babygitter::RepoAnalyzer
- Inherits:
-
Object
- Object
- Babygitter::RepoAnalyzer
- Includes:
- Errorclasses
- Defined in:
- lib/babygitter/repo_analyzer.rb,
lib/babygitter/repo_analyzer/author.rb,
lib/babygitter/repo_analyzer/branch.rb
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#authors_names ⇒ Object
readonly
Returns the value of attribute authors_names.
-
#began ⇒ Object
readonly
Returns the value of attribute began.
-
#branch_names ⇒ Object
readonly
Returns the value of attribute branch_names.
-
#branches ⇒ Object
readonly
Returns the value of attribute branches.
-
#lastest_commit ⇒ Object
readonly
Returns the value of attribute lastest_commit.
-
#master_branch ⇒ Object
readonly
Returns the value of attribute master_branch.
-
#project_name ⇒ Object
readonly
Returns the value of attribute project_name.
-
#remote_url ⇒ Object
readonly
Returns the value of attribute remote_url.
-
#submodule_list ⇒ Object
readonly
Returns the value of attribute submodule_list.
-
#total_commits ⇒ Object
readonly
Returns the value of attribute total_commits.
Instance Method Summary collapse
- #analyze_branches ⇒ Object
-
#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.
- #find_branch(name) ⇒ Object
- #find_unique_commits_per_branch ⇒ Object
-
#first_committed_commit ⇒ Object
Gets the last commited commit * Calls get_all_commits_in_repo * Gets the last commit in the array.
-
#get_all_commits_in_repo ⇒ Object
Gets all the commits in the repo from all branches and sorts them from newest to oldest TODO should this be a private method.
-
#get_authors ⇒ Object
Collects the names of the authors in each branch, calls unique on them and sorts them by alphabetically.
-
#get_branch_names(repo) ⇒ Object
Gets the names of the branches in the repo.
- #get_master_branch ⇒ Object
- #get_project_name ⇒ Object
- #get_remote_url ⇒ Object
-
#get_total_uniq_commits_in_repo ⇒ Object
Gets the amount of uniq commits in the repo * Calls get_all_commits_in_repo * collects the commits’ commit ids.
- #go_down_branch_from_head(branch) ⇒ Object
- #in_which_branches(commit_id) ⇒ Object
-
#initialize(path, options = {}, master_branch_name = nil) ⇒ RepoAnalyzer
constructor
A new instance of RepoAnalyzer.
- #inspect ⇒ Object
-
#last_commited_commit ⇒ Object
Gets the last commited commit * Calls get_all_commits_in_repo * Gets the first commit in the array.
- #set_master_branch(master_branch_name) ⇒ Object
- #submodule_codes ⇒ Object
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, = {}, master_branch_name=nil) Babygitter.repo_path = path repo = Grit::Repo.new(path, ) @path = repo.path @bare = [: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 = @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_names ⇒ Object (readonly)
Returns the value of attribute authors_names.
6 7 8 |
# File 'lib/babygitter/repo_analyzer.rb', line 6 def @authors_names end |
#began ⇒ Object (readonly)
Returns the value of attribute began.
6 7 8 |
# File 'lib/babygitter/repo_analyzer.rb', line 6 def began @began end |
#branch_names ⇒ Object (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 |
#branches ⇒ Object (readonly)
Returns the value of attribute branches.
6 7 8 |
# File 'lib/babygitter/repo_analyzer.rb', line 6 def branches @branches end |
#lastest_commit ⇒ Object (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_branch ⇒ Object (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_name ⇒ Object (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_url ⇒ Object (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_list ⇒ Object (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_commits ⇒ Object (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_branches ⇒ Object
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.}.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_branch ⇒ Object
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_commit ⇒ Object
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_repo ⇒ Object
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. }.reverse end |
#get_authors ⇒ Object
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 @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_branch ⇒ Object
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_name ⇒ Object
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_url ⇒ Object
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_repo ⇒ Object
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 |
#inspect ⇒ Object
157 158 159 |
# File 'lib/babygitter/repo_analyzer.rb', line 157 def inspect %Q{#<Babygitter::Repo #{@project_name}>} end |
#last_commited_commit ⇒ Object
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_codes ⇒ Object
106 107 108 |
# File 'lib/babygitter/repo_analyzer.rb', line 106 def submodule_codes `cd #{@path.gsub(/\/.git$/, "")}; git submodule` unless @bare end |