class ShellAdapter

Adapter which handles shell access.

Public Class Methods

activity_interval(directory) click to toggle source
# File lib/git_survey/shell_adapter.rb, line 67
def self.activity_interval(directory)
  earliest = init_date(directory).to_f
  latest = latest_activity_date(directory).to_f

  diff = latest - earliest
  (diff / (365 * 24 * 60 * 60)).round(2)
end
authors(anonymized, directory) click to toggle source

git shortlog -s -n

# File lib/git_survey/shell_adapter.rb, line 91
def self.authors(anonymized, directory)
  hash_from_line_with_delimiter(anonymized, directory, COMMAND_AUTHORS, "\t")
end
hot_files(anonymized, directory, number) click to toggle source

git log –pretty=format: –name-only | sort | uniq -c | sort -rg | head -10

# File lib/git_survey/shell_adapter.rb, line 96
def self.hot_files(anonymized, directory, number)
  number_with_empty_lines = number.to_i + 1
  command = COMMAND_FILES_HOT + number_with_empty_lines.to_s
  hash_from_line_with_delimiter(anonymized, directory, command, ' ')
end
init_date(directory) click to toggle source

git log –date=iso8601-strict –reverse |head -3 |grep “Date”

# File lib/git_survey/shell_adapter.rb, line 56
def self.init_date(directory)
  string = run_shell_command(directory, COMMAND_REPO_INIT_DATE)
  Time.iso8601(date_at_the_end_of_line(string))
end
latest_activity_date(directory) click to toggle source

git log –date=iso8601-strict |head -3 |grep “Date”

# File lib/git_survey/shell_adapter.rb, line 62
def self.latest_activity_date(directory)
  string = run_shell_command(directory, COMMAND_LATEST_ACTIVITY_DATE)
  Time.iso8601(date_at_the_end_of_line(string))
end
number_of_branches(directory) click to toggle source

git branch | wc -l

# File lib/git_survey/shell_adapter.rb, line 86
def self.number_of_branches(directory)
  run_shell_command(directory, COMMAND_BRANCHES_NUMBER).to_i
end
number_of_commits(directory, branch) click to toggle source

git rev-list –count <revision>

# File lib/git_survey/shell_adapter.rb, line 76
def self.number_of_commits(directory, branch)
  run_shell_command(directory, COMMAND_COMMITS_NUMBER + branch).to_i
end
number_of_files(directory) click to toggle source

git ls-files | wc -l

# File lib/git_survey/shell_adapter.rb, line 81
def self.number_of_files(directory)
  run_shell_command(directory, COMMAND_FILES_NUMBER).to_i
end
repo_name(anonimize, directory) click to toggle source

basename `git rev-parse –show-toplevel`

# File lib/git_survey/shell_adapter.rb, line 50
def self.repo_name(anonimize, directory)
  name = run_shell_command(directory, COMMAND_REPO_NAME).strip
  Helpers.anonymized(anonimize, name)
end

Private Class Methods

date_at_the_end_of_line(string) click to toggle source
# File lib/git_survey/shell_adapter.rb, line 23
def self.date_at_the_end_of_line(string)
  string.split(' ').last
end
hash_from_line_with_delimiter(anonymized, directory, command, delimiter) click to toggle source
# File lib/git_survey/shell_adapter.rb, line 28
def self.hash_from_line_with_delimiter(anonymized,
                                       directory,
                                       command,
                                       delimiter)

  list = run_shell_command(directory, command)
         .split("\n")
  empty = 'empty' # git outputs empty lines too, they should be removed.

  result = {}
  list.each do |line|
    parts = line.split(delimiter)
    value = parts.first
    key = parts.last == value ? empty : Helpers.anonymized(anonymized, parts.last)

    result[key] = value.to_i
  end
  result.reject { |key, _value| key.to_s.match(empty) }
end
run_shell_command(directory, cmd) click to toggle source
# File lib/git_survey/shell_adapter.rb, line 17
def self.run_shell_command(directory, cmd)
  result = `cd #{directory}; #{cmd}`
  result
end