Code

git-notify: Optionally call mail(1) without "-a"
[nagiosplug.git] / tools / git-notify
index 289a5f645a0f9f69b427edab8f6a063138d2c47f..b3223a87a384f85b92d913e775b786e543da0267 100755 (executable)
 #
 # Usage: git-notify [options] [--] old-sha1 new-sha1 refname
 #
+#   -A        Omit the author name from the mail subject
 #   -C        Show committer in the body if different from the author
 #   -c name   Send CIA notifications under specified project name
+#   -H        The mail(1) utility doesn't accept headers via "-a"
 #   -m addr   Send mail notifications to specified address
 #   -n max    Set max number of individual mails to send
 #   -r name   Set the git repository name
 #   -s bytes  Set the maximum diff size in bytes (-1 for no limit)
+#   -T        Prefix the mail subject with a [repository name] tag
 #   -t file   Prevent duplicate notifications by saving state to this file
 #   -U mask   Set the umask for creating the state file
 #   -u url    Set the URL to the gitweb browser
@@ -51,6 +54,15 @@ my $debug = 0;
 
 # configuration parameters
 
+# the $mailer doesn't accept headers via "-a" (can be set with the -H option)
+my $legacy_mail = git_config( "notify.legacymail" );
+
+# omit the author from the mail subject (can be set with the -A option)
+my $omit_author = git_config( "notify.omitauthor" );
+
+# prefix the mail subject with a [repository name] tag (can be set with the -T option)
+my $emit_repo = git_config( "notify.emitrepository" );
+
 # show the committer if different from the author (can be set with the -C option)
 my $show_committer = git_config( "notify.showcommitter" );
 
@@ -93,12 +105,15 @@ my @revlist_options;
 sub usage()
 {
     print "Usage: $0 [options] [--] old-sha1 new-sha1 refname\n";
+    print "   -A        Omit the author name from the mail subject\n";
     print "   -C        Show committer in the body if different from the author\n";
     print "   -c name   Send CIA notifications under specified project name\n";
+    print "   -H        The mail(1) utility doesn't accept headers via `-a'\n";
     print "   -m addr   Send mail notifications to specified address\n";
     print "   -n max    Set max number of individual mails to send\n";
     print "   -r name   Set the git repository name\n";
     print "   -s bytes  Set the maximum diff size in bytes (-1 for no limit)\n";
+    print "   -T        Prefix the mail subject with a [repository name] tag\n";
     print "   -t file   Prevent duplicate notifications by saving state to this file\n";
     print "   -U mask   Set the umask for creating the state file\n";
     print "   -u url    Set the URL to the gitweb browser\n";
@@ -135,7 +150,10 @@ sub git_rev_list(@)
     while (<REVLIST>)
     {
         chomp;
-        die "Invalid commit: $_" if not /^[0-9a-f]{40}$/;
+        unless (grep {$_ eq "--pretty"} @args)
+        {
+            die "Invalid commit: $_" if not /^[0-9a-f]{40}$/;
+        }
         push @$revlist, $_;
     }
     close REVLIST or die $! ? "Cannot execute rev-list: $!" : "rev-list exited with status: $?";
@@ -275,12 +293,15 @@ sub parse_options()
         my $arg = shift @ARGV;
 
         if ($arg eq '--') { last; }
+        elsif ($arg eq '-A') { $omit_author = 1; }
         elsif ($arg eq '-C') { $show_committer = 1; }
         elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; }
+        elsif ($arg eq '-H') { $legacy_mail = 1; }
         elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; }
         elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
         elsif ($arg eq '-r') { $repos_name = shift @ARGV; }
         elsif ($arg eq '-s') { $max_diff_size = shift @ARGV; }
+        elsif ($arg eq '-T') { $emit_repo = 1; }
         elsif ($arg eq '-t') { $state_file = shift @ARGV; }
         elsif ($arg eq '-U') { $mode_mask = shift @ARGV; }
         elsif ($arg eq '-u') { $gitweb_url = shift @ARGV; }
@@ -299,7 +320,10 @@ sub parse_options()
 sub mail_notification($$$@)
 {
     my ($name, $subject, $content_type, @text) = @_;
+
+    $subject = "[$repos_name] $subject" if $emit_repo;
     $subject = encode("MIME-Q",$subject);
+
     if ($debug)
     {
         binmode STDOUT, ":utf8";
@@ -315,7 +339,13 @@ sub mail_notification($$$@)
         return unless defined $pid;
         if (!$pid)
         {
-            exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
+            my @mailer_options = ( "-s", $subject );
+
+            unless ($legacy_mail)
+            {
+                push @mailer_options, "-a", "Content-Type: $content_type";
+            }
+            exec $mailer, @mailer_options, $name or die "Cannot exec $mailer";
         }
         binmode MAIL, ":utf8";
         print MAIL join("\n", @text), "\n";
@@ -334,6 +364,18 @@ sub get_repos_name()
     return $repos;
 }
 
+# return the type of the given object
+sub get_object_type($)
+{
+    my $obj = shift;
+
+    open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
+    my $type = <TYPE>;
+    chomp $type;
+    close TYPE or die $! ? "Cannot execute cat-file: $!" : "cat-file exited with status: $?";
+    return $type;
+}
+
 # extract the information from a commit or tag object and return a hash containing the various fields
 sub get_object_info($)
 {
@@ -344,10 +386,7 @@ sub get_object_info($)
 
     $info{"encoding"} = "utf-8";
 
-    open TYPE, "-|" or exec "git", "cat-file", "-t", $obj or die "cannot run git-cat-file";
-    my $type = <TYPE>;
-    chomp $type;
-    close TYPE or die $! ? "Cannot execute cat-file: $!" : "cat-file exited with status: $?";
+    my $type = get_object_type($obj);
 
     open OBJ, "-|" or exec "git", "cat-file", $type, $obj or die "cannot run git-cat-file";
     while (<OBJ>)
@@ -429,15 +468,16 @@ sub send_commit_notice($$)
     {
         push @notice, format_table(
           "Module: $repos_name",
-          "Branch: $ref",
-          "Tag: $obj",
+          "Tag: $ref",
+          "SHA1: $obj",
           "Tagger:" . $info{"tagger"},
           "Date:" . format_date($info{"tagger_date"},$info{"tagger_tz"}),
           $url ? "URL: $url" : undef),
           "",
           join "\n", @{$info{"log"}};
 
-        $subject = "Tag " . $info{"tag"} . ": " . $info{"tagger_name"};
+        $subject = "Tag " . $info{"tag"} . ": ";
+        $subject .= $info{"tagger_name"} . ": " unless $omit_author;
     }
     else
     {
@@ -471,10 +511,10 @@ sub send_commit_notice($$)
         {
             push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj_string" if $gitweb_url;
         }
-        $subject = $info{"author_name"};
+        $subject = $info{"author_name"} . ": " unless $omit_author;
     }
 
-    $subject .= ": " . truncate_str(${$info{"log"}}[0],50);
+    $subject .= truncate_str(${$info{"log"}}[0],50);
     $_ = decode($info{"encoding"}, $_) for @notice;
     mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
 }
@@ -553,13 +593,18 @@ sub send_global_notice($$$)
 sub send_all_notices($$$)
 {
     my ($old_sha1, $new_sha1, $ref) = @_;
-    my ($reftype, $refname, $action, @notice);
+    my ($reftype, $refname, $tagtype, $action, @notice);
 
     return if ($ref =~ /^refs\/remotes\//
         or (@include_list && !grep {$_ eq $ref} @include_list));
     die "The name \"$ref\" doesn't sound like a local branch or tag"
         if not (($reftype, $refname) = ($ref =~ /^refs\/(head|tag)s\/(.+)/));
 
+    if ($reftype eq "tag")
+    {
+        $tagtype = get_object_type($ref) eq "tag" ? "annotated" : "lightweight";
+    }
+
     if ($new_sha1 eq '0' x 40)
     {
         $action = "removed";
@@ -567,6 +612,11 @@ sub send_all_notices($$$)
     }
     elsif ($old_sha1 eq '0' x 40)
     {
+        if ($reftype eq "tag" and $tagtype eq "annotated")
+        {
+            send_commit_notice( $refname, $new_sha1 ) if $commitlist_address;
+            return;
+        }
         $action = "created";
         @notice = ( "SHA1: $new_sha1" );
     }