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.
13 #include "run-command.h"
14 #include "parse-options.h"
17 #include "gpg-interface.h"
18 #include "sha1-array.h"
21 static const char * const git_tag_usage
[] = {
22 N_("git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]"),
23 N_("git tag -d <tagname>..."),
24 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] "
25 "\n\t\t[<pattern>...]"),
26 N_("git tag -v <tagname>..."),
30 #define STRCMP_SORT 0 /* must be zero */
32 #define SORT_MASK 0x7fff
33 #define REVERSE_SORT 0x8000
36 const char **patterns
;
39 struct string_list tags
;
40 struct commit_list
*with_commit
;
43 static struct sha1_array points_at
;
44 static unsigned int colopts
;
46 static int match_pattern(const char **patterns
, const char *ref
)
48 /* no pattern means match everything */
51 for (; *patterns
; patterns
++)
52 if (!wildmatch(*patterns
, ref
, 0, NULL
))
57 static const unsigned char *match_points_at(const char *refname
,
58 const unsigned char *sha1
)
60 const unsigned char *tagged_sha1
= NULL
;
63 if (sha1_array_lookup(&points_at
, sha1
) >= 0)
65 obj
= parse_object(sha1
);
67 die(_("malformed object at '%s'"), refname
);
68 if (obj
->type
== OBJ_TAG
)
69 tagged_sha1
= ((struct tag
*)obj
)->tagged
->sha1
;
70 if (tagged_sha1
&& sha1_array_lookup(&points_at
, tagged_sha1
) >= 0)
75 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
77 for (; want
; want
= want
->next
)
78 if (!hashcmp(want
->item
->object
.sha1
, c
->object
.sha1
))
83 enum contains_result
{
84 CONTAINS_UNKNOWN
= -1,
90 * Test whether the candidate or one of its parents is contained in the list.
91 * Do not recurse to find out, though, but return -1 if inconclusive.
93 static enum contains_result
contains_test(struct commit
*candidate
,
94 const struct commit_list
*want
)
96 /* was it previously marked as containing a want commit? */
97 if (candidate
->object
.flags
& TMP_MARK
)
99 /* or marked as not possibly containing a want commit? */
100 if (candidate
->object
.flags
& UNINTERESTING
)
103 if (in_commit_list(want
, candidate
)) {
104 candidate
->object
.flags
|= TMP_MARK
;
108 if (parse_commit(candidate
) < 0)
115 * Mimicking the real stack, this stack lives on the heap, avoiding stack
118 * At each recursion step, the stack items points to the commits whose
119 * ancestors are to be inspected.
124 struct commit
*commit
;
125 struct commit_list
*parents
;
129 static void push_to_stack(struct commit
*candidate
, struct stack
*stack
)
131 int index
= stack
->nr
++;
132 ALLOC_GROW(stack
->stack
, stack
->nr
, stack
->alloc
);
133 stack
->stack
[index
].commit
= candidate
;
134 stack
->stack
[index
].parents
= candidate
->parents
;
137 static enum contains_result
contains(struct commit
*candidate
,
138 const struct commit_list
*want
)
140 struct stack stack
= { 0, 0, NULL
};
141 int result
= contains_test(candidate
, want
);
143 if (result
!= CONTAINS_UNKNOWN
)
146 push_to_stack(candidate
, &stack
);
148 struct stack_entry
*entry
= &stack
.stack
[stack
.nr
- 1];
149 struct commit
*commit
= entry
->commit
;
150 struct commit_list
*parents
= entry
->parents
;
153 commit
->object
.flags
|= UNINTERESTING
;
157 * If we just popped the stack, parents->item has been marked,
158 * therefore contains_test will return a meaningful 0 or 1.
160 else switch (contains_test(parents
->item
, want
)) {
162 commit
->object
.flags
|= TMP_MARK
;
166 entry
->parents
= parents
->next
;
168 case CONTAINS_UNKNOWN
:
169 push_to_stack(parents
->item
, &stack
);
174 return contains_test(candidate
, want
);
177 static void show_tag_lines(const unsigned char *sha1
, int lines
)
181 enum object_type type
;
182 char *buf
, *sp
, *eol
;
185 buf
= read_sha1_file(sha1
, &type
, &size
);
187 die_errno("unable to read object %s", sha1_to_hex(sha1
));
188 if (type
!= OBJ_COMMIT
&& type
!= OBJ_TAG
)
191 die("an empty %s object %s?",
192 typename(type
), sha1_to_hex(sha1
));
195 sp
= strstr(buf
, "\n\n");
199 /* only take up to "lines" lines, and strip the signature from a tag */
201 size
= parse_signature(buf
, size
);
202 for (i
= 0, sp
+= 2; i
< lines
&& sp
< buf
+ size
; i
++) {
205 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
206 len
= eol
? eol
- sp
: size
- (sp
- buf
);
207 fwrite(sp
, len
, 1, stdout
);
216 static int show_reference(const char *refname
, const unsigned char *sha1
,
217 int flag
, void *cb_data
)
219 struct tag_filter
*filter
= cb_data
;
221 if (match_pattern(filter
->patterns
, refname
)) {
222 if (filter
->with_commit
) {
223 struct commit
*commit
;
225 commit
= lookup_commit_reference_gently(sha1
, 1);
228 if (!contains(commit
, filter
->with_commit
))
232 if (points_at
.nr
&& !match_points_at(refname
, sha1
))
235 if (!filter
->lines
) {
237 string_list_append(&filter
->tags
, refname
);
239 printf("%s\n", refname
);
242 printf("%-15s ", refname
);
243 show_tag_lines(sha1
, filter
->lines
);
250 static int sort_by_version(const void *a_
, const void *b_
)
252 const struct string_list_item
*a
= a_
;
253 const struct string_list_item
*b
= b_
;
254 return versioncmp(a
->string
, b
->string
);
257 static int list_tags(const char **patterns
, int lines
,
258 struct commit_list
*with_commit
, int sort
)
260 struct tag_filter filter
;
262 filter
.patterns
= patterns
;
263 filter
.lines
= lines
;
265 filter
.with_commit
= with_commit
;
266 memset(&filter
.tags
, 0, sizeof(filter
.tags
));
267 filter
.tags
.strdup_strings
= 1;
269 for_each_tag_ref(show_reference
, (void *) &filter
);
272 if ((sort
& SORT_MASK
) == VERCMP_SORT
)
273 qsort(filter
.tags
.items
, filter
.tags
.nr
,
274 sizeof(struct string_list_item
), sort_by_version
);
275 if (sort
& REVERSE_SORT
)
276 for (i
= filter
.tags
.nr
- 1; i
>= 0; i
--)
277 printf("%s\n", filter
.tags
.items
[i
].string
);
279 for (i
= 0; i
< filter
.tags
.nr
; i
++)
280 printf("%s\n", filter
.tags
.items
[i
].string
);
281 string_list_clear(&filter
.tags
, 0);
286 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
287 const unsigned char *sha1
);
289 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
294 unsigned char sha1
[20];
296 for (p
= argv
; *p
; p
++) {
297 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
299 error(_("tag name too long: %.*s..."), 50, *p
);
303 if (read_ref(ref
, sha1
)) {
304 error(_("tag '%s' not found."), *p
);
308 if (fn(*p
, ref
, sha1
))
314 static int delete_tag(const char *name
, const char *ref
,
315 const unsigned char *sha1
)
317 if (delete_ref(ref
, sha1
, 0))
319 printf(_("Deleted tag '%s' (was %s)\n"), name
, find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
323 static int verify_tag(const char *name
, const char *ref
,
324 const unsigned char *sha1
)
326 const char *argv_verify_tag
[] = {"verify-tag",
327 "-v", "SHA1_HEX", NULL
};
328 argv_verify_tag
[2] = sha1_to_hex(sha1
);
330 if (run_command_v_opt(argv_verify_tag
, RUN_GIT_CMD
))
331 return error(_("could not verify the tag '%s'"), name
);
335 static int do_sign(struct strbuf
*buffer
)
337 return sign_buffer(buffer
, buffer
, get_signing_key());
340 static const char tag_template
[] =
341 N_("\nWrite a message for tag:\n %s\n"
342 "Lines starting with '%c' will be ignored.\n");
344 static const char tag_template_nocleanup
[] =
345 N_("\nWrite a message for tag:\n %s\n"
346 "Lines starting with '%c' will be kept; you may remove them"
347 " yourself if you want to.\n");
349 static int git_tag_config(const char *var
, const char *value
, void *cb
)
351 int status
= git_gpg_config(var
, value
, cb
);
354 if (starts_with(var
, "column."))
355 return git_column_config(var
, value
, "tag", &colopts
);
356 return git_default_config(var
, value
, cb
);
359 static void write_tag_body(int fd
, const unsigned char *sha1
)
362 enum object_type type
;
365 buf
= read_sha1_file(sha1
, &type
, &size
);
369 sp
= strstr(buf
, "\n\n");
371 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
375 sp
+= 2; /* skip the 2 LFs */
376 write_or_die(fd
, sp
, parse_signature(sp
, buf
+ size
- sp
));
381 static int build_tag_object(struct strbuf
*buf
, int sign
, unsigned char *result
)
383 if (sign
&& do_sign(buf
) < 0)
384 return error(_("unable to sign the tag"));
385 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
386 return error(_("unable to write tag file"));
390 struct create_tag_options
{
391 unsigned int message_given
:1;
400 static void create_tag(const unsigned char *object
, const char *tag
,
401 struct strbuf
*buf
, struct create_tag_options
*opt
,
402 unsigned char *prev
, unsigned char *result
)
404 enum object_type type
;
405 char header_buf
[1024];
409 type
= sha1_object_info(object
, NULL
);
410 if (type
<= OBJ_NONE
)
411 die(_("bad object type."));
413 header_len
= snprintf(header_buf
, sizeof(header_buf
),
421 git_committer_info(IDENT_STRICT
));
423 if (header_len
> sizeof(header_buf
) - 1)
424 die(_("tag header too big."));
426 if (!opt
->message_given
) {
429 /* write the template message before editing: */
430 path
= git_pathdup("TAG_EDITMSG");
431 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
433 die_errno(_("could not create file '%s'"), path
);
435 if (!is_null_sha1(prev
)) {
436 write_tag_body(fd
, prev
);
438 struct strbuf buf
= STRBUF_INIT
;
439 strbuf_addch(&buf
, '\n');
440 if (opt
->cleanup_mode
== CLEANUP_ALL
)
441 strbuf_commented_addf(&buf
, _(tag_template
), tag
, comment_line_char
);
443 strbuf_commented_addf(&buf
, _(tag_template_nocleanup
), tag
, comment_line_char
);
444 write_or_die(fd
, buf
.buf
, buf
.len
);
445 strbuf_release(&buf
);
449 if (launch_editor(path
, buf
, NULL
)) {
451 _("Please supply the message using either -m or -F option.\n"));
456 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
457 stripspace(buf
, opt
->cleanup_mode
== CLEANUP_ALL
);
459 if (!opt
->message_given
&& !buf
->len
)
460 die(_("no tag message?"));
462 strbuf_insert(buf
, 0, header_buf
, header_len
);
464 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
466 fprintf(stderr
, _("The tag message has been left in %s\n"),
471 unlink_or_warn(path
);
481 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
483 struct msg_arg
*msg
= opt
->value
;
488 strbuf_addstr(&(msg
->buf
), "\n\n");
489 strbuf_addstr(&(msg
->buf
), arg
);
494 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
500 strbuf_addf(sb
, "refs/tags/%s", name
);
502 return check_refname_format(sb
->buf
, 0);
505 static int parse_opt_points_at(const struct option
*opt
__attribute__((unused
)),
506 const char *arg
, int unset
)
508 unsigned char sha1
[20];
511 sha1_array_clear(&points_at
);
515 return error(_("switch 'points-at' requires an object"));
516 if (get_sha1(arg
, sha1
))
517 return error(_("malformed object name '%s'"), arg
);
518 sha1_array_append(&points_at
, sha1
);
522 static int parse_opt_sort(const struct option
*opt
, const char *arg
, int unset
)
524 int *sort
= opt
->value
;
528 flags
|= REVERSE_SORT
;
531 if (starts_with(arg
, "version:")) {
534 } else if (starts_with(arg
, "v:")) {
539 if (strcmp(arg
, "refname"))
540 die(_("unsupported sort specification %s"), arg
);
545 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
547 struct strbuf buf
= STRBUF_INIT
;
548 struct strbuf ref
= STRBUF_INIT
;
549 unsigned char object
[20], prev
[20];
550 const char *object_ref
, *tag
;
551 struct ref_lock
*lock
;
552 struct create_tag_options opt
;
553 char *cleanup_arg
= NULL
;
554 int annotate
= 0, force
= 0, lines
= -1;
555 int cmdmode
= 0, sort
= 0;
556 const char *msgfile
= NULL
, *keyid
= NULL
;
557 struct msg_arg msg
= { 0, STRBUF_INIT
};
558 struct commit_list
*with_commit
= NULL
;
559 struct option options
[] = {
560 OPT_CMDMODE('l', "list", &cmdmode
, N_("list tag names"), 'l'),
561 { OPTION_INTEGER
, 'n', NULL
, &lines
, N_("n"),
562 N_("print <n> lines of each tag message"),
563 PARSE_OPT_OPTARG
, NULL
, 1 },
564 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete tags"), 'd'),
565 OPT_CMDMODE('v', "verify", &cmdmode
, N_("verify tags"), 'v'),
567 OPT_GROUP(N_("Tag creation options")),
568 OPT_BOOL('a', "annotate", &annotate
,
569 N_("annotated tag, needs a message")),
570 OPT_CALLBACK('m', "message", &msg
, N_("message"),
571 N_("tag message"), parse_msg_arg
),
572 OPT_FILENAME('F', "file", &msgfile
, N_("read message from file")),
573 OPT_BOOL('s', "sign", &opt
.sign
, N_("annotated and GPG-signed tag")),
574 OPT_STRING(0, "cleanup", &cleanup_arg
, N_("mode"),
575 N_("how to strip spaces and #comments from message")),
576 OPT_STRING('u', "local-user", &keyid
, N_("key-id"),
577 N_("use another key to sign the tag")),
578 OPT__FORCE(&force
, N_("replace the tag if exists")),
579 OPT_COLUMN(0, "column", &colopts
, N_("show tag list in columns")),
581 OPTION_CALLBACK
, 0, "sort", &sort
, N_("type"), N_("sort tags"),
582 PARSE_OPT_NONEG
, parse_opt_sort
585 OPT_GROUP(N_("Tag listing options")),
587 OPTION_CALLBACK
, 0, "contains", &with_commit
, N_("commit"),
588 N_("print only tags that contain the commit"),
589 PARSE_OPT_LASTARG_DEFAULT
,
590 parse_opt_with_commit
, (intptr_t)"HEAD",
593 OPTION_CALLBACK
, 0, "with", &with_commit
, N_("commit"),
594 N_("print only tags that contain the commit"),
595 PARSE_OPT_HIDDEN
| PARSE_OPT_LASTARG_DEFAULT
,
596 parse_opt_with_commit
, (intptr_t)"HEAD",
599 OPTION_CALLBACK
, 0, "points-at", NULL
, N_("object"),
600 N_("print only tags of the object"), 0, parse_opt_points_at
605 git_config(git_tag_config
, NULL
);
607 memset(&opt
, 0, sizeof(opt
));
609 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
613 set_signing_key(keyid
);
617 if (argc
== 0 && !cmdmode
)
620 if ((annotate
|| msg
.given
|| msgfile
|| force
) && (cmdmode
!= 0))
621 usage_with_options(git_tag_usage
, options
);
623 finalize_colopts(&colopts
, -1);
624 if (cmdmode
== 'l' && lines
!= -1) {
625 if (explicitly_enable_column(colopts
))
626 die(_("--column and -n are incompatible"));
629 if (cmdmode
== 'l') {
631 if (column_active(colopts
)) {
632 struct column_options copts
;
633 memset(&copts
, 0, sizeof(copts
));
635 run_column_filter(colopts
, &copts
);
637 if (lines
!= -1 && sort
)
638 die(_("--sort and -n are incompatible"));
639 ret
= list_tags(argv
, lines
== -1 ? 0 : lines
, with_commit
, sort
);
640 if (column_active(colopts
))
641 stop_column_filter();
645 die(_("-n option is only allowed with -l."));
647 die(_("--contains option is only allowed with -l."));
649 die(_("--points-at option is only allowed with -l."));
651 return for_each_tag_name(argv
, delete_tag
);
653 return for_each_tag_name(argv
, verify_tag
);
655 if (msg
.given
|| msgfile
) {
656 if (msg
.given
&& msgfile
)
657 die(_("only one -F or -m option is allowed."));
660 strbuf_addbuf(&buf
, &(msg
.buf
));
662 if (!strcmp(msgfile
, "-")) {
663 if (strbuf_read(&buf
, 0, 1024) < 0)
664 die_errno(_("cannot read '%s'"), msgfile
);
666 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
667 die_errno(_("could not open or read '%s'"),
675 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
677 die(_("too many params"));
679 if (get_sha1(object_ref
, object
))
680 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
682 if (strbuf_check_tag_ref(&ref
, tag
))
683 die(_("'%s' is not a valid tag name."), tag
);
685 if (read_ref(ref
.buf
, prev
))
688 die(_("tag '%s' already exists"), tag
);
690 opt
.message_given
= msg
.given
|| msgfile
;
692 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
693 opt
.cleanup_mode
= CLEANUP_ALL
;
694 else if (!strcmp(cleanup_arg
, "verbatim"))
695 opt
.cleanup_mode
= CLEANUP_NONE
;
696 else if (!strcmp(cleanup_arg
, "whitespace"))
697 opt
.cleanup_mode
= CLEANUP_SPACE
;
699 die(_("Invalid cleanup mode %s"), cleanup_arg
);
702 create_tag(object
, tag
, &buf
, &opt
, prev
, object
);
704 lock
= lock_any_ref_for_update(ref
.buf
, prev
, 0, NULL
);
706 die(_("%s: cannot lock the ref"), ref
.buf
);
707 if (write_ref_sha1(lock
, object
, NULL
) < 0)
708 die(_("%s: cannot update the ref"), ref
.buf
);
709 if (force
&& !is_null_sha1(prev
) && hashcmp(prev
, object
))
710 printf(_("Updated tag '%s' (was %s)\n"), tag
, find_unique_abbrev(prev
, DEFAULT_ABBREV
));
712 strbuf_release(&buf
);
713 strbuf_release(&ref
);