gitmail 29.2 KB
Newer Older
1
2
#!/usr/bin/perl -w
#
3
# EMULAB-COPYRIGHT
4
# Copyright (c) 2009-2011 University of Utah and the Flux Group.
5
# All rights reserved.
6
#
7
# To set this script up:
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1) Copy or link it to .git/hooks/post-receive in your repository. Make sure
#    it's world-readable and executable.
# 2) Set configuration options by editng the values of variables directly
#    below
#       OR
#    Set the simple values with git options: run
#       git config --add hooks.gitmail.<optname> value
#       (for example:
#           git config --add hooks.gitmail.alwaysmail ricci@cs.utah.edu
# 3) Test it by running it with the -d and -t options, which will not send
#    mail and will give you a chance to make sure everything looks right
#
# TODO:
#    Users can add notifications for themselves
#

use strict;
25
use IPC::Open2;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use POSIX 'setsid';
use Getopt::Long;
sub get_config($$);
my $CONFIGBASE = "hooks.gitmail";

######################################################################
# Configuration Options
# Options that use get_config can be set using 'git config' - if not
# set, the second parameter is used as the default
######################################################################

#
# If debugging is enabled, prints a lot of messages and doesn't actually send
# mail.
#
my $debug = get_config("debug",undef);

#
# If set, just picks up the most recent commits instead of reading them from
# stdin. Note that this one doesn't look in the git config; that wouldn't
# make any sense.
#
my $testmode = undef;

50
#
51
52
53
# If set, hide trivial merges (those where no diff hunks needed to be modified
# as part of the merge).  Trivial merges that have a non-empty commit message
# are still shown in case the committer decided to document the merge.
54
55
56
#
my $hide_trivial_merges = get_config("hidetrivialmerges",undef);

57
58
59
60
61
#
# Command-line options - have to do this before setting other options, since
# we want to be able to turn on debugging early
#
my %opt;
62
Getopt::Long::Configure("no_ignore_case");
63
if (!GetOptions(\%opt, 'd', 'h', 't', 'T=s', 'o=s@') || @ARGV || $opt{h}) {
64
    print STDERR "Usage: gitmail [-h|-d]\n";
65
66
67
68
69
70
    print STDERR "  -h     this message\n";
    print STDERR "  -d     enable debugging output and don't send mail\n";
    print STDERR "  -t     test mode - operate on last 3 commits to master\n";
    print STDERR "  -T br  like '-t', but use branch 'br' instead of master\n";
    print STDERR "  -o o=v give option o the value v (may be given multiple\n";
    print STDERR "         times)\n";
71
    exit 1;
72
73
}

74
my $testbranch = "master";
75
76
if ($opt{d}) { $debug    = 1; }
if ($opt{t}) { $testmode = 1; }
77
if ($opt{T}) { $testmode = 1; $testbranch = $opt{T} }
78
79
80
81
82
83
84
85

#
# Name of this repository - set it to a string to get it included in the
# mail subject line and the X-Git-Repo header
#
my $reponame = get_config("reponame",undef);

#
86
87
88
89
90
91
92
93
# Data structure mapping branch names and path names to email address. Each
# entry is a triple:
#    Branch name
#    File path
#    Mail address
# If *both* the branch and path match for a commit, mail will be sent to the
# associated address. The branch and paths are interpreted as perl regexps,
# with the special value 'undef' matching anything at all. Any of these may
94
# be array references: for the branch and path fields, if *any* of the
95
96
# elements in the array match, the field is considered to match. For the email
# address field, the mail will be sent to all addresses in the array.
97
98
#
# *NOTE* This are perl regexps, not shell globs! *NOTE*
99
# *NOTE* This can also be read from a file - see below *NOTE*
100
#
101
102
103
104
105
106
107
108
109
my @mailto = (
  # Branch              # Path             # Send mail to
# Examples
# [ undef,              undef,             'cvs-testbed@flux.utah.edu' ],
# [ undef,              'snmpit',         ['ricci@flux.utah.edu',
#                                          'sklower@vangogh.cs.berkeley.edu']],
# [ ['^ricci-',
#    '^test-'],         'assign/',         'ricci+assign@flux.utah.edu'],
# [ 'gitmail',          'tools/git',       'ricci+git@flux.utah.edu']
110
111
);

112
113
114
115
116
117
118
119
120
121
#
# If set, look in a file for the @mailto structure.  This file should be
# executable perl code that returns an array following the same format as the
# @mailto array below. For example, the file could contain:
# ( [ undef, undef, 'me@example.com'] )
#
# *NOTE* If this is set, overrides the @mailto setting above *NOTE*
#
my $mailconfigfile = get_config("mailconfigfile",undef);

122
123
124
125
126
127
128
129
130
131
#
# Default mail address - if none of the more specific regular expressions
# match, send to this address
#
my $defmail = get_config("defmail",undef);

#
# If set, *always* send mail to this address (even if one or more regexps
# match). ($defmail will still be used if no regexps match)
#
132
133
134
135
136
137
138
my @alwaysmail = get_config("alwaysmail",undef);

#
# If set, set the 'Reply-To' header in the mail, so that discussion can
# take place on, for example, a particular development mailing list
#
my $replyto = get_config("replyto",undef);
139
140
141
142
143
144
145
146
147
148
149
150
151
152

#
# If set to true, detach and run in background - the push doesn't return until
# the hook finishes, so doing this means the pusher doesn't have to wait for
# us to finish
# Note: Not well tested!
#
my $detach = get_config("detach",undef);

#
# If set to true, send a separate mail message for every single commit. If
# false, pushes of multiple commits along a branch get included in the same
# mail.
#
153
154
155
156
157
158
159
my $separate_mail = get_config("separatemail",undef);

#
# If set, these arguments will be passed *in addition* to the regular arguments
# to 'git show' to produce descriptions of commits
#
my $showcommit_extra = get_config("showcommitextra",undef);
160

Ryan Jackson's avatar
Ryan Jackson committed
161
162
163
164
#
# If set, check all commit objects to see if they exist in the repository
# at the specified path.  If a commit object exists in this repository,
# do not include its log in the email message.
165
166
167
# Note: This should point at the .git directory - for a 'bare' repository,
# this is just the path the repository. For a 'reglar' repository, this is the
# .git/ directory *inside* the repositry.
Ryan Jackson's avatar
Ryan Jackson committed
168
169
170
#
my $exclude_repo = get_config("excluderepo",undef);

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
######################################################################
# Constants
######################################################################

#
# Programs we use
#
my $GIT = "git";
my $SENDMAIL = get_config("sendmail","sendmail");

#
# Magic 'hash' that indicates an empty or non-existant rev
#
my $EMPTYREV = "0"x40;

#
# Types of changes
#
my $CT_CREATE = "create";
my $CT_UPDATE = "update";
my $CT_DELETE = "delete";
192
193
my $CT_REWIND = "rewind";
my $CT_REBASE = "rebase";
194
195
196
197
198
199
200
201
202

#
# Tired of typing this and getting it wrong
#
my $STDERRNULL = " 2> /dev/null";

######################################################################
# Function Prototypes
######################################################################
203
204
sub change_type($$$);
sub ref_type($);
205
206
sub rev_type($);
sub revparse($);
207
sub changed_files(@);
208
sub get_mail_addresses($@);
209
sub get_merge_base($$);
210
211
sub uniq(@);
sub flatten_arrayref($);
Ryan Jackson's avatar
Ryan Jackson committed
212
sub commit_mail($\@$@);
213
214
215
216
sub get_commits($$$);
sub send_mail($$@);
sub short_refname($);
sub debug(@);
217
218
sub object_exists($$);
sub filter_out_objects_in_repo($@);
219
220
221
222
223
224
225

######################################################################
# Main Body
######################################################################

debug("starting");

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#
# Read from the mail config file, if requested. We do this before detaching
# so that we can report an error if one occurs.
#
if ($mailconfigfile) {
    #
    # We open the file, read the contents, then eval them. If we didn't get any
    # errors, the result becomes the new contents of the @mailto array
    #
    if (!open(MCF,"<$mailconfigfile")) {
        warn "gitmail: Unable to open config file $mailconfigfile - $!\n";
    } else {
        my @mailcode = <MCF>;
        close MCF;

        #
        # Have to turn array back into a single string before we can call
        # eval on it. Put the result in a temp. variable so that we don't
        # overwrite @mailto if there is an error
        #
        my @mailconfig = eval join("\n",@mailcode);

        #
        # If there were any errors in the eval, they will be found in the magic
        # variable $@ - however, they will also have been printed to stderr, so
        # don't print again
        #
        if ($@) {
254
            warn "gitmail: Error in $mailconfigfile: $@\n";
255
256
257
258
259
260
261
262
263
        } else {
            @mailto = @mailconfig;
        }
    }
}

#
# Get the actual references
#
264
265
my @reflines;
if ($testmode) {
266
267
268
269
270
271
272
    my $fullref = `git rev-parse --symbolic-full-name $testbranch`;
    if (!$fullref) {
	    exit(1);
    }
    my $newrev = `git rev-parse $fullref $STDERRNULL`;
    chomp $newrev;

273
274
275
    #
    # Provide a simple way to grab some commits - the three most recent ones
    #
276
    @reflines = ("$newrev~2 $newrev $fullref");
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
} else {
    #
    # Get all of the references that are being pushed from stdin - we do this in
    # one slurp so that we can detach below
    #
    @reflines = <STDIN>;
    debug("finished reading stdin");
}

#
# Detach?
#
if ($detach && !$debug) {
    # Stolen from perlipc manpage
    chdir '/'               or die "Can't chdir to /: $!";
    open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
    open STDOUT, '>/dev/null'
                            or die "Can't write to /dev/null: $!";
    defined(my $pid = fork) or die "Can't fork: $!";
    exit if $pid;
    setsid                  or die "Can't start a new session: $!";
    open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
}

#
# Loop over all of the references we got on stdin
#
foreach my $refline (@reflines) {
305
306
307
    my @commits;
    my @changed_files;

308
309
310
311
312
313
314
315
316
    chomp $refline;
    debug("Read line '$refline'");

    #
    # Each line we get on stdin gives us an old revision, a new revision, and
    # a reference (such as the branch name). It's our job to figure out what
    # happened in the middle
    #
    my ($oldrev, $newrev, $refname) = split(/\s+/, $refline);
317
    my $ref_type = ref_type($refname);
318
319
320
321
322
323
324
325
326
327
328
329

    #
    # Use rev-parse so that fancy symbolic names, etc. can be used
    # Note: revparse can die if the name given is bogus
    #
    $oldrev = revparse($oldrev);
    $newrev = revparse($newrev);

    #
    # Figure out what type of change occured (update, creation, deletion, etc.)
    # and what type of objects (commit, tree, etc.) we got
    #
330
    my $ct = change_type($oldrev,$newrev,$ref_type);
331
332
333
334
335
336
    my $old_type = rev_type($oldrev);
    my $new_type = rev_type($newrev);

    debug("Change type: $ct ($old_type,$new_type)");

    #
337
    # For now, only handle commit objects.  Tag objects require extra work.
338
    #
339
    if ($new_type && $new_type ne "commit") {
340
341
342
343
344
        debug("Skipping non-commit");
        next;
    }

    #
345
346
    # Figure out which commits we're interested in based on reference type
    # and change type.
347
    #
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    if ($ref_type eq 'tag') {
	if ($ct eq $CT_DELETE) {
	    # We want to know where the tag used to point before deletion
	    push @commits, $oldrev;
	} else {
	    # Tags only have delete, create, and update.  Rewind and rebase
	    # don't make sense in tag context.
	    #
	    # We only care about the new value of the tag here.
	    push @commits, $newrev;
	}
    } elsif ($ref_type eq 'branch') {
	if ($ct eq $CT_DELETE) {
	    # We want to know where the branch used to point before deletion
	    push @commits, $oldrev;
	} elsif ($ct eq $CT_REWIND) {
	    # There's no new history to show, but we still want to see where
	    # the branch now points.
	    push @commits, $newrev;
	} else {
	    @commits = get_commits($oldrev,$newrev,$refname);
	    # We only want to see *new* commits, which means that commits already
	    # in the main repository need to be excluded too.
	    if (defined $exclude_repo) {
		@commits = filter_out_objects_in_repo($exclude_repo, @commits);
	    }
	}
    }

    next unless (@commits);
Ryan Jackson's avatar
Ryan Jackson committed
378
379
    debug("commits are: ", join(" ",@commits));

380
    @changed_files = changed_files(@commits);
381
382
383
384
385
386
387
388
389
390
391
392
393
    debug("Changed files: ", join(",",@changed_files));

    #
    # Based on the list of files, figure out who to mail
    #
    my @mailaddrs = get_mail_addresses($refname,@changed_files);
    debug("Mailing to: ", join(",",@mailaddrs));

    #
    # Send off the mail!
    #
    if (@mailaddrs) {
        print "Sending email notification to ", join(",",@mailaddrs), "\n";
Ryan Jackson's avatar
Ryan Jackson committed
394
        commit_mail($ct,@commits,$refname,@mailaddrs);
395
396
397
398
399
400
401
402
403
404
405
406
407
    }
}

debug("finishing");

######################################################################
# Functions
######################################################################

#
# Does this change represent the creation, deletion, or update of an object?
# Takes old and new revs
#
408
409
sub change_type($$$) {
    my ($oldrev, $newrev, $ref_type) = @_;
410

411
412
413
414
415
416
417
418
    #
    # We can detect creates and deletes by looking for a special 'null'
    # revision
    #
    if ($oldrev eq $EMPTYREV) {
        return $CT_CREATE;
    } elsif ($newrev eq $EMPTYREV) {
        return $CT_DELETE;
419
420
    } elsif ($ref_type eq 'tag') {
	    return $CT_UPDATE;
421
    } else {
422
	my $merge_base = get_merge_base($oldrev,$newrev);
423
	my $oldrev = revparse($oldrev);
424
	my $newrev = revparse($newrev);
425
	if ($merge_base eq $oldrev) {
426
427
428
	    return $CT_UPDATE;
	} elsif ($merge_base eq $newrev) {
	    return $CT_REWIND;
429
	} else {
430
	    return $CT_REBASE;
431
	}
432
433
434
435
436
437
438
439
440
441
442
443
444
    }
}

#
# Find out what type an object has
#
sub rev_type($) {
    my ($rev) = @_;
    my $rev_type = `git cat-file -t '$rev' $STDERRNULL`;
    chomp $rev_type;
    return $rev_type;
}

445
446
447
448
449
450
451
452
453
454
455
456
457
458
#
# Find out what type of reference this is
#
sub ref_type($) {
    my ($ref) = @_;
    my $type;
    if ($ref =~ m#^refs/heads/#) {
	    $type = 'branch';
    } elsif ($ref =~ m#^refs/tags/#) {
	    $type = 'tag';
    }
    return $type;
}

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#
# Parse (possibly) symbolic revision name into hash
# Note: Dies if the revision name is bogus!
#
sub revparse($) {
    my ($rev) = @_;
    open(RP,"$GIT rev-parse $rev $STDERRNULL |");
    my $parsedrev = <RP>;
    my $okay = close(RP);
    if (!$okay) {
        die "gitmail: $rev is not a valid revision\n";
    }
    chomp $parsedrev;
    return $parsedrev;
}

#
476
477
# Given a list of commit object hashes, return the list of files changed by
# all commits.
478
#
479
480
sub changed_files(@) {
    my %files;
481

482
483
    debug("running '$GIT diff-tree --stdin -r --name-only --no-commit-id' on '@_'");
    my $pid = open2(\*OUT, \*IN, "$GIT diff-tree --stdin -r --name-only --no-commit-id");
484

485
486
487
488
489
490
491
492
    print IN "$_\n" for (@_);
    close(IN);

    while (<OUT>) {
	    chomp;
	    $files{$_} = 1;
    }
    close(OUT);
Ryan Jackson's avatar
Ryan Jackson committed
493

494
495
496
497
498
499
500
    waitpid($pid, 0);
    my $rc = $? >> 8;
    if ($rc) {
	    die "'git diff-tree' exited with return code $rc\n";
    }

    return keys(%files);
Ryan Jackson's avatar
Ryan Jackson committed
501
502
}

503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#
# Given a refname and a list of filenames, return the set of email addresses
# the report should be sent to
#
sub get_mail_addresses($@) {
    my ($refname, @changedfiles) = @_;
    my @addrs;
    my $matched = 0;

    #
    # Note: we use flatten_arrayref so that either individual strings or array
    # references containing lists of addresses can be used
    #

    #
    # If there's an address we're always supposed to send to, include that now
    #
520
521
522
    if (@alwaysmail) {
        push @addrs, @alwaysmail;
        debug("Used alwaysmail address(es) " . join(",",@alwaysmail));
523
524
525
    }

    #
526
527
528
    # Find out if this is a branch, and of so, what it's name is. If it's not,
    # set the branch name to be empty, so that only empty regexps will match
    # it
529
    #
530
    my $branchname = "";
531
    if ($refname =~ /^refs\/heads\/(.*)/) {
532
       $branchname = $1;
533
534
535
    }

    #
536
    # Loop through each entry, making sure both branch and path match
537
    #
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
    ENTRY: foreach my $entry (@mailto) {
        my ($branches, $paths, $addresses) = @$entry;
        my @branches = flatten_arrayref($branches);
        my @paths = flatten_arrayref($paths);
        my @addresses = flatten_arrayref($addresses);

        #
        # If the branch doesn't match, go on to the next entry
        #
        my $branch_matched = 0;
        BRANCH: foreach my $branchRE (@branches) {
            if (!defined($branchRE)) {
                debug("Empty branch matched");
                $branch_matched = 1;
                last BRANCH;
            } elsif ($branchname =~ $branchRE) {
                debug("Matched branch regexp /$branchRE/");
                $branch_matched = 1;
                last BRANCH;
557
558
            }
        }
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

        if (!$branch_matched) {
            next ENTRY;
        }

        #
        # If the path doesn't match, go on to the next entry
        #
        my $paths_matched = 0;
        PATH: foreach my $regexp (@paths) {
            if (!defined($regexp)) {
                debug("  Empty path matched");
                $paths_matched = 1;
                last PATH;
            }
            # Have to check against every file in the changeset
            foreach my $file (@changedfiles) {
                if ($file =~ $regexp) {
                    debug("  Matched path regexp /$regexp/");
                    $paths_matched = 1;
                    last PATH;
                }
            }
        }

        if (!$paths_matched) {
            debug("  Path match failed");
            next ENTRY;
        }

        #
        # Great, made it through - we add all addresses, we'll weed out
        # duplicates later
        #
        debug("  Adding adddresses ", join(",",@addresses));
        $matched = 1;
        push @addrs, @addresses;
596
597
598
599
600
601
602
603
604
    }

    #
    # Fall back to default if no matches (note that an earlier match with an
    # empty list of addresses will cause this case to not be triggered - this
    # is intentional)
    #
    if (!$matched && defined($defmail)) {
        @addrs = flatten_arrayref($defmail);
605
        debug("Used default address $defmail");
606
    }
607

608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
    #
    # Pull out unique values to return
    #
    return uniq(@addrs);
}

#
# Return only the unique elements of the supplied list. Input does not have
# to be sorted, sort order of output is undefined.
#
sub uniq(@) {
    my %uniq;
    map { $uniq{$_} = 1 } @_;
    return keys %uniq;
}

#
# If the parameter is a scalar, just return a one-element array containing the
# scalar. If it's a reference to an array, return the array referenced.
#
sub flatten_arrayref($) {
    my ($ref) = @_;
    if (ref($ref) eq "ARRAY") {
        return @$ref;
    } else {
        return ($ref);
    }
}

#
# Send mail about a regular update commit
#
Ryan Jackson's avatar
Ryan Jackson committed
640
641
sub commit_mail($\@$@) {
    my ($ct,$commits,$refname,@mailaddrs) = @_;
642
643
644

    #
    # Construct the subject line. For now, we just say what repo (if defined)
645
    # and what branch/tag it happened on
646
647
    #
    my $subject = "git commit: ";
648
    my $ref_type;
649
    my $short_rev;
650
651
652
    if (defined($reponame)) {
        $subject .= "[$reponame] ";
    }
653

654
    $ref_type = ref_type($refname);
655
656

    $subject .= $ref_type . ' ' . short_refname($refname);
657

658
    my $what_happened;
659
    if ($ct eq $CT_UPDATE) {
660
661
662
663
664
        $what_happened .= 'updated';
    } elsif ($ct eq $CT_REWIND) {
        $what_happened .= 'rewound';
    } elsif ($ct eq $CT_REBASE) {
        $what_happened .= 'rebased';
665
    } elsif ($ct eq $CT_CREATE) {
666
        $what_happened .= 'created';
667
    } elsif ($ct eq $CT_DELETE) {
668
        $what_happened .= 'deleted';
669
670
    }

671
    $subject .= ' ' . $what_happened;
672
    my $actionstring = ucfirst($ref_type) . ' ' . short_refname($refname) .
673
                       " has been $what_happened";
674

675
676
677
678
    $short_rev = `git rev-parse --short $refname $STDERRNULL`;
    chomp $short_rev;
    $subject .= " ($short_rev)" if ($short_rev);

679
680
681
682
683
684
    if ($ct eq $CT_REBASE) {
        $actionstring .= ".  The following commits are new or have been modified:";
    } elsif ($ct eq $CT_REWIND) {
        $actionstring .= "  to point to the following commit:";
    } elsif ($ct eq $CT_DELETE) {
	$actionstring .= ".  It previously pointed to the following commit:";
685
686
    }

687
688
    $actionstring .= "\n\n";

689
    my @fullbody;
Ryan Jackson's avatar
Ryan Jackson committed
690
    foreach my $rev (@$commits) {
691
692
693
694
        #
        # Just use regular git show command, with purty +/- summary at the
        # bottom (formatted to be narrow enough for email)
        #
695
696
697
698
699
700
701
        my $showcommand = "$GIT show --numstat --shortstat ";
        if (defined($showcommit_extra)) {
            $showcommand .= "$showcommit_extra ";
        }
        $showcommand .= "'$rev'";
        debug("running '$showcommand'");
        my @body = `$showcommand`;
702
703
704
705
706
        
        if ($hide_trivial_merges) {
	        my $is_merge = 0;
	        my $body_lines = 0;
	        my $subject;
707
708
709
710
711
712
	        my @diff_lines;

		# This is a bit of a hack.  It assumes that the output
		# of git-show will not change.  We could use --pretty
		# to produce exactly the format we want, but since we've
		# already called git-show we'll just use that.
713
714
715
716
717
718
719
720
721
722
	        for (@body) {
		        $is_merge = 1 if (/^Merge:/);
			if (/^ /) {
				if (not defined $subject) {
					$subject = $_;
				} else {
					$body_lines++;
				}
			}
		}
723
724
725
726

		if ($is_merge) {
			@diff_lines = `$GIT diff-tree --no-commit-id --cc $rev`;
		}
727
		
728
729
730
731
		if ($is_merge && !@diff_lines && !$body_lines) {
			# We have a trivial merge, i.e. no hunks were modified as
			# part of the merge.  We still want to show the commit if
			# the commit message is non-standard.
732
733
734
			next;
		}
	}
735
736
737
738
739
740
741
742
743
744
745
746

        if ($separate_mail) {
            # Send this message by itself
            send_mail($subject,$actionstring . join("",@body),@mailaddrs);
        } else {
            push @fullbody,\@body;
        }
    }

    #
    # Send all the changes together in one message
    #
747
    if (!$separate_mail && @fullbody) {
748
        send_mail($subject,
749
750
            $actionstring . join("\n" . "-"x72 . "\n\n",
                                 map { join "",@$_} @fullbody),
751
752
753
754
755
756
757
758
759
760
761
            @mailaddrs);
    }
}

#
# Given two revisions, get a list of the commits that occured between them
# TODO: Consider leaving out already-reachable commits like the contrib script
#       does, or at least making it an option
#
sub get_commits($$$) {
    my ($oldrev,$newrev,$refname) = @_;
762
    my $ct = change_type($oldrev,$newrev, ref_type($refname));
763
764
765

    #
    # If this is an update, we can just ask git for the revisions between the
766
    # two revisions we were given.
767
    #
768
769
770
771
772
773
774
775
    if ($ct eq $CT_UPDATE) {
	my $revstring = "$oldrev..$newrev";

	debug("running '$GIT rev-list --reverse --date-order '$revstring'");
	my @revs = `$GIT rev-list --reverse --date-order '$revstring'`;
	chomp @revs;
        return @revs;
    } elsif ($ct eq $CT_REBASE) {
776
777
        debug("running '$GIT cherry '$oldrev' '$newrev'");
	# Only return revs prefixed with a '+' since commits prefixed with a
778
779
780
781
782
783
784
785
786
	# '-' are already in the repository with a different commit hash.
	#
	# The '-' commits are the same as their non-rebased counterparts, except
	# their ancestry is different.  For the email messages, we don't care
	# about these since we should have seen the original commits already.
	#
	# The '+' commits are either new or are rebased commits whose *content*
	# has changed.  We definitely want to see these.  Note that this only
	# applies to the content of the commit, not the commit message.
787
	my @revs;
788
	my @all_revs;
789
	for (`$GIT cherry '$oldrev' '$newrev'`) {
790
		debug($_);
791
		chomp;
792
793
794
		@_ = split /\s+/, $_;
		unshift @revs, $_[1] if ($_[0] eq '+');
		unshift @all_revs, $_[1];
795
	}
796

797
798
799
800
801
	# If cherry finds that all of the commits are already present,
	# report 'em all anyway.  We still need to know that the rebase
	# happened, and reporting just the head doesn't make any sense.
	@revs = @all_revs if (!@revs);
        return @revs;
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
    } elsif ($ct eq $CT_CREATE) {
        #
        # If it's a create, we have to be a bit more fancy: we look for all
        # commits reachable from the new branch, but *not* reachable from any
        # other branch (otherwise, we get all of the history back to the dawn
        # of time). I stole this magic from the contrib post-receive-email hook
        # from the git distro.
        #

        #
        # Get the list of all other branch heads
        #
        my @other_branches = `$GIT for-each-ref --format='%(refname)' refs/heads/ | grep -F -v $refname`;
        chomp @other_branches;

        my $other_branches = join(" ",@other_branches);
        debug("other branches are: '$other_branches'");

        #
        # Get a list of commits that are reachable from this branch, and no
        # others.
        #
        debug("running '$GIT rev-parse --not $other_branches | $GIT rev-list --pretty --stdin $newrev'");
        my @commits = `$GIT rev-parse --not $other_branches | $GIT rev-list --reverse --date-order --stdin $newrev`;

827
828
829
830
831
832
        # We always want to be notified when a branch is created, so if there are no commits reachable
        # from only this branch just report on the head of the branch.
        push @commits, $newrev if (!@commits);

        debug("commits are @commits");

833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
        chomp @commits;
        return @commits;
    }

    #
    # The following magic is from the contrib post-receive hook, and it tries
    # to avoid sending the same commit in mail twice, by excluding commits that
    # are reachable from other branches
    #
    #my @other_branches = `$GIT for-each-ref --format='%(refname)' refs/heads/ | grep -F -v $refname`;
    #chomp @other_branches;
    #my $other_branches = join(" ",@other_branches);
    #if ($debug) {
    #    print "other_branches magic: '$other_branches'\n";
    #}
    #my $rev_parse = `$GIT rev-parse --not $other_branches`;
    #if ($debug) {
    #    print "rev_parse magic: \n" . $rev_parse;
    #}
    #my @commits = `$GIT rev-parse --not $other_branches | $GIT rev-list --pretty --stdin $revspec`;

}

#
# Send out some mail (or not, if in debug mode)
#
sub send_mail($$@) {
    my ($subject, $body, @to) = @_;

    if ($debug) {
           open(MAIL,">&STDERR");
           print MAIL "\n\n";
    } else {
           # sendmail args:
           # -oem causes errors in mail to be "mailed back"
           # -odb deliver mail in background
           # -t causes sendmail to look in the message for 'To:' 'Cc:' and
           #    'Bcc:' lines
           open(MAIL, "| $SENDMAIL -odb -oem -t");
    }

    print MAIL "To: " . join(", ",@to) . "\n";
    print MAIL "Subject: $subject\n";
876
877
878
879
880
    
    # If requested, set the Reply-To header
    if (defined($replyto)) {
        print MAIL "Reply-To: $replyto\n"
    }
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902

    #
    # Add an X-Git-Repo header to help people procmail
    # TODO: Add branch too!
    #
    if (defined($reponame)) {
        print MAIL "X-Git-Repo: $reponame\n";
    }

    #
    # Marks end of headers
    #
    print MAIL "\n";

    print MAIL $body;

}

#
# Given a full refname, pull off the last part for pretty printing
#
sub short_refname($) {
903
904
905
906
    my ($ref) = @_;
    my $refname = `git rev-parse --abbrev-ref $ref $STDERRNULL`;
    chomp $refname;

907
    # Fall back to full name if rev-parse fails for some reason
908
    $refname = $ref if (!$refname);
909
910
911
912
913
914
915
916
917
    debug("got short refname \"$refname\"");


    # If the ref didn't get shortened, it may be because it was deleted.  Just
    # chop off 'refs/heads' or 'refs/tags' and return the rest.
    if ($refname =~ m#^refs/(?:heads|tags)/(.*)#) {
	    $refname = $1;
    }

918
    return $refname;
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
}

#
# Print only if in debug mode
#
sub debug(@) {
    if ($debug) {
        print STDERR "*** gitmail: ", @_, "\n";
    }
}

#
# Return either the config value associated with the repo or the second
# argument, which supplies the default.
#
sub get_config($$) {
    my ($var,$default) = @_;
Ryan Jackson's avatar
Ryan Jackson committed
936

937
938
939
940
941
942
943
944
    my $multivalue = 0;

    # This lets us check whether the caller wanted multiple values or not -
    # if we were called in list context, this will be true
    if (wantarray()) {
        $multivalue = 1;
    }

945
946
947
948
    #
    # Allow the user to override on command line
    #
    if ($opt{o}) {
949
        my @values;
950
951
952
953
        foreach my $pair (@{$opt{o}}) {
            my ($name,$value) = split /=/, $pair;
            if ($name eq $var) {
                debug("Using config value $value for $name from command line");
954
955
956
957
958
                if ($multivalue) {
                    push @values, $value;
                } else {
                    return $value;
                }
959
960
            }
        }
961
962
963
        if ($multivalue && (@values > 0)) {
            return @values;
        }
964
965
    }

966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
    my $getcommand = "--get";
    if ($multivalue) {
        $getcommand = "--get-all";
    }

    my @value = `git config $getcommand $CONFIGBASE.$var`;
    chomp @value;

    if (@value) {
        if ($multivalue) {
            foreach my $value (@value) {
                debug("Got $value from git config for $var");
            }
            return @value;
        } else {
            debug("Got $value[0] from git config for $var");
            return $value[0];
        }
984
985
    } else {
        debug("Using " , defined($default)?$default : "(undef)" , " for $var");
986
987
988
989
990
        if ($multivalue) {
            return ($default);
        } else {
            return $default;
        }
991
992
993
    }
}

994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
#
# Returns the merge base (i.e., common ancestor) of
# the two supplied revisions.
#
sub get_merge_base($$)
{
	my ($rev_a, $rev_b) = @_;
	my $mb = `git merge-base '$rev_a' '$rev_b'`;
	chomp $mb;

	return $mb
}

1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
#
# Returns 1 if the given object (identified by hash) exists in the given
# repository (identified by path); 0 if it does not .
#
sub object_exists($$) {
    my ($obj, $repo) = @_;

    #
    # 'cat-file -e' is like 'test -e', and don't be fooled by the name, it
    # works on non-files too
    #
    debug("running $GIT --git-dir='$repo' cat-file -e $obj");
    my $rv = system "$GIT --git-dir='$repo' cat-file -e $obj";
    if ($rv) {
        return 0;
    } else {
        return 1;
    }
}

#
# Return a list of all objects from the input list that do *not* exist in
# the given repo
#
sub filter_out_objects_in_repo($@) {
    my $repo = shift @_;
    return grep { !object_exists($_,$repo)} @_;
}