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"
18 static const char * const git_tag_usage
[] = {
19 "git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
20 "git tag -d <tagname>...",
21 "git tag -l [-n[<num>]] [<pattern>...]",
22 "git tag -v <tagname>...",
26 static char signingkey
[1000];
29 const char **patterns
;
31 struct commit_list
*with_commit
;
34 static int match_pattern(const char **patterns
, const char *ref
)
36 /* no pattern means match everything */
39 for (; *patterns
; patterns
++)
40 if (!fnmatch(*patterns
, ref
, 0))
45 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
47 for (; want
; want
= want
->next
)
48 if (!hashcmp(want
->item
->object
.sha1
, c
->object
.sha1
))
53 static int contains_recurse(struct commit
*candidate
,
54 const struct commit_list
*want
)
56 struct commit_list
*p
;
58 /* was it previously marked as containing a want commit? */
59 if (candidate
->object
.flags
& TMP_MARK
)
61 /* or marked as not possibly containing a want commit? */
62 if (candidate
->object
.flags
& UNINTERESTING
)
65 if (in_commit_list(want
, candidate
))
68 if (parse_commit(candidate
) < 0)
71 /* Otherwise recurse and mark ourselves for future traversals. */
72 for (p
= candidate
->parents
; p
; p
= p
->next
) {
73 if (contains_recurse(p
->item
, want
)) {
74 candidate
->object
.flags
|= TMP_MARK
;
78 candidate
->object
.flags
|= UNINTERESTING
;
82 static int contains(struct commit
*candidate
, const struct commit_list
*want
)
84 return contains_recurse(candidate
, want
);
87 static void show_tag_lines(const unsigned char *sha1
, int lines
)
91 enum object_type type
;
95 buf
= read_sha1_file(sha1
, &type
, &size
);
97 die_errno("unable to read object %s", sha1_to_hex(sha1
));
104 sp
= strstr(buf
, "\n\n");
109 /* only take up to "lines" lines, and strip the signature */
110 size
= parse_signature(buf
, size
);
111 for (i
= 0, sp
+= 2; i
< lines
&& sp
< buf
+ size
; i
++) {
114 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
115 len
= eol
? eol
- sp
: size
- (sp
- buf
);
116 fwrite(sp
, len
, 1, stdout
);
124 static int show_reference(const char *refname
, const unsigned char *sha1
,
125 int flag
, void *cb_data
)
127 struct tag_filter
*filter
= cb_data
;
129 if (match_pattern(filter
->patterns
, refname
)) {
130 if (filter
->with_commit
) {
131 struct commit
*commit
;
133 commit
= lookup_commit_reference_gently(sha1
, 1);
136 if (!contains(commit
, filter
->with_commit
))
140 if (!filter
->lines
) {
141 printf("%s\n", refname
);
144 printf("%-15s ", refname
);
145 show_tag_lines(sha1
, filter
->lines
);
152 static int list_tags(const char **patterns
, int lines
,
153 struct commit_list
*with_commit
)
155 struct tag_filter filter
;
157 filter
.patterns
= patterns
;
158 filter
.lines
= lines
;
159 filter
.with_commit
= with_commit
;
161 for_each_tag_ref(show_reference
, (void *) &filter
);
166 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
167 const unsigned char *sha1
);
169 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
174 unsigned char sha1
[20];
176 for (p
= argv
; *p
; p
++) {
177 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
179 error(_("tag name too long: %.*s..."), 50, *p
);
183 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
184 error(_("tag '%s' not found."), *p
);
188 if (fn(*p
, ref
, sha1
))
194 static int delete_tag(const char *name
, const char *ref
,
195 const unsigned char *sha1
)
197 if (delete_ref(ref
, sha1
, 0))
199 printf(_("Deleted tag '%s' (was %s)\n"), name
, find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
203 static int verify_tag(const char *name
, const char *ref
,
204 const unsigned char *sha1
)
206 const char *argv_verify_tag
[] = {"verify-tag",
207 "-v", "SHA1_HEX", NULL
};
208 argv_verify_tag
[2] = sha1_to_hex(sha1
);
210 if (run_command_v_opt(argv_verify_tag
, RUN_GIT_CMD
))
211 return error(_("could not verify the tag '%s'"), name
);
215 static int do_sign(struct strbuf
*buffer
)
217 struct child_process gpg
;
224 if (strlcpy(signingkey
, git_committer_info(IDENT_ERROR_ON_NO_NAME
),
225 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
226 return error(_("committer info too long."));
227 bracket
= strchr(signingkey
, '>');
232 /* When the username signingkey is bad, program could be terminated
233 * because gpg exits without reading and then write gets SIGPIPE. */
234 signal(SIGPIPE
, SIG_IGN
);
236 memset(&gpg
, 0, sizeof(gpg
));
242 args
[2] = signingkey
;
245 if (start_command(&gpg
))
246 return error(_("could not run gpg."));
248 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
251 finish_command(&gpg
);
252 return error(_("gpg did not accept the tag data"));
255 len
= strbuf_read(buffer
, gpg
.out
, 1024);
258 if (finish_command(&gpg
) || !len
|| len
< 0)
259 return error(_("gpg failed to sign the tag"));
261 /* Strip CR from the line endings, in case we are on Windows. */
262 for (i
= j
= 0; i
< buffer
->len
; i
++)
263 if (buffer
->buf
[i
] != '\r') {
265 buffer
->buf
[j
] = buffer
->buf
[i
];
268 strbuf_setlen(buffer
, j
);
273 static const char tag_template
[] =
276 "# Write a tag message\n"
279 static void set_signingkey(const char *value
)
281 if (strlcpy(signingkey
, value
, sizeof(signingkey
)) >= sizeof(signingkey
))
282 die(_("signing key value too long (%.10s...)"), value
);
285 static int git_tag_config(const char *var
, const char *value
, void *cb
)
287 if (!strcmp(var
, "user.signingkey")) {
289 return config_error_nonbool(var
);
290 set_signingkey(value
);
294 return git_default_config(var
, value
, cb
);
297 static void write_tag_body(int fd
, const unsigned char *sha1
)
300 enum object_type type
;
303 buf
= read_sha1_file(sha1
, &type
, &size
);
307 sp
= strstr(buf
, "\n\n");
309 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
313 sp
+= 2; /* skip the 2 LFs */
314 write_or_die(fd
, sp
, parse_signature(sp
, buf
+ size
- sp
));
319 static int build_tag_object(struct strbuf
*buf
, int sign
, unsigned char *result
)
321 if (sign
&& do_sign(buf
) < 0)
322 return error(_("unable to sign the tag"));
323 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
324 return error(_("unable to write tag file"));
328 static void create_tag(const unsigned char *object
, const char *tag
,
329 struct strbuf
*buf
, int message
, int sign
,
330 unsigned char *prev
, unsigned char *result
)
332 enum object_type type
;
333 char header_buf
[1024];
337 type
= sha1_object_info(object
, NULL
);
338 if (type
<= OBJ_NONE
)
339 die(_("bad object type."));
341 header_len
= snprintf(header_buf
, sizeof(header_buf
),
349 git_committer_info(IDENT_ERROR_ON_NO_NAME
));
351 if (header_len
> sizeof(header_buf
) - 1)
352 die(_("tag header too big."));
357 /* write the template message before editing: */
358 path
= git_pathdup("TAG_EDITMSG");
359 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
361 die_errno(_("could not create file '%s'"), path
);
363 if (!is_null_sha1(prev
))
364 write_tag_body(fd
, prev
);
366 write_or_die(fd
, _(tag_template
), strlen(_(tag_template
)));
369 if (launch_editor(path
, buf
, NULL
)) {
371 _("Please supply the message using either -m or -F option.\n"));
378 if (!message
&& !buf
->len
)
379 die(_("no tag message?"));
381 strbuf_insert(buf
, 0, header_buf
, header_len
);
383 if (build_tag_object(buf
, sign
, result
) < 0) {
385 fprintf(stderr
, _("The tag message has been left in %s\n"),
390 unlink_or_warn(path
);
400 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
402 struct msg_arg
*msg
= opt
->value
;
407 strbuf_addstr(&(msg
->buf
), "\n\n");
408 strbuf_addstr(&(msg
->buf
), arg
);
413 static int strbuf_check_tag_ref(struct strbuf
*sb
, const char *name
)
416 return CHECK_REF_FORMAT_ERROR
;
419 strbuf_addf(sb
, "refs/tags/%s", name
);
421 return check_ref_format(sb
->buf
);
424 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
426 struct strbuf buf
= STRBUF_INIT
;
427 struct strbuf ref
= STRBUF_INIT
;
428 unsigned char object
[20], prev
[20];
429 const char *object_ref
, *tag
;
430 struct ref_lock
*lock
;
432 int annotate
= 0, sign
= 0, force
= 0, lines
= -1,
433 list
= 0, delete = 0, verify
= 0;
434 const char *msgfile
= NULL
, *keyid
= NULL
;
435 struct msg_arg msg
= { 0, STRBUF_INIT
};
436 struct commit_list
*with_commit
= NULL
;
437 struct option options
[] = {
438 OPT_BOOLEAN('l', NULL
, &list
, "list tag names"),
439 { OPTION_INTEGER
, 'n', NULL
, &lines
, "n",
440 "print <n> lines of each tag message",
441 PARSE_OPT_OPTARG
, NULL
, 1 },
442 OPT_BOOLEAN('d', NULL
, &delete, "delete tags"),
443 OPT_BOOLEAN('v', NULL
, &verify
, "verify tags"),
445 OPT_GROUP("Tag creation options"),
446 OPT_BOOLEAN('a', NULL
, &annotate
,
447 "annotated tag, needs a message"),
448 OPT_CALLBACK('m', NULL
, &msg
, "message",
449 "tag message", parse_msg_arg
),
450 OPT_FILENAME('F', NULL
, &msgfile
, "read message from file"),
451 OPT_BOOLEAN('s', NULL
, &sign
, "annotated and GPG-signed tag"),
452 OPT_STRING('u', NULL
, &keyid
, "key-id",
453 "use another key to sign the tag"),
454 OPT__FORCE(&force
, "replace the tag if exists"),
456 OPT_GROUP("Tag listing options"),
458 OPTION_CALLBACK
, 0, "contains", &with_commit
, "commit",
459 "print only tags that contain the commit",
460 PARSE_OPT_LASTARG_DEFAULT
,
461 parse_opt_with_commit
, (intptr_t)"HEAD",
466 git_config(git_tag_config
, NULL
);
468 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
472 set_signingkey(keyid
);
476 if (argc
== 0 && !(delete || verify
))
479 if ((annotate
|| msg
.given
|| msgfile
|| force
) &&
480 (list
|| delete || verify
))
481 usage_with_options(git_tag_usage
, options
);
483 if (list
+ delete + verify
> 1)
484 usage_with_options(git_tag_usage
, options
);
486 return list_tags(argv
, lines
== -1 ? 0 : lines
,
489 die(_("-n option is only allowed with -l."));
491 die(_("--contains option is only allowed with -l."));
493 return for_each_tag_name(argv
, delete_tag
);
495 return for_each_tag_name(argv
, verify_tag
);
497 if (msg
.given
|| msgfile
) {
498 if (msg
.given
&& msgfile
)
499 die(_("only one -F or -m option is allowed."));
502 strbuf_addbuf(&buf
, &(msg
.buf
));
504 if (!strcmp(msgfile
, "-")) {
505 if (strbuf_read(&buf
, 0, 1024) < 0)
506 die_errno(_("cannot read '%s'"), msgfile
);
508 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
509 die_errno(_("could not open or read '%s'"),
517 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
519 die(_("too many params"));
521 if (get_sha1(object_ref
, object
))
522 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
524 if (strbuf_check_tag_ref(&ref
, tag
))
525 die(_("'%s' is not a valid tag name."), tag
);
527 if (!resolve_ref(ref
.buf
, prev
, 1, NULL
))
530 die(_("tag '%s' already exists"), tag
);
533 create_tag(object
, tag
, &buf
, msg
.given
|| msgfile
,
536 lock
= lock_any_ref_for_update(ref
.buf
, prev
, 0);
538 die(_("%s: cannot lock the ref"), ref
.buf
);
539 if (write_ref_sha1(lock
, object
, NULL
) < 0)
540 die(_("%s: cannot update the ref"), ref
.buf
);
541 if (force
&& hashcmp(prev
, object
))
542 printf(_("Updated tag '%s' (was %s)\n"), tag
, find_unique_abbrev(prev
, DEFAULT_ABBREV
));
544 strbuf_release(&buf
);
545 strbuf_release(&ref
);