Merge branch 'jk/merge-subtree-heuristics'
[git] / builtin / show-ref.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "object-store.h"
5 #include "object.h"
6 #include "tag.h"
7 #include "string-list.h"
8 #include "parse-options.h"
9
10 static const char * const show_ref_usage[] = {
11         N_("git show-ref [-q | --quiet] [--verify] [--head] [-d | --dereference] [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [<pattern>...]"),
12         N_("git show-ref --exclude-existing[=<pattern>]"),
13         NULL
14 };
15
16 static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
17            quiet, hash_only, abbrev, exclude_arg;
18 static const char **pattern;
19 static const char *exclude_existing_arg;
20
21 static void show_one(const char *refname, const struct object_id *oid)
22 {
23         const char *hex;
24         struct object_id peeled;
25
26         if (!has_sha1_file(oid->hash))
27                 die("git show-ref: bad ref %s (%s)", refname,
28                     oid_to_hex(oid));
29
30         if (quiet)
31                 return;
32
33         hex = find_unique_abbrev(oid, abbrev);
34         if (hash_only)
35                 printf("%s\n", hex);
36         else
37                 printf("%s %s\n", hex, refname);
38
39         if (!deref_tags)
40                 return;
41
42         if (!peel_ref(refname, &peeled)) {
43                 hex = find_unique_abbrev(&peeled, abbrev);
44                 printf("%s %s^{}\n", hex, refname);
45         }
46 }
47
48 static int show_ref(const char *refname, const struct object_id *oid,
49                     int flag, void *cbdata)
50 {
51         if (show_head && !strcmp(refname, "HEAD"))
52                 goto match;
53
54         if (tags_only || heads_only) {
55                 int match;
56
57                 match = heads_only && starts_with(refname, "refs/heads/");
58                 match |= tags_only && starts_with(refname, "refs/tags/");
59                 if (!match)
60                         return 0;
61         }
62         if (pattern) {
63                 int reflen = strlen(refname);
64                 const char **p = pattern, *m;
65                 while ((m = *p++) != NULL) {
66                         int len = strlen(m);
67                         if (len > reflen)
68                                 continue;
69                         if (memcmp(m, refname + reflen - len, len))
70                                 continue;
71                         if (len == reflen)
72                                 goto match;
73                         if (refname[reflen - len - 1] == '/')
74                                 goto match;
75                 }
76                 return 0;
77         }
78
79 match:
80         found_match++;
81
82         show_one(refname, oid);
83
84         return 0;
85 }
86
87 static int add_existing(const char *refname, const struct object_id *oid,
88                         int flag, void *cbdata)
89 {
90         struct string_list *list = (struct string_list *)cbdata;
91         string_list_insert(list, refname);
92         return 0;
93 }
94
95 /*
96  * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
97  * and
98  * (1) strip "^{}" at the end of line if any;
99  * (2) ignore if match is provided and does not head-match refname;
100  * (3) warn if refname is not a well-formed refname and skip;
101  * (4) ignore if refname is a ref that exists in the local repository;
102  * (5) otherwise output the line.
103  */
104 static int exclude_existing(const char *match)
105 {
106         static struct string_list existing_refs = STRING_LIST_INIT_DUP;
107         char buf[1024];
108         int matchlen = match ? strlen(match) : 0;
109
110         for_each_ref(add_existing, &existing_refs);
111         while (fgets(buf, sizeof(buf), stdin)) {
112                 char *ref;
113                 int len = strlen(buf);
114
115                 if (len > 0 && buf[len - 1] == '\n')
116                         buf[--len] = '\0';
117                 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
118                         len -= 3;
119                         buf[len] = '\0';
120                 }
121                 for (ref = buf + len; buf < ref; ref--)
122                         if (isspace(ref[-1]))
123                                 break;
124                 if (match) {
125                         int reflen = buf + len - ref;
126                         if (reflen < matchlen)
127                                 continue;
128                         if (strncmp(ref, match, matchlen))
129                                 continue;
130                 }
131                 if (check_refname_format(ref, 0)) {
132                         warning("ref '%s' ignored", ref);
133                         continue;
134                 }
135                 if (!string_list_has_string(&existing_refs, ref)) {
136                         printf("%s\n", buf);
137                 }
138         }
139         return 0;
140 }
141
142 static int hash_callback(const struct option *opt, const char *arg, int unset)
143 {
144         hash_only = 1;
145         /* Use full length SHA1 if no argument */
146         if (!arg)
147                 return 0;
148         return parse_opt_abbrev_cb(opt, arg, unset);
149 }
150
151 static int exclude_existing_callback(const struct option *opt, const char *arg,
152                                      int unset)
153 {
154         exclude_arg = 1;
155         *(const char **)opt->value = arg;
156         return 0;
157 }
158
159 static const struct option show_ref_options[] = {
160         OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
161         OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
162         OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
163                     "requires exact ref path")),
164         OPT_HIDDEN_BOOL('h', NULL, &show_head,
165                         N_("show the HEAD reference, even if it would be filtered out")),
166         OPT_BOOL(0, "head", &show_head,
167           N_("show the HEAD reference, even if it would be filtered out")),
168         OPT_BOOL('d', "dereference", &deref_tags,
169                     N_("dereference tags into object IDs")),
170         { OPTION_CALLBACK, 's', "hash", &abbrev, N_("n"),
171           N_("only show SHA1 hash using <n> digits"),
172           PARSE_OPT_OPTARG, &hash_callback },
173         OPT__ABBREV(&abbrev),
174         OPT__QUIET(&quiet,
175                    N_("do not print results to stdout (useful with --verify)")),
176         { OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
177           N_("pattern"), N_("show refs from stdin that aren't in local repository"),
178           PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
179         OPT_END()
180 };
181
182 int cmd_show_ref(int argc, const char **argv, const char *prefix)
183 {
184         argc = parse_options(argc, argv, prefix, show_ref_options,
185                              show_ref_usage, 0);
186
187         if (exclude_arg)
188                 return exclude_existing(exclude_existing_arg);
189
190         pattern = argv;
191         if (!*pattern)
192                 pattern = NULL;
193
194         if (verify) {
195                 if (!pattern)
196                         die("--verify requires a reference");
197                 while (*pattern) {
198                         struct object_id oid;
199
200                         if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) &&
201                             !read_ref(*pattern, &oid)) {
202                                 show_one(*pattern, &oid);
203                         }
204                         else if (!quiet)
205                                 die("'%s' - not a valid ref", *pattern);
206                         else
207                                 return 1;
208                         pattern++;
209                 }
210                 return 0;
211         }
212
213         if (show_head)
214                 head_ref(show_ref, NULL);
215         for_each_ref(show_ref, NULL);
216         if (!found_match) {
217                 if (verify && !quiet)
218                         die("No match");
219                 return 1;
220         }
221         return 0;
222 }