Last active
December 20, 2015 01:49
-
-
Save issm/6051695 to your computer and use it in GitHub Desktop.
`bb/1234` なブランチの場合,コミットメッセージの後に `BB refs #1234` がつくように. Git-Hooks と同様,`foobar/id/1234` なブランチの場合には `refs #1234` がつく感じ.
This file contains hidden or 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 | |
use strict; | |
use warnings; | |
use File::Basename; | |
use File::Spec; | |
sub main { | |
my ($msgfile) = @_; | |
my @msg = do { | |
open my $fh, '<', $msgfile; | |
my @l = map { s/\n*$//; $_ } grep !/^#/, <$fh>; | |
}; | |
return 0 if $msg[0] =~ /\#\d+[\s\n]*$/; | |
my $branch = get_current_branch(); | |
### bb/{issue_id} | |
if ( my @m = $branch =~ m!^bb/(\d+)$! ) { | |
my ($issue_id) = @m; | |
my $suffix = sprintf( 'BB refs #%d', $issue_id ); | |
$msg[0] = append_suffix( $msg[0], $suffix ); | |
} | |
### .../id/{ticket_id} | |
elsif ( @m = $branch =~ m!(?:^.*?)?id/(\d+)$! ) { | |
my ($ticket_id) = @m; | |
my $suffix = sprintf( 'refs #%d', $ticket_id ); | |
$msg[0] = append_suffix( $msg[0], $suffix ); | |
} | |
else { | |
return 0; | |
} | |
my $msg = join "\n", @msg; | |
open my $fh, '>', $msgfile; | |
print $fh $msg; | |
close $fh; | |
return 0; | |
} | |
sub git_dir { | |
my $dir = File::Spec->catdir( File::Spec->rel2abs( dirname __FILE__ ), '..' ); | |
do 1 while $dir =~ s!/[^/]+/\.\.!!; | |
return $dir; | |
} | |
sub get_current_branch { | |
my $gitdir = git_dir(); | |
my (undef, $HEAD) = split / +/s, `cat $gitdir/HEAD`; # `cat` gets "ref: refs/heads/..." | |
my ($branch) = $HEAD =~ m!^refs/heads/(.+)$!; | |
return $branch; | |
} | |
sub append_suffix { | |
my ($text, $suffix) = @_; | |
$text =~ s/\s*$/ /; | |
$text .= $suffix; | |
return $text; | |
} | |
my $exit = main( @ARGV ); | |
exit $exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment