class Githook::Util

Public Class Methods

all_hooks() click to toggle source

include enabled_hooks and disabled_hooks

# File lib/githook/util.rb, line 48
def self.all_hooks
  Dir.glob('.git/hooks/*')
     .map { |path| path.split('/').last }
     .select { |name| !name.include?('.') || name.include?('.disable') }
     .map { |name| name.gsub('.disable', '') }
     .uniq
     .map { |name| name.tr('-', '_') }
end
branch_name() click to toggle source
# File lib/githook/util.rb, line 63
def self.branch_name
  `git symbolic-ref --short HEAD`.strip
end
changed_files() click to toggle source

get changed files, include added or modified

# File lib/githook/util.rb, line 108
def self.changed_files
  added_or_modified_reg = /A|AM|^M/
  `git status --porcelain`.split(/\n/)
                          .select do |file_name_with_status|
    file_name_with_status =~ added_or_modified_reg
  end
                          .map do |file_name_with_status|
    file_name_with_status.split(' ')[1]
  end
end
changed_ruby_files() click to toggle source
# File lib/githook/util.rb, line 119
def self.changed_ruby_files
  changed_files
    .select do |file_name|
    File.extname(file_name) == '.rb'
  end
    .join(' ')
end
commit_msg_empty?(commit_msg_arr) click to toggle source

check whether origin commit msg is empty

# File lib/githook/util.rb, line 90
def self.commit_msg_empty?(commit_msg_arr)
  commit_msg_arr.each do |line|
    return false unless line.strip.empty?
  end
  true
end
commit_msg_file() click to toggle source
# File lib/githook/util.rb, line 59
def self.commit_msg_file
  '.git/COMMIT_EDITMSG'
end
get_commit_msg(commit_msg_file) click to toggle source
# File lib/githook/util.rb, line 67
def self.get_commit_msg(commit_msg_file)
  commit_msg = []
  # trim begining empty lines
  File.open(commit_msg_file, 'r') do |f|
    f.readlines.each do |line|
      next if line[0] == '#'
      next if commit_msg.empty? && line.strip.empty?
      commit_msg << line
    end
  end
  # trim redundant tail empty lines
  unless commit_msg.empty?
    last_not_empty_line = 0
    commit_msg.each_with_index do |line, index|
      last_not_empty_line = index unless line.strip.empty?
    end
    commit_msg = commit_msg[0..last_not_empty_line]
  end
  # remove every line right blank space, include "\n"
  commit_msg.map(&:rstrip)
end
interactive_delete_files(path_arr, type) click to toggle source
# File lib/githook/util.rb, line 20
def self.interactive_delete_files(path_arr, type)
  if path_arr.empty?
    puts "There are no #{type}."
  else
    puts "There are following #{type}:"
    puts path_arr
    print 'Are you sure want to delete all of them [y/(n)]: '
    # https://stackoverflow.com/a/40643667/2998877
    choice = STDIN.gets
    return if %W[n N \n].include?(choice[0])

    path_arr.each do |path|
      FileUtils.rm(path)
      puts "Delete #{path}"
    end
  end
end
log_task(task_name) click to toggle source
# File lib/githook/util.rb, line 3
def self.log_task(task_name)
  puts "[#{Time.now.strftime('%H:%m:%S')}] #{task_name.tr('_', ' ')}"
end
prefill_msg(commit_msg_file, pre_msg) click to toggle source

write the pre msg at the begining of commit_msg_file

# File lib/githook/util.rb, line 98
def self.prefill_msg(commit_msg_file, pre_msg)
  File.open(commit_msg_file, 'r+') do |f|
    ori_content = f.read
    f.seek(0, IO::SEEK_SET)
    f.puts pre_msg
    f.puts ori_content
  end
end
run_tasks(hook_stage) click to toggle source
# File lib/githook/util.rb, line 7
def self.run_tasks(hook_stage)
  tasks = fetch(hook_stage, [])
  tasks.each do |task|
    if Rake::Task.task_defined?(task)
      Rake::Task[task].invoke
    else
      puts "#{task} task doesn't exist."
    end
  end
end