class Nohoch::CLI

Attributes

directory[R]
files[R]
options[R]
out[R]
user_file_stats[R]

Public Class Methods

new() click to toggle source
# File lib/nohoch.rb, line 16
def initialize
  @options = Struct.new(:verbose, :directory, :branch, :files).new(false, Dir.pwd, "origin/master", [])
  @user_file_stats = UserFileStats.new()
  @out = []
  parse_opts
  set_directory
  get_files
  stats
  # p @options
  # parse_stats
  # @user_file_stats.stats.sort_by {|_, file_stat| -file_stat.added}.each {|_, v| p v.to_s}
  # @user_file_stats.top("values-prod-usw2-kubernetes.yaml", 5).each_with_index {|e, i| p "[#{i+1}] #{e.to_s}"}

  # random_file = @user_file_stats.files.sample
  # @user_file_stats.top(random_file, 5).each_with_index {|e, i| p "[#{i+1}] #{e.to_s}"}
  @user_file_stats.all_top(5).each do |_, file_stats|
    file_stats.each_with_index {|e, i| p "[#{i+1}] #{e.to_s}"}
  end
end

Public Instance Methods

get_files() click to toggle source
# File lib/nohoch.rb, line 82
def get_files
  Dir.chdir(@directory){
    # git ls-tree -r master --name-only
    IO.popen(["git", "ls-tree", "-r", @options.branch, "--name-only", :err=>[:child, :out]]) {|git_io|
      @files = git_io.read.split(/\n+/)
    }
  }
end
parse_opts() click to toggle source
# File lib/nohoch.rb, line 36
def parse_opts
  OptionParser.new do |opts|
    opts.banner = "Usage: nohoch [options]"
    opts.on("-dDIRECTORY", "--directory=DIRECTORY", "git directory to check") { |o| @options.directory = o }
    opts.on("-bBRANCH", "--branch=BRANCH", "git branch") { |o| @options.branch = o }
    opts.on("-fFILE,...", "--files FILE,...", "file") { |o| @options.files = o.split(",") }
    opts.on("-v", "--[no-]verbose", "Run verbosely") { |o| @options.verbose = o }
  end.parse!
end
set_directory() click to toggle source
# File lib/nohoch.rb, line 46
def set_directory
  directory = Pathname.new(@options.directory)
  raise "Not a dir" unless directory.directory?
  raise "Directory does not contain an initialized git directroy" unless directory.join(".git").directory?
  @directory = directory
end
stats() click to toggle source
# File lib/nohoch.rb, line 53
def stats
  files = @options.files.empty? ? @files : @files.filter {|f| @options.files.include?(f) }
  files.each do |filename|
    Dir.chdir(@directory){
      # git log --no-merges --pretty=%an --numstat -10
      # git --no-pager log --pretty=oneline --follow -- $FILE
      # git --no-pager log --pretty=tformat:"%h %an %s" --follow -- $FILE
      IO.popen(["git", "--no-pager", "log", "--no-merges", "--follow", "--pretty=%an", "--numstat", "--", filename, :err=>[:child, :out]]) {|git_io|
        # TODO check that the output is in the correct format and raise exception if it is not.
        # capture the helpful debug info.
        result = git_io.read
        p "[DEBUG] git log result: #{result}" if @options.verbose
        out = result.split(/\n+/)
        out.each_slice(2) do |user, stat|
          matches = /(?<added>[\d-]+)\t(?<deleted>[\d-]+)\t(?<file>.*)/.match(stat)
          p "[DEBUG] user: #{user} matches: #{matches.inspect}" if @options.verbose
          added = matches[:added].to_i
          deleted = matches[:deleted].to_i
          file = matches[:file].chomp

          # puts "#{user} added #{added} lines and deleted #{deleted} lines in #{file}"
          @user_file_stats.add(FileStat.new(user.chomp, added, deleted, file))
        end
      }
    }
  end
end