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];
28 static int core_clock_skew
= 86400;
33 struct commit_list
*with_commit
;
36 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
38 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
40 for (; want
; want
= want
->next
)
41 if (!hashcmp(want
->item
->object
.sha1
, c
->object
.sha1
))
46 static int contains_recurse(struct commit
*candidate
,
47 const struct commit_list
*want
,
50 struct commit_list
*p
;
52 /* was it previously marked as containing a want commit? */
53 if (candidate
->object
.flags
& TMP_MARK
)
55 /* or marked as not possibly containing a want commit? */
56 if (candidate
->object
.flags
& UNINTERESTING
)
59 if (in_commit_list(want
, candidate
))
62 if (parse_commit(candidate
) < 0)
65 /* stop searching if we go too far back in time */
66 if (candidate
->date
< cutoff
)
69 /* Otherwise recurse and mark ourselves for future traversals. */
70 for (p
= candidate
->parents
; p
; p
= p
->next
) {
71 if (contains_recurse(p
->item
, want
, cutoff
)) {
72 candidate
->object
.flags
|= TMP_MARK
;
76 candidate
->object
.flags
|= UNINTERESTING
;
80 static int contains(struct commit
*candidate
, const struct commit_list
*want
)
82 unsigned long cutoff
= 0;
84 if (core_clock_skew
>= 0) {
85 const struct commit_list
*c
;
86 unsigned long min_date
= ULONG_MAX
;
87 for (c
= want
; c
; c
= c
->next
) {
88 if (parse_commit(c
->item
) < 0)
90 if (c
->item
->date
< min_date
)
91 min_date
= c
->item
->date
;
93 if (min_date
> core_clock_skew
)
94 cutoff
= min_date
- core_clock_skew
;
97 return contains_recurse(candidate
, want
, cutoff
);
100 static int show_reference(const char *refname
, const unsigned char *sha1
,
101 int flag
, void *cb_data
)
103 struct tag_filter
*filter
= cb_data
;
105 if (!fnmatch(filter
->pattern
, refname
, 0)) {
108 enum object_type type
;
109 char *buf
, *sp
, *eol
;
112 if (filter
->with_commit
) {
113 struct commit
*commit
;
115 commit
= lookup_commit_reference_gently(sha1
, 1);
118 if (!contains(commit
, filter
->with_commit
))
122 if (!filter
->lines
) {
123 printf("%s\n", refname
);
126 printf("%-15s ", refname
);
128 buf
= read_sha1_file(sha1
, &type
, &size
);
133 sp
= strstr(buf
, "\n\n");
138 /* only take up to "lines" lines, and strip the signature */
140 i
< filter
->lines
&& sp
< buf
+ size
&&
141 prefixcmp(sp
, PGP_SIGNATURE
"\n");
145 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
146 len
= eol
? eol
- sp
: size
- (sp
- buf
);
147 fwrite(sp
, len
, 1, stdout
);
159 static int list_tags(const char *pattern
, int lines
,
160 struct commit_list
*with_commit
)
162 struct tag_filter filter
;
167 filter
.pattern
= pattern
;
168 filter
.lines
= lines
;
169 filter
.with_commit
= with_commit
;
171 for_each_tag_ref(show_reference
, (void *) &filter
);
176 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
177 const unsigned char *sha1
);
179 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
184 unsigned char sha1
[20];
186 for (p
= argv
; *p
; p
++) {
187 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
189 error("tag name too long: %.*s...", 50, *p
);
193 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
194 error("tag '%s' not found.", *p
);
198 if (fn(*p
, ref
, sha1
))
204 static int delete_tag(const char *name
, const char *ref
,
205 const unsigned char *sha1
)
207 if (delete_ref(ref
, sha1
, 0))
209 printf("Deleted tag '%s' (was %s)\n", name
, find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
213 static int verify_tag(const char *name
, const char *ref
,
214 const unsigned char *sha1
)
216 const char *argv_verify_tag
[] = {"verify-tag",
217 "-v", "SHA1_HEX", NULL
};
218 argv_verify_tag
[2] = sha1_to_hex(sha1
);
220 if (run_command_v_opt(argv_verify_tag
, RUN_GIT_CMD
))
221 return error("could not verify the tag '%s'", name
);
225 static int do_sign(struct strbuf
*buffer
)
227 struct child_process gpg
;
234 if (strlcpy(signingkey
, git_committer_info(IDENT_ERROR_ON_NO_NAME
),
235 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
236 return error("committer info too long.");
237 bracket
= strchr(signingkey
, '>');
242 /* When the username signingkey is bad, program could be terminated
243 * because gpg exits without reading and then write gets SIGPIPE. */
244 signal(SIGPIPE
, SIG_IGN
);
246 memset(&gpg
, 0, sizeof(gpg
));
252 args
[2] = signingkey
;
255 if (start_command(&gpg
))
256 return error("could not run gpg.");
258 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
261 finish_command(&gpg
);
262 return error("gpg did not accept the tag data");
265 len
= strbuf_read(buffer
, gpg
.out
, 1024);
268 if (finish_command(&gpg
) || !len
|| len
< 0)
269 return error("gpg failed to sign the tag");
271 /* Strip CR from the line endings, in case we are on Windows. */
272 for (i
= j
= 0; i
< buffer
->len
; i
++)
273 if (buffer
->buf
[i
] != '\r') {
275 buffer
->buf
[j
] = buffer
->buf
[i
];
278 strbuf_setlen(buffer
, j
);
283 static const char tag_template
[] =
286 "# Write a tag message\n"
289 static void set_signingkey(const char *value
)
291 if (strlcpy(signingkey
, value
, sizeof(signingkey
)) >= sizeof(signingkey
))
292 die("signing key value too long (%.10s...)", value
);
295 static int git_tag_config(const char *var
, const char *value
, void *cb
)
297 if (!strcmp(var
, "user.signingkey")) {
299 return config_error_nonbool(var
);
300 set_signingkey(value
);
304 if (!strcmp(var
, "core.clockskew")) {
305 if (!value
|| !strcmp(value
, "none"))
306 core_clock_skew
= -1;
308 core_clock_skew
= git_config_int(var
, value
);
312 return git_default_config(var
, value
, cb
);
315 static void write_tag_body(int fd
, const unsigned char *sha1
)
318 enum object_type type
;
319 char *buf
, *sp
, *eob
;
322 buf
= read_sha1_file(sha1
, &type
, &size
);
326 sp
= strstr(buf
, "\n\n");
328 if (!sp
|| !size
|| type
!= OBJ_TAG
) {
332 sp
+= 2; /* skip the 2 LFs */
333 eob
= strstr(sp
, "\n" PGP_SIGNATURE
"\n");
337 len
= buf
+ size
- sp
;
338 write_or_die(fd
, sp
, len
);
343 static int build_tag_object(struct strbuf
*buf
, int sign
, unsigned char *result
)
345 if (sign
&& do_sign(buf
) < 0)
346 return error("unable to sign the tag");
347 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
348 return error("unable to write tag file");
352 static void create_tag(const unsigned char *object
, const char *tag
,
353 struct strbuf
*buf
, int message
, int sign
,
354 unsigned char *prev
, unsigned char *result
)
356 enum object_type type
;
357 char header_buf
[1024];
361 type
= sha1_object_info(object
, NULL
);
362 if (type
<= OBJ_NONE
)
363 die("bad object type.");
365 header_len
= snprintf(header_buf
, sizeof(header_buf
),
373 git_committer_info(IDENT_ERROR_ON_NO_NAME
));
375 if (header_len
> sizeof(header_buf
) - 1)
376 die("tag header too big.");
381 /* write the template message before editing: */
382 path
= git_pathdup("TAG_EDITMSG");
383 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
385 die_errno("could not create file '%s'", path
);
387 if (!is_null_sha1(prev
))
388 write_tag_body(fd
, prev
);
390 write_or_die(fd
, tag_template
, strlen(tag_template
));
393 if (launch_editor(path
, buf
, NULL
)) {
395 "Please supply the message using either -m or -F option.\n");
402 if (!message
&& !buf
->len
)
403 die("no tag message?");
405 strbuf_insert(buf
, 0, header_buf
, header_len
);
407 if (build_tag_object(buf
, sign
, result
) < 0) {
409 fprintf(stderr
, "The tag message has been left in %s\n",
414 unlink_or_warn(path
);
424 static int parse_msg_arg(const struct option
*opt
, const char *arg
, int unset
)
426 struct msg_arg
*msg
= opt
->value
;
431 strbuf_addstr(&(msg
->buf
), "\n\n");
432 strbuf_addstr(&(msg
->buf
), arg
);
437 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
439 struct strbuf buf
= STRBUF_INIT
;
440 unsigned char object
[20], prev
[20];
442 const char *object_ref
, *tag
;
443 struct ref_lock
*lock
;
445 int annotate
= 0, sign
= 0, force
= 0, lines
= -1,
446 list
= 0, delete = 0, verify
= 0;
447 const char *msgfile
= NULL
, *keyid
= NULL
;
448 struct msg_arg msg
= { 0, STRBUF_INIT
};
449 struct commit_list
*with_commit
= NULL
;
450 struct option options
[] = {
451 OPT_BOOLEAN('l', NULL
, &list
, "list tag names"),
452 { OPTION_INTEGER
, 'n', NULL
, &lines
, "n",
453 "print <n> lines of each tag message",
454 PARSE_OPT_OPTARG
, NULL
, 1 },
455 OPT_BOOLEAN('d', NULL
, &delete, "delete tags"),
456 OPT_BOOLEAN('v', NULL
, &verify
, "verify tags"),
458 OPT_GROUP("Tag creation options"),
459 OPT_BOOLEAN('a', NULL
, &annotate
,
460 "annotated tag, needs a message"),
461 OPT_CALLBACK('m', NULL
, &msg
, "msg",
462 "message for the tag", parse_msg_arg
),
463 OPT_FILENAME('F', NULL
, &msgfile
, "message in a file"),
464 OPT_BOOLEAN('s', NULL
, &sign
, "annotated and GPG-signed tag"),
465 OPT_STRING('u', NULL
, &keyid
, "key-id",
466 "use another key to sign the tag"),
467 OPT_BOOLEAN('f', "force", &force
, "replace the tag if exists"),
469 OPT_GROUP("Tag listing options"),
471 OPTION_CALLBACK
, 0, "contains", &with_commit
, "commit",
472 "print only tags that contain the commit",
473 PARSE_OPT_LASTARG_DEFAULT
,
474 parse_opt_with_commit
, (intptr_t)"HEAD",
479 git_config(git_tag_config
, NULL
);
481 argc
= parse_options(argc
, argv
, prefix
, options
, git_tag_usage
, 0);
485 set_signingkey(keyid
);
489 if (argc
== 0 && !(delete || verify
))
492 if ((annotate
|| msg
.given
|| msgfile
|| force
) &&
493 (list
|| delete || verify
))
494 usage_with_options(git_tag_usage
, options
);
496 if (list
+ delete + verify
> 1)
497 usage_with_options(git_tag_usage
, options
);
499 return list_tags(argv
[0], lines
== -1 ? 0 : lines
,
502 die("-n option is only allowed with -l.");
504 die("--contains option is only allowed with -l.");
506 return for_each_tag_name(argv
, delete_tag
);
508 return for_each_tag_name(argv
, verify_tag
);
510 if (msg
.given
|| msgfile
) {
511 if (msg
.given
&& msgfile
)
512 die("only one -F or -m option is allowed.");
515 strbuf_addbuf(&buf
, &(msg
.buf
));
517 if (!strcmp(msgfile
, "-")) {
518 if (strbuf_read(&buf
, 0, 1024) < 0)
519 die_errno("cannot read '%s'", msgfile
);
521 if (strbuf_read_file(&buf
, msgfile
, 1024) < 0)
522 die_errno("could not open or read '%s'",
530 object_ref
= argc
== 2 ? argv
[1] : "HEAD";
532 die("too many params");
534 if (get_sha1(object_ref
, object
))
535 die("Failed to resolve '%s' as a valid ref.", object_ref
);
537 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
538 die("tag name too long: %.*s...", 50, tag
);
539 if (check_ref_format(ref
))
540 die("'%s' is not a valid tag name.", tag
);
542 if (!resolve_ref(ref
, prev
, 1, NULL
))
545 die("tag '%s' already exists", tag
);
548 create_tag(object
, tag
, &buf
, msg
.given
|| msgfile
,
551 lock
= lock_any_ref_for_update(ref
, prev
, 0);
553 die("%s: cannot lock the ref", ref
);
554 if (write_ref_sha1(lock
, object
, NULL
) < 0)
555 die("%s: cannot update the ref", ref
);
556 if (force
&& hashcmp(prev
, object
))
557 printf("Updated tag '%s' (was %s)\n", tag
, find_unique_abbrev(prev
, DEFAULT_ABBREV
));
559 strbuf_release(&buf
);