Class: Githook::Util
- Inherits:
-
Object
- Object
- Githook::Util
- Defined in:
- lib/githook/util.rb
Class Method Summary collapse
-
.all_hooks ⇒ Object
include enabled_hooks and disabled_hooks.
- .branch_name ⇒ Object
-
.commit_msg_empty?(commit_msg_arr) ⇒ Boolean
check whether origin commit msg is empty.
- .commit_msg_file ⇒ Object
- .get_commit_msg(commit_msg_file) ⇒ Object
- .interactive_delete_files(path_arr, type) ⇒ Object
- .log_task(task_name) ⇒ Object
-
.prefill_msg(commit_msg_file, pre_msg) ⇒ Object
write the pre msg at the begining of commit_msg_file.
- .run_tasks(hook_stage) ⇒ Object
Class Method Details
.all_hooks ⇒ Object
include enabled_hooks and disabled_hooks
48 49 50 51 52 53 54 55 |
# 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.gsub("-", "_") } end |
.branch_name ⇒ Object
63 64 65 |
# File 'lib/githook/util.rb', line 63 def self.branch_name `git symbolic-ref --short HEAD`.strip end |
.commit_msg_empty?(commit_msg_arr) ⇒ Boolean
check whether origin commit msg is empty
90 91 92 93 94 95 |
# 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 ⇒ Object
59 60 61 |
# File 'lib/githook/util.rb', line 59 def self.commit_msg_file ".git/COMMIT_EDITMSG" end |
.get_commit_msg(commit_msg_file) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/githook/util.rb', line 20 def self.interactive_delete_files(path_arr, type) if path_arr.length == 0 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 ["N", "n", "\n"].include?(choice[0]) path_arr.each do |path| FileUtils.rm(path) puts "Delete #{path}" end end end |
.log_task(task_name) ⇒ Object
3 4 5 |
# File 'lib/githook/util.rb', line 3 def self.log_task(task_name) puts "[#{Time.now.strftime('%H:%m:%S')}] #{task_name.gsub('_', ' ')}" end |
.prefill_msg(commit_msg_file, pre_msg) ⇒ Object
write the pre msg at the begining of commit_msg_file
98 99 100 101 102 103 104 105 |
# 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) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# 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 |