2 # This is a generic update hook script for GIT repositories.
3 # Written by Martin Mares <mj@ucw.cz> and placed into public domain.
13 my $subject_prefix = "GIT";
17 'mail-to=s' => \$mail_to,
18 'subject-prefix=s' => \$subject_prefix,
19 'max-diff-size=s' => \$max_diff_size,
20 ) and @ARGV == 3 or die <<AMEN ;
21 Usage: $0 [<options>] <refname> <sha1-old> <sha1-new>
24 --mail-to=<address> Send mail to the given address
25 --max-diff-size=<bytes> If the diff is too long, send just a summary
26 --subject-prefix=<px> Prefix subjects with [<px>] (default: GIT)
29 my ($ref, $old, $new) = @ARGV;
30 $ref =~ s{^refs/heads/}{} or exit 0;
31 $old ne $new or exit 0;
33 my $subj = '[' . $subject_prefix . ($ref eq 'master' ? '' : "/$ref") . ']';
34 my $tmpdir = File::Temp->newdir() or die;
35 my $outname = "$tmpdir/0";
36 my $out = IO::File->new($outname, '>') or die;
39 open STDOUT, '>&', $out or die;
41 my @rev_list_options = ('--pretty', '--no-abbrev', '--date=iso');
42 my @diff_options = ('-C');
45 # Is there any branch pointing to $new ?
46 for (`git branch -v --no-abbrev`) {
48 my ($name, $sha) = /^..(\S+)\s+(\S+)/ or die;
49 if ($name ne $ref && $sha eq $new) {
57 print $out "Most recent commits:\n\n";
58 system 'git', 'rev-list', @rev_list_options, '--max-count=20', $new;
67 # Creation of a branch
68 $subj .= ' Created branch';
69 my $copy_of = scan_branches();
70 if (defined $copy_of) {
71 $subj .= " as a copy of $copy_of";
72 print $out "Created branch $ref as a copy of $copy_of.\n";
74 print $out "Created branch $ref.\n\n";
77 } elsif ($new =~ /^0+$/) {
78 # Deletion of a branch
79 $subj .= ' Branch deleted';
80 print $out "Deleted branch $ref.\n\nPrevious tip was $old.\n";
82 my $lca = `git merge-base $old $new`; die if $?;
86 # Scan all commits first and construct subject
87 my @commits = `git rev-list $old..$new --pretty=oneline --no-abbrev --no-merges`; $? and die;
94 # If there are multiple commits, print an overall diffstat first
97 print $out "Overall diffstat:\n\n";
98 system 'git', 'diff', '--stat', $old, $new;
99 print $out "\nCommits:\n\n";
101 my $pos_after_header = output_size();
103 # Show individual commits with diffs and stats
104 system 'git', 'log', @rev_list_options, '--reverse', @diff_options, '--patch', '--stat', "$old..$new";
106 # If the file is too long, truncate it and print just a summary
107 if (defined($max_diff_size) && output_size() > $max_diff_size) {
108 $out->truncate($pos_after_header);
110 print $out "Diff was too long, printing just a summary.\n\n";
111 system 'git', 'log', @rev_list_options, '--reverse', "$old..$new";
113 } elsif ($lca eq $new) {
115 $subj .= ' Branch rewound';
116 print $out "Rewound branch $ref to commit $new.\n\n";
119 # Otherwise it is a rebase
120 $subj .= ' Branch rebased';
121 print $out "Rebased branch $ref to commit $new.\n\n";
122 print $out "Commits from common ancestor:\n\n";
123 system 'git', 'rev-list', @rev_list_options, $new, "^$old";
128 if (defined $mail_to) {
130 open STDIN, '<', $outname;
135 'set charset="utf-8"; set send_charset="us-ascii:iso-8859-2:utf-8"',
139 print STDERR "Subject: $subj\n\n";