]> mj.ucw.cz Git - git-tools.git/blob - update2
Update2: Avoid File::Temp->newdir(), as it requires too new Perl
[git-tools.git] / update2
1 #!/usr/bin/perl
2 # This is a generic update hook script for GIT repositories.
3 # Written by Martin Mares <mj@ucw.cz> and placed into public domain.
4
5 use strict;
6 use warnings;
7
8 use Getopt::Long;
9 use IO::File;
10 use File::Temp;
11
12 my $mail_to;
13 my $subject_prefix = "GIT";
14 my $max_diff_size;
15
16 GetOptions(
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>
22
23 Options:
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)
27 AMEN
28
29 my ($ref, $old, $new) = @ARGV;
30 $ref =~ s{^refs/heads/}{} or exit 0;
31 $old ne $new or exit 0;
32
33 my $subj = '[' . $subject_prefix . ($ref eq 'master' ? '' : "/$ref") . ']';
34 my $out = File::Temp->new() or die;
35 my $outname = $out->filename;
36 $out->autoflush(1);
37 close STDOUT;
38 open STDOUT, '>&', $out or die;
39
40 my @rev_list_options = ('--pretty', '--no-abbrev', '--date=iso');
41 my @diff_options = ('-C');
42
43 sub scan_branches() {
44         # Is there any branch pointing to $new ?
45         for (`git branch -v --no-abbrev`) {
46                 chomp;
47                 my ($name, $sha) = /^..(\S+)\s+(\S+)/ or die;
48                 if ($name ne $ref && $sha eq $new) {
49                         return $name;
50                 }
51         }
52         return;
53 }
54
55 sub most_recent() {
56         print $out "Most recent commits:\n\n";
57         system 'git', 'rev-list', @rev_list_options, '--max-count=20', $new;
58 }
59
60 sub output_size() {
61         $out->seek(0, 2);
62         return $out->tell;
63 }
64
65 if ($old =~ /^0+$/) {
66         # Creation of a branch
67         $subj .= ' Created branch';
68         my $copy_of = scan_branches();
69         if (defined $copy_of) {
70                 $subj .= " as a copy of $copy_of";
71                 print $out "Created branch $ref as a copy of $copy_of.\n";
72         } else {
73                 print $out "Created branch $ref.\n\n";
74                 most_recent();
75         }
76 } elsif ($new =~ /^0+$/) {
77         # Deletion of a branch
78         $subj .= ' Branch deleted';
79         print $out "Deleted branch $ref.\n\nPrevious tip was $old.\n";
80 } else {
81         my $lca = `git merge-base $old $new`; die if $?;
82         chomp $lca;
83         if ($lca eq $old) {
84                 # Fast forward
85                 # Scan all commits first and construct subject
86                 my @commits = `git rev-list $old..$new --pretty=oneline --no-abbrev --no-merges`; $? and die;
87                 @commits or exit;
88                 my $c = $commits[0];
89                 chomp $c;
90                 $c =~ s{^\S+\s+}{};
91                 $subj .= " $c";
92
93                 # If there are multiple commits, print an overall diffstat first
94                 if (@commits > 1) {
95                         $subj .= ' [...]';
96                         print $out "Overall diffstat:\n\n";
97                         system 'git', 'diff', '--stat', $old, $new;
98                         print $out "\nCommits:\n\n";
99                 }
100                 my $pos_after_header = output_size();
101
102                 # Show individual commits with diffs and stats
103                 system 'git', 'log', @rev_list_options, '--reverse', @diff_options, '--patch', '--stat', "$old..$new";
104
105                 # If the file is too long, truncate it and print just a summary
106                 if (defined($max_diff_size) && output_size() > $max_diff_size) {
107                         $out->truncate($pos_after_header);
108                         output_size();
109                         print $out "Diff was too long, printing just a summary.\n\n";
110                         system 'git', 'log', @rev_list_options, '--reverse', "$old..$new";
111                 }
112         } elsif ($lca eq $new) {
113                 # Rewind
114                 $subj .= ' Branch rewound';
115                 print $out "Rewound branch $ref to commit $new.\n\n";
116                 most_recent();
117         } else {
118                 # Otherwise it is a rebase
119                 $subj .= ' Branch rebased';
120                 print $out "Rebased branch $ref to commit $new.\n\n";
121                 print $out "Commits from common ancestor:\n\n";
122                 system 'git', 'rev-list', @rev_list_options, $new, "^$old";
123         }
124 }
125
126 $out->close();
127 if (defined $mail_to) {
128         close STDIN;
129         open STDIN, '<', $outname;
130         system 'mutt',
131                 '-F/dev/null',
132                 '-x',
133                 '-e',
134                 'set charset="utf-8"; set send_charset="us-ascii:iso-8859-2:utf-8"',
135                 '-s', $subj,
136                 $mail_to;
137 } else {
138         print STDERR "Subject: $subj\n\n";
139         `cat >&2 $outname`;
140 }