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];
26 void launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
28 const char *editor
, *terminal
;
30 editor
= getenv("GIT_EDITOR");
31 if (!editor
&& editor_program
)
32 editor
= editor_program
;
34 editor
= getenv("VISUAL");
36 editor
= getenv("EDITOR");
38 terminal
= getenv("TERM");
39 if (!editor
&& (!terminal
|| !strcmp(terminal
, "dumb"))) {
41 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
42 "Please supply the message using either -m or -F option.\n");
49 if (strcmp(editor
, ":")) {
50 size_t len
= strlen(editor
);
55 strbuf_init(&arg0
, 0);
56 if (strcspn(editor
, "$ \t'") != len
) {
57 /* there are specials */
58 strbuf_addf(&arg0
, "%s \"$@\"", editor
);
67 if (run_command_v_opt_cd_env(args
, 0, NULL
, env
))
68 die("There was a problem with the editor %s.", editor
);
69 strbuf_release(&arg0
);
74 if (strbuf_read_file(buffer
, path
, 0) < 0)
75 die("could not read message file '%s': %s",
76 path
, strerror(errno
));
84 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
86 static int show_reference(const char *refname
, const unsigned char *sha1
,
87 int flag
, void *cb_data
)
89 struct tag_filter
*filter
= cb_data
;
91 if (!fnmatch(filter
->pattern
, refname
, 0)) {
94 enum object_type type
;
99 printf("%s\n", refname
);
102 printf("%-15s ", refname
);
104 buf
= read_sha1_file(sha1
, &type
, &size
);
109 sp
= strstr(buf
, "\n\n");
114 /* only take up to "lines" lines, and strip the signature */
116 i
< filter
->lines
&& sp
< buf
+ size
&&
117 prefixcmp(sp
, PGP_SIGNATURE
"\n");
121 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
122 len
= eol
? eol
- sp
: size
- (sp
- buf
);
123 fwrite(sp
, len
, 1, stdout
);
135 static int list_tags(const char *pattern
, int lines
)
137 struct tag_filter filter
;
142 filter
.pattern
= pattern
;
143 filter
.lines
= lines
;
145 for_each_tag_ref(show_reference
, (void *) &filter
);
150 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
151 const unsigned char *sha1
);
153 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
158 unsigned char sha1
[20];
160 for (p
= argv
; *p
; p
++) {
161 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
163 error("tag name too long: %.*s...", 50, *p
);
167 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
168 error("tag '%s' not found.", *p
);
172 if (fn(*p
, ref
, sha1
))
178 static int delete_tag(const char *name
, const char *ref
,
179 const unsigned char *sha1
)
181 if (delete_ref(ref
, sha1
))
183 printf("Deleted tag '%s'\n", name
);
187 static int verify_tag(const char *name
, const char *ref
,
188 const unsigned char *sha1
)
190 const char *argv_verify_tag
[] = {"git-verify-tag",
191 "-v", "SHA1_HEX", NULL
};
192 argv_verify_tag
[2] = sha1_to_hex(sha1
);
194 if (run_command_v_opt(argv_verify_tag
, 0))
195 return error("could not verify the tag '%s'", name
);
199 static int do_sign(struct strbuf
*buffer
)
201 struct child_process gpg
;
207 if (strlcpy(signingkey
, git_committer_info(IDENT_ERROR_ON_NO_NAME
),
208 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
209 return error("committer info too long.");
210 bracket
= strchr(signingkey
, '>');
215 /* When the username signingkey is bad, program could be terminated
216 * because gpg exits without reading and then write gets SIGPIPE. */
217 signal(SIGPIPE
, SIG_IGN
);
219 memset(&gpg
, 0, sizeof(gpg
));
225 args
[2] = signingkey
;
228 if (start_command(&gpg
))
229 return error("could not run gpg.");
231 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
234 finish_command(&gpg
);
235 return error("gpg did not accept the tag data");
238 len
= strbuf_read(buffer
, gpg
.out
, 1024);
241 if (finish_command(&gpg
) || !len
|| len
< 0)
242 return error("gpg failed to sign the tag");
245 /* strip CR from the line endings */
248 for (i
= j
= 0; i
< buffer
->len
; i
++)
249 if (buffer
->buf
[i
] != '\r') {
251 buffer
->buf
[j
] = buffer
->buf
[i
];
254 strbuf_setlen(buffer
, j
);
261 static const char tag_template
[] =
264 "# Write a tag message\n"
267 static void set_signingkey(const char *value
)
269 if (strlcpy(signingkey
, value
, sizeof(signingkey
)) >= sizeof(signingkey
))
270 die("signing key value too long (%.10s...)", value
);
273 static int git_tag_config(const char *var
, const char *value
)
275 if (!strcmp(var
, "user.signingkey")) {
277 return config_error_nonbool(value
);
278 set_signingkey(value
);
282 return git_default_config(var
, value
);
285 static void write_tag_body(int fd
, const unsigned char *sha1
)
288 enum object_type type
;
289 char *buf
, *sp
, *eob
;
292 buf
= read_sha1_file(sha1
, &type
, &size
);
296 sp
= strstr(buf
, "\n\n");
298 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
302 sp
+= 2; /* skip the 2 LFs */
303 eob
= strstr(sp
, "\n" PGP_SIGNATURE
"\n");
307 len
= buf
+ size
- sp
;
308 write_or_die(fd
, sp
, len
);
313 static void create_tag(const unsigned char *object
, const char *tag
,
314 struct strbuf
*buf
, int message
, int sign
,
315 unsigned char *prev
, unsigned char *result
)
317 enum object_type type
;
318 char header_buf
[1024];
321 type
= sha1_object_info(object
, NULL
);
322 if (type
<= OBJ_NONE
)
323 die("bad object type.");
325 header_len
= snprintf(header_buf
, sizeof(header_buf
),
333 git_committer_info(IDENT_ERROR_ON_NO_NAME
));
335 if (header_len
> sizeof(header_buf
) - 1)
336 die("tag header too big.");
342 /* write the template message before editing: */
343 path
= xstrdup(git_path("TAG_EDITMSG"));
344 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
346 die("could not create file '%s': %s",
347 path
, strerror(errno
));
349 if (!is_null_sha1(prev
))
350 write_tag_body(fd
, prev
);
352 write_or_die(fd
, tag_template
, strlen(tag_template
));
355 launch_editor(path
, buf
, NULL
);
363 if (!message
&& !buf
->len
)
364 die("no tag message?");
366 strbuf_insert(buf
, 0, header_buf
, header_len
);
368 if (sign
&& do_sign(buf
) < 0)
369 die("unable to sign the tag");
370 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
371 die("unable to write tag file");
379 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
381 struct msg_arg
*msg
= opt
->value
;
386 strbuf_addstr(&(msg
->buf
), "\n\n");
387 strbuf_addstr(&(msg
->buf
), arg
);
392 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
395 unsigned char object
[20], prev
[20];
397 const char *object_ref
, *tag
;
398 struct ref_lock
*lock
;
400 int annotate
= 0, sign
= 0, force
= 0, lines
= 0,
401 list
= 0, delete = 0, verify
= 0;
402 char *msgfile
= NULL
, *keyid
= NULL
;
403 struct msg_arg msg
= { 0, STRBUF_INIT
};
404 struct option options
[] = {
405 OPT_BOOLEAN('l', NULL
, &list
, "list tag names"),
406 { OPTION_INTEGER
, 'n', NULL
, &lines
, NULL
,
407 "print n lines of each tag message",
408 PARSE_OPT_OPTARG
, NULL
, 1 },
409 OPT_BOOLEAN('d', NULL
, &delete, "delete tags"),
410 OPT_BOOLEAN('v', NULL
, &verify
, "verify tags"),
412 OPT_GROUP("Tag creation options"),
413 OPT_BOOLEAN('a', NULL
, &annotate
,
414 "annotated tag, needs a message"),
415 OPT_CALLBACK('m', NULL
, &msg
, "msg",
416 "message for the tag", parse_msg_arg
),
417 OPT_STRING('F', NULL
, &msgfile
, "file", "message in a file"),
418 OPT_BOOLEAN('s', NULL
, &sign
, "annotated and GPG-signed tag"),
419 OPT_STRING('u', NULL
, &keyid
, "key-id",
420 "use another key to sign the tag"),
421 OPT_BOOLEAN('f', NULL
, &force
, "replace the tag if exists"),
425 git_config(git_tag_config
);
427 argc
= parse_options(argc
, argv
, options
, git_tag_usage
, 0);
431 set_signingkey(keyid
);
437 return list_tags(argv
[0], lines
);
439 return for_each_tag_name(argv
, delete_tag
);
441 return for_each_tag_name(argv
, verify_tag
);
443 strbuf_init(&buf
, 0);
444 if (msg
.given
|| msgfile
) {
445 if (msg
.given
&& msgfile
)
446 die("only one -F or -m option is allowed.");
449 strbuf_addbuf(&buf
, &(msg
.buf
));
451 if (!strcmp(msgfile
, "-")) {
452 if (strbuf_read(&buf
, 0, 1024) < 0)
453 die("cannot read %s", msgfile
);
455 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
456 die("could not open or read '%s': %s",
457 msgfile
, strerror(errno
));
464 usage_with_options(git_tag_usage
, options
);
465 return list_tags(NULL
, lines
);
469 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
471 die("too many params");
473 if (get_sha1(object_ref
, object
))
474 die("Failed to resolve '%s' as a valid ref.", object_ref
);
476 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
477 die("tag name too long: %.*s...", 50, tag
);
478 if (check_ref_format(ref
))
479 die("'%s' is not a valid tag name.", tag
);
481 if (!resolve_ref(ref
, prev
, 1, NULL
))
484 die("tag '%s' already exists", tag
);
487 create_tag(object
, tag
, &buf
, msg
.given
|| msgfile
,
490 lock
= lock_any_ref_for_update(ref
, prev
, 0);
492 die("%s: cannot lock the ref", ref
);
493 if (write_ref_sha1(lock
, object
, NULL
) < 0)
494 die("%s: cannot update the ref", ref
);
496 strbuf_release(&buf
);