|
#!/usr/bin/env ruby |
|
# MIT License |
|
|
|
# Copyright (c) 2020 iFixit |
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
# of this software and associated documentation files (the "Software"), to deal |
|
# in the Software without restriction, including without limitation the rights |
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
# copies of the Software, and to permit persons to whom the Software is |
|
# furnished to do so, subject to the following conditions: |
|
|
|
# The above copyright notice and this permission notice shall be included in all |
|
# copies or substantial portions of the Software. |
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
# SOFTWARE. |
|
|
|
message_file = ARGV[0] |
|
type = ARGV[1] |
|
case type |
|
when 'message', 'merge', 'squash' |
|
# When the user passes `-m` on the CLI, don't touch it |
|
return |
|
end |
|
|
|
def matches_once(lines, regex) |
|
matches = lines.select { |i| regex =~ i } |
|
matches.length == 1 |
|
end |
|
|
|
def for_single_match(lines, regexes) |
|
one_time = regexes.select { |re| matches_once(lines, re) } |
|
pp one_time |
|
return if one_time.empty? |
|
|
|
matching = one_time[0] |
|
|
|
matches = lines.select { |i| matching =~ i } |
|
yield matches[0] if matches.length == 1 |
|
end |
|
|
|
def get_method_name(filename, function) |
|
match = /function\s+(\w+)\s*\(/.match(function) |
|
return filename unless match |
|
|
|
function_name = match[1] |
|
if /static/ =~ function |
|
"#{filename}::#{function_name}" |
|
else |
|
"#{filename}->#{function_name}" |
|
end |
|
end |
|
|
|
def get_suggestion(filename) |
|
for_single_match( |
|
`git diff --cached`.each_line, |
|
[/^\+.*function\s+\w/, /^\-.*function\s+\w/, /function\s+\w/] |
|
) do |function| |
|
return get_method_name(filename, function) |
|
end |
|
for_single_match( |
|
`git diff --cached`.each_line, |
|
[/class\s+\w/] |
|
) do |classname| |
|
match = /class\s+(\w+)/.match(classname) |
|
return match[1] |
|
end |
|
filename |
|
end |
|
|
|
status = `git status --porcelain` |
|
for_single_match status.each_line, [/^M/] do |changed_file| |
|
match = /^M..(.*)\.\w+$/.match(changed_file) |
|
description = get_suggestion(File.basename(match[1])) |
|
suggested = "#{description}: \n" |
|
message = IO.read(message_file) |
|
IO.write(message_file, suggested + message) |
|
end |