Fix show-ref usage for --dereference.
[git/git-bigfiles.git] / builtin-show-ref.c
blobfab359bb42b9fb2b9ec1749a7772f14206cf5418
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] [--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;
10 static const char **pattern;
12 static int show_ref(const char *refname, const unsigned char *sha1)
14 struct object *obj;
16 if (tags_only || heads_only) {
17 int match;
19 match = heads_only && !strncmp(refname, "refs/heads/", 11);
20 match |= tags_only && !strncmp(refname, "refs/tags/", 10);
21 if (!match)
22 return 0;
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;
41 return 0;
44 match:
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));
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);
62 return 0;
65 int cmd_show_ref(int argc, const char **argv, const char *prefix)
67 int i;
69 for (i = 1; i < argc; i++) {
70 const char *arg = argv[i];
71 if (*arg != '-') {
72 pattern = argv + i;
73 break;
75 if (!strcmp(arg, "--")) {
76 pattern = argv + i + 1;
77 if (!*pattern)
78 pattern = NULL;
79 break;
81 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
82 quiet = 1;
83 continue;
85 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
86 show_head = 1;
87 continue;
89 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
90 deref_tags = 1;
91 continue;
93 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
94 hash_only = 1;
95 continue;
97 if (!strcmp(arg, "--verify")) {
98 verify = 1;
99 continue;
101 if (!strcmp(arg, "--tags")) {
102 tags_only = 1;
103 continue;
105 if (!strcmp(arg, "--heads")) {
106 heads_only = 1;
107 continue;
109 usage(show_ref_usage);
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;
119 return 0;