Class: Githook::Util

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

Constant Summary collapse

DEF_BRANCH_NAME_REG =

default valid branch name: feature/24_add_enable_task will generate commit message: FEATURE #24 - Add enable task

/^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/
DEF_MSG_SUMMARY_REG =
/^(FEATURE|BUG|MISC|REFACTOR)(\s#\d+)* - ([A-Z].*)[^.]$/
DEF_MSG_SUMMARY_FORMAT =
"FEAUTER|BUG|MISC|REFACTOR #issue_num - Summary"
DEF_MSG_BODY_REG =
/^- ([a-z].*)[^.]$/
DEF_MSG_BODY_FORMAT =
"- detail"

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.gsub("-", "_") }
end

.branch_nameObject



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

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

.check_msg_format?(commit_msg_arr) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/githook/util.rb', line 134

def self.check_msg_format?(commit_msg_arr)
  summary = commit_msg_arr[0] || ""
  second_line = commit_msg_arr[1] || ""
  body = commit_msg_arr[2..-1] || []

  valid = summary.start_with?("Merge branch") || DEF_MSG_SUMMARY_REG.match(summary)
  unless valid
    puts "Commit message summary \"#{summary}\" format isn't correct."
    puts "Expected format: \"#{DEF_MSG_SUMMARY_FORMAT}\""
    return false
  end

  valid = second_line.strip.empty?
  unless valid
    puts "Commit message the first line after summary should be blank."
    return false
  end

  body.each do |line|
    unless DEF_MSG_BODY_REG.match(line)
      puts "Commit message body line \"#{line}\" format isn't correct."
      puts "Expected format: \"#{DEF_MSG_BODY_FORMAT}\""
      return false
    end
  end
  true
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

.gen_pre_msg(branch_name) ⇒ Object

generate pre msg according branch name why do I pass branch_name as a parameter, not implement it inside the gen_pre_msg, because this is easy to test, it a kind of inject dependency thinking.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/githook/util.rb', line 103

def self.gen_pre_msg(branch_name)
  match_group = DEF_BRANCH_NAME_REG.match(branch_name)
  if match_group
    issue_type = match_group[1].upcase
    issue_num = match_group[2]
    issue_content = match_group[3]

    issue_type = "BUG" if issue_type == "HOTFIX"
    issue_num = " \##{issue_num}" unless issue_num.empty?
    issue_content = issue_content.tr("_", " ").strip.capitalize

    "#{issue_type}#{issue_num} - #{issue_content}"
  else
    "MISC - "
  end
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



121
122
123
124
125
126
127
128
# File 'lib/githook/util.rb', line 121

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