-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-logzilla-post-receive.pl
executable file
·100 lines (78 loc) · 2.62 KB
/
git-logzilla-post-receive.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env perl
# Based on code by: Devendra Gera, 2008
# Copyright (C) 2010, Rowan James <rowanj@burninator.net>
# License: GPL v2 or later
use strict;
use List::MoreUtils qw(uniq);
sub add_comment {
my $bug_number = shift;
my $comment = shift;
# print STDERR "Hook: adding comment \"$comment\" to bug $bug_number\n";
open(COMMENT_PIPE, "|-") || exec 'git-bugz-comment', "$bug_number";
print COMMENT_PIPE "$comment";
close(COMMENT_PIPE);
}
# Define a function to add git notes to each bug it modifies
sub process_commit {
my $commit_info = shift;
my $commit_refname = shift;
my $commit_id = ((split(/\s+/,$commit_info))[0]);
# print "Proccessing message for commit $commit_id\n";
my $commit_msg = `git whatchanged -n 1 $commit_id`;
#print "base commit_msg=$commit_msg\n";
# author
my ($author) = ( $commit_msg =~ /^Author:\s+(.*)$/m );
# files
my @filelist = grep ( /^:/, split( /\n/, $commit_msg ) );
# prepare comment
$commit_msg =~ s/^.*?Date://s;# eat everything till the Date: header
$commit_msg =~ s/^.*?\n//m;# eat the date line completely
$commit_msg =~ s/^:.*?$//mg;# eat the file list from the msg.
$commit_msg =~ s/^\s+|\s+$//g ;# strip leading and trailing whitespace
my $bug_regex = 'bug\s*(?:#|)\s*(?P<bug>\d+)';
my (@bug_numbers) = uniq(sort( $commit_msg =~ /$bug_regex/gi ));
my $bug_list;
foreach my $bug_number(@bug_numbers) {
$bug_list .= "\tbug $bug_number\n";
}
chomp $bug_list;
my $change_list;
foreach my $change(@filelist) {
$change =~ s/^:[0-7]{6}\s[0-7]{6}\s.*\s([MCRADU])[0-9]*\s(.*)$/$1 $2\n/;
$change_list .= $change;
}
chomp $change_list;
my $comment = <<END;
$author committed $commit_refname
\t($commit_id)
$commit_msg
Changed:
$change_list
END
if (scalar(@bug_numbers) > 1) {
$comment .= "Referenced bugs:\n$bug_list";
}
foreach my $bug_number(@bug_numbers) {
add_comment($bug_number, $comment);
}
}
# for each ref that's being updated
# we get the previous HEAD, the new HEAD, and the name of the ref
# read lines from STDIN until the pipe is closed (EOF)
my $line;
while (defined($line = <STDIN>)) {
chomp $line;
my ($oldrev, $newrev, $refname) = split /\s+/, $line;
if ($refname ne "refs/heads/master") {
# skip updates that aren't on master
# For branches, they'll be processed when (if) they're merged
# for tags, etc. we don't want to process them anyway.
next;
}
my $commit_list = `git log --pretty=oneline $oldrev..$newrev`;
my @commits = reverse(split("\n", $commit_list));
foreach my $commit(@commits) {
process_commit($commit, $refname);
}
}
exit;