3 # Copyright 2005 Alexandre Julliard
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of
8 # the License, or (at your option) any later version.
15 binmode STDIN, ':utf8';
16 binmode STDOUT, ':utf8';
21 # some parameters you may want to change
23 # set this to something that takes "-s"
24 my $mailer = "/usr/bin/mail";
29 # configuration parameters
31 # base URL of the gitweb repository browser
32 my $gitweb_url = "http://cgit.freedesktop.org/gstreamer";
34 # default repository name
35 my $repos_name = get_repos_name();
37 # max size of diffs in bytes
38 my $max_diff_size = 10000;
40 # address for mail notices
41 my $commitlist_address = 'gstreamer-commits@lists.freedesktop.org';
42 #my $commitlist_address = "bilboed";
44 # max number of individual notices before falling back to a single global notice
45 my $max_individual_notices = 100;
47 # format an integer date + timezone as string
48 # algorithm taken from git's date.c
55 my $minutes = (-$tz / 100) * 60 + (-$tz % 100);
56 $time -= $minutes * 60;
60 my $minutes = ($tz / 100) * 60 + ($tz % 100);
61 $time += $minutes * 60;
63 return gmtime($time) . sprintf " %+05d", $tz;
66 # fetch a parameter from the git config file
71 open CONFIG, "-|" or exec "git", "config", $param;
74 close CONFIG or $ret = undef;
78 # send an email notification
79 sub mail_notification($$$@)
81 my ($name, $subject, $content_type, @text) = @_;
82 $subject = encode("MIME-Q",$subject);
85 print "---------------------\n";
87 print "Subject: $subject\n";
88 print "Content-Type: $content_type\n";
89 print "\n", join("\n", @text), "\n";
93 my $pid = open MAIL, "|-";
94 return unless defined $pid;
97 exec $mailer, "-s", $subject, $name, or die "Cannot exec $mailer";
99 print MAIL join("\n", @text), "\n";
104 # get the default repository name
107 my $dir = `git rev-parse --git-dir`;
109 my $repos = realpath($dir);
110 $repos =~ s/(.*?)((\.git\/)?\.git)$/$1/;
111 $repos =~ s/(.*)\/([^\/]+)\/?$/$2/;
115 # extract the information from a commit or tag object and return a hash containing the various fields
116 sub get_object_info($)
123 open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
128 open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file";
134 last if /^-----BEGIN PGP SIGNATURE-----/;
137 elsif (/^(author|committer|tagger) ((.*)(<.*>)) (\d+) ([+-]\d+)$/)
140 $info{$1 . "_name"} = $3;
141 $info{$1 . "_email"} = $4;
142 $info{$1 . "_date"} = $5;
143 $info{$1 . "_tz"} = $6;
149 elsif (/^$/) { $do_log = 1; }
153 $info{"type"} = $type;
154 $info{"log"} = \@log;
158 # send a commit notice to a mailing list
159 sub send_commit_notice($$)
162 my %info = get_object_info($obj);
166 printf "sending e-mail for $obj\n";
168 # TODO normal tags are not identified
169 if ($info{"type"} eq "tag")
172 "Module: $repos_name",
175 $gitweb_url ? "URL: $gitweb_url/tag/?id=$obj\n" : "",
176 "Tagger: " . $info{"tagger"},
177 "Date: " . format_date($info{"tagger_date"},$info{"tagger_tz"}),
179 join "\n", @{$info{"log"}};
180 $subject = "Tag " . $info{"tag"} . ": " . ${$info{"log"}}[0];
185 "Module: $repos_name",
188 $gitweb_url ? "URL: $gitweb_url/commit/?id=$obj\n" : "",
189 "Author: " . $info{"author"},
190 "Date: " . format_date($info{"author_date"},$info{"author_tz"}),
192 join "\n", @{$info{"log"}},
197 open STAT, "-|" or exec "git", "diff-tree", "--stat", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
198 push @notice, join("", <STAT>);
201 open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
202 my $diff = join( "", <DIFF> );
205 if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
211 push @notice, "Diff: $gitweb_url/diff/?id=$obj" if $gitweb_url;
214 if ($ref eq 'master')
216 $subject = $repos_name . ": " . ${$info{"log"}}[0];
220 $subject = "[$ref] " . $repos_name . ": " . ${$info{"log"}}[0];
224 mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
227 # send a global commit notice when there are too many commits for individual mails
228 sub send_global_notice($$$)
230 my ($ref, $old_sha1, $new_sha1) = @_;
233 open LIST, "-|" or exec "git", "rev-list", "--pretty", "^$old_sha1", "$new_sha1" or die "cannot exec git-rev-list";
237 s/^commit /URL: $gitweb_url\/commit\/?id=/ if $gitweb_url;
242 mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @notice);
245 # send all the notices
246 sub send_all_notices($$$)
248 my ($old_sha1, $new_sha1, $ref) = @_;
250 $ref =~ s/^refs\/heads\///;
252 if ($old_sha1 eq '0' x 40) # new ref
254 send_commit_notice( $ref, $new_sha1 ) if $commitlist_address;
260 open LIST, "-|" or exec "git", "rev-list", "--topo-order", "^$old_sha1", "$new_sha1" or die "cannot exec git-rev-list";
264 die "invalid commit $_" unless /^[0-9a-f]{40}$/;
265 unshift @commits, $_;
269 if (@commits > $max_individual_notices)
271 send_global_notice( $ref, $old_sha1, $new_sha1 ) if $commitlist_address;
275 foreach my $commit (@commits)
277 send_commit_notice( $ref, $commit ) if $commitlist_address;
281 # append repository path to URL
282 $gitweb_url .= "/$repos_name" if $gitweb_url;
287 if (/^([0-9a-f]{40}) ([0-9a-f]{40}) (.*)$/) { send_all_notices( $1, $2, $3 ); }