]> mj.ucw.cz Git - git-tools.git/blobdiff - update2
Update2: Remember original stdout
[git-tools.git] / update2
diff --git a/update2 b/update2
index e27c46226ed6c98c51f39971a324bb3c8a594ebe..32426c897a1f3dfca1e874db2b7dc880a2ec6e45 100755 (executable)
--- a/update2
+++ b/update2
@@ -8,6 +8,7 @@ use warnings;
 use Getopt::Long;
 use IO::File;
 use File::Temp;
+use POSIX;
 
 my $mail_to;
 my $subject_prefix = "GIT";
@@ -30,11 +31,14 @@ my ($ref, $old, $new) = @ARGV;
 $ref =~ s{^refs/heads/}{} or exit 0;
 $old ne $new or exit 0;
 
+my $repo = POSIX::getcwd();
+$repo =~ s{.*/}{};
+
 my $subj = '[' . $subject_prefix . ($ref eq 'master' ? '' : "/$ref") . ']';
-my $tmpdir = File::Temp->newdir() or die;
-my $outname = "$tmpdir/0";
-my $out = IO::File->new($outname, '>') or die;
+my $out = File::Temp->new() or die;
+my $outname = $out->filename;
 $out->autoflush(1);
+open ORIG_STDOUT, '>&', \*STDOUT;
 close STDOUT;
 open STDOUT, '>&', $out or die;
 
@@ -69,15 +73,15 @@ if ($old =~ /^0+$/) {
        my $copy_of = scan_branches();
        if (defined $copy_of) {
                $subj .= " as a copy of $copy_of";
-               print $out "Created branch $ref as a copy of $copy_of.\n";
+               print $out "Created branch $ref as a copy of $copy_of ($new).\n";
        } else {
-               print $out "Created branch $ref.\n\n";
+               print $out "Created branch $ref ($new).\n\n";
                most_recent();
        }
 } elsif ($new =~ /^0+$/) {
        # Deletion of a branch
        $subj .= ' Branch deleted';
-       print $out "Deleted branch $ref.\n\nPrevious tip was $old.\n";
+       print $out "Deleted branch $ref ($old).\n";
 } else {
        my $lca = `git merge-base $old $new`; die if $?;
        chomp $lca;
@@ -90,6 +94,7 @@ if ($old =~ /^0+$/) {
                chomp $c;
                $c =~ s{^\S+\s+}{};
                $subj .= " $c";
+               print $out "Push to branch $ref ($old -> $new)\n\n";
 
                # If there are multiple commits, print an overall diffstat first
                if (@commits > 1) {
@@ -101,24 +106,24 @@ if ($old =~ /^0+$/) {
                my $pos_after_header = output_size();
 
                # Show individual commits with diffs and stats
-               system 'git', 'log', @rev_list_options, '--reverse', @diff_options, '--patch', '--stat', "$old..$new";
+               system 'git', 'log', @rev_list_options, @diff_options, '-p', '--stat', "$old..$new";
 
                # If the file is too long, truncate it and print just a summary
                if (defined($max_diff_size) && output_size() > $max_diff_size) {
                        $out->truncate($pos_after_header);
                        output_size();
                        print $out "Diff was too long, printing just a summary.\n\n";
-                       system 'git', 'log', @rev_list_options, '--reverse', "$old..$new";
+                       system 'git', 'log', @rev_list_options, "$old..$new";
                }
        } elsif ($lca eq $new) {
                # Rewind
                $subj .= ' Branch rewound';
-               print $out "Rewound branch $ref to commit $new.\n\n";
+               print $out "Rewound branch $ref ($old -> $new).\n\n";
                most_recent();
        } else {
                # Otherwise it is a rebase
                $subj .= ' Branch rebased';
-               print $out "Rebased branch $ref to commit $new.\n\n";
+               print $out "Rebased branch $ref ($old -> $new).\n\n";
                print $out "Commits from common ancestor:\n\n";
                system 'git', 'rev-list', @rev_list_options, $new, "^$old";
        }
@@ -131,11 +136,16 @@ if (defined $mail_to) {
        system 'mutt',
                '-F/dev/null',
                '-x',
-               '-e',
-               'set charset="utf-8"; set send_charset="us-ascii:iso-8859-2:utf-8"',
+               '-e', 'set charset="utf-8"',
+               '-e', 'set send_charset="us-ascii:iso-8859-2:utf-8"',
+               '-e', "my_hdr X-Git-Repo: $repo",
+               '-e', "my_hdr X-Git-Branch: $ref",
+               '-e', "my_hdr X-Git-Old-SHA: $old",
+               '-e', "my_hdr X-Git-New-SHA: $new",
                '-s', $subj,
                $mail_to;
 } else {
-       print STDERR "Subject: $subj\n\n";
-       `cat >&2 $outname`;
+       open STDOUT, '>&', \*ORIG_STDOUT;
+       print "Subject: $subj\n\n";
+       system 'cat', $outname;
 }