summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorHolger Weiss <holger@zedat.fu-berlin.de>2009-10-24 09:42:52 (GMT)
committerHolger Weiss <holger@zedat.fu-berlin.de>2009-10-24 09:42:52 (GMT)
commit7f1844835dac4df2e8fe98d72d6fe77d5117eb2e (patch)
tree0e189e4441cd237d9a13e5b01332174e2a7547a2 /tools
parente7e9a99117d7e0a7189393b3a04366393620efab (diff)
downloadmonitoring-plugins-7f1844835dac4df2e8fe98d72d6fe77d5117eb2e.tar.gz
Import git-update-mirror and git-notify
Import the (self-written) git-update-mirror script, which updates clones of Git repositories and then calls git-notify (in just the same way as a post-receive hook would be called by Git). The git-notify script is imported from git://source.winehq.org/git/tools.git (commit: 03d66f34) and generates notifications on repository changes. We'll use these scripts for generating our commit e-mails.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/git-notify431
-rwxr-xr-xtools/git-update-mirror94
2 files changed, 525 insertions, 0 deletions
diff --git a/tools/git-notify b/tools/git-notify
new file mode 100755
index 0000000..d79cfcc
--- /dev/null
+++ b/tools/git-notify
@@ -0,0 +1,431 @@
1#!/usr/bin/perl -w
2#
3# Tool to send git commit notifications
4#
5# Copyright 2005 Alexandre Julliard
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of
10# the License, or (at your option) any later version.
11#
12#
13# This script is meant to be called from .git/hooks/post-receive.
14#
15# Usage: git-notify [options] [--] old-sha1 new-sha1 refname
16#
17# -c name Send CIA notifications under specified project name
18# -m addr Send mail notifications to specified address
19# -n max Set max number of individual mails to send
20# -r name Set the git repository name
21# -s bytes Set the maximum diff size in bytes (-1 for no limit)
22# -u url Set the URL to the gitweb browser
23# -i branch If at least one -i is given, report only for specified branches
24# -x branch Exclude changes to the specified branch from reports
25# -X Exclude merge commits
26#
27
28use strict;
29use open ':utf8';
30use Encode 'encode';
31use Cwd 'realpath';
32
33binmode STDIN, ':utf8';
34binmode STDOUT, ':utf8';
35
36sub git_config($);
37sub get_repos_name();
38
39# some parameters you may want to change
40
41# set this to something that takes "-s"
42my $mailer = "/usr/bin/mail";
43
44# CIA notification address
45my $cia_address = "cia\@cia.navi.cx";
46
47# debug mode
48my $debug = 0;
49
50# configuration parameters
51
52# base URL of the gitweb repository browser (can be set with the -u option)
53my $gitweb_url = git_config( "notify.baseurl" );
54
55# default repository name (can be changed with the -r option)
56my $repos_name = git_config( "notify.repository" ) || get_repos_name();
57
58# max size of diffs in bytes (can be changed with the -s option)
59my $max_diff_size = git_config( "notify.maxdiff" ) || 10000;
60
61# address for mail notices (can be set with -m option)
62my $commitlist_address = git_config( "notify.mail" );
63
64# project name for CIA notices (can be set with -c option)
65my $cia_project_name = git_config( "notify.cia" );
66
67# max number of individual notices before falling back to a single global notice (can be set with -n option)
68my $max_individual_notices = git_config( "notify.maxnotices" ) || 100;
69
70# branches to include
71my @include_list = split /\s+/, git_config( "notify.include" ) || "";
72
73# branches to exclude
74my @exclude_list = split /\s+/, git_config( "notify.exclude" ) || "";
75
76# Extra options to git rev-list
77my @revlist_options;
78
79sub usage()
80{
81 print "Usage: $0 [options] [--] old-sha1 new-sha1 refname\n";
82 print " -c name Send CIA notifications under specified project name\n";
83 print " -m addr Send mail notifications to specified address\n";
84 print " -n max Set max number of individual mails to send\n";
85 print " -r name Set the git repository name\n";
86 print " -s bytes Set the maximum diff size in bytes (-1 for no limit)\n";
87 print " -u url Set the URL to the gitweb browser\n";
88 print " -i branch If at least one -i is given, report only for specified branches\n";
89 print " -x branch Exclude changes to the specified branch from reports\n";
90 print " -X Exclude merge commits\n";
91 exit 1;
92}
93
94sub xml_escape($)
95{
96 my $str = shift;
97 $str =~ s/&/&amp;/g;
98 $str =~ s/</&lt;/g;
99 $str =~ s/>/&gt;/g;
100 my @chars = unpack "U*", $str;
101 $str = join "", map { ($_ > 127) ? sprintf "&#%u;", $_ : chr($_); } @chars;
102 return $str;
103}
104
105# format an integer date + timezone as string
106# algorithm taken from git's date.c
107sub format_date($$)
108{
109 my ($time,$tz) = @_;
110
111 if ($tz < 0)
112 {
113 my $minutes = (-$tz / 100) * 60 + (-$tz % 100);
114 $time -= $minutes * 60;
115 }
116 else
117 {
118 my $minutes = ($tz / 100) * 60 + ($tz % 100);
119 $time += $minutes * 60;
120 }
121 return gmtime($time) . sprintf " %+05d", $tz;
122}
123
124# fetch a parameter from the git config file
125sub git_config($)
126{
127 my ($param) = @_;
128
129 open CONFIG, "-|" or exec "git", "config", $param;
130 my $ret = <CONFIG>;
131 chomp $ret if $ret;
132 close CONFIG or $ret = undef;
133 return $ret;
134}
135
136# parse command line options
137sub parse_options()
138{
139 while (@ARGV && $ARGV[0] =~ /^-/)
140 {
141 my $arg = shift @ARGV;
142
143 if ($arg eq '--') { last; }
144 elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; }
145 elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; }
146 elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
147 elsif ($arg eq '-r') { $repos_name = shift @ARGV; }
148 elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; }
149 elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; }
150 elsif ($arg eq '-i') { push @include_list, shift @ARGV; }
151 elsif ($arg eq '-x') { push @exclude_list, shift @ARGV; }
152 elsif ($arg eq '-X') { push @revlist_options, "--no-merges"; }
153 elsif ($arg eq '-d') { $debug++; }
154 else { usage(); }
155 }
156 if (@ARGV && $#ARGV != 2) { usage(); }
157 @exclude_list = map { "^$_"; } @exclude_list;
158}
159
160# send an email notification
161sub mail_notification($$$@)
162{
163 my ($name, $subject, $content_type, @text) = @_;
164 $subject = encode("MIME-Q",$subject);
165 if ($debug)
166 {
167 print "---------------------\n";
168 print "To: $name\n";
169 print "Subject: $subject\n";
170 print "Content-Type: $content_type\n";
171 print "\n", join("\n", @text), "\n";
172 }
173 else
174 {
175 my $pid = open MAIL, "|-";
176 return unless defined $pid;
177 if (!$pid)
178 {
179 exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
180 }
181 print MAIL join("\n", @text), "\n";
182 close MAIL;
183 }
184}
185
186# get the default repository name
187sub get_repos_name()
188{
189 my $dir = `git rev-parse --git-dir`;
190 chomp $dir;
191 my $repos = realpath($dir);
192 $repos =~ s/(.*?)((\.git\/)?\.git)$/$1/;
193 $repos =~ s/(.*)\/([^\/]+)\/?$/$2/;
194 return $repos;
195}
196
197# extract the information from a commit or tag object and return a hash containing the various fields
198sub get_object_info($)
199{
200 my $obj = shift;
201 my %info = ();
202 my @log = ();
203 my $do_log = 0;
204
205 open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
206 my $type = <TYPE>;
207 chomp $type;
208 close TYPE;
209
210 open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file";
211 while (<OBJ>)
212 {
213 chomp;
214 if ($do_log)
215 {
216 last if /^-----BEGIN PGP SIGNATURE-----/;
217 push @log, $_;
218 }
219 elsif (/^(author|committer|tagger) ((.*)(<.*>)) (\d+) ([+-]\d+)$/)
220 {
221 $info{$1} = $2;
222 $info{$1 . "_name"} = $3;
223 $info{$1 . "_email"} = $4;
224 $info{$1 . "_date"} = $5;
225 $info{$1 . "_tz"} = $6;
226 }
227 elsif (/^tag (.*)$/)
228 {
229 $info{"tag"} = $1;
230 }
231 elsif (/^$/) { $do_log = 1; }
232 }
233 close OBJ;
234
235 $info{"type"} = $type;
236 $info{"log"} = \@log;
237 return %info;
238}
239
240# send a commit notice to a mailing list
241sub send_commit_notice($$)
242{
243 my ($ref,$obj) = @_;
244 my %info = get_object_info($obj);
245 my @notice = ();
246 my $subject;
247
248 if ($info{"type"} eq "tag")
249 {
250 push @notice,
251 "Module: $repos_name",
252 "Branch: $ref",
253 "Tag: $obj",
254 $gitweb_url ? "URL: $gitweb_url/?a=tag;h=$obj\n" : "",
255 "Tagger: " . $info{"tagger"},
256 "Date: " . format_date($info{"tagger_date"},$info{"tagger_tz"}),
257 "",
258 join "\n", @{$info{"log"}};
259 $subject = "Tag " . $info{"tag"} . " : " . $info{"tagger_name"} . ": " . ${$info{"log"}}[0];
260 }
261 else
262 {
263 push @notice,
264 "Module: $repos_name",
265 "Branch: $ref",
266 "Commit: $obj",
267 $gitweb_url ? "URL: $gitweb_url/?a=commit;h=$obj\n" : "",
268 "Author: " . $info{"author"},
269 "Date: " . format_date($info{"author_date"},$info{"author_tz"}),
270 "",
271 join "\n", @{$info{"log"}},
272 "",
273 "---",
274 "";
275
276 open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
277 push @notice, join("", <STAT>);
278 close STAT;
279
280 open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
281 my $diff = join( "", <DIFF> );
282 close DIFF;
283
284 if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
285 {
286 push @notice, $diff;
287 }
288 else
289 {
290 push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url;
291 }
292
293 $subject = $info{"author_name"} . ": " . ${$info{"log"}}[0];
294 }
295
296 mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
297}
298
299# send a commit notice to the CIA server
300sub send_cia_notice($$)
301{
302 my ($ref,$commit) = @_;
303 my %info = get_object_info($commit);
304 my @cia_text = ();
305
306 return if $info{"type"} ne "commit";
307
308 push @cia_text,
309 "<message>",
310 " <generator>",
311 " <name>git-notify script for CIA</name>",
312 " </generator>",
313 " <source>",
314 " <project>" . xml_escape($cia_project_name) . "</project>",
315 " <module>" . xml_escape($repos_name) . "</module>",
316 " <branch>" . xml_escape($ref). "</branch>",
317 " </source>",
318 " <body>",
319 " <commit>",
320 " <revision>" . substr($commit,0,10) . "</revision>",
321 " <author>" . xml_escape($info{"author"}) . "</author>",
322 " <log>" . xml_escape(join "\n", @{$info{"log"}}) . "</log>",
323 " <files>";
324
325 open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", "-M", $commit or die "cannot run git-diff-tree";
326 while (<COMMIT>)
327 {
328 chomp;
329 if (/^([AMD])\t(.*)$/)
330 {
331 my ($action, $file) = ($1, $2);
332 my %actions = ( "A" => "add", "M" => "modify", "D" => "remove" );
333 next unless defined $actions{$action};
334 push @cia_text, " <file action=\"$actions{$action}\">" . xml_escape($file) . "</file>";
335 }
336 elsif (/^R\d+\t(.*)\t(.*)$/)
337 {
338 my ($old, $new) = ($1, $2);
339 push @cia_text, " <file action=\"rename\" to=\"" . xml_escape($new) . "\">" . xml_escape($old) . "</file>";
340 }
341 }
342 close COMMIT;
343
344 push @cia_text,
345 " </files>",
346 $gitweb_url ? " <url>" . xml_escape("$gitweb_url/?a=commit;h=$commit") . "</url>" : "",
347 " </commit>",
348 " </body>",
349 " <timestamp>" . $info{"author_date"} . "</timestamp>",
350 "</message>";
351
352 mail_notification($cia_address, "DeliverXML", "text/xml", @cia_text);
353}
354
355# send a global commit notice when there are too many commits for individual mails
356sub send_global_notice($$$)
357{
358 my ($ref, $old_sha1, $new_sha1) = @_;
359 my @notice = ();
360
361 push @revlist_options, "--pretty";
362 open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
363 while (<LIST>)
364 {
365 chomp;
366 s/^commit /URL: $gitweb_url\/?a=commit;h=/ if $gitweb_url;
367 push @notice, $_;
368 }
369 close LIST;
370
371 mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice);
372}
373
374# send all the notices
375sub send_all_notices($$$)
376{
377 my ($old_sha1, $new_sha1, $ref) = @_;
378
379 $ref =~ s/^refs\/heads\///;
380
381 return if (@include_list && !grep {$_ eq $ref} @include_list);
382
383 if ($old_sha1 eq '0' x 40) # new ref
384 {
385 send_commit_notice( $ref, $new_sha1 ) if $commitlist_address;
386 return;
387 }
388
389 my @commits = ();
390
391 open LIST, "-|" or exec "git", "rev-list", @revlist_options, "^$old_sha1", "$new_sha1", @exclude_list or die "cannot exec git-rev-list";
392 while (<LIST>)
393 {
394 chomp;
395 die "invalid commit $_" unless /^[0-9a-f]{40}$/;
396 unshift @commits, $_;
397 }
398 close LIST;
399
400 if (@commits > $max_individual_notices)
401 {
402 send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address;
403 return;
404 }
405
406 foreach my $commit (@commits)
407 {
408 send_commit_notice( $ref, $commit ) if $commitlist_address;
409 send_cia_notice( $ref, $commit ) if $cia_project_name;
410 }
411}
412
413parse_options();
414
415# append repository path to URL
416$gitweb_url .= "/$repos_name.git" if $gitweb_url;
417
418if (@ARGV)
419{
420 send_all_notices( $ARGV[0], $ARGV[1], $ARGV[2] );
421}
422else # read them from stdin
423{
424 while (<>)
425 {
426 chomp;
427 if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $1, $2, $3 ); }
428 }
429}
430
431exit 0;
diff --git a/tools/git-update-mirror b/tools/git-update-mirror
new file mode 100755
index 0000000..1c6eea5
--- /dev/null
+++ b/tools/git-update-mirror
@@ -0,0 +1,94 @@
1#!/bin/sh
2#
3# Copyright (c) 2009 Nagios Plugins Development Team
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18set -e
19set -u
20
21prefix='/home/git'
22
23PATH="$prefix/opt/git/bin:/bin:/usr/bin"
24export PATH
25
26gitnotify="$prefix/libexec/git-notify"
27recipient='Nagios Plugins Commit List <nagiosplug-checkins@lists.sourceforge.net>'
28maxcommits=100
29maxdiffsize=$((300 * 1024))
30gitweburl='http://repo.or.cz/w'
31tempprefix='/dev/shm'
32fourtyzeros=$(printf '%.40u' 0)
33myself=${0##*/}
34
35checkrefs()
36{
37 turn=$1
38
39 git show-ref | while read object ref
40 do
41 refdir="$tempdir/${ref%/*}"
42 reffile="$tempdir/$ref"
43
44 if [ $turn -eq 2 -a -f "$reffile" ] \
45 && grep "^1 $object$" "$reffile" >'/dev/null'
46 then # The ref has not been modified.
47 rm -f "$reffile"
48 else
49 mkdir -p "$refdir"
50 echo "$turn $object" >>"$reffile"
51 fi
52 done
53}
54
55if [ $# -lt 1 ]
56then
57 echo >&2 "Usage: $myself <repository> ..."
58 exit 1
59fi
60
61tempdir=$(mktemp -d "$tempprefix/$myself.XXXXXX")
62tempfile=$(mktemp "$tempprefix/$myself.XXXXXX")
63trap 'rm -rf "$tempdir" "$tempfile"' EXIT
64
65for repository in "$@"
66do
67 cd "$repository"
68
69 checkrefs 1
70 if ! git remote update --prune >"$tempfile" 2>&1
71 then
72 cat >&2 "$tempfile"
73 exit 1
74 fi
75 git fetch --quiet --tags
76 checkrefs 2
77
78 find "$tempdir" -type 'f' -print | sed 's!^\./!!' | while read reffile
79 do
80 ref=${reffile#$tempdir/}
81 old=$(awk '$1 == "1" { print $2; exit }' "$reffile")
82 new=$(awk '$1 == "2" { print $2; exit }' "$reffile")
83 old=${old:-$fourtyzeros}
84 new=${new:-$fourtyzeros}
85
86 echo "$old" "$new" "$ref"
87 done | $gitnotify \
88 -m "$recipient" \
89 -n "$maxcommits" \
90 -s "$maxdiffsize" \
91 -u "$gitweburl"
92
93 cd "$OLDPWD"
94done