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 const char *args
[] = { editor
, path
, NULL
};
52 if (run_command_v_opt_cd_env(args
, 0, NULL
, env
))
53 die("There was a problem with the editor %s.", editor
);
58 if (strbuf_read_file(buffer
, path
, 0) < 0)
59 die("could not read message file '%s': %s",
60 path
, strerror(errno
));
68 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
70 static int show_reference(const char *refname
, const unsigned char *sha1
,
71 int flag
, void *cb_data
)
73 struct tag_filter
*filter
= cb_data
;
75 if (!fnmatch(filter
->pattern
, refname
, 0)) {
78 enum object_type type
;
83 printf("%s\n", refname
);
86 printf("%-15s ", refname
);
88 buf
= read_sha1_file(sha1
, &type
, &size
);
93 sp
= strstr(buf
, "\n\n");
98 /* only take up to "lines" lines, and strip the signature */
100 i
< filter
->lines
&& sp
< buf
+ size
&&
101 prefixcmp(sp
, PGP_SIGNATURE
"\n");
105 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
106 len
= eol
? eol
- sp
: size
- (sp
- buf
);
107 fwrite(sp
, len
, 1, stdout
);
119 static int list_tags(const char *pattern
, int lines
)
121 struct tag_filter filter
;
126 filter
.pattern
= pattern
;
127 filter
.lines
= lines
;
129 for_each_tag_ref(show_reference
, (void *) &filter
);
134 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
135 const unsigned char *sha1
);
137 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
142 unsigned char sha1
[20];
144 for (p
= argv
; *p
; p
++) {
145 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
147 error("tag name too long: %.*s...", 50, *p
);
151 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
152 error("tag '%s' not found.", *p
);
156 if (fn(*p
, ref
, sha1
))
162 static int delete_tag(const char *name
, const char *ref
,
163 const unsigned char *sha1
)
165 if (delete_ref(ref
, sha1
))
167 printf("Deleted tag '%s'\n", name
);
171 static int verify_tag(const char *name
, const char *ref
,
172 const unsigned char *sha1
)
174 const char *argv_verify_tag
[] = {"git-verify-tag",
175 "-v", "SHA1_HEX", NULL
};
176 argv_verify_tag
[2] = sha1_to_hex(sha1
);
178 if (run_command_v_opt(argv_verify_tag
, 0))
179 return error("could not verify the tag '%s'", name
);
183 static int do_sign(struct strbuf
*buffer
)
185 struct child_process gpg
;
191 if (strlcpy(signingkey
, git_committer_info(IDENT_ERROR_ON_NO_NAME
),
192 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
193 return error("committer info too long.");
194 bracket
= strchr(signingkey
, '>');
199 /* When the username signingkey is bad, program could be terminated
200 * because gpg exits without reading and then write gets SIGPIPE. */
201 signal(SIGPIPE
, SIG_IGN
);
203 memset(&gpg
, 0, sizeof(gpg
));
209 args
[2] = signingkey
;
212 if (start_command(&gpg
))
213 return error("could not run gpg.");
215 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
217 finish_command(&gpg
);
218 return error("gpg did not accept the tag data");
222 len
= strbuf_read(buffer
, gpg
.out
, 1024);
224 if (finish_command(&gpg
) || !len
|| len
< 0)
225 return error("gpg failed to sign the tag");
228 return error("could not read the entire signature from gpg.");
233 static const char tag_template
[] =
236 "# Write a tag message\n"
239 static void set_signingkey(const char *value
)
241 if (strlcpy(signingkey
, value
, sizeof(signingkey
)) >= sizeof(signingkey
))
242 die("signing key value too long (%.10s...)", value
);
245 static int git_tag_config(const char *var
, const char *value
)
247 if (!strcmp(var
, "user.signingkey")) {
249 die("user.signingkey without value");
250 set_signingkey(value
);
254 return git_default_config(var
, value
);
257 static void write_tag_body(int fd
, const unsigned char *sha1
)
260 enum object_type type
;
261 char *buf
, *sp
, *eob
;
264 buf
= read_sha1_file(sha1
, &type
, &size
);
268 sp
= strstr(buf
, "\n\n");
270 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
274 sp
+= 2; /* skip the 2 LFs */
275 eob
= strstr(sp
, "\n" PGP_SIGNATURE
"\n");
279 len
= buf
+ size
- sp
;
280 write_or_die(fd
, sp
, len
);
285 static void create_tag(const unsigned char *object
, const char *tag
,
286 struct strbuf
*buf
, int message
, int sign
,
287 unsigned char *prev
, unsigned char *result
)
289 enum object_type type
;
290 char header_buf
[1024];
293 type
= sha1_object_info(object
, NULL
);
294 if (type
<= OBJ_NONE
)
295 die("bad object type.");
297 header_len
= snprintf(header_buf
, sizeof(header_buf
),
305 git_committer_info(IDENT_ERROR_ON_NO_NAME
));
307 if (header_len
> sizeof(header_buf
) - 1)
308 die("tag header too big.");
314 /* write the template message before editing: */
315 path
= xstrdup(git_path("TAG_EDITMSG"));
316 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
318 die("could not create file '%s': %s",
319 path
, strerror(errno
));
321 if (!is_null_sha1(prev
))
322 write_tag_body(fd
, prev
);
324 write_or_die(fd
, tag_template
, strlen(tag_template
));
327 launch_editor(path
, buf
, NULL
);
335 if (!message
&& !buf
->len
)
336 die("no tag message?");
338 strbuf_insert(buf
, 0, header_buf
, header_len
);
340 if (sign
&& do_sign(buf
) < 0)
341 die("unable to sign the tag");
342 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
343 die("unable to write tag file");
351 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
353 struct msg_arg
*msg
= opt
->value
;
358 strbuf_addstr(&(msg
->buf
), "\n\n");
359 strbuf_addstr(&(msg
->buf
), arg
);
364 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
367 unsigned char object
[20], prev
[20];
369 const char *object_ref
, *tag
;
370 struct ref_lock
*lock
;
372 int annotate
= 0, sign
= 0, force
= 0, lines
= 0,
373 list
= 0, delete = 0, verify
= 0;
374 char *msgfile
= NULL
, *keyid
= NULL
;
375 struct msg_arg msg
= { 0, STRBUF_INIT
};
376 struct option options
[] = {
377 OPT_BOOLEAN('l', NULL
, &list
, "list tag names"),
378 { OPTION_INTEGER
, 'n', NULL
, &lines
, NULL
,
379 "print n lines of each tag message",
380 PARSE_OPT_OPTARG
, NULL
, 1 },
381 OPT_BOOLEAN('d', NULL
, &delete, "delete tags"),
382 OPT_BOOLEAN('v', NULL
, &verify
, "verify tags"),
384 OPT_GROUP("Tag creation options"),
385 OPT_BOOLEAN('a', NULL
, &annotate
,
386 "annotated tag, needs a message"),
387 OPT_CALLBACK('m', NULL
, &msg
, "msg",
388 "message for the tag", parse_msg_arg
),
389 OPT_STRING('F', NULL
, &msgfile
, "file", "message in a file"),
390 OPT_BOOLEAN('s', NULL
, &sign
, "annotated and GPG-signed tag"),
391 OPT_STRING('u', NULL
, &keyid
, "key-id",
392 "use another key to sign the tag"),
393 OPT_BOOLEAN('f', NULL
, &force
, "replace the tag if exists"),
397 git_config(git_tag_config
);
399 argc
= parse_options(argc
, argv
, options
, git_tag_usage
, 0);
403 set_signingkey(keyid
);
409 return list_tags(argv
[0], lines
);
411 return for_each_tag_name(argv
, delete_tag
);
413 return for_each_tag_name(argv
, verify_tag
);
415 strbuf_init(&buf
, 0);
416 if (msg
.given
|| msgfile
) {
417 if (msg
.given
&& msgfile
)
418 die("only one -F or -m option is allowed.");
421 strbuf_addbuf(&buf
, &(msg
.buf
));
423 if (!strcmp(msgfile
, "-")) {
424 if (strbuf_read(&buf
, 0, 1024) < 0)
425 die("cannot read %s", msgfile
);
427 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
428 die("could not open or read '%s': %s",
429 msgfile
, strerror(errno
));
436 usage_with_options(git_tag_usage
, options
);
437 return list_tags(NULL
, lines
);
441 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
443 die("too many params");
445 if (get_sha1(object_ref
, object
))
446 die("Failed to resolve '%s' as a valid ref.", object_ref
);
448 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
449 die("tag name too long: %.*s...", 50, tag
);
450 if (check_ref_format(ref
))
451 die("'%s' is not a valid tag name.", tag
);
453 if (!resolve_ref(ref
, prev
, 1, NULL
))
456 die("tag '%s' already exists", tag
);
459 create_tag(object
, tag
, &buf
, msg
.given
|| msgfile
,
462 lock
= lock_any_ref_for_update(ref
, prev
, 0);
464 die("%s: cannot lock the ref", ref
);
465 if (write_ref_sha1(lock
, object
, NULL
) < 0)
466 die("%s: cannot update the ref", ref
);
468 strbuf_release(&buf
);