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
);
54 if (strcspn(editor
, "$ \t'") != len
) {
55 /* there are specials */
58 args
[i
++] = "$0 \"$@\"";
64 if (run_command_v_opt_cd_env(args
, 0, NULL
, env
))
65 die("There was a problem with the editor %s.", editor
);
70 if (strbuf_read_file(buffer
, path
, 0) < 0)
71 die("could not read message file '%s': %s",
72 path
, strerror(errno
));
80 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
82 static int show_reference(const char *refname
, const unsigned char *sha1
,
83 int flag
, void *cb_data
)
85 struct tag_filter
*filter
= cb_data
;
87 if (!fnmatch(filter
->pattern
, refname
, 0)) {
90 enum object_type type
;
95 printf("%s\n", refname
);
98 printf("%-15s ", refname
);
100 buf
= read_sha1_file(sha1
, &type
, &size
);
105 sp
= strstr(buf
, "\n\n");
110 /* only take up to "lines" lines, and strip the signature */
112 i
< filter
->lines
&& sp
< buf
+ size
&&
113 prefixcmp(sp
, PGP_SIGNATURE
"\n");
117 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
118 len
= eol
? eol
- sp
: size
- (sp
- buf
);
119 fwrite(sp
, len
, 1, stdout
);
131 static int list_tags(const char *pattern
, int lines
)
133 struct tag_filter filter
;
138 filter
.pattern
= pattern
;
139 filter
.lines
= lines
;
141 for_each_tag_ref(show_reference
, (void *) &filter
);
146 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
147 const unsigned char *sha1
);
149 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
154 unsigned char sha1
[20];
156 for (p
= argv
; *p
; p
++) {
157 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
159 error("tag name too long: %.*s...", 50, *p
);
163 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
164 error("tag '%s' not found.", *p
);
168 if (fn(*p
, ref
, sha1
))
174 static int delete_tag(const char *name
, const char *ref
,
175 const unsigned char *sha1
)
177 if (delete_ref(ref
, sha1
))
179 printf("Deleted tag '%s'\n", name
);
183 static int verify_tag(const char *name
, const char *ref
,
184 const unsigned char *sha1
)
186 const char *argv_verify_tag
[] = {"git-verify-tag",
187 "-v", "SHA1_HEX", NULL
};
188 argv_verify_tag
[2] = sha1_to_hex(sha1
);
190 if (run_command_v_opt(argv_verify_tag
, 0))
191 return error("could not verify the tag '%s'", name
);
195 static int do_sign(struct strbuf
*buffer
)
197 struct child_process gpg
;
203 if (strlcpy(signingkey
, git_committer_info(IDENT_ERROR_ON_NO_NAME
),
204 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
205 return error("committer info too long.");
206 bracket
= strchr(signingkey
, '>');
211 /* When the username signingkey is bad, program could be terminated
212 * because gpg exits without reading and then write gets SIGPIPE. */
213 signal(SIGPIPE
, SIG_IGN
);
215 memset(&gpg
, 0, sizeof(gpg
));
221 args
[2] = signingkey
;
224 if (start_command(&gpg
))
225 return error("could not run gpg.");
227 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
229 finish_command(&gpg
);
230 return error("gpg did not accept the tag data");
234 len
= strbuf_read(buffer
, gpg
.out
, 1024);
236 if (finish_command(&gpg
) || !len
|| len
< 0)
237 return error("gpg failed to sign the tag");
240 return error("could not read the entire signature from gpg.");
245 static const char tag_template
[] =
248 "# Write a tag message\n"
251 static void set_signingkey(const char *value
)
253 if (strlcpy(signingkey
, value
, sizeof(signingkey
)) >= sizeof(signingkey
))
254 die("signing key value too long (%.10s...)", value
);
257 static int git_tag_config(const char *var
, const char *value
)
259 if (!strcmp(var
, "user.signingkey")) {
261 die("user.signingkey without value");
262 set_signingkey(value
);
266 return git_default_config(var
, value
);
269 static void write_tag_body(int fd
, const unsigned char *sha1
)
272 enum object_type type
;
273 char *buf
, *sp
, *eob
;
276 buf
= read_sha1_file(sha1
, &type
, &size
);
280 sp
= strstr(buf
, "\n\n");
282 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
286 sp
+= 2; /* skip the 2 LFs */
287 eob
= strstr(sp
, "\n" PGP_SIGNATURE
"\n");
291 len
= buf
+ size
- sp
;
292 write_or_die(fd
, sp
, len
);
297 static void create_tag(const unsigned char *object
, const char *tag
,
298 struct strbuf
*buf
, int message
, int sign
,
299 unsigned char *prev
, unsigned char *result
)
301 enum object_type type
;
302 char header_buf
[1024];
305 type
= sha1_object_info(object
, NULL
);
306 if (type
<= OBJ_NONE
)
307 die("bad object type.");
309 header_len
= snprintf(header_buf
, sizeof(header_buf
),
317 git_committer_info(IDENT_ERROR_ON_NO_NAME
));
319 if (header_len
> sizeof(header_buf
) - 1)
320 die("tag header too big.");
326 /* write the template message before editing: */
327 path
= xstrdup(git_path("TAG_EDITMSG"));
328 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
330 die("could not create file '%s': %s",
331 path
, strerror(errno
));
333 if (!is_null_sha1(prev
))
334 write_tag_body(fd
, prev
);
336 write_or_die(fd
, tag_template
, strlen(tag_template
));
339 launch_editor(path
, buf
, NULL
);
347 if (!message
&& !buf
->len
)
348 die("no tag message?");
350 strbuf_insert(buf
, 0, header_buf
, header_len
);
352 if (sign
&& do_sign(buf
) < 0)
353 die("unable to sign the tag");
354 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
355 die("unable to write tag file");
363 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
365 struct msg_arg
*msg
= opt
->value
;
370 strbuf_addstr(&(msg
->buf
), "\n\n");
371 strbuf_addstr(&(msg
->buf
), arg
);
376 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
379 unsigned char object
[20], prev
[20];
381 const char *object_ref
, *tag
;
382 struct ref_lock
*lock
;
384 int annotate
= 0, sign
= 0, force
= 0, lines
= 0,
385 list
= 0, delete = 0, verify
= 0;
386 char *msgfile
= NULL
, *keyid
= NULL
;
387 struct msg_arg msg
= { 0, STRBUF_INIT
};
388 struct option options
[] = {
389 OPT_BOOLEAN('l', NULL
, &list
, "list tag names"),
390 { OPTION_INTEGER
, 'n', NULL
, &lines
, NULL
,
391 "print n lines of each tag message",
392 PARSE_OPT_OPTARG
, NULL
, 1 },
393 OPT_BOOLEAN('d', NULL
, &delete, "delete tags"),
394 OPT_BOOLEAN('v', NULL
, &verify
, "verify tags"),
396 OPT_GROUP("Tag creation options"),
397 OPT_BOOLEAN('a', NULL
, &annotate
,
398 "annotated tag, needs a message"),
399 OPT_CALLBACK('m', NULL
, &msg
, "msg",
400 "message for the tag", parse_msg_arg
),
401 OPT_STRING('F', NULL
, &msgfile
, "file", "message in a file"),
402 OPT_BOOLEAN('s', NULL
, &sign
, "annotated and GPG-signed tag"),
403 OPT_STRING('u', NULL
, &keyid
, "key-id",
404 "use another key to sign the tag"),
405 OPT_BOOLEAN('f', NULL
, &force
, "replace the tag if exists"),
409 git_config(git_tag_config
);
411 argc
= parse_options(argc
, argv
, options
, git_tag_usage
, 0);
415 set_signingkey(keyid
);
421 return list_tags(argv
[0], lines
);
423 return for_each_tag_name(argv
, delete_tag
);
425 return for_each_tag_name(argv
, verify_tag
);
427 strbuf_init(&buf
, 0);
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("cannot read %s", msgfile
);
439 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
440 die("could not open or read '%s': %s",
441 msgfile
, strerror(errno
));
448 usage_with_options(git_tag_usage
, options
);
449 return list_tags(NULL
, lines
);
453 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
455 die("too many params");
457 if (get_sha1(object_ref
, object
))
458 die("Failed to resolve '%s' as a valid ref.", object_ref
);
460 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
461 die("tag name too long: %.*s...", 50, tag
);
462 if (check_ref_format(ref
))
463 die("'%s' is not a valid tag name.", tag
);
465 if (!resolve_ref(ref
, prev
, 1, NULL
))
468 die("tag '%s' already exists", tag
);
471 create_tag(object
, tag
, &buf
, msg
.given
|| msgfile
,
474 lock
= lock_any_ref_for_update(ref
, prev
, 0);
476 die("%s: cannot lock the ref", ref
);
477 if (write_ref_sha1(lock
, object
, NULL
) < 0)
478 die("%s: cannot update the ref", ref
);
480 strbuf_release(&buf
);