Class: Githook::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/githook/util.rb

Class Method Summary collapse

Class Method Details

.all_hooksObject

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.tr('-', '_') }
end

.branch_nameObject



63
64
65
# File 'lib/githook/util.rb', line 63

def self.branch_name
  `git symbolic-ref --short HEAD`.strip
end

.changed_filesObject

get changed files, include added or modified



108
109
110
111
112
113
114
115
116
117
# 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_filesObject



119
120
121
122
123
124
125
# 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) ⇒ Boolean

check whether origin commit msg is empty

Returns:

  • (Boolean)


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_fileObject



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.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) ⇒ 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.tr('_', ' ')}"
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