summaryrefslogtreecommitdiff
path: root/git-notify
blob: 78bb2b60196a95c40f89810ee2c3aa13646da3aa (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl -w

# based on ciabot.pl:
# # Loosely based on cvslog by Russ Allbery <rra@stanford.edu>
# # Copyright 1998  Board of Trustees, Leland Stanford Jr. University
# # Copyright 2001, 2003, 2004, 2005  Petr Baudis <pasky@ucw.cz>

# Copyright 2010 Peter Palfrader

# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 2, as published by the
# Free Software Foundation.

# use in post-receive like that:
# | tmpfile=`mktemp`
# | trap "rm -f '$tmpfile'" EXIT
# |
# | cwd="`pwd`"
# | REPO_NAME="`basename '$cwd'`"
# | while read oldrev newrev refname; do
# |         echo "$oldrev $newrev $refname" >> "$tmpfile"
# | done
# |
# | while read oldrev newrev refname; do
# |         for merged in $(git-rev-list $newrev ^$oldrev | tac); do
# |                 ./hooks/nsa-bot "$merged" "$refname" "$REPO_NAME"
# |         done
# | done < "$tmpfile"
# |
# | # <other postrecv stuff>
# | #. /usr/share/doc/git-core/contrib/hooks/post-receive-email

use strict;
use POSIX qw(strftime);
use vars qw ($project $from_email $dest_email $sendmail);

$project = 'weasel';
$from_email = 'commit@commit.noreply.org';
$dest_email = 'commit@commit.noreply.org';

$sendmail = '/usr/sbin/sendmail';

die ("Usage: $0 <commit> <branch> <repos> [<project> [<from_email> [<dest_email>] ] ]") if (scalar @ARGV < 3);
# The commit stuff
my $commit = $ARGV[0];
my $branch = $ARGV[1];
my $repos = $ARGV[2];
if (scalar @ARGV > 3) {
  $project = $ARGV[3];
}
if (scalar @ARGV > 4) {
  $from_email = $ARGV[4];
}
if (scalar @ARGV > 5) {
  $dest_email = $ARGV[5];
}

open COMMIT, "git-cat-file commit $commit|" or die "git-cat-file commit $commit: $!";
my $state = 0;
my $logmsg = '';
my $line;
my $tree;
my @parent;
my $author;
my $timestamp;
my $committer;
my @files;
while (defined ($line = <COMMIT>)) {
  if ($state == 1) {
    $logmsg .= $line;
    $state++;
    next;
  } elsif ($state > 1) {
    next;
  }

  chomp $line;
  unless ($line) {
    $state = 1;
    next;
  }

  my ($key, $value) = split(/ /, $line, 2);
  if ($key eq 'tree') {
    $tree = $value;
  } elsif ($key eq 'parent') {
    push(@parent, $value);
  } elsif ($key eq 'author') {
    ($author, $timestamp) = $value =~ /^(.*)\s+([0-9]+)\s+.?[0-9]{4}$/;
    $author = $value unless $author;
  } elsif ($key eq 'committer') {
    $committer = $value;
  }
}
close COMMIT;

die ("Did not learn parent or tree.\n") unless (scalar @parent > 0 and defined $tree);

open DIFF, "git-diff-tree -r $parent[0] $tree|" or die "git-diff-tree $parent[0] $tree: $!";
while (defined ($line = <DIFF>)) {
  chomp $line;
  my @f;
  (undef, @f) = split(/\t/, $line, 2);
  push (@files, @f);
}
close DIFF;


$branch =~ s#refs/heads/##;
if (defined $repos) {
	$branch = "$repos/$branch";
}
my $message = "[$branch] ".strftime("%Y-%m-%d %H:%M:%S", gmtime($timestamp))." $author: $logmsg";

open (MAIL, "| $sendmail -t -oi -oem") or die "Cannot execute $sendmail : " . ($?>>8);
#open (MAIL, "| /bin/cat") or die ("Cannot cat!\n");
print MAIL <<EOF;
From: $from_email
To: $dest_email
Subject: Announce $project

EOF
print MAIL $message;
close MAIL;
die "$0: sendmail exit status " . ($? >> 8) . "\n" unless ($? == 0);

# vi: set sw=2: