]> mj.ucw.cz Git - git-tools.git/blob - update2
Update2: Notifications on changes of tags
[git-tools.git] / update2
1 #!/usr/bin/perl
2 # This is a generic update/post-receive 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 use POSIX;
12
13 my $mail_to;
14 my $subject_prefix = "GIT";
15 my $max_diff_size;
16
17 GetOptions(
18         'mail-to=s' => \$mail_to,
19         'subject-prefix=s' => \$subject_prefix,
20         'max-diff-size=s' => \$max_diff_size,
21 ) and (@ARGV == 3 || !@ARGV) or die <<AMEN ;
22 Usage as update hook: $0 [<options>] <refname> <sha1-old> <sha1-new>
23 Usage as post-receive hook: $0 [<options>]
24
25 Options:
26 --mail-to=<address>     Send mail to the given address
27 --max-diff-size=<bytes> If the diff is too long, send just a summary
28 --subject-prefix=<px>   Prefix subjects with [<px>] (default: GIT)
29 AMEN
30
31 my $repo = POSIX::getcwd();
32 $repo =~ s{.*/}{};
33
34 my @rev_list_options = ('--pretty', '--no-abbrev', '--date=iso');
35 my @diff_options = ('-C');
36
37 sub update_ref($$$);
38
39 open ORIG_STDOUT, '>&', \*STDOUT;
40
41 if (@ARGV) {
42         update_ref($ARGV[0], $ARGV[1], $ARGV[2]);
43 } else {
44         while (<STDIN>) {
45                 chomp;
46                 my ($old, $new, $ref) = /^(\S+) (\S+) (.*)/ or die "Error parsing hook input ($_)\n";
47                 update_ref($ref, $old, $new);
48         }
49 }
50
51 sub scan_branches($$) {
52         my ($ref, $new) = @_;
53         # Is there any branch pointing to $new (other than $ref)?
54         for (`git branch -v --no-abbrev`) {
55                 chomp;
56                 my ($name, $sha) = /^..(\S+)\s+(\S+)/ or die;
57                 if ((!defined($ref) || $name ne $ref) && $sha eq $new) {
58                         return $name;
59                 }
60         }
61         return;
62 }
63
64 sub scan_commits($$) {
65         my ($old, $new) = @_;
66         my @commits = ();
67         for (`git rev-list $old..$new --pretty=format:"# %H (%P) %s"`) {
68                 chomp;
69                 /^# / or next;
70                 my ($hash, $parents, $subject) = m{^# (\S+) \(([^)]*)\) (.*)} or die;
71                 push @commits, {
72                         hash => $hash,
73                         parents => [ split /\s+/, $parents ],
74                         subject => $subject,
75                 };
76         }
77         return @commits;
78 }
79
80 sub most_recent($) {
81         my ($new) = @_;
82         print STDOUT "Most recent commits:\n\n";
83         system 'git', 'rev-list', @rev_list_options, '--max-count=20', $new;
84 }
85
86 sub output_size($) {
87         my ($out) = @_;
88         $out->seek(0, 2);
89         return $out->tell;
90 }
91
92 sub update_branch($$$$$)
93 {
94         my ($branch, $old, $new, $out, $headers) = @_;
95
96         my $subj = '[' . $subject_prefix . ($branch eq 'master' ? '' : "/$branch") . ']';
97         if ($old =~ /^0+$/) {
98                 # Creation of a branch
99                 $subj .= ' Created branch';
100                 my $copy_of = scan_branches($branch, $new);
101                 if (defined $copy_of) {
102                         $subj .= " as a copy of $copy_of";
103                         print $out "Created branch $branch as a copy of $copy_of ($new).\n";
104                 } else {
105                         print $out "Created branch $branch ($new).\n\n";
106                         most_recent($new);
107                 }
108         } elsif ($new =~ /^0+$/) {
109                 # Deletion of a branch
110                 $subj .= ' Branch deleted';
111                 print $out "Deleted branch $branch ($old).\n";
112         } else {
113                 my $lca = `git merge-base $old $new`; die if $?;
114                 chomp $lca;
115                 if ($lca eq $old) {
116                         # Fast forward ... scan all objects
117                         my @commits = scan_commits($old, $new);
118                         my @nonmerges = grep { @{$_->{parents}} == 1 } @commits;
119                         @commits or return;
120
121                         # Construct subject
122                         # Try to recognize simple merges and display them as such
123                         my $c0 = $commits[0];
124                         my $n0 = $nonmerges[0];
125                         my $c0p = $c0->{parents};
126                         if (@{$c0p} == 2 &&
127                             ($c0p->[0] eq $old || $c0p->[1] eq $old) &&
128                             $c0->{subject} =~ m{^\s*Merge branch '([^']*)' into (\S+)} &&
129                             (($1 eq $branch) != ($2 eq $branch))) {
130                                 # Pushed a merge of the current branch with another local branch
131                                 $subj .= ' ' . $c0->{subject};
132                         } elsif ($n0) {
133                                 # Otherwise take the subject of the first non-merge commit
134                                 $subj .= ' ' . $n0->{subject};
135                         } else {
136                                 # If there is none, take the first merge
137                                 $subj .= ' ' . $c0->{subject};
138                         }
139
140                         print $out "Push to branch $branch ($old -> $new)\n\n";
141
142                         # If there are multiple commits, mention that
143                         if (@nonmerges > 1) {
144                                 $subj .= ' [' . (scalar @commits) . ' commits]';
145                                 print $out 'Pushed ', (scalar @commits), " commits. Overall diffstat:\n\n";
146                         }
147
148                         # Print an overall diffstat
149                         system 'git', 'diff', '--stat', $old, $new;
150                         print $out "\n";
151                         my $pos_after_header = output_size($out);
152
153                         # Show individual commits with diffs
154                         system 'git', 'log', @rev_list_options, @diff_options, '-p', "$old..$new";
155
156                         # If the file is too long, truncate it and print just a summary
157                         if (defined($max_diff_size) && output_size($out) > $max_diff_size) {
158                                 $out->truncate($pos_after_header);
159                                 output_size($out);
160                                 print $out "Diff was too long, printing just a summary.\n\n";
161                                 system 'git', 'log', @rev_list_options, "$old..$new";
162                         }
163                 } elsif ($lca eq $new) {
164                         # Rewind
165                         $subj .= ' Branch rewound';
166                         print $out "Rewound branch $branch ($old -> $new).\n\n";
167                         most_recent($new);
168                 } else {
169                         # Otherwise it is a rebase
170                         $subj .= ' Branch rebased';
171                         print $out "Rebased branch $branch ($old -> $new).\n\n";
172                         print $out "Commits from common ancestor:\n\n";
173                         system 'git', 'rev-list', @rev_list_options, $new, "^$old";
174                 }
175         }
176
177         $headers->{'Subject'} = $subj;
178         $headers->{'X-Git-Branch'} = $branch;
179         return 1;
180 }
181
182 sub update_tag($$$$$)
183 {
184         my ($tag, $old, $new, $out, $headers) = @_;
185
186         my $subj = '[' . $subject_prefix . ']';
187         if ($new =~ /^0+$/) {
188                 $subj .= " Deleted tag $tag";
189                 print $out "Deleted tag $tag ($old).\n";
190         } else {
191                 my $copy_of = scan_branches(undef, $new);
192                 my $cp = defined($copy_of) ? " to branch $copy_of" : "";
193                 if ($old =~ /^0+/) {
194                         $subj .= " Created tag $tag$cp";
195                         print $out "Created tag $tag$cp ($new).\n\n";
196                 } else {
197                         $subj .= " Changed tag $tag$cp";
198                         print $out "Changed tag $tag$cp ($old -> $new).\n\n";
199                 }
200                 most_recent($new);
201         }
202
203         $headers->{'Subject'} = $subj;
204         $headers->{'X-Git-Tag'} = $tag;
205         return 1;
206 }
207
208 sub update_ref($$$)
209 {
210         my ($ref, $old, $new) = @_;
211         $old ne $new or return;
212         my ($type, $name) = ($ref =~ m{^refs/([^/]*)/(.*)}) or return;
213
214         my $out = File::Temp->new() or die;
215         my $outname = $out->filename;
216         $out->autoflush(1);
217         close STDOUT;
218         open STDOUT, '>&', $out or die;
219
220         my $headers = {
221                 'X-Git-Repo' => $repo,
222                 'X-Git-Old-SHA' => $old,
223                 'X-Git-New-SHA' => $new,
224         };
225
226         my $send;
227         if ($type eq 'heads') { $send = update_branch($name, $old, $new, $out, $headers); }
228         elsif ($type eq 'tags') { $send = update_tag($name, $old, $new, $out, $headers); }
229         $out->close();
230         $send or return;
231
232         if (defined $mail_to) {
233                 close STDIN;
234                 open STDIN, '<', $outname;
235                 my @mutt = (
236                         'mutt',
237                         '-F/dev/null',
238                         '-x',
239                         '-e', 'set charset="utf-8"',
240                         '-e', 'set send_charset="us-ascii:iso-8859-2:utf-8"',
241                         '-s', $headers->{'Subject'},
242                 );
243                 delete $headers->{'Subject'};
244                 push @mutt, map { ('-e', "my_hdr $_: " . $headers->{$_}) } keys %$headers;
245                 system @mutt, $mail_to;
246         } else {
247                 open STDOUT, '>&', \*ORIG_STDOUT;
248                 print map { "$_: " . $headers->{$_} . "\n" } sort keys %$headers;
249                 print "\n";
250                 system 'cat', $outname;
251                 print "\n";
252         }
253 }