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