[nagiosplug] git-notify: Make abbreviating Gitweb URLs optional

Holger Weiss hweiss at users.sourceforge.net
Sat Nov 7 10:42:04 CET 2009


 Module: nagiosplug
 Branch: master
 Commit: 40ef91694655a3abb425d994a1b862f956ac081c
 Author: Holger Weiss <holger at zedat.fu-berlin.de>
   Date: Sat Nov  7 02:23:32 2009 +0100
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=40ef916

git-notify: Make abbreviating Gitweb URLs optional

The SHA1 object name part of Gitweb URLs is now only shortened if the
user requested this by specifying the new "-z" option (or by setting
"notify.shortURLs").

While at it, also shorten the additional URL which references a diff in
e-mail notifications which don't include that diff inline because its
size exceeds the maximum number of bytes specified via "-s".

Note that while the abbreviated SHA1 object names will be unique at push
time, this cannot be guaranteed for the future, so the shortened URLs
might break some day.

---

 tools/git-notify |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/tools/git-notify b/tools/git-notify
index 0031fcd..289a5f6 100755
--- a/tools/git-notify
+++ b/tools/git-notify
@@ -27,6 +27,7 @@
 #   -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;
@@ -56,6 +57,9 @@ 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();
 
@@ -101,6 +105,7 @@ sub usage()
     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;
 }
 
@@ -282,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(); }
     }
@@ -404,17 +410,19 @@ sub send_commit_notice($$)
     my ($ref,$obj) = @_;
     my %info = get_object_info($obj);
     my @notice = ();
-    my ($url,$subject);
+    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")
@@ -461,7 +469,7 @@ sub send_commit_notice($$)
         }
         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"};
     }





More information about the Commits mailing list