Class: Gitview
Overview
This class is used to gather git repo information using backticks or system(). None of the methods expects parameters to be passed into them. Future versions will take arguments for more extensible operation.
Instance Method Summary collapse
-
#git_version ⇒ Object
Get the version of git.
-
#repo_branches ⇒ Object
Count branches of the repository.
-
#repo_commits ⇒ Object
Count total commits in the master branch excluding merges.
-
#repo_committers ⇒ Object
Use output of git shortlog -sn and truncate to array with first 3 lines to create a list of the 3 most active committers to the repository.
-
#repo_email ⇒ Object
Determine repo user email.
-
#repo_exists? ⇒ Boolean
Checks to see if a repo exists in the current working directory.
-
#repo_files ⇒ Object
Count files in the repository.
-
#repo_firstcommit ⇒ Object
Identify first commit in the repository.
-
#repo_lastcommit ⇒ Object
Identify most recent commit in the repository.
-
#repo_leastchanged ⇒ Object
Identify least changed files in the repository, using the class array @@allchanged created by repo_mostchanged().
-
#repo_mostchanged ⇒ Object
Identify most changed files in the repository.
-
#repo_name ⇒ Object
Use the name of the current working directory as the name of the git repository.
-
#repo_remotes ⇒ Object
Gather list of non-local copies of the repository.
-
#repo_status ⇒ Object
Check the repo to see if it is current or not.
-
#repo_user ⇒ Object
Determine repo user name.
Instance Method Details
#git_version ⇒ Object
Get the version of git
44 45 46 |
# File 'lib/gitview.rb', line 44 def git_version @git_version = `git --version`.chomp end |
#repo_branches ⇒ Object
Count branches of the repository.
106 107 108 109 110 |
# File 'lib/gitview.rb', line 106 def repo_branches branches = `git branch`.split(/\n/) @repo_branches = branches.select { |b| b !~ /master.*/ } @repo_branches.count end |
#repo_commits ⇒ Object
Count total commits in the master branch excluding merges.
115 116 117 118 |
# File 'lib/gitview.rb', line 115 def repo_commits @@repo_commits = `git log master --no-merges --reverse --format="%ai"`.split(/\n/) @@repo_commits.count end |
#repo_committers ⇒ Object
Use output of git shortlog -sn and truncate to array with first 3 lines to create a list of the 3 most active committers to the repository.
163 164 165 166 |
# File 'lib/gitview.rb', line 163 def repo_committers committers = `git shortlog -sn`.split(/\n/) @repo_committers = committers.values_at(0..2) end |
#repo_email ⇒ Object
Determine repo user email. If unset locally and globally, “unset@unset” is reported.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gitview.rb', line 78 def repo_email local_email = `git config --local user.email` global_email = `git config --global user.email` if local_email == "" @repo_email = global_email @repo_email = "unset@unset" if global_email == "" else @repo_email = local_email end end |
#repo_exists? ⇒ Boolean
Checks to see if a repo exists in the current working directory. If one is not found, the program is aborted.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/gitview.rb', line 17 def repo_exists? direxists = File.directory?("./.git") if direxists == false abort "No repo found in current working directory!\n\n" else cfgexists = File.exists?("./.git/config") if cfgexists == false abort "No repo config found!\n\n" else @repo_exists = true end end end |
#repo_files ⇒ Object
Count files in the repository.
92 93 94 |
# File 'lib/gitview.rb', line 92 def repo_files @repo_files = `git ls-files`.split(/\n/).length end |
#repo_firstcommit ⇒ Object
Identify first commit in the repository.
123 124 125 126 |
# File 'lib/gitview.rb', line 123 def repo_firstcommit # let's reuse the class variable @@repo_commits @@repo_commits.first end |
#repo_lastcommit ⇒ Object
Identify most recent commit in the repository.
131 132 133 134 |
# File 'lib/gitview.rb', line 131 def repo_lastcommit # let's reuse the class variable @@repo_commits @@repo_commits.last end |
#repo_leastchanged ⇒ Object
Identify least changed files in the repository, using the class array @@allchanged created by repo_mostchanged()
153 154 155 156 157 |
# File 'lib/gitview.rb', line 153 def repo_leastchanged # let's reuse our deduped array from repo_mostchanged() lc = @@allchanged.first(3).reverse @repo_leastchanged = Hash[*lc.flatten] end |
#repo_mostchanged ⇒ Object
Identify most changed files in the repository
139 140 141 142 143 144 145 146 147 |
# File 'lib/gitview.rb', line 139 def repo_mostchanged changes = `git log master --pretty=format: --name-only`.split(/\n/) mostchanged = changes.select { |c| c =~ /[[:alpha:]]/ } deduped = Hash.new 0 mostchanged.each { |v| deduped.store(v,deduped[v]+1) } @@allchanged = deduped.sort_by { |k, v| v } mc = @@allchanged.last(3).reverse @repo_mostchanged = Hash[*mc.flatten] end |
#repo_name ⇒ Object
Use the name of the current working directory as the name of the git repository.
35 36 37 38 39 |
# File 'lib/gitview.rb', line 35 def repo_name @repo_name = `git rev-parse --show-toplevel`.chomp.split("/").last @repo_name.to_s "#{@repo_name}" end |
#repo_remotes ⇒ Object
Gather list of non-local copies of the repository.
99 100 101 |
# File 'lib/gitview.rb', line 99 def repo_remotes @repo_remotes = `git remote -v`.split(/\n/) end |
#repo_status ⇒ Object
Check the repo to see if it is current or not
51 52 53 54 55 56 57 58 |
# File 'lib/gitview.rb', line 51 def repo_status status = system("git status --porcelain -z") if status == "" @repo_status = "Not current!" else @repo_status = "Current" end end |
#repo_user ⇒ Object
Determine repo user name. If unset locally and globally, “unset” is reported.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gitview.rb', line 63 def repo_user local_user = `git config --local user.name` global_user = `git config --global user.name` if local_user == "" @repo_user = global_user @repo_user = "unset" if global_user == "" else @repo_user = local_user end end |