builtin-show-ref.con commit Add [-s|--hash] option to Linus' show-ref. (c40abef)
   1#include "cache.h"
   2#include "refs.h"
   3#include "object.h"
   4#include "tag.h"
   5
   6static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--deref] [-s|--hash] [--tags] [--heads] [--] [pattern*]";
   7
   8static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
   9        found_match = 0, verify = 0, quiet = 0, hash_only = 0;
  10static const char **pattern;
  11
  12static int show_ref(const char *refname, const unsigned char *sha1)
  13{
  14        struct object *obj;
  15
  16        if (tags_only || heads_only) {
  17                int match;
  18
  19                match = heads_only && !strncmp(refname, "refs/heads/", 11);
  20                match |= tags_only && !strncmp(refname, "refs/tags/", 10);
  21                if (!match)
  22                        return 0;
  23        }
  24        if (pattern) {
  25                int reflen = strlen(refname);
  26                const char **p = pattern, *m;
  27                while ((m = *p++) != NULL) {
  28                        int len = strlen(m);
  29                        if (len > reflen)
  30                                continue;
  31                        if (memcmp(m, refname + reflen - len, len))
  32                                continue;
  33                        if (len == reflen)
  34                                goto match;
  35                        /* "--verify" requires an exact match */
  36                        if (verify)
  37                                continue;
  38                        if (refname[reflen - len - 1] == '/')
  39                                goto match;
  40                }
  41                return 0;
  42        }
  43
  44match:
  45        found_match++;
  46        obj = parse_object(sha1);
  47        if (!obj) {
  48                if (quiet)
  49                        return 0;
  50                die("git-show-ref: bad ref %s (%s)", refname, sha1_to_hex(sha1));
  51        }
  52        if (quiet)
  53                return 0;
  54        if (hash_only)
  55                printf("%s\n", sha1_to_hex(sha1));
  56        else
  57                printf("%s %s\n", sha1_to_hex(sha1), refname);
  58        if (deref_tags && obj->type == OBJ_TAG) {
  59                obj = deref_tag(obj, refname, 0);
  60                printf("%s %s^{}\n", sha1_to_hex(obj->sha1), refname);
  61        }
  62        return 0;
  63}
  64
  65int cmd_show_ref(int argc, const char **argv, const char *prefix)
  66{
  67        int i;
  68
  69        for (i = 1; i < argc; i++) {
  70                const char *arg = argv[i];
  71                if (*arg != '-') {
  72                        pattern = argv + i;
  73                        break;
  74                }
  75                if (!strcmp(arg, "--")) {
  76                        pattern = argv + i + 1;
  77                        if (!*pattern)
  78                                pattern = NULL;
  79                        break;
  80                }
  81                if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
  82                        quiet = 1;
  83                        continue;
  84                }
  85                if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
  86                        show_head = 1;
  87                        continue;
  88                }
  89                if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
  90                        deref_tags = 1;
  91                        continue;
  92                }
  93                if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
  94                        hash_only = 1;
  95                        continue;
  96                }
  97                if (!strcmp(arg, "--verify")) {
  98                        verify = 1;
  99                        continue;
 100                }
 101                if (!strcmp(arg, "--tags")) {
 102                        tags_only = 1;
 103                        continue;
 104                }
 105                if (!strcmp(arg, "--heads")) {
 106                        heads_only = 1;
 107                        continue;
 108                }
 109                usage(show_ref_usage);
 110        }
 111        if (show_head)
 112                head_ref(show_ref);
 113        for_each_ref(show_ref);
 114        if (!found_match) {
 115                if (verify && !quiet)
 116                        die("No match");
 117                return 1;
 118        }
 119        return 0;
 120}