builtin-show-ref.con commit Merge branch 'maint' (bd45fec)
   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|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--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, abbrev = 0;
  10static const char **pattern;
  11
  12static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
  13{
  14        struct object *obj;
  15        const char *hex;
  16
  17        if (tags_only || heads_only) {
  18                int match;
  19
  20                match = heads_only && !strncmp(refname, "refs/heads/", 11);
  21                match |= tags_only && !strncmp(refname, "refs/tags/", 10);
  22                if (!match)
  23                        return 0;
  24        }
  25        if (pattern) {
  26                int reflen = strlen(refname);
  27                const char **p = pattern, *m;
  28                while ((m = *p++) != NULL) {
  29                        int len = strlen(m);
  30                        if (len > reflen)
  31                                continue;
  32                        if (memcmp(m, refname + reflen - len, len))
  33                                continue;
  34                        if (len == reflen)
  35                                goto match;
  36                        /* "--verify" requires an exact match */
  37                        if (verify)
  38                                continue;
  39                        if (refname[reflen - len - 1] == '/')
  40                                goto match;
  41                }
  42                return 0;
  43        }
  44
  45match:
  46        found_match++;
  47        obj = parse_object(sha1);
  48        if (!obj) {
  49                if (quiet)
  50                        return 0;
  51                die("git-show-ref: bad ref %s (%s)", refname, sha1_to_hex(sha1));
  52        }
  53        if (quiet)
  54                return 0;
  55
  56        hex = find_unique_abbrev(sha1, abbrev);
  57        if (hash_only)
  58                printf("%s\n", hex);
  59        else
  60                printf("%s %s\n", hex, refname);
  61        if (deref_tags && obj->type == OBJ_TAG) {
  62                obj = deref_tag(obj, refname, 0);
  63                hex = find_unique_abbrev(obj->sha1, abbrev);
  64                printf("%s %s^{}\n", hex, refname);
  65        }
  66        return 0;
  67}
  68
  69int cmd_show_ref(int argc, const char **argv, const char *prefix)
  70{
  71        int i;
  72
  73        for (i = 1; i < argc; i++) {
  74                const char *arg = argv[i];
  75                if (*arg != '-') {
  76                        pattern = argv + i;
  77                        break;
  78                }
  79                if (!strcmp(arg, "--")) {
  80                        pattern = argv + i + 1;
  81                        if (!*pattern)
  82                                pattern = NULL;
  83                        break;
  84                }
  85                if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
  86                        quiet = 1;
  87                        continue;
  88                }
  89                if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
  90                        show_head = 1;
  91                        continue;
  92                }
  93                if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
  94                        deref_tags = 1;
  95                        continue;
  96                }
  97                if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
  98                        hash_only = 1;
  99                        continue;
 100                }
 101                if (!strncmp(arg, "--hash=", 7) ||
 102                    (!strncmp(arg, "--abbrev", 8) &&
 103                     (arg[8] == '=' || arg[8] == '\0'))) {
 104                        if (arg[3] != 'h' && !arg[8])
 105                                /* --abbrev only */
 106                                abbrev = DEFAULT_ABBREV;
 107                        else {
 108                                /* --hash= or --abbrev= */
 109                                char *end;
 110                                if (arg[3] == 'h') {
 111                                        hash_only = 1;
 112                                        arg += 7;
 113                                }
 114                                else
 115                                        arg += 9;
 116                                abbrev = strtoul(arg, &end, 10);
 117                                if (*end || abbrev > 40)
 118                                        usage(show_ref_usage);
 119                                if (abbrev < MINIMUM_ABBREV)
 120                                        abbrev = MINIMUM_ABBREV;
 121                        }
 122                        continue;
 123                }
 124                if (!strcmp(arg, "--verify")) {
 125                        verify = 1;
 126                        continue;
 127                }
 128                if (!strcmp(arg, "--tags")) {
 129                        tags_only = 1;
 130                        continue;
 131                }
 132                if (!strcmp(arg, "--heads")) {
 133                        heads_only = 1;
 134                        continue;
 135                }
 136                usage(show_ref_usage);
 137        }
 138        if (show_head)
 139                head_ref(show_ref, NULL);
 140        for_each_ref(show_ref, NULL);
 141        if (!found_match) {
 142                if (verify && !quiet)
 143                        die("No match");
 144                return 1;
 145        }
 146        return 0;
 147}