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"
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>...",
19 "git tag -l [-n[<num>]] [<pattern>]",
20 "git tag -v <tagname>...",
24 static char signingkey
[1000];
29 struct commit_list
*with_commit
;
32 static int show_reference(const char *refname
, const unsigned char *sha1
,
33 int flag
, void *cb_data
)
35 struct tag_filter
*filter
= cb_data
;
37 if (!fnmatch(filter
->pattern
, refname
, 0)) {
40 enum object_type type
;
44 if (filter
->with_commit
) {
45 struct commit
*commit
;
47 commit
= lookup_commit_reference_gently(sha1
, 1);
50 if (!is_descendant_of(commit
, filter
->with_commit
))
55 printf("%s\n", refname
);
58 printf("%-15s ", refname
);
60 buf
= read_sha1_file(sha1
, &type
, &size
);
65 sp
= strstr(buf
, "\n\n");
70 /* only take up to "lines" lines, and strip the signature */
71 size
= parse_signature(buf
, size
);
73 i
< filter
->lines
&& sp
< buf
+ size
;
77 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
78 len
= eol
? eol
- sp
: size
- (sp
- buf
);
79 fwrite(sp
, len
, 1, stdout
);
91 static int list_tags(const char *pattern
, int lines
,
92 struct commit_list
*with_commit
)
94 struct tag_filter filter
;
99 filter
.pattern
= pattern
;
100 filter
.lines
= lines
;
101 filter
.with_commit
= with_commit
;
103 for_each_tag_ref(show_reference
, (void *) &filter
);
108 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
109 const unsigned char *sha1
);
111 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
116 unsigned char sha1
[20];
118 for (p
= argv
; *p
; p
++) {
119 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
121 error(_("tag name too long: %.*s..."), 50, *p
);
125 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
126 error(_("tag '%s' not found."), *p
);
130 if (fn(*p
, ref
, sha1
))
136 static int delete_tag(const char *name
, const char *ref
,
137 const unsigned char *sha1
)
139 if (delete_ref(ref
, sha1
, 0))
141 printf(_("Deleted tag '%s' (was %s)\n"), name
, find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
145 static int verify_tag(const char *name
, const char *ref
,
146 const unsigned char *sha1
)
148 const char *argv_verify_tag
[] = {"verify-tag",
149 "-v", "SHA1_HEX", NULL
};
150 argv_verify_tag
[2] = sha1_to_hex(sha1
);
152 if (run_command_v_opt(argv_verify_tag
, RUN_GIT_CMD
))
153 return error(_("could not verify the tag '%s'"), name
);
157 static int do_sign(struct strbuf
*buffer
)
159 struct child_process gpg
;
166 if (strlcpy(signingkey
, git_committer_info(IDENT_ERROR_ON_NO_NAME
),
167 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
168 return error(_("committer info too long."));
169 bracket
= strchr(signingkey
, '>');
174 /* When the username signingkey is bad, program could be terminated
175 * because gpg exits without reading and then write gets SIGPIPE. */
176 signal(SIGPIPE
, SIG_IGN
);
178 memset(&gpg
, 0, sizeof(gpg
));
184 args
[2] = signingkey
;
187 if (start_command(&gpg
))
188 return error(_("could not run gpg."));
190 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
193 finish_command(&gpg
);
194 return error(_("gpg did not accept the tag data"));
197 len
= strbuf_read(buffer
, gpg
.out
, 1024);
200 if (finish_command(&gpg
) || !len
|| len
< 0)
201 return error(_("gpg failed to sign the tag"));
203 /* Strip CR from the line endings, in case we are on Windows. */
204 for (i
= j
= 0; i
< buffer
->len
; i
++)
205 if (buffer
->buf
[i
] != '\r') {
207 buffer
->buf
[j
] = buffer
->buf
[i
];
210 strbuf_setlen(buffer
, j
);
215 static const char tag_template
[] =
218 "# Write a tag message\n"
221 static void set_signingkey(const char *value
)
223 if (strlcpy(signingkey
, value
, sizeof(signingkey
)) >= sizeof(signingkey
))
224 die(_("signing key value too long (%.10s...)"), value
);
227 static int git_tag_config(const char *var
, const char *value
, void *cb
)
229 if (!strcmp(var
, "user.signingkey")) {
231 return config_error_nonbool(var
);
232 set_signingkey(value
);
236 return git_default_config(var
, value
, cb
);
239 static void write_tag_body(int fd
, const unsigned char *sha1
)
242 enum object_type type
;
245 buf
= read_sha1_file(sha1
, &type
, &size
);
249 sp
= strstr(buf
, "\n\n");
251 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
255 sp
+= 2; /* skip the 2 LFs */
256 write_or_die(fd
, sp
, parse_signature(sp
, buf
+ size
- sp
));
261 static int build_tag_object(struct strbuf
*buf
, int sign
, unsigned char *result
)
263 if (sign
&& do_sign(buf
) < 0)
264 return error(_("unable to sign the tag"));
265 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
266 return error(_("unable to write tag file"));
270 static void create_tag(const unsigned char *object
, const char *tag
,
271 struct strbuf
*buf
, int message
, int sign
,
272 unsigned char *prev
, unsigned char *result
)
274 enum object_type type
;
275 char header_buf
[1024];
279 type
= sha1_object_info(object
, NULL
);
280 if (type
<= OBJ_NONE
)
281 die(_("bad object type."));
283 header_len
= snprintf(header_buf
, sizeof(header_buf
),
291 git_committer_info(IDENT_ERROR_ON_NO_NAME
));
293 if (header_len
> sizeof(header_buf
) - 1)
294 die(_("tag header too big."));
299 /* write the template message before editing: */
300 path
= git_pathdup("TAG_EDITMSG");
301 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
303 die_errno(_("could not create file '%s'"), path
);
305 if (!is_null_sha1(prev
))
306 write_tag_body(fd
, prev
);
308 write_or_die(fd
, _(tag_template
), strlen(_(tag_template
)));
311 if (launch_editor(path
, buf
, NULL
)) {
313 _("Please supply the message using either -m or -F option.\n"));
320 if (!message
&& !buf
->len
)
321 die(_("no tag message?"));
323 strbuf_insert(buf
, 0, header_buf
, header_len
);
325 if (build_tag_object(buf
, sign
, result
) < 0) {
327 fprintf(stderr
, _("The tag message has been left in %s\n"),
332 unlink_or_warn(path
);
342 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
344 struct msg_arg
*msg
= opt
->value
;
349 strbuf_addstr(&(msg
->buf
), "\n\n");
350 strbuf_addstr(&(msg
->buf
), arg
);
355 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
357 struct strbuf buf
= STRBUF_INIT
;
358 unsigned char object
[20], prev
[20];
360 const char *object_ref
, *tag
;
361 struct ref_lock
*lock
;
363 int annotate
= 0, sign
= 0, force
= 0, lines
= -1,
364 list
= 0, delete = 0, verify
= 0;
365 const char *msgfile
= NULL
, *keyid
= NULL
;
366 struct msg_arg msg
= { 0, STRBUF_INIT
};
367 struct commit_list
*with_commit
= NULL
;
368 struct option options
[] = {
369 OPT_BOOLEAN('l', NULL
, &list
, "list tag names"),
370 { OPTION_INTEGER
, 'n', NULL
, &lines
, "n",
371 "print <n> lines of each tag message",
372 PARSE_OPT_OPTARG
, NULL
, 1 },
373 OPT_BOOLEAN('d', NULL
, &delete, "delete tags"),
374 OPT_BOOLEAN('v', NULL
, &verify
, "verify tags"),
376 OPT_GROUP("Tag creation options"),
377 OPT_BOOLEAN('a', NULL
, &annotate
,
378 "annotated tag, needs a message"),
379 OPT_CALLBACK('m', NULL
, &msg
, "message",
380 "tag message", parse_msg_arg
),
381 OPT_FILENAME('F', NULL
, &msgfile
, "read message from file"),
382 OPT_BOOLEAN('s', NULL
, &sign
, "annotated and GPG-signed tag"),
383 OPT_STRING('u', NULL
, &keyid
, "key-id",
384 "use another key to sign the tag"),
385 OPT__FORCE(&force
, "replace the tag if exists"),
387 OPT_GROUP("Tag listing options"),
389 OPTION_CALLBACK
, 0, "contains", &with_commit
, "commit",
390 "print only tags that contain the commit",
391 PARSE_OPT_LASTARG_DEFAULT
,
392 parse_opt_with_commit
, (intptr_t)"HEAD",
397 git_config(git_tag_config
, NULL
);
399 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
403 set_signingkey(keyid
);
407 if (argc
== 0 && !(delete || verify
))
410 if ((annotate
|| msg
.given
|| msgfile
|| force
) &&
411 (list
|| delete || verify
))
412 usage_with_options(git_tag_usage
, options
);
414 if (list
+ delete + verify
> 1)
415 usage_with_options(git_tag_usage
, options
);
417 return list_tags(argv
[0], lines
== -1 ? 0 : lines
,
420 die(_("-n option is only allowed with -l."));
422 die(_("--contains option is only allowed with -l."));
424 return for_each_tag_name(argv
, delete_tag
);
426 return for_each_tag_name(argv
, verify_tag
);
428 if (msg
.given
|| msgfile
) {
429 if (msg
.given
&& msgfile
)
430 die(_("only one -F or -m option is allowed."));
433 strbuf_addbuf(&buf
, &(msg
.buf
));
435 if (!strcmp(msgfile
, "-")) {
436 if (strbuf_read(&buf
, 0, 1024) < 0)
437 die_errno(_("cannot read '%s'"), msgfile
);
439 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
440 die_errno(_("could not open or read '%s'"),
448 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
450 die(_("too many params"));
452 if (get_sha1(object_ref
, object
))
453 die(_("Failed to resolve '%s' as a valid ref."), object_ref
);
455 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
456 die(_("tag name too long: %.*s..."), 50, tag
);
457 if (check_ref_format(ref
))
458 die(_("'%s' is not a valid tag name."), tag
);
460 if (!resolve_ref(ref
, prev
, 1, NULL
))
463 die(_("tag '%s' already exists"), tag
);
466 create_tag(object
, tag
, &buf
, msg
.given
|| msgfile
,
469 lock
= lock_any_ref_for_update(ref
, prev
, 0);
471 die(_("%s: cannot lock the ref"), ref
);
472 if (write_ref_sha1(lock
, object
, NULL
) < 0)
473 die(_("%s: cannot update the ref"), ref
);
474 if (force
&& hashcmp(prev
, object
))
475 printf(_("Updated tag '%s' (was %s)\n"), tag
, find_unique_abbrev(prev
, DEFAULT_ABBREV
));
477 strbuf_release(&buf
);