class Gitview

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.

Public Instance Methods

git_version() click to toggle source

Get the version of git

# File lib/gitview.rb, line 44
def git_version
  @git_version = `git --version`.chomp
end
repo_branches() click to toggle source

Count branches of the repository.

# 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() click to toggle source

Count total commits in the master branch excluding merges.

# 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() click to toggle source

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.

# 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() click to toggle source

Determine repo user email. If unset locally and globally, “unset@unset” is reported.

# 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?() click to toggle source

Checks to see if a repo exists in the current working directory. If one is not found, the program is aborted.

# 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() click to toggle source

Count files in the repository.

# File lib/gitview.rb, line 92
def repo_files
  @repo_files = `git ls-files`.split(/\n/).length
end
repo_firstcommit() click to toggle source

Identify first commit in the repository.

# File lib/gitview.rb, line 123
def repo_firstcommit
  # let's reuse the class variable @@repo_commits
  @@repo_commits.first
end
repo_lastcommit() click to toggle source

Identify most recent commit in the repository.

# File lib/gitview.rb, line 131
def repo_lastcommit
  # let's reuse the class variable @@repo_commits
  @@repo_commits.last
end
repo_leastchanged() click to toggle source

Identify least changed files in the repository, using the class array @@allchanged created by repo_mostchanged()

# 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() click to toggle source

Identify most changed files in the repository

# 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() click to toggle source

Use the name of the current working directory as the name of the git repository.

# 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() click to toggle source

Gather list of non-local copies of the repository.

# File lib/gitview.rb, line 99
def repo_remotes
  @repo_remotes = `git remote -v`.split(/\n/)
end
repo_status() click to toggle source

Check the repo to see if it is current or not

# 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() click to toggle source

Determine repo user name. If unset locally and globally, “unset” is reported.

# 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