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