]> mj.ucw.cz Git - git-tools.git/blob - update2
Update2: Remember original stdout
[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 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 or die <<AMEN ;
22 Usage: $0 [<options>] <refname> <sha1-old> <sha1-new>
23
24 Options:
25 --mail-to=<address>     Send mail to the given address
26 --max-diff-size=<bytes> If the diff is too long, send just a summary
27 --subject-prefix=<px>   Prefix subjects with [<px>] (default: GIT)
28 AMEN
29
30 my ($ref, $old, $new) = @ARGV;
31 $ref =~ s{^refs/heads/}{} or exit 0;
32 $old ne $new or exit 0;
33
34 my $repo = POSIX::getcwd();
35 $repo =~ s{.*/}{};
36
37 my $subj = '[' . $subject_prefix . ($ref eq 'master' ? '' : "/$ref") . ']';
38 my $out = File::Temp->new() or die;
39 my $outname = $out->filename;
40 $out->autoflush(1);
41 open ORIG_STDOUT, '>&', \*STDOUT;
42 close STDOUT;
43 open STDOUT, '>&', $out or die;
44
45 my @rev_list_options = ('--pretty', '--no-abbrev', '--date=iso');
46 my @diff_options = ('-C');
47
48 sub scan_branches() {
49         # Is there any branch pointing to $new ?
50         for (`git branch -v --no-abbrev`) {
51                 chomp;
52                 my ($name, $sha) = /^..(\S+)\s+(\S+)/ or die;
53                 if ($name ne $ref && $sha eq $new) {
54                         return $name;
55                 }
56         }
57         return;
58 }
59
60 sub most_recent() {
61         print $out "Most recent commits:\n\n";
62         system 'git', 'rev-list', @rev_list_options, '--max-count=20', $new;
63 }
64
65 sub output_size() {
66         $out->seek(0, 2);
67         return $out->tell;
68 }
69
70 if ($old =~ /^0+$/) {
71         # Creation of a branch
72         $subj .= ' Created branch';
73         my $copy_of = scan_branches();
74         if (defined $copy_of) {
75                 $subj .= " as a copy of $copy_of";
76                 print $out "Created branch $ref as a copy of $copy_of ($new).\n";
77         } else {
78                 print $out "Created branch $ref ($new).\n\n";
79                 most_recent();
80         }
81 } elsif ($new =~ /^0+$/) {
82         # Deletion of a branch
83         $subj .= ' Branch deleted';
84         print $out "Deleted branch $ref ($old).\n";
85 } else {
86         my $lca = `git merge-base $old $new`; die if $?;
87         chomp $lca;
88         if ($lca eq $old) {
89                 # Fast forward
90                 # Scan all commits first and construct subject
91                 my @commits = `git rev-list $old..$new --pretty=oneline --no-abbrev --no-merges`; $? and die;
92                 @commits or exit;
93                 my $c = $commits[0];
94                 chomp $c;
95                 $c =~ s{^\S+\s+}{};
96                 $subj .= " $c";
97                 print $out "Push to branch $ref ($old -> $new)\n\n";
98
99                 # If there are multiple commits, print an overall diffstat first
100                 if (@commits > 1) {
101                         $subj .= ' [...]';
102                         print $out "Overall diffstat:\n\n";
103                         system 'git', 'diff', '--stat', $old, $new;
104                         print $out "\nCommits:\n\n";
105                 }
106                 my $pos_after_header = output_size();
107
108                 # Show individual commits with diffs and stats
109                 system 'git', 'log', @rev_list_options, @diff_options, '-p', '--stat', "$old..$new";
110
111                 # If the file is too long, truncate it and print just a summary
112                 if (defined($max_diff_size) && output_size() > $max_diff_size) {
113                         $out->truncate($pos_after_header);
114                         output_size();
115                         print $out "Diff was too long, printing just a summary.\n\n";
116                         system 'git', 'log', @rev_list_options, "$old..$new";
117                 }
118         } elsif ($lca eq $new) {
119                 # Rewind
120                 $subj .= ' Branch rewound';
121                 print $out "Rewound branch $ref ($old -> $new).\n\n";
122                 most_recent();
123         } else {
124                 # Otherwise it is a rebase
125                 $subj .= ' Branch rebased';
126                 print $out "Rebased branch $ref ($old -> $new).\n\n";
127                 print $out "Commits from common ancestor:\n\n";
128                 system 'git', 'rev-list', @rev_list_options, $new, "^$old";
129         }
130 }
131
132 $out->close();
133 if (defined $mail_to) {
134         close STDIN;
135         open STDIN, '<', $outname;
136         system 'mutt',
137                 '-F/dev/null',
138                 '-x',
139                 '-e', 'set charset="utf-8"',
140                 '-e', 'set send_charset="us-ascii:iso-8859-2:utf-8"',
141                 '-e', "my_hdr X-Git-Repo: $repo",
142                 '-e', "my_hdr X-Git-Branch: $ref",
143                 '-e', "my_hdr X-Git-Old-SHA: $old",
144                 '-e', "my_hdr X-Git-New-SHA: $new",
145                 '-s', $subj,
146                 $mail_to;
147 } else {
148         open STDOUT, '>&', \*ORIG_STDOUT;
149         print "Subject: $subj\n\n";
150         system 'cat', $outname;
151 }