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"
14#include "parse-options.h"
15#include "diff.h"
16#include "revision.h"
17#include "gpg-interface.h"
18#include "sha1-array.h"
19#include "column.h"
20#include "ref-filter.h"
21
22static const char * const git_tag_usage[] = {
23 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
24 N_("git tag -d <tagname>..."),
25 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
26 "\n\t\t[<pattern>...]"),
27 N_("git tag -v <tagname>..."),
28 NULL
29};
30
31static unsigned int colopts;
32
33static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
34{
35 struct ref_array array;
36 char *format, *to_free = NULL;
37 int i;
38
39 memset(&array, 0, sizeof(array));
40
41 if (filter->lines == -1)
42 filter->lines = 0;
43
44 if (filter->lines) {
45 to_free = xstrfmt("%s %%(contents:lines=%d)",
46 "%(align:15)%(refname:short)%(end)", filter->lines);
47 format = to_free;
48 } else
49 format = "%(refname:short)";
50
51 verify_ref_format(format);
52 filter_refs(&array, filter, FILTER_REFS_TAGS);
53 ref_array_sort(sorting, &array);
54
55 for (i = 0; i < array.nr; i++)
56 show_ref_array_item(array.items[i], format, 0);
57 ref_array_clear(&array);
58 free(to_free);
59
60 return 0;
61}
62
63typedef int (*each_tag_name_fn)(const char *name, const char *ref,
64 const unsigned char *sha1);
65
66static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
67{
68 const char **p;
69 char ref[PATH_MAX];
70 int had_error = 0;
71 unsigned char sha1[20];
72
73 for (p = argv; *p; p++) {
74 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
75 >= sizeof(ref)) {
76 error(_("tag name too long: %.*s..."), 50, *p);
77 had_error = 1;
78 continue;
79 }
80 if (read_ref(ref, sha1)) {
81 error(_("tag '%s' not found."), *p);
82 had_error = 1;
83 continue;
84 }
85 if (fn(*p, ref, sha1))
86 had_error = 1;
87 }
88 return had_error;
89}
90
91static int delete_tag(const char *name, const char *ref,
92 const unsigned char *sha1)
93{
94 if (delete_ref(ref, sha1, 0))
95 return 1;
96 printf(_("Deleted tag '%s' (was %s)\n"), name, find_unique_abbrev(sha1, DEFAULT_ABBREV));
97 return 0;
98}
99
100static int verify_tag(const char *name, const char *ref,
101 const unsigned char *sha1)
102{
103 const char *argv_verify_tag[] = {"verify-tag",
104 "-v", "SHA1_HEX", NULL};
105 argv_verify_tag[2] = sha1_to_hex(sha1);
106
107 if (run_command_v_opt(argv_verify_tag, RUN_GIT_CMD))
108 return error(_("could not verify the tag '%s'"), name);
109 return 0;
110}
111
112static int do_sign(struct strbuf *buffer)
113{
114 return sign_buffer(buffer, buffer, get_signing_key());
115}
116
117static const char tag_template[] =
118 N_("\nWrite a message for tag:\n %s\n"
119 "Lines starting with '%c' will be ignored.\n");
120
121static const char tag_template_nocleanup[] =
122 N_("\nWrite a message for tag:\n %s\n"
123 "Lines starting with '%c' will be kept; you may remove them"
124 " yourself if you want to.\n");
125
126/* Parse arg given and add it the ref_sorting array */
127static int parse_sorting_string(const char *arg, struct ref_sorting **sorting_tail)
128{
129 struct ref_sorting *s;
130 int len;
131
132 s = xcalloc(1, sizeof(*s));
133 s->next = *sorting_tail;
134 *sorting_tail = s;
135
136 if (*arg == '-') {
137 s->reverse = 1;
138 arg++;
139 }
140 if (skip_prefix(arg, "version:", &arg) ||
141 skip_prefix(arg, "v:", &arg))
142 s->version = 1;
143
144 len = strlen(arg);
145 s->atom = parse_ref_filter_atom(arg, arg+len);
146
147 return 0;
148}
149
150static int git_tag_config(const char *var, const char *value, void *cb)
151{
152 int status;
153 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
154
155 if (!strcmp(var, "tag.sort")) {
156 if (!value)
157 return config_error_nonbool(var);
158 parse_sorting_string(value, sorting_tail);
159 return 0;
160 }
161
162 status = git_gpg_config(var, value, cb);
163 if (status)
164 return status;
165 if (starts_with(var, "column."))
166 return git_column_config(var, value, "tag", &colopts);
167 return git_default_config(var, value, cb);
168}
169
170static void write_tag_body(int fd, const unsigned char *sha1)
171{
172 unsigned long size;
173 enum object_type type;
174 char *buf, *sp;
175
176 buf = read_sha1_file(sha1, &type, &size);
177 if (!buf)
178 return;
179 /* skip header */
180 sp = strstr(buf, "\n\n");
181
182 if (!sp || !size || type != OBJ_TAG) {
183 free(buf);
184 return;
185 }
186 sp += 2; /* skip the 2 LFs */
187 write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
188
189 free(buf);
190}
191
192static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
193{
194 if (sign && do_sign(buf) < 0)
195 return error(_("unable to sign the tag"));
196 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
197 return error(_("unable to write tag file"));
198 return 0;
199}
200
201struct create_tag_options {
202 unsigned int message_given:1;
203 unsigned int sign;
204 enum {
205 CLEANUP_NONE,
206 CLEANUP_SPACE,
207 CLEANUP_ALL
208 } cleanup_mode;
209};
210
211static void create_tag(const unsigned char *object, const char *tag,
212 struct strbuf *buf, struct create_tag_options *opt,
213 unsigned char *prev, unsigned char *result)
214{
215 enum object_type type;
216 char header_buf[1024];
217 int header_len;
218 char *path = NULL;
219
220 type = sha1_object_info(object, NULL);
221 if (type <= OBJ_NONE)
222 die(_("bad object type."));
223
224 header_len = snprintf(header_buf, sizeof(header_buf),
225 "object %s\n"
226 "type %s\n"
227 "tag %s\n"
228 "tagger %s\n\n",
229 sha1_to_hex(object),
230 typename(type),
231 tag,
232 git_committer_info(IDENT_STRICT));
233
234 if (header_len > sizeof(header_buf) - 1)
235 die(_("tag header too big."));
236
237 if (!opt->message_given) {
238 int fd;
239
240 /* write the template message before editing: */
241 path = git_pathdup("TAG_EDITMSG");
242 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
243 if (fd < 0)
244 die_errno(_("could not create file '%s'"), path);
245
246 if (!is_null_sha1(prev)) {
247 write_tag_body(fd, prev);
248 } else {
249 struct strbuf buf = STRBUF_INIT;
250 strbuf_addch(&buf, '\n');
251 if (opt->cleanup_mode == CLEANUP_ALL)
252 strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char);
253 else
254 strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char);
255 write_or_die(fd, buf.buf, buf.len);
256 strbuf_release(&buf);
257 }
258 close(fd);
259
260 if (launch_editor(path, buf, NULL)) {
261 fprintf(stderr,
262 _("Please supply the message using either -m or -F option.\n"));
263 exit(1);
264 }
265 }
266
267 if (opt->cleanup_mode != CLEANUP_NONE)
268 stripspace(buf, opt->cleanup_mode == CLEANUP_ALL);
269
270 if (!opt->message_given && !buf->len)
271 die(_("no tag message?"));
272
273 strbuf_insert(buf, 0, header_buf, header_len);
274
275 if (build_tag_object(buf, opt->sign, result) < 0) {
276 if (path)
277 fprintf(stderr, _("The tag message has been left in %s\n"),
278 path);
279 exit(128);
280 }
281 if (path) {
282 unlink_or_warn(path);
283 free(path);
284 }
285}
286
287struct msg_arg {
288 int given;
289 struct strbuf buf;
290};
291
292static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
293{
294 struct msg_arg *msg = opt->value;
295
296 if (!arg)
297 return -1;
298 if (msg->buf.len)
299 strbuf_addstr(&(msg->buf), "\n\n");
300 strbuf_addstr(&(msg->buf), arg);
301 msg->given = 1;
302 return 0;
303}
304
305static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
306{
307 if (name[0] == '-')
308 return -1;
309
310 strbuf_reset(sb);
311 strbuf_addf(sb, "refs/tags/%s", name);
312
313 return check_refname_format(sb->buf, 0);
314}
315
316int cmd_tag(int argc, const char **argv, const char *prefix)
317{
318 struct strbuf buf = STRBUF_INIT;
319 struct strbuf ref = STRBUF_INIT;
320 unsigned char object[20], prev[20];
321 const char *object_ref, *tag;
322 struct create_tag_options opt;
323 char *cleanup_arg = NULL;
324 int create_reflog = 0;
325 int annotate = 0, force = 0;
326 int cmdmode = 0;
327 const char *msgfile = NULL, *keyid = NULL;
328 struct msg_arg msg = { 0, STRBUF_INIT };
329 struct ref_transaction *transaction;
330 struct strbuf err = STRBUF_INIT;
331 struct ref_filter filter;
332 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
333 struct option options[] = {
334 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
335 { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
336 N_("print <n> lines of each tag message"),
337 PARSE_OPT_OPTARG, NULL, 1 },
338 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
339 OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'),
340
341 OPT_GROUP(N_("Tag creation options")),
342 OPT_BOOL('a', "annotate", &annotate,
343 N_("annotated tag, needs a message")),
344 OPT_CALLBACK('m', "message", &msg, N_("message"),
345 N_("tag message"), parse_msg_arg),
346 OPT_FILENAME('F', "file", &msgfile, N_("read message from file")),
347 OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")),
348 OPT_STRING(0, "cleanup", &cleanup_arg, N_("mode"),
349 N_("how to strip spaces and #comments from message")),
350 OPT_STRING('u', "local-user", &keyid, N_("key-id"),
351 N_("use another key to sign the tag")),
352 OPT__FORCE(&force, N_("replace the tag if exists")),
353 OPT_BOOL(0, "create-reflog", &create_reflog, N_("create_reflog")),
354
355 OPT_GROUP(N_("Tag listing options")),
356 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
357 OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
358 OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
359 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
360 N_("field name to sort on"), &parse_opt_ref_sorting),
361 {
362 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
363 N_("print only tags of the object"), 0, parse_opt_object_name
364 },
365 OPT_END()
366 };
367
368 git_config(git_tag_config, sorting_tail);
369
370 memset(&opt, 0, sizeof(opt));
371 memset(&filter, 0, sizeof(filter));
372 filter.lines = -1;
373
374 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
375
376 if (keyid) {
377 opt.sign = 1;
378 set_signing_key(keyid);
379 }
380 if (opt.sign)
381 annotate = 1;
382 if (argc == 0 && !cmdmode)
383 cmdmode = 'l';
384
385 if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
386 usage_with_options(git_tag_usage, options);
387
388 finalize_colopts(&colopts, -1);
389 if (cmdmode == 'l' && filter.lines != -1) {
390 if (explicitly_enable_column(colopts))
391 die(_("--column and -n are incompatible"));
392 colopts = 0;
393 }
394 if (!sorting)
395 sorting = ref_default_sorting();
396 if (cmdmode == 'l') {
397 int ret;
398 if (column_active(colopts)) {
399 struct column_options copts;
400 memset(&copts, 0, sizeof(copts));
401 copts.padding = 2;
402 run_column_filter(colopts, &copts);
403 }
404 filter.name_patterns = argv;
405 ret = list_tags(&filter, sorting);
406 if (column_active(colopts))
407 stop_column_filter();
408 return ret;
409 }
410 if (filter.lines != -1)
411 die(_("-n option is only allowed with -l."));
412 if (filter.with_commit)
413 die(_("--contains option is only allowed with -l."));
414 if (filter.points_at.nr)
415 die(_("--points-at option is only allowed with -l."));
416 if (cmdmode == 'd')
417 return for_each_tag_name(argv, delete_tag);
418 if (cmdmode == 'v')
419 return for_each_tag_name(argv, verify_tag);
420
421 if (msg.given || msgfile) {
422 if (msg.given && msgfile)
423 die(_("only one -F or -m option is allowed."));
424 annotate = 1;
425 if (msg.given)
426 strbuf_addbuf(&buf, &(msg.buf));
427 else {
428 if (!strcmp(msgfile, "-")) {
429 if (strbuf_read(&buf, 0, 1024) < 0)
430 die_errno(_("cannot read '%s'"), msgfile);
431 } else {
432 if (strbuf_read_file(&buf, msgfile, 1024) < 0)
433 die_errno(_("could not open or read '%s'"),
434 msgfile);
435 }
436 }
437 }
438
439 tag = argv[0];
440
441 object_ref = argc == 2 ? argv[1] : "HEAD";
442 if (argc > 2)
443 die(_("too many params"));
444
445 if (get_sha1(object_ref, object))
446 die(_("Failed to resolve '%s' as a valid ref."), object_ref);
447
448 if (strbuf_check_tag_ref(&ref, tag))
449 die(_("'%s' is not a valid tag name."), tag);
450
451 if (read_ref(ref.buf, prev))
452 hashclr(prev);
453 else if (!force)
454 die(_("tag '%s' already exists"), tag);
455
456 opt.message_given = msg.given || msgfile;
457
458 if (!cleanup_arg || !strcmp(cleanup_arg, "strip"))
459 opt.cleanup_mode = CLEANUP_ALL;
460 else if (!strcmp(cleanup_arg, "verbatim"))
461 opt.cleanup_mode = CLEANUP_NONE;
462 else if (!strcmp(cleanup_arg, "whitespace"))
463 opt.cleanup_mode = CLEANUP_SPACE;
464 else
465 die(_("Invalid cleanup mode %s"), cleanup_arg);
466
467 if (annotate)
468 create_tag(object, tag, &buf, &opt, prev, object);
469
470 transaction = ref_transaction_begin(&err);
471 if (!transaction ||
472 ref_transaction_update(transaction, ref.buf, object, prev,
473 create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
474 NULL, &err) ||
475 ref_transaction_commit(transaction, &err))
476 die("%s", err.buf);
477 ref_transaction_free(transaction);
478 if (force && !is_null_sha1(prev) && hashcmp(prev, object))
479 printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
480
481 strbuf_release(&err);
482 strbuf_release(&buf);
483 strbuf_release(&ref);
484 return 0;
485}