git-blame: fix rev parameter handling.
[git/mergetool.git] / builtin-show-ref.c
blob06ec400d7f3f2e589111a561a6b18da067a02f1f
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "tag.h"
6 static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*]";
8 static 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;
10 static const char **pattern;
12 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
14 struct object *obj;
15 const char *hex;
17 if (tags_only || heads_only) {
18 int match;
20 match = heads_only && !strncmp(refname, "refs/heads/", 11);
21 match |= tags_only && !strncmp(refname, "refs/tags/", 10);
22 if (!match)
23 return 0;
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;
42 return 0;
45 match:
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));
53 if (quiet)
54 return 0;
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);
66 return 0;
69 int cmd_show_ref(int argc, const char **argv, const char *prefix)
71 int i;
73 for (i = 1; i < argc; i++) {
74 const char *arg = argv[i];
75 if (*arg != '-') {
76 pattern = argv + i;
77 break;
79 if (!strcmp(arg, "--")) {
80 pattern = argv + i + 1;
81 if (!*pattern)
82 pattern = NULL;
83 break;
85 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
86 quiet = 1;
87 continue;
89 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
90 show_head = 1;
91 continue;
93 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
94 deref_tags = 1;
95 continue;
97 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
98 hash_only = 1;
99 continue;
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;
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;
122 continue;
124 if (!strcmp(arg, "--verify")) {
125 verify = 1;
126 continue;
128 if (!strcmp(arg, "--tags")) {
129 tags_only = 1;
130 continue;
132 if (!strcmp(arg, "--heads")) {
133 heads_only = 1;
134 continue;
136 usage(show_ref_usage);
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;
146 return 0;