Class: Gitview

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gitview.rb

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

Instance Method Details

#git_versionObject

Get the version of git



44
45
46
# File 'lib/gitview.rb', line 44

def git_version
  @git_version = `git --version`.chomp
end

#repo_branchesObject

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_commitsObject

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_committersObject

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_emailObject

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.

Returns:

  • (Boolean)


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_filesObject

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_firstcommitObject

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_lastcommitObject

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_leastchangedObject

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_mostchangedObject

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_nameObject

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_remotesObject

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_statusObject

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_userObject

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