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