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