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