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"
20 static const char * const git_tag_usage
[] = {
21 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
22 "git tag -d <tagname>...",
23 "git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] "
24 "\n\t\t[<pattern>...]",
25 "git tag -v <tagname>...",
30 const char **patterns
;
32 struct commit_list
*with_commit
;
35 static struct sha1_array points_at
;
37 static int match_pattern(const char **patterns
, const char *ref
)
39 /* no pattern means match everything */
42 for (; *patterns
; patterns
++)
43 if (!fnmatch(*patterns
, ref
, 0))
48 static const unsigned char *match_points_at(const char *refname
,
49 const unsigned char *sha1
)
51 const unsigned char *tagged_sha1
= NULL
;
54 if (sha1_array_lookup(&points_at
, sha1
) >= 0)
56 obj
= parse_object(sha1
);
58 die(_("malformed object at '%s'"), refname
);
59 if (obj
->type
== OBJ_TAG
)
60 tagged_sha1
= ((struct tag
*)obj
)->tagged
->sha1
;
61 if (tagged_sha1
&& sha1_array_lookup(&points_at
, tagged_sha1
) >= 0)
66 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
68 for (; want
; want
= want
->next
)
69 if (!hashcmp(want
->item
->object
.sha1
, c
->object
.sha1
))
74 static int contains_recurse(struct commit
*candidate
,
75 const struct commit_list
*want
)
77 struct commit_list
*p
;
79 /* was it previously marked as containing a want commit? */
80 if (candidate
->object
.flags
& TMP_MARK
)
82 /* or marked as not possibly containing a want commit? */
83 if (candidate
->object
.flags
& UNINTERESTING
)
86 if (in_commit_list(want
, candidate
))
89 if (parse_commit(candidate
) < 0)
92 /* Otherwise recurse and mark ourselves for future traversals. */
93 for (p
= candidate
->parents
; p
; p
= p
->next
) {
94 if (contains_recurse(p
->item
, want
)) {
95 candidate
->object
.flags
|= TMP_MARK
;
99 candidate
->object
.flags
|= UNINTERESTING
;
103 static int contains(struct commit
*candidate
, const struct commit_list
*want
)
105 return contains_recurse(candidate
, want
);
108 static void show_tag_lines(const unsigned char *sha1
, int lines
)
112 enum object_type type
;
113 char *buf
, *sp
, *eol
;
116 buf
= read_sha1_file(sha1
, &type
, &size
);
118 die_errno("unable to read object %s", sha1_to_hex(sha1
));
119 if (type
!= OBJ_COMMIT
&& type
!= OBJ_TAG
)
122 die("an empty %s object %s?",
123 typename(type
), sha1_to_hex(sha1
));
126 sp
= strstr(buf
, "\n\n");
130 /* only take up to "lines" lines, and strip the signature from a tag */
132 size
= parse_signature(buf
, size
);
133 for (i
= 0, sp
+= 2; i
< lines
&& sp
< buf
+ size
; i
++) {
136 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
137 len
= eol
? eol
- sp
: size
- (sp
- buf
);
138 fwrite(sp
, len
, 1, stdout
);
147 static int show_reference(const char *refname
, const unsigned char *sha1
,
148 int flag
, void *cb_data
)
150 struct tag_filter
*filter
= cb_data
;
152 if (match_pattern(filter
->patterns
, refname
)) {
153 if (filter
->with_commit
) {
154 struct commit
*commit
;
156 commit
= lookup_commit_reference_gently(sha1
, 1);
159 if (!contains(commit
, filter
->with_commit
))
163 if (points_at
.nr
&& !match_points_at(refname
, sha1
))
166 if (!filter
->lines
) {
167 printf("%s\n", refname
);
170 printf("%-15s ", refname
);
171 show_tag_lines(sha1
, filter
->lines
);
178 static int list_tags(const char **patterns
, int lines
,
179 struct commit_list
*with_commit
)
181 struct tag_filter filter
;
183 filter
.patterns
= patterns
;
184 filter
.lines
= lines
;
185 filter
.with_commit
= with_commit
;
187 for_each_tag_ref(show_reference
, (void *) &filter
);
192 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
193 const unsigned char *sha1
);
195 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
200 unsigned char sha1
[20];
202 for (p
= argv
; *p
; p
++) {
203 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
205 error(_("tag name too long: %.*s..."), 50, *p
);
209 if (read_ref(ref
, sha1
)) {
210 error(_("tag '%s' not found."), *p
);
214 if (fn(*p
, ref
, sha1
))
220 static int delete_tag(const char *name
, const char *ref
,
221 const unsigned char *sha1
)
223 if (delete_ref(ref
, sha1
, 0))
225 printf(_("Deleted tag '%s' (was %s)\n"), name
, find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
229 static int verify_tag(const char *name
, const char *ref
,
230 const unsigned char *sha1
)
232 const char *argv_verify_tag
[] = {"verify-tag",
233 "-v", "SHA1_HEX", NULL
};
234 argv_verify_tag
[2] = sha1_to_hex(sha1
);
236 if (run_command_v_opt(argv_verify_tag
, RUN_GIT_CMD
))
237 return error(_("could not verify the tag '%s'"), name
);
241 static int do_sign(struct strbuf
*buffer
)
243 return sign_buffer(buffer
, buffer
, get_signing_key());
246 static const char tag_template
[] =
249 "# Write a tag message\n"
250 "# Lines starting with '#' will be ignored.\n"
253 static const char tag_template_nocleanup
[] =
256 "# Write a tag message\n"
257 "# Lines starting with '#' will be kept; you may remove them"
258 " yourself if you want to.\n"
261 static int git_tag_config(const char *var
, const char *value
, void *cb
)
263 int status
= git_gpg_config(var
, value
, cb
);
266 return git_default_config(var
, value
, cb
);
269 static void write_tag_body(int fd
, const unsigned char *sha1
)
272 enum object_type type
;
275 buf
= read_sha1_file(sha1
, &type
, &size
);
279 sp
= strstr(buf
, "\n\n");
281 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
285 sp
+= 2; /* skip the 2 LFs */
286 write_or_die(fd
, sp
, parse_signature(sp
, buf
+ size
- sp
));
291 static int build_tag_object(struct strbuf
*buf
, int sign
, unsigned char *result
)
293 if (sign
&& do_sign(buf
) < 0)
294 return error(_("unable to sign the tag"));
295 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
296 return error(_("unable to write tag file"));
300 struct create_tag_options
{
301 unsigned int message_given
:1;
310 static void create_tag(const unsigned char *object
, const char *tag
,
311 struct strbuf
*buf
, struct create_tag_options
*opt
,
312 unsigned char *prev
, unsigned char *result
)
314 enum object_type type
;
315 char header_buf
[1024];
319 type
= sha1_object_info(object
, NULL
);
320 if (type
<= OBJ_NONE
)
321 die(_("bad object type."));
323 header_len
= snprintf(header_buf
, sizeof(header_buf
),
331 git_committer_info(IDENT_ERROR_ON_NO_NAME
));
333 if (header_len
> sizeof(header_buf
) - 1)
334 die(_("tag header too big."));
336 if (!opt
->message_given
) {
339 /* write the template message before editing: */
340 path
= git_pathdup("TAG_EDITMSG");
341 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
343 die_errno(_("could not create file '%s'"), path
);
345 if (!is_null_sha1(prev
))
346 write_tag_body(fd
, prev
);
347 else if (opt
->cleanup_mode
== CLEANUP_ALL
)
348 write_or_die(fd
, _(tag_template
),
349 strlen(_(tag_template
)));
351 write_or_die(fd
, _(tag_template_nocleanup
),
352 strlen(_(tag_template_nocleanup
)));
355 if (launch_editor(path
, buf
, NULL
)) {
357 _("Please supply the message using either -m or -F option.\n"));
362 if (opt
->cleanup_mode
!= CLEANUP_NONE
)
363 stripspace(buf
, opt
->cleanup_mode
== CLEANUP_ALL
);
365 if (!opt
->message_given
&& !buf
->len
)
366 die(_("no tag message?"));
368 strbuf_insert(buf
, 0, header_buf
, header_len
);
370 if (build_tag_object(buf
, opt
->sign
, result
) < 0) {
372 fprintf(stderr
, _("The tag message has been left in %s\n"),
377 unlink_or_warn(path
);
387 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
389 struct msg_arg
*msg
= opt
->value
;
394 strbuf_addstr(&(msg
->buf
), "\n\n");
395 strbuf_addstr(&(msg
->buf
), arg
);
400 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
406 strbuf_addf(sb
, "refs/tags/%s", name
);
408 return check_refname_format(sb
->buf
, 0);
411 static int parse_opt_points_at(const struct option
*opt
__attribute__((unused
)),
412 const char *arg
, int unset
)
414 unsigned char sha1
[20];
417 sha1_array_clear(&points_at
);
421 return error(_("switch 'points-at' requires an object"));
422 if (get_sha1(arg
, sha1
))
423 return error(_("malformed object name '%s'"), arg
);
424 sha1_array_append(&points_at
, sha1
);
428 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
430 struct strbuf buf
= STRBUF_INIT
;
431 struct strbuf ref
= STRBUF_INIT
;
432 unsigned char object
[20], prev
[20];
433 const char *object_ref
, *tag
;
434 struct ref_lock
*lock
;
435 struct create_tag_options opt
;
436 char *cleanup_arg
= NULL
;
437 int annotate
= 0, force
= 0, lines
= -1, list
= 0,
438 delete = 0, verify
= 0;
439 const char *msgfile
= NULL
, *keyid
= NULL
;
440 struct msg_arg msg
= { 0, STRBUF_INIT
};
441 struct commit_list
*with_commit
= NULL
;
442 struct option options
[] = {
443 OPT_BOOLEAN('l', "list", &list
, "list tag names"),
444 { OPTION_INTEGER
, 'n', NULL
, &lines
, "n",
445 "print <n> lines of each tag message",
446 PARSE_OPT_OPTARG
, NULL
, 1 },
447 OPT_BOOLEAN('d', "delete", &delete, "delete tags"),
448 OPT_BOOLEAN('v', "verify", &verify
, "verify tags"),
450 OPT_GROUP("Tag creation options"),
451 OPT_BOOLEAN('a', "annotate", &annotate
,
452 "annotated tag, needs a message"),
453 OPT_CALLBACK('m', "message", &msg
, "message",
454 "tag message", parse_msg_arg
),
455 OPT_FILENAME('F', "file", &msgfile
, "read message from file"),
456 OPT_BOOLEAN('s', "sign", &opt
.sign
, "annotated and GPG-signed tag"),
457 OPT_STRING(0, "cleanup", &cleanup_arg
, "mode",
458 "how to strip spaces and #comments from message"),
459 OPT_STRING('u', "local-user", &keyid
, "key-id",
460 "use another key to sign the tag"),
461 OPT__FORCE(&force
, "replace the tag if exists"),
463 OPT_GROUP("Tag listing options"),
465 OPTION_CALLBACK
, 0, "contains", &with_commit
, "commit",
466 "print only tags that contain the commit",
467 PARSE_OPT_LASTARG_DEFAULT
,
468 parse_opt_with_commit
, (intptr_t)"HEAD",
471 OPTION_CALLBACK
, 0, "points-at", NULL
, "object",
472 "print only tags of the object", 0, parse_opt_points_at
477 git_config(git_tag_config
, NULL
);
479 memset(&opt
, 0, sizeof(opt
));
481 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
485 set_signing_key(keyid
);
489 if (argc
== 0 && !(delete || verify
))
492 if ((annotate
|| msg
.given
|| msgfile
|| force
) &&
493 (list
|| delete || verify
))
494 usage_with_options(git_tag_usage
, options
);
496 if (list
+ delete + verify
> 1)
497 usage_with_options(git_tag_usage
, options
);
499 return list_tags(argv
, lines
== -1 ? 0 : lines
,
502 die(_("-n option is only allowed with -l."));
504 die(_("--contains option is only allowed with -l."));
506 die(_("--points-at option is only allowed with -l."));
508 return for_each_tag_name(argv
, delete_tag
);
510 return for_each_tag_name(argv
, verify_tag
);
512 if (msg
.given
|| msgfile
) {
513 if (msg
.given
&& msgfile
)
514 die(_("only one -F or -m option is allowed."));
517 strbuf_addbuf(&buf
, &(msg
.buf
));
519 if (!strcmp(msgfile
, "-")) {
520 if (strbuf_read(&buf
, 0, 1024) < 0)
521 die_errno(_("cannot read '%s'"), msgfile
);
523 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
524 die_errno(_("could not open or read '%s'"),
532 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
534 die(_("too many params"));
536 if (get_sha1(object_ref
, object
))
537 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
539 if (strbuf_check_tag_ref(&ref
, tag
))
540 die(_("'%s' is not a valid tag name."), tag
);
542 if (read_ref(ref
.buf
, prev
))
545 die(_("tag '%s' already exists"), tag
);
547 opt
.message_given
= msg
.given
|| msgfile
;
549 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "strip"))
550 opt
.cleanup_mode
= CLEANUP_ALL
;
551 else if (!strcmp(cleanup_arg
, "verbatim"))
552 opt
.cleanup_mode
= CLEANUP_NONE
;
553 else if (!strcmp(cleanup_arg
, "whitespace"))
554 opt
.cleanup_mode
= CLEANUP_SPACE
;
556 die(_("Invalid cleanup mode %s"), cleanup_arg
);
559 create_tag(object
, tag
, &buf
, &opt
, prev
, object
);
561 lock
= lock_any_ref_for_update(ref
.buf
, prev
, 0);
563 die(_("%s: cannot lock the ref"), ref
.buf
);
564 if (write_ref_sha1(lock
, object
, NULL
) < 0)
565 die(_("%s: cannot update the ref"), ref
.buf
);
566 if (force
&& hashcmp(prev
, object
))
567 printf(_("Updated tag '%s' (was %s)\n"), tag
, find_unique_abbrev(prev
, DEFAULT_ABBREV
));
569 strbuf_release(&buf
);
570 strbuf_release(&ref
);