]>
Commit | Line | Data |
---|---|---|
62e09ce9 CR |
1 | /* |
2 | * Builtin "git tag" | |
3 | * | |
4 | * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>, | |
5 | * Carlos Rica <jasampler@gmail.com> | |
6 | * Based on git-tag.sh and mktag.c by Linus Torvalds. | |
7 | */ | |
8 | ||
9 | #include "cache.h" | |
10 | #include "builtin.h" | |
11 | #include "refs.h" | |
12 | #include "tag.h" | |
13 | #include "run-command.h" | |
39686585 CR |
14 | #include "parse-options.h" |
15 | ||
16 | static const char * const git_tag_usage[] = { | |
1b1dd23f SB |
17 | "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]", |
18 | "git tag -d <tagname>...", | |
19 | "git tag -l [-n[<num>]] [<pattern>]", | |
20 | "git tag -v <tagname>...", | |
39686585 CR |
21 | NULL |
22 | }; | |
62e09ce9 CR |
23 | |
24 | static char signingkey[1000]; | |
25 | ||
62e09ce9 CR |
26 | struct tag_filter { |
27 | const char *pattern; | |
28 | int lines; | |
32c35cfb | 29 | struct commit_list *with_commit; |
62e09ce9 CR |
30 | }; |
31 | ||
62e09ce9 CR |
32 | static int show_reference(const char *refname, const unsigned char *sha1, |
33 | int flag, void *cb_data) | |
34 | { | |
35 | struct tag_filter *filter = cb_data; | |
36 | ||
37 | if (!fnmatch(filter->pattern, refname, 0)) { | |
38 | int i; | |
39 | unsigned long size; | |
40 | enum object_type type; | |
41 | char *buf, *sp, *eol; | |
42 | size_t len; | |
43 | ||
32c35cfb JG |
44 | if (filter->with_commit) { |
45 | struct commit *commit; | |
46 | ||
47 | commit = lookup_commit_reference_gently(sha1, 1); | |
48 | if (!commit) | |
49 | return 0; | |
50 | if (!is_descendant_of(commit, filter->with_commit)) | |
51 | return 0; | |
52 | } | |
53 | ||
62e09ce9 CR |
54 | if (!filter->lines) { |
55 | printf("%s\n", refname); | |
56 | return 0; | |
57 | } | |
58 | printf("%-15s ", refname); | |
59 | ||
e1f14cce MH |
60 | buf = read_sha1_file(sha1, &type, &size); |
61 | if (!buf || !size) | |
62e09ce9 | 62 | return 0; |
e1f14cce MH |
63 | |
64 | /* skip header */ | |
65 | sp = strstr(buf, "\n\n"); | |
66 | if (!sp) { | |
e317cfaf CR |
67 | free(buf); |
68 | return 0; | |
69 | } | |
62e09ce9 | 70 | /* only take up to "lines" lines, and strip the signature */ |
81536b2d | 71 | size = parse_signature(buf, size); |
e317cfaf | 72 | for (i = 0, sp += 2; |
81536b2d | 73 | i < filter->lines && sp < buf + size; |
62e09ce9 CR |
74 | i++) { |
75 | if (i) | |
76 | printf("\n "); | |
77 | eol = memchr(sp, '\n', size - (sp - buf)); | |
78 | len = eol ? eol - sp : size - (sp - buf); | |
79 | fwrite(sp, len, 1, stdout); | |
80 | if (!eol) | |
81 | break; | |
82 | sp = eol + 1; | |
83 | } | |
84 | putchar('\n'); | |
85 | free(buf); | |
86 | } | |
87 | ||
88 | return 0; | |
89 | } | |
90 | ||
32c35cfb JG |
91 | static int list_tags(const char *pattern, int lines, |
92 | struct commit_list *with_commit) | |
62e09ce9 CR |
93 | { |
94 | struct tag_filter filter; | |
62e09ce9 CR |
95 | |
96 | if (pattern == NULL) | |
18e32b5b | 97 | pattern = "*"; |
62e09ce9 | 98 | |
18e32b5b | 99 | filter.pattern = pattern; |
62e09ce9 | 100 | filter.lines = lines; |
32c35cfb | 101 | filter.with_commit = with_commit; |
62e09ce9 CR |
102 | |
103 | for_each_tag_ref(show_reference, (void *) &filter); | |
104 | ||
62e09ce9 CR |
105 | return 0; |
106 | } | |
107 | ||
e317cfaf | 108 | typedef int (*each_tag_name_fn)(const char *name, const char *ref, |
62e09ce9 CR |
109 | const unsigned char *sha1); |
110 | ||
e317cfaf | 111 | static int for_each_tag_name(const char **argv, each_tag_name_fn fn) |
62e09ce9 CR |
112 | { |
113 | const char **p; | |
114 | char ref[PATH_MAX]; | |
115 | int had_error = 0; | |
116 | unsigned char sha1[20]; | |
117 | ||
118 | for (p = argv; *p; p++) { | |
119 | if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p) | |
120 | >= sizeof(ref)) { | |
121 | error("tag name too long: %.*s...", 50, *p); | |
122 | had_error = 1; | |
123 | continue; | |
124 | } | |
125 | if (!resolve_ref(ref, sha1, 1, NULL)) { | |
126 | error("tag '%s' not found.", *p); | |
127 | had_error = 1; | |
128 | continue; | |
129 | } | |
130 | if (fn(*p, ref, sha1)) | |
131 | had_error = 1; | |
132 | } | |
133 | return had_error; | |
134 | } | |
135 | ||
136 | static int delete_tag(const char *name, const char *ref, | |
137 | const unsigned char *sha1) | |
138 | { | |
eca35a25 | 139 | if (delete_ref(ref, sha1, 0)) |
62e09ce9 | 140 | return 1; |
0a043b1f | 141 | printf("Deleted tag '%s' (was %s)\n", name, find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
62e09ce9 CR |
142 | return 0; |
143 | } | |
144 | ||
145 | static int verify_tag(const char *name, const char *ref, | |
146 | const unsigned char *sha1) | |
147 | { | |
a6ccbbdb | 148 | const char *argv_verify_tag[] = {"verify-tag", |
62e09ce9 CR |
149 | "-v", "SHA1_HEX", NULL}; |
150 | argv_verify_tag[2] = sha1_to_hex(sha1); | |
151 | ||
a6ccbbdb | 152 | if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD)) |
62e09ce9 CR |
153 | return error("could not verify the tag '%s'", name); |
154 | return 0; | |
155 | } | |
156 | ||
fd17f5b5 | 157 | static int do_sign(struct strbuf *buffer) |
62e09ce9 CR |
158 | { |
159 | struct child_process gpg; | |
160 | const char *args[4]; | |
161 | char *bracket; | |
162 | int len; | |
c4adea82 | 163 | int i, j; |
62e09ce9 CR |
164 | |
165 | if (!*signingkey) { | |
774751a8 | 166 | if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME), |
e317cfaf | 167 | sizeof(signingkey)) > sizeof(signingkey) - 1) |
62e09ce9 CR |
168 | return error("committer info too long."); |
169 | bracket = strchr(signingkey, '>'); | |
170 | if (bracket) | |
171 | bracket[1] = '\0'; | |
172 | } | |
173 | ||
aba91192 CR |
174 | /* When the username signingkey is bad, program could be terminated |
175 | * because gpg exits without reading and then write gets SIGPIPE. */ | |
176 | signal(SIGPIPE, SIG_IGN); | |
177 | ||
62e09ce9 CR |
178 | memset(&gpg, 0, sizeof(gpg)); |
179 | gpg.argv = args; | |
180 | gpg.in = -1; | |
181 | gpg.out = -1; | |
182 | args[0] = "gpg"; | |
183 | args[1] = "-bsau"; | |
184 | args[2] = signingkey; | |
185 | args[3] = NULL; | |
186 | ||
187 | if (start_command(&gpg)) | |
188 | return error("could not run gpg."); | |
189 | ||
fd17f5b5 | 190 | if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) { |
aba91192 | 191 | close(gpg.in); |
e72ae288 | 192 | close(gpg.out); |
aba91192 CR |
193 | finish_command(&gpg); |
194 | return error("gpg did not accept the tag data"); | |
195 | } | |
62e09ce9 | 196 | close(gpg.in); |
fd17f5b5 | 197 | len = strbuf_read(buffer, gpg.out, 1024); |
e72ae288 | 198 | close(gpg.out); |
62e09ce9 | 199 | |
aba91192 CR |
200 | if (finish_command(&gpg) || !len || len < 0) |
201 | return error("gpg failed to sign the tag"); | |
62e09ce9 | 202 | |
c4adea82 JS |
203 | /* Strip CR from the line endings, in case we are on Windows. */ |
204 | for (i = j = 0; i < buffer->len; i++) | |
205 | if (buffer->buf[i] != '\r') { | |
206 | if (i != j) | |
207 | buffer->buf[j] = buffer->buf[i]; | |
208 | j++; | |
209 | } | |
210 | strbuf_setlen(buffer, j); | |
211 | ||
fd17f5b5 | 212 | return 0; |
62e09ce9 CR |
213 | } |
214 | ||
215 | static const char tag_template[] = | |
216 | "\n" | |
217 | "#\n" | |
218 | "# Write a tag message\n" | |
219 | "#\n"; | |
220 | ||
be15f505 LT |
221 | static void set_signingkey(const char *value) |
222 | { | |
223 | if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey)) | |
224 | die("signing key value too long (%.10s...)", value); | |
225 | } | |
226 | ||
ef90d6d4 | 227 | static int git_tag_config(const char *var, const char *value, void *cb) |
62e09ce9 CR |
228 | { |
229 | if (!strcmp(var, "user.signingkey")) { | |
230 | if (!value) | |
c96a6d36 | 231 | return config_error_nonbool(var); |
be15f505 | 232 | set_signingkey(value); |
62e09ce9 CR |
233 | return 0; |
234 | } | |
235 | ||
ef90d6d4 | 236 | return git_default_config(var, value, cb); |
62e09ce9 CR |
237 | } |
238 | ||
bab8118a MH |
239 | static void write_tag_body(int fd, const unsigned char *sha1) |
240 | { | |
241 | unsigned long size; | |
242 | enum object_type type; | |
e10dfb62 | 243 | char *buf, *sp; |
bab8118a MH |
244 | |
245 | buf = read_sha1_file(sha1, &type, &size); | |
246 | if (!buf) | |
247 | return; | |
248 | /* skip header */ | |
249 | sp = strstr(buf, "\n\n"); | |
250 | ||
251 | if (!sp || !size || type != OBJ_TAG) { | |
252 | free(buf); | |
253 | return; | |
254 | } | |
255 | sp += 2; /* skip the 2 LFs */ | |
e10dfb62 | 256 | write_or_die(fd, sp, parse_signature(sp, buf + size - sp)); |
bab8118a MH |
257 | |
258 | free(buf); | |
259 | } | |
260 | ||
3927bbe9 JK |
261 | static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result) |
262 | { | |
263 | if (sign && do_sign(buf) < 0) | |
264 | return error("unable to sign the tag"); | |
265 | if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0) | |
266 | return error("unable to write tag file"); | |
267 | return 0; | |
268 | } | |
269 | ||
62e09ce9 | 270 | static void create_tag(const unsigned char *object, const char *tag, |
fd17f5b5 | 271 | struct strbuf *buf, int message, int sign, |
bd46c9a9 | 272 | unsigned char *prev, unsigned char *result) |
62e09ce9 CR |
273 | { |
274 | enum object_type type; | |
fd17f5b5 PH |
275 | char header_buf[1024]; |
276 | int header_len; | |
3927bbe9 | 277 | char *path = NULL; |
62e09ce9 CR |
278 | |
279 | type = sha1_object_info(object, NULL); | |
e317cfaf | 280 | if (type <= OBJ_NONE) |
62e09ce9 CR |
281 | die("bad object type."); |
282 | ||
283 | header_len = snprintf(header_buf, sizeof(header_buf), | |
284 | "object %s\n" | |
285 | "type %s\n" | |
286 | "tag %s\n" | |
287 | "tagger %s\n\n", | |
288 | sha1_to_hex(object), | |
289 | typename(type), | |
290 | tag, | |
774751a8 | 291 | git_committer_info(IDENT_ERROR_ON_NO_NAME)); |
62e09ce9 | 292 | |
e317cfaf | 293 | if (header_len > sizeof(header_buf) - 1) |
62e09ce9 CR |
294 | die("tag header too big."); |
295 | ||
296 | if (!message) { | |
62e09ce9 CR |
297 | int fd; |
298 | ||
299 | /* write the template message before editing: */ | |
a4f34cbb | 300 | path = git_pathdup("TAG_EDITMSG"); |
62e09ce9 CR |
301 | fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
302 | if (fd < 0) | |
d824cbba | 303 | die_errno("could not create file '%s'", path); |
bab8118a MH |
304 | |
305 | if (!is_null_sha1(prev)) | |
306 | write_tag_body(fd, prev); | |
307 | else | |
308 | write_or_die(fd, tag_template, strlen(tag_template)); | |
62e09ce9 CR |
309 | close(fd); |
310 | ||
7198203a SB |
311 | if (launch_editor(path, buf, NULL)) { |
312 | fprintf(stderr, | |
313 | "Please supply the message using either -m or -F option.\n"); | |
314 | exit(1); | |
315 | } | |
62e09ce9 | 316 | } |
62e09ce9 | 317 | |
6d69b6f6 | 318 | stripspace(buf, 1); |
62e09ce9 | 319 | |
fd17f5b5 | 320 | if (!message && !buf->len) |
62e09ce9 CR |
321 | die("no tag message?"); |
322 | ||
fd17f5b5 | 323 | strbuf_insert(buf, 0, header_buf, header_len); |
62e09ce9 | 324 | |
3927bbe9 JK |
325 | if (build_tag_object(buf, sign, result) < 0) { |
326 | if (path) | |
327 | fprintf(stderr, "The tag message has been left in %s\n", | |
328 | path); | |
329 | exit(128); | |
330 | } | |
331 | if (path) { | |
691f1a28 | 332 | unlink_or_warn(path); |
3927bbe9 JK |
333 | free(path); |
334 | } | |
62e09ce9 CR |
335 | } |
336 | ||
bd46c9a9 JH |
337 | struct msg_arg { |
338 | int given; | |
339 | struct strbuf buf; | |
340 | }; | |
341 | ||
342 | static int parse_msg_arg(const struct option *opt, const char *arg, int unset) | |
343 | { | |
344 | struct msg_arg *msg = opt->value; | |
345 | ||
346 | if (!arg) | |
347 | return -1; | |
348 | if (msg->buf.len) | |
349 | strbuf_addstr(&(msg->buf), "\n\n"); | |
350 | strbuf_addstr(&(msg->buf), arg); | |
351 | msg->given = 1; | |
352 | return 0; | |
353 | } | |
354 | ||
62e09ce9 CR |
355 | int cmd_tag(int argc, const char **argv, const char *prefix) |
356 | { | |
f285a2d7 | 357 | struct strbuf buf = STRBUF_INIT; |
62e09ce9 | 358 | unsigned char object[20], prev[20]; |
62e09ce9 CR |
359 | char ref[PATH_MAX]; |
360 | const char *object_ref, *tag; | |
62e09ce9 CR |
361 | struct ref_lock *lock; |
362 | ||
6fa8342b | 363 | int annotate = 0, sign = 0, force = 0, lines = -1, |
78d776a9 | 364 | list = 0, delete = 0, verify = 0; |
dbd0f5c7 | 365 | const char *msgfile = NULL, *keyid = NULL; |
bd46c9a9 | 366 | struct msg_arg msg = { 0, STRBUF_INIT }; |
32c35cfb | 367 | struct commit_list *with_commit = NULL; |
39686585 | 368 | struct option options[] = { |
78d776a9 | 369 | OPT_BOOLEAN('l', NULL, &list, "list tag names"), |
e3a0ca87 SB |
370 | { OPTION_INTEGER, 'n', NULL, &lines, "n", |
371 | "print <n> lines of each tag message", | |
39686585 CR |
372 | PARSE_OPT_OPTARG, NULL, 1 }, |
373 | OPT_BOOLEAN('d', NULL, &delete, "delete tags"), | |
374 | OPT_BOOLEAN('v', NULL, &verify, "verify tags"), | |
375 | ||
376 | OPT_GROUP("Tag creation options"), | |
377 | OPT_BOOLEAN('a', NULL, &annotate, | |
378 | "annotated tag, needs a message"), | |
bd46c9a9 JH |
379 | OPT_CALLBACK('m', NULL, &msg, "msg", |
380 | "message for the tag", parse_msg_arg), | |
df217ed6 | 381 | OPT_FILENAME('F', NULL, &msgfile, "message in a file"), |
39686585 CR |
382 | OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"), |
383 | OPT_STRING('u', NULL, &keyid, "key-id", | |
384 | "use another key to sign the tag"), | |
76946b76 | 385 | OPT__FORCE(&force, "replace the tag if exists"), |
32c35cfb JG |
386 | |
387 | OPT_GROUP("Tag listing options"), | |
388 | { | |
389 | OPTION_CALLBACK, 0, "contains", &with_commit, "commit", | |
390 | "print only tags that contain the commit", | |
391 | PARSE_OPT_LASTARG_DEFAULT, | |
392 | parse_opt_with_commit, (intptr_t)"HEAD", | |
393 | }, | |
39686585 CR |
394 | OPT_END() |
395 | }; | |
396 | ||
ef90d6d4 | 397 | git_config(git_tag_config, NULL); |
62e09ce9 | 398 | |
37782920 | 399 | argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0); |
62e09ce9 | 400 | |
be15f505 LT |
401 | if (keyid) { |
402 | sign = 1; | |
403 | set_signingkey(keyid); | |
404 | } | |
c1a41b9d JH |
405 | if (sign) |
406 | annotate = 1; | |
6fa8342b ST |
407 | if (argc == 0 && !(delete || verify)) |
408 | list = 1; | |
c1a41b9d | 409 | |
6fa8342b ST |
410 | if ((annotate || msg.given || msgfile || force) && |
411 | (list || delete || verify)) | |
412 | usage_with_options(git_tag_usage, options); | |
413 | ||
414 | if (list + delete + verify > 1) | |
415 | usage_with_options(git_tag_usage, options); | |
39686585 | 416 | if (list) |
32c35cfb JG |
417 | return list_tags(argv[0], lines == -1 ? 0 : lines, |
418 | with_commit); | |
6fa8342b ST |
419 | if (lines != -1) |
420 | die("-n option is only allowed with -l."); | |
32c35cfb JG |
421 | if (with_commit) |
422 | die("--contains option is only allowed with -l."); | |
39686585 CR |
423 | if (delete) |
424 | return for_each_tag_name(argv, delete_tag); | |
425 | if (verify) | |
426 | return for_each_tag_name(argv, verify_tag); | |
427 | ||
bd46c9a9 JH |
428 | if (msg.given || msgfile) { |
429 | if (msg.given && msgfile) | |
39686585 CR |
430 | die("only one -F or -m option is allowed."); |
431 | annotate = 1; | |
bd46c9a9 JH |
432 | if (msg.given) |
433 | strbuf_addbuf(&buf, &(msg.buf)); | |
39686585 CR |
434 | else { |
435 | if (!strcmp(msgfile, "-")) { | |
387e7e19 | 436 | if (strbuf_read(&buf, 0, 1024) < 0) |
0721c314 | 437 | die_errno("cannot read '%s'", msgfile); |
387e7e19 | 438 | } else { |
39686585 | 439 | if (strbuf_read_file(&buf, msgfile, 1024) < 0) |
d824cbba TR |
440 | die_errno("could not open or read '%s'", |
441 | msgfile); | |
62e09ce9 | 442 | } |
62e09ce9 | 443 | } |
62e09ce9 CR |
444 | } |
445 | ||
39686585 | 446 | tag = argv[0]; |
62e09ce9 | 447 | |
39686585 CR |
448 | object_ref = argc == 2 ? argv[1] : "HEAD"; |
449 | if (argc > 2) | |
62e09ce9 CR |
450 | die("too many params"); |
451 | ||
452 | if (get_sha1(object_ref, object)) | |
453 | die("Failed to resolve '%s' as a valid ref.", object_ref); | |
454 | ||
e317cfaf | 455 | if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1) |
62e09ce9 CR |
456 | die("tag name too long: %.*s...", 50, tag); |
457 | if (check_ref_format(ref)) | |
458 | die("'%s' is not a valid tag name.", tag); | |
459 | ||
460 | if (!resolve_ref(ref, prev, 1, NULL)) | |
461 | hashclr(prev); | |
462 | else if (!force) | |
463 | die("tag '%s' already exists", tag); | |
464 | ||
465 | if (annotate) | |
bd46c9a9 JH |
466 | create_tag(object, tag, &buf, msg.given || msgfile, |
467 | sign, prev, object); | |
62e09ce9 CR |
468 | |
469 | lock = lock_any_ref_for_update(ref, prev, 0); | |
470 | if (!lock) | |
471 | die("%s: cannot lock the ref", ref); | |
472 | if (write_ref_sha1(lock, object, NULL) < 0) | |
473 | die("%s: cannot update the ref", ref); | |
0a043b1f MG |
474 | if (force && hashcmp(prev, object)) |
475 | printf("Updated tag '%s' (was %s)\n", tag, find_unique_abbrev(prev, DEFAULT_ABBREV)); | |
62e09ce9 | 476 | |
fd17f5b5 | 477 | strbuf_release(&buf); |
62e09ce9 CR |
478 | return 0; |
479 | } |