Code

git-notify: Make abbreviating Gitweb URLs optional
[nagiosplug.git] / tools / git-notify
index f524fd7348d4822620c110f609a5ccdc1dfe5876..289a5f645a0f9f69b427edab8f6a063138d2c47f 100755 (executable)
 #
 # Usage: git-notify [options] [--] old-sha1 new-sha1 refname
 #
+#   -C        Show committer in the body if different from the author
 #   -c name   Send CIA notifications under specified project name
 #   -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 file   Set the file to use for reading and saving state
+#   -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
 #   -i branch If at least one -i is given, report only for specified branches
 #   -x branch Exclude changes to the specified branch from reports
 #   -X        Exclude merge commits
+#   -z        Try to abbreviate the SHA1 name within gitweb URLs (unsafe)
 #
 
 use strict;
@@ -47,14 +49,17 @@ my $cia_address = "cia\@cia.navi.cx";
 # debug mode
 my $debug = 0;
 
-# number of generated (non-CIA) notifications
-my $sent_notices = 0;
-
 # configuration parameters
 
+# show the committer if different from the author (can be set with the -C option)
+my $show_committer = git_config( "notify.showcommitter" );
+
 # base URL of the gitweb repository browser (can be set with the -u option)
 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" );
+
 # default repository name (can be changed with the -r option)
 my $repos_name = git_config( "notify.repository" ) || get_repos_name();
 
@@ -76,8 +81,8 @@ my @include_list = split /\s+/, git_config( "notify.include" ) || "";
 # branches to exclude
 my @exclude_list = split /\s+/, git_config( "notify.exclude" ) || "";
 
-# the state file we use (can be changed with the -t option)
-my $state_file = git_config( "notify.statefile" ) || "/var/tmp/git-notify.state";
+# the state file we use (can be set with the -t option)
+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;
@@ -88,17 +93,19 @@ my @revlist_options;
 sub usage()
 {
     print "Usage: $0 [options] [--] old-sha1 new-sha1 refname\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 "   -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 file   Set the file to use for reading and saving state\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";
     print "   -i branch If at least one -i is given, report only for specified branches\n";
     print "   -x branch Exclude changes to the specified branch from reports\n";
     print "   -X        Exclude merge commits\n";
+    print "   -z        Try to abbreviate the SHA1 name within gitweb URLs (unsafe)\n";
     exit 1;
 }
 
@@ -147,7 +154,7 @@ sub save_commits($)
     close STATE or die "Cannot close $state_file: $!";
 }
 
-# for the given range, return the new hashes and append them to the state file
+# for the given range, return the new hashes (and append them to the state file)
 sub get_new_commits($$)
 {
     my ($old_sha1, $new_sha1) = @_;
@@ -159,9 +166,9 @@ sub get_new_commits($$)
 
     my $revlist = git_rev_list(@args);
 
-    if (not -e $state_file)  # initialize the state file with all hashes
+    if (not defined $state_file or not -e $state_file)
     {
-        save_commits(git_rev_list("--all", "--full-history"));
+        save_commits(git_rev_list("--all", "--full-history")) if defined $state_file;
         return $revlist;
     }
 
@@ -268,6 +275,7 @@ sub parse_options()
         my $arg = shift @ARGV;
 
         if ($arg eq '--') { last; }
+        elsif ($arg eq '-C') { $show_committer = 1; }
         elsif ($arg eq '-c') { $cia_project_name = shift @ARGV; }
         elsif ($arg eq '-m') { $commitlist_address = shift @ARGV; }
         elsif ($arg eq '-n') { $max_individual_notices = shift @ARGV; }
@@ -279,6 +287,7 @@ sub parse_options()
         elsif ($arg eq '-i') { push @include_list, shift @ARGV; }
         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(); }
     }
@@ -310,7 +319,7 @@ sub mail_notification($$$@)
         }
         binmode MAIL, ":utf8";
         print MAIL join("\n", @text), "\n";
-        close MAIL or die $! ? "Cannot execute $mailer: $!" : "$mailer exited with status: $?";
+        close MAIL or warn $! ? "Cannot execute $mailer: $!" : "$mailer exited with status: $?";
     }
 }
 
@@ -393,7 +402,6 @@ sub send_ref_notice($$@)
 
     mail_notification($commitlist_address, "$refname $reftype $action",
         "text/plain; charset=us-ascii", @notice);
-    $sent_notices++;
 }
 
 # send a commit notice to a mailing list
@@ -402,23 +410,19 @@ sub send_commit_notice($$)
     my ($ref,$obj) = @_;
     my %info = get_object_info($obj);
     my @notice = ();
-    my ($url,$subject);
-
-    open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
-    my $diff = join("", <DIFF>);
-    close DIFF or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
-
-    return if length($diff) == 0;
+    my ($url,$subject,$obj_string);
 
     if ($gitweb_url)
     {
-        open REVPARSE, "-|" or exec "git", "rev-parse", "--short", $obj or die "cannot exec git-rev-parse";
-        my $short_obj = <REVPARSE>;
-        close REVPARSE or die $! ? "Cannot execute rev-parse: $!" : "rev-parse exited with status: $?";
-
-        $short_obj = $obj if not defined $short_obj;
-        chomp $short_obj;
-        $url = "$gitweb_url/?a=$info{type};h=$short_obj";
+        if ($abbreviate_url)
+        {
+            open REVPARSE, "-|" or exec "git", "rev-parse", "--short", $obj or die "cannot exec git-rev-parse";
+            $obj_string = <REVPARSE>;
+            chomp $obj_string if defined $obj_string;
+            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";
     }
 
     if ($info{"type"} eq "tag")
@@ -442,7 +446,7 @@ sub send_commit_notice($$)
           "Branch: $ref",
           "Commit: $obj",
           "Author:" . $info{"author"},
-          $info{"committer"} ne $info{"author"} ? "Committer:" . $info{"committer"} : undef,
+          $show_committer && $info{"committer"} ne $info{"author"} ? "Committer:" . $info{"committer"} : undef,
           "Date:" . format_date($info{"author_date"},$info{"author_tz"}),
           $url ? "URL: $url" : undef),
           "",
@@ -455,13 +459,17 @@ sub send_commit_notice($$)
         push @notice, join("", <STAT>);
         close STAT or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
 
+        open DIFF, "-|" or exec "git", "diff-tree", "-p", "-M", "--no-commit-id", $obj or die "cannot exec git-diff-tree";
+        my $diff = join("", <DIFF>);
+        close DIFF or die $! ? "Cannot execute diff-tree: $!" : "diff-tree exited with status: $?";
+
         if (($max_diff_size == -1) || (length($diff) < $max_diff_size))
         {
             push @notice, $diff;
         }
         else
         {
-            push @notice, "Diff:   $gitweb_url/?a=commitdiff;h=$obj" if $gitweb_url;
+            push @notice, "Diff: $gitweb_url/?a=commitdiff;h=$obj_string" if $gitweb_url;
         }
         $subject = $info{"author_name"};
     }
@@ -469,7 +477,6 @@ sub send_commit_notice($$)
     $subject .= ": " . truncate_str(${$info{"log"}}[0],50);
     $_ = decode($info{"encoding"}, $_) for @notice;
     mail_notification($commitlist_address, $subject, "text/plain; charset=UTF-8", @notice);
-    $sent_notices++;
 }
 
 # send a commit notice to the CIA server
@@ -540,7 +547,6 @@ sub send_global_notice($$$)
     }
 
     mail_notification($commitlist_address, "New commits on branch $ref", "text/plain; charset=UTF-8", @$notice);
-    $sent_notices++;
 }
 
 # send all the notices
@@ -585,7 +591,7 @@ sub send_all_notices($$$)
         {
             send_global_notice( $refname, $old_sha1, $new_sha1 ) if $commitlist_address;
         }
-        else
+        elsif (@$commits > 0)
         {
             foreach my $commit (@$commits)
             {
@@ -593,7 +599,7 @@ sub send_all_notices($$$)
                 send_cia_notice( $refname, $commit ) if $cia_project_name;
             }
         }
-        if ($sent_notices == 0 and $commitlist_address)
+        elsif ($commitlist_address)
         {
             @notice = ( "Old SHA1: $old_sha1", "New SHA1: $new_sha1" );
             send_ref_notice( $ref, "modified", @notice );