blob: 41e5e71cad660d26ddc90ffeaa383fd7bf10f79f [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Linus Torvalds358ddb62006-09-15 11:19:32 -07002#include "cache.h"
3#include "refs.h"
4#include "object.h"
5#include "tag.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01006#include "string-list.h"
Stephen Boyd69932bc2009-06-20 21:40:46 -07007#include "parse-options.h"
Linus Torvalds358ddb62006-09-15 11:19:32 -07008
Stephen Boyd69932bc2009-06-20 21:40:46 -07009static const char * const show_ref_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070010 N_("git show-ref [-q | --quiet] [--verify] [--head] [-d | --dereference] [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [<pattern>...]"),
Junio C Hamano33e8fc82015-10-16 11:27:42 -070011 N_("git show-ref --exclude-existing[=<pattern>]"),
Stephen Boyd69932bc2009-06-20 21:40:46 -070012 NULL
13};
Linus Torvalds358ddb62006-09-15 11:19:32 -070014
Stephen Boyd69932bc2009-06-20 21:40:46 -070015static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
16 quiet, hash_only, abbrev, exclude_arg;
Linus Torvalds358ddb62006-09-15 11:19:32 -070017static const char **pattern;
Stephen Boyd69932bc2009-06-20 21:40:46 -070018static const char *exclude_existing_arg;
Linus Torvalds358ddb62006-09-15 11:19:32 -070019
Michael Haggertyf0a011f2015-05-25 18:38:51 +000020static void show_one(const char *refname, const struct object_id *oid)
Junio C Hamano64fe0312006-12-17 19:27:49 -080021{
Vladimir Panteleevf1627042017-01-23 18:00:56 +000022 const char *hex;
23 struct object_id peeled;
24
Vladimir Panteleevd01b8202017-01-23 18:00:58 +000025 if (!has_sha1_file(oid->hash))
26 die("git show-ref: bad ref %s (%s)", refname,
27 oid_to_hex(oid));
28
Vladimir Panteleev14144d32017-01-23 18:00:57 +000029 if (quiet)
30 return;
31
Vladimir Panteleevf1627042017-01-23 18:00:56 +000032 hex = find_unique_abbrev(oid->hash, abbrev);
Junio C Hamano64fe0312006-12-17 19:27:49 -080033 if (hash_only)
34 printf("%s\n", hex);
35 else
36 printf("%s %s\n", hex, refname);
Vladimir Panteleevf1627042017-01-23 18:00:56 +000037
38 if (!deref_tags)
39 return;
40
brian m. carlsonb420d902017-10-15 22:07:02 +000041 if (!peel_ref(refname, &peeled)) {
Vladimir Panteleevf1627042017-01-23 18:00:56 +000042 hex = find_unique_abbrev(peeled.hash, abbrev);
43 printf("%s %s^{}\n", hex, refname);
44 }
Junio C Hamano64fe0312006-12-17 19:27:49 -080045}
46
Michael Haggertyf0a011f2015-05-25 18:38:51 +000047static int show_ref(const char *refname, const struct object_id *oid,
48 int flag, void *cbdata)
Linus Torvalds358ddb62006-09-15 11:19:32 -070049{
Doug Bell3f3d0ce2013-07-16 19:05:14 -050050 if (show_head && !strcmp(refname, "HEAD"))
51 goto match;
52
Linus Torvalds358ddb62006-09-15 11:19:32 -070053 if (tags_only || heads_only) {
54 int match;
55
Christian Couder59556542013-11-30 21:55:40 +010056 match = heads_only && starts_with(refname, "refs/heads/");
57 match |= tags_only && starts_with(refname, "refs/tags/");
Linus Torvalds358ddb62006-09-15 11:19:32 -070058 if (!match)
59 return 0;
60 }
61 if (pattern) {
62 int reflen = strlen(refname);
63 const char **p = pattern, *m;
64 while ((m = *p++) != NULL) {
65 int len = strlen(m);
66 if (len > reflen)
67 continue;
68 if (memcmp(m, refname + reflen - len, len))
69 continue;
70 if (len == reflen)
71 goto match;
Linus Torvalds358ddb62006-09-15 11:19:32 -070072 if (refname[reflen - len - 1] == '/')
73 goto match;
74 }
75 return 0;
76 }
77
78match:
79 found_match++;
Junio C Hamanocf0adba2006-11-19 13:22:44 -080080
Michael Haggertyf0a011f2015-05-25 18:38:51 +000081 show_one(refname, oid);
Junio C Hamanocf0adba2006-11-19 13:22:44 -080082
Linus Torvalds358ddb62006-09-15 11:19:32 -070083 return 0;
84}
85
Michael Haggertyf0a011f2015-05-25 18:38:51 +000086static int add_existing(const char *refname, const struct object_id *oid,
87 int flag, void *cbdata)
Junio C Hamanoed9f7c92006-12-17 17:57:19 -080088{
Johannes Schindelinc455c872008-07-21 19:03:49 +010089 struct string_list *list = (struct string_list *)cbdata;
Julian Phillips78a395d2010-06-26 00:41:35 +010090 string_list_insert(list, refname);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -080091 return 0;
92}
93
94/*
95 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
96 * and
97 * (1) strip "^{}" at the end of line if any;
98 * (2) ignore if match is provided and does not head-match refname;
99 * (3) warn if refname is not a well-formed refname and skip;
100 * (4) ignore if refname is a ref that exists in the local repository;
101 * (5) otherwise output the line.
102 */
103static int exclude_existing(const char *match)
104{
Michael Haggerty66ce0362013-05-25 11:08:22 +0200105 static struct string_list existing_refs = STRING_LIST_INIT_DUP;
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800106 char buf[1024];
107 int matchlen = match ? strlen(match) : 0;
108
Michael Haggertyf0a011f2015-05-25 18:38:51 +0000109 for_each_ref(add_existing, &existing_refs);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800110 while (fgets(buf, sizeof(buf), stdin)) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800111 char *ref;
Junio C Hamanod8285af2006-12-18 13:33:47 -0800112 int len = strlen(buf);
113
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800114 if (len > 0 && buf[len - 1] == '\n')
115 buf[--len] = '\0';
Junio C Hamanod8285af2006-12-18 13:33:47 -0800116 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800117 len -= 3;
118 buf[len] = '\0';
119 }
120 for (ref = buf + len; buf < ref; ref--)
121 if (isspace(ref[-1]))
122 break;
123 if (match) {
124 int reflen = buf + len - ref;
125 if (reflen < matchlen)
126 continue;
127 if (strncmp(ref, match, matchlen))
128 continue;
129 }
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200130 if (check_refname_format(ref, 0)) {
Miklos Vajna5620e772009-03-24 02:09:16 +0100131 warning("ref '%s' ignored", ref);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800132 continue;
133 }
Johannes Schindelinc455c872008-07-21 19:03:49 +0100134 if (!string_list_has_string(&existing_refs, ref)) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800135 printf("%s\n", buf);
136 }
137 }
138 return 0;
139}
140
Stephen Boyd69932bc2009-06-20 21:40:46 -0700141static int hash_callback(const struct option *opt, const char *arg, int unset)
142{
143 hash_only = 1;
144 /* Use full length SHA1 if no argument */
145 if (!arg)
146 return 0;
147 return parse_opt_abbrev_cb(opt, arg, unset);
148}
149
150static int exclude_existing_callback(const struct option *opt, const char *arg,
151 int unset)
152{
153 exclude_arg = 1;
154 *(const char **)opt->value = arg;
155 return 0;
156}
157
Stephen Boyd69932bc2009-06-20 21:40:46 -0700158static const struct option show_ref_options[] = {
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200159 OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
160 OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
161 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
Nguyễn Thái Ngọc Duyc9120b12012-08-20 19:32:45 +0700162 "requires exact ref path")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200163 OPT_HIDDEN_BOOL('h', NULL, &show_head,
164 N_("show the HEAD reference, even if it would be filtered out")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200165 OPT_BOOL(0, "head", &show_head,
Doug Bell3f3d0ce2013-07-16 19:05:14 -0500166 N_("show the HEAD reference, even if it would be filtered out")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200167 OPT_BOOL('d', "dereference", &deref_tags,
Nguyễn Thái Ngọc Duyc9120b12012-08-20 19:32:45 +0700168 N_("dereference tags into object IDs")),
169 { OPTION_CALLBACK, 's', "hash", &abbrev, N_("n"),
170 N_("only show SHA1 hash using <n> digits"),
Stephen Boyd69932bc2009-06-20 21:40:46 -0700171 PARSE_OPT_OPTARG, &hash_callback },
172 OPT__ABBREV(&abbrev),
Jonathan Nieder8c839682010-11-08 13:54:48 -0600173 OPT__QUIET(&quiet,
Nguyễn Thái Ngọc Duyc9120b12012-08-20 19:32:45 +0700174 N_("do not print results to stdout (useful with --verify)")),
Stephen Boyd69932bc2009-06-20 21:40:46 -0700175 { OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
Nguyễn Thái Ngọc Duyc9120b12012-08-20 19:32:45 +0700176 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
Stephen Boyd69932bc2009-06-20 21:40:46 -0700177 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
Stephen Boyd69932bc2009-06-20 21:40:46 -0700178 OPT_END()
179};
180
Linus Torvalds358ddb62006-09-15 11:19:32 -0700181int cmd_show_ref(int argc, const char **argv, const char *prefix)
182{
Stephen Boyd69932bc2009-06-20 21:40:46 -0700183 argc = parse_options(argc, argv, prefix, show_ref_options,
René Scharfe42fdf862015-11-17 11:26:05 +0100184 show_ref_usage, 0);
Linus Torvalds358ddb62006-09-15 11:19:32 -0700185
Stephen Boyd69932bc2009-06-20 21:40:46 -0700186 if (exclude_arg)
187 return exclude_existing(exclude_existing_arg);
188
189 pattern = argv;
190 if (!*pattern)
191 pattern = NULL;
Junio C Hamano26cdd1e2006-12-17 18:08:52 -0800192
193 if (verify) {
Dmitry V. Levin8ab40a22007-02-23 20:12:33 +0300194 if (!pattern)
195 die("--verify requires a reference");
Junio C Hamano26cdd1e2006-12-17 18:08:52 -0800196 while (*pattern) {
Michael Haggertyf0a011f2015-05-25 18:38:51 +0000197 struct object_id oid;
Dmitry V. Levin8ab40a22007-02-23 20:12:33 +0300198
Vladimir Panteleevec7c51b2017-01-23 18:00:55 +0000199 if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) &&
brian m. carlson34c290a2017-10-15 22:06:56 +0000200 !read_ref(*pattern, &oid)) {
Vladimir Panteleev14144d32017-01-23 18:00:57 +0000201 show_one(*pattern, &oid);
Junio C Hamanodd914292006-12-17 18:53:24 -0800202 }
Junio C Hamano26cdd1e2006-12-17 18:08:52 -0800203 else if (!quiet)
204 die("'%s' - not a valid ref", *pattern);
205 else
206 return 1;
207 pattern++;
208 }
209 return 0;
210 }
211
Linus Torvalds358ddb62006-09-15 11:19:32 -0700212 if (show_head)
Michael Haggertyf0a011f2015-05-25 18:38:51 +0000213 head_ref(show_ref, NULL);
214 for_each_ref(show_ref, NULL);
Linus Torvalds358ddb62006-09-15 11:19:32 -0700215 if (!found_match) {
216 if (verify && !quiet)
217 die("No match");
218 return 1;
219 }
220 return 0;
221}