Code

git-notify: Be careful with "--no-merge"
[nagiosplug.git] / tools / git-notify
index 2970f007b4e51c4b4f94011e9625393a058f101f..851429658fd238d820a06e6d6e2db62bea3431b3 100755 (executable)
@@ -21,6 +21,7 @@
 #   -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        Enable compatibility with SourceForge's gitweb URLs
 #   -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
@@ -42,11 +43,11 @@ sub get_repos_name();
 
 # some parameters you may want to change
 
-# set this to something that takes "-s"
-my $mailer = "/usr/bin/mail";
+# sendmail's pathname
+my $sendmail = "/usr/sbin/sendmail";
 
 # CIA notification address
-my $cia_address = "cia\@cia.navi.cx";
+my $cia_address = "cia\@cia.vc";
 
 # debug mode
 my $debug = 0;
@@ -68,6 +69,12 @@ my $gitweb_url = git_config( "notify.baseurl" );
 # abbreviate the SHA1 name within gitweb URLs (can be set with the -z option)
 my $abbreviate_url = git_config( "notify.shorturls" );
 
+# don't report merge commits (can be set with the -X option)
+my $ignore_merges = git_config( "notify.ignoremerges" );
+
+# enable compatibility with SourceForge's gitweb (can be set with the -S option)
+my $sourceforge = git_config( "notify.sourceforge" );
+
 # default repository name (can be changed with the -r option)
 my $repos_name = git_config( "notify.repository" ) || get_repos_name();
 
@@ -95,9 +102,6 @@ my $state_file = git_config( "notify.statefile" );
 # umask for creating the state file (can be set with -U option)
 my $mode_mask = git_config( "notify.umask" ) || 002;
 
-# Extra options to git rev-list
-my @revlist_options;
-
 sub usage()
 {
     print "Usage: $0 [options] [--] old-sha1 new-sha1 refname\n";
@@ -107,6 +111,7 @@ sub usage()
     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        Enable compatibility with SourceForge's gitweb URLs\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";
@@ -140,7 +145,7 @@ sub git_rev_list(@)
     die "Cannot open pipe: $!" if not defined $pid;
     if (!$pid)
     {
-        exec "git", "rev-list", @revlist_options, @args or die "Cannot execute rev-list: $!";
+        exec "git", "rev-list", "--reverse", @args or die "Cannot execute rev-list: $!";
     }
     while (<REVLIST>)
     {
@@ -176,6 +181,7 @@ sub get_new_commits($$)
 
     @args = ( "^$old_sha1" ) unless $old_sha1 eq '0' x 40;
     push @args, $new_sha1, @exclude_list;
+    unshift @args, "--no-merges" if $ignore_merges;
 
     my $revlist = git_rev_list(@args);
 
@@ -294,14 +300,15 @@ sub parse_options()
         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') { $sourceforge = 1; }
         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; }
         elsif ($arg eq '-i') { push @include_list, shift @ARGV; }
+        elsif ($arg eq '-X') { $ignore_merges = 1; }
         elsif ($arg eq '-x') { push @exclude_list, shift @ARGV; }
-        elsif ($arg eq '-X') { push @revlist_options, "--no-merges"; }
         elsif ($arg eq '-z') { $abbreviate_url = 1; }
         elsif ($arg eq '-d') { $debug++; }
         else { usage(); }
@@ -315,17 +322,16 @@ sub mail_notification($$$@)
 {
     my ($name, $subject, $content_type, @text) = @_;
 
-    $subject = "[$repos_name] $subject" if $emit_repo;
+    $subject = "[$repos_name] $subject" if ($emit_repo and $name ne $cia_address);
     $subject = encode("MIME-Q",$subject);
 
+    my @header = ("To: $name", "Subject: $subject", "Content-Type: $content_type");
+
     if ($debug)
     {
         binmode STDOUT, ":utf8";
         print "---------------------\n";
-        print "To: $name\n";
-        print "Subject: $subject\n";
-        print "Content-Type: $content_type\n";
-        print "\n", join("\n", @text), "\n";
+        print join("\n", @header), "\n\n", join("\n", @text), "\n";
     }
     else
     {
@@ -333,11 +339,11 @@ sub mail_notification($$$@)
         return unless defined $pid;
         if (!$pid)
         {
-            exec $mailer, "-s", $subject, "-a", "Content-Type: $content_type", $name or die "Cannot exec $mailer";
+            exec $sendmail, "-t", "-oi", "-oem" or die "Cannot exec $sendmail";
         }
         binmode MAIL, ":utf8";
-        print MAIL join("\n", @text), "\n";
-        close MAIL or warn $! ? "Cannot execute $mailer: $!" : "$mailer exited with status: $?";
+        print MAIL join("\n", @header), "\n\n", join("\n", @text), "\n";
+        close MAIL or warn $! ? "Cannot execute $sendmail: $!" : "$sendmail exited with status: $?";
     }
 }
 
@@ -423,7 +429,7 @@ sub send_ref_notice($$@)
         ($reftype eq "tag" ? "Tag:" : "Branch:") . $refname,
         @notice,
         ($action ne "removed" and $gitweb_url)
-            ? "URL: $gitweb_url/?a=shortlog;h=$ref" : undef),
+            ? "URL: ${gitweb_url}a=shortlog;h=$ref" : undef),
         "",
         "The $refname $reftype has been $action.");
 
@@ -449,7 +455,7 @@ sub send_commit_notice($$)
             close REVPARSE or die $! ? "Cannot execute rev-parse: $!" : "rev-parse exited with status: $?";
         }
         $obj_string = $obj if not defined $obj_string;
-        $url = "$gitweb_url/?a=$info{type};h=$obj_string";
+        $url = "${gitweb_url}a=$info{type};h=$obj_string";
     }
 
     if ($info{"type"} eq "tag")
@@ -497,7 +503,7 @@ sub send_commit_notice($$)
         }
         else
         {
-            push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj_string" if $gitweb_url;
+            push @notice, "Diff: ${gitweb_url}a=commitdiff;h=$obj_string" if $gitweb_url;
         }
         $subject = $info{"author_name"} . ": " unless $omit_author;
     }
@@ -554,7 +560,7 @@ sub send_cia_notice($$)
 
     push @cia_text,
         "      </files>",
-        $gitweb_url ? "      <url>" . xml_escape("$gitweb_url/?a=commit;h=$commit") . "</url>" : "",
+        $gitweb_url ? "      <url>" . xml_escape("${gitweb_url}a=commit;h=$commit") . "</url>" : "",
         "    </commit>",
         "  </body>",
         "  <timestamp>" . $info{"author_date"} . "</timestamp>",
@@ -571,7 +577,7 @@ sub send_global_notice($$$)
 
     foreach my $rev (@$notice)
     {
-        $rev =~ s/^commit /URL:    $gitweb_url\/?a=commit;h=/ if $gitweb_url;
+        $rev =~ s/^commit /URL:    ${gitweb_url}a=commit;h=/ if $gitweb_url;
     }
 
     mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @$notice);
@@ -650,7 +656,9 @@ parse_options();
 umask( $mode_mask );
 
 # append repository path to URL
-$gitweb_url .= "/$repos_name.git" if $gitweb_url;
+if ($gitweb_url) {
+    $gitweb_url .= $sourceforge ? "/$repos_name;" : "/$repos_name.git/?";
+}
 
 if (@ARGV)
 {