Skip to content

Instantly share code, notes, and snippets.

@andyg0808
Last active June 25, 2020 23:05
Show Gist options
  • Save andyg0808/88120bb9489a3d1da6d410ee4916aed2 to your computer and use it in GitHub Desktop.
Save andyg0808/88120bb9489a3d1da6d410ee4916aed2 to your computer and use it in GitHub Desktop.
Git prepare-commit-msg hook

Summary

This hook automatically inserts an appropriate filename description in the suggested commit message for each commit you make which touches a single file.

Examples

These are inserted as the first line of the message opened in your editor when running git commit. Messages entered with -m will not be affected:

If you've only touched a single static method

User::create: 

If you've only touched a single instance method

User->getName:

If you've only touched a single file, and none of the above apply

User: 

Limitations

This is currently only made to work with PHP files: if only one method is touched, it will insert the method name as part of the description.

#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment