Last active
May 2, 2017 09:52
-
-
Save teddywing/49a1547477bbd2d2407d94706a8a6214 to your computer and use it in GitHub Desktop.
Git hook that adds "Refs #[issue number]" to commit messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd project/ | |
cp -i prepare-commit-msg .git/hooks/ | |
chmod +x .git/hooks/prepare-commit-msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl -w | |
# | |
# prepare-commit-msg | |
# | |
# Git commit hook that modifies the message of a new commit to write in the | |
# current issue number if present in the branch name. | |
use strict; | |
my $filename = $ARGV[0]; | |
my $commit_mode = $ARGV[1]; | |
# For new regular commits | |
if (!defined $commit_mode) { | |
my $branch_name = `git rev-parse --abbrev-ref HEAD`; | |
my @match = $branch_name =~ /^(\d+)-/; | |
my $issue_number = $match[0]; | |
# If the current branch has an initial issue number | |
# Write "Refs #<$issue_number>" to the first line of the commit message | |
if ($issue_number) { | |
open my $f_in, '<:encoding(UTF-8)', $filename; | |
open my $f_out, '>:encoding(UTF-8)', "$filename.new"; | |
print $f_out "\n\nRefs #" . $issue_number; | |
while (<$f_in>) { | |
print $f_out $_; | |
} | |
close $f_out; | |
close $f_in; | |
unlink $filename; | |
rename "$filename.new", $filename; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment