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