chiark / gitweb /
git_get_ref: Move to Dgit.pm and reimplement
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 27 May 2015 20:28:39 +0000 (21:28 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 31 May 2015 11:17:08 +0000 (12:17 +0100)
Use
  git for-each-ref --format=%(objectname) [r]efs/whatever

This pattern won't match literally as a prefix because a ref name
cannot contain [, so it must match according to fnmatch.  whatever
cannot contain fnmatch metacharacters because they're not valid in ref
names.

This is a better idiom because it might directly look up the ref in
question (although currently it probably doesn't).

Debian/Dgit.pm
dgit

index a270cd8..32c2bc5 100644 (file)
@@ -18,7 +18,7 @@ BEGIN {
     @EXPORT      = qw(debiantag server_branch server_ref
                       stat_exists fail ensuredir waitstatusmsg failedcmd
                       cmdoutput cmdoutput_errok
-                      git_rev_parse git_for_each_ref
+                      git_rev_parse git_get_ref git_for_each_ref
                       git_for_each_tag_referring is_fast_fwd
                       $package_re $component_re $deliberately_re
                       $branchprefix
@@ -205,6 +205,14 @@ sub git_for_each_ref ($$) {
     $!=0; $?=0; close GFER or die "$pattern $? $!";
 }
 
+sub git_get_ref ($) {
+    # => '' if no such ref
+    my ($refname) = @_;
+    my $_ = $refname;
+    s{^refs/}{[r]efs/} or die "$refname $_ ?";
+    return cmdoutput qw(git for-each-ref --format=%(objectname)), $_;
+}
+
 sub git_for_each_tag_referring ($$) {
     my ($objreferring, $func) = @_;
     # calls $func->($tagobjid,$refobjid,$fullrefname,$tagname);
diff --git a/dgit b/dgit
index 1e348f3..42f9f45 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -667,23 +667,6 @@ sub parsechangelog {
     return $c;
 }
 
-sub git_get_ref ($) {
-    my ($refname) = @_;
-    my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
-    if (!defined $got) {
-       $?==256 or fail "git show-ref failed (status $?)";
-       printdebug "ref $refname= [show-ref exited 1]\n";
-       return '';
-    }
-    if ($got =~ m/^(\w+) \Q$refname\E$/m) {
-       printdebug "ref $refname=$1\n";
-       return $1;
-    } else {
-       printdebug "ref $refname= [no match]\n";
-       return '';
-    }
-}
-
 sub must_getcwd () {
     my $d = getcwd();
     defined $d or fail "getcwd failed: $!";