add for_each_reflog_ent() iterator
[gitweb.git] / builtin-show-ref.c
index 23e0ff8fbfea1ca29e4beb18c95475dd8bdae05b..853f13f6ae9df60e340a6988a9d74bb246df4df6 100644 (file)
@@ -4,12 +4,21 @@
 #include "tag.h"
 #include "path-list.h"
 
-static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] | --filter-invalid < ref-list";
+static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] < ref-list";
 
 static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
        found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
 static const char **pattern;
 
+static void show_one(const char *refname, const unsigned char *sha1)
+{
+       const char *hex = find_unique_abbrev(sha1, abbrev);
+       if (hash_only)
+               printf("%s\n", hex);
+       else
+               printf("%s %s\n", hex, refname);
+}
+
 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
 {
        struct object *obj;
@@ -58,11 +67,7 @@ static int show_ref(const char *refname, const unsigned char *sha1, int flag, vo
        if (quiet)
                return 0;
 
-       hex = find_unique_abbrev(sha1, abbrev);
-       if (hash_only)
-               printf("%s\n", hex);
-       else
-               printf("%s %s\n", hex, refname);
+       show_one(refname, sha1);
 
        if (!deref_tags)
                return 0;
@@ -111,11 +116,12 @@ static int exclude_existing(const char *match)
 
        for_each_ref(add_existing, &existing_refs);
        while (fgets(buf, sizeof(buf), stdin)) {
-               int len = strlen(buf);
                char *ref;
+               int len = strlen(buf);
+
                if (len > 0 && buf[len - 1] == '\n')
                        buf[--len] = '\0';
-               if (!strcmp(buf + len - 3, "^{}")) {
+               if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
                        len -= 3;
                        buf[len] = '\0';
                }
@@ -175,13 +181,13 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
                if (!strncmp(arg, "--hash=", 7) ||
                    (!strncmp(arg, "--abbrev", 8) &&
                     (arg[8] == '=' || arg[8] == '\0'))) {
-                       if (arg[3] != 'h' && !arg[8])
+                       if (arg[2] != 'h' && !arg[8])
                                /* --abbrev only */
                                abbrev = DEFAULT_ABBREV;
                        else {
                                /* --hash= or --abbrev= */
                                char *end;
-                               if (arg[3] == 'h') {
+                               if (arg[2] == 'h') {
                                        hash_only = 1;
                                        arg += 7;
                                }
@@ -218,9 +224,11 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
                unsigned char sha1[20];
 
                while (*pattern) {
-                       if (resolve_ref(*pattern, sha1, 1, NULL))
-                               printf("%s %s\n", sha1_to_hex(sha1),
-                                      *pattern);
+                       if (!strncmp(*pattern, "refs/", 5) &&
+                           resolve_ref(*pattern, sha1, 1, NULL)) {
+                               if (!quiet)
+                                       show_one(*pattern, sha1);
+                       }
                        else if (!quiet)
                                die("'%s' - not a valid ref", *pattern);
                        else