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"
15 static const char builtin_tag_usage
[] =
16 "git-tag [-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg> | -F <file>] <tagname> [<head>]";
18 static char signingkey
[1000];
20 static void launch_editor(const char *path
, struct strbuf
*buffer
)
22 const char *editor
, *terminal
;
23 struct child_process child
;
26 editor
= getenv("GIT_EDITOR");
27 if (!editor
&& editor_program
)
28 editor
= editor_program
;
30 editor
= getenv("VISUAL");
32 editor
= getenv("EDITOR");
34 terminal
= getenv("TERM");
35 if (!editor
&& (!terminal
|| !strcmp(terminal
, "dumb"))) {
37 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
38 "Please supply the message using either -m or -F option.\n");
45 memset(&child
, 0, sizeof(child
));
51 if (run_command(&child
))
52 die("There was a problem with the editor %s.", editor
);
54 if (strbuf_read_file(buffer
, path
, 0) < 0)
55 die("could not read message file '%s': %s",
56 path
, strerror(errno
));
64 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
66 static int show_reference(const char *refname
, const unsigned char *sha1
,
67 int flag
, void *cb_data
)
69 struct tag_filter
*filter
= cb_data
;
71 if (!fnmatch(filter
->pattern
, refname
, 0)) {
74 enum object_type type
;
79 printf("%s\n", refname
);
82 printf("%-15s ", refname
);
84 buf
= read_sha1_file(sha1
, &type
, &size
);
89 sp
= strstr(buf
, "\n\n");
94 /* only take up to "lines" lines, and strip the signature */
96 i
< filter
->lines
&& sp
< buf
+ size
&&
97 prefixcmp(sp
, PGP_SIGNATURE
"\n");
101 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
102 len
= eol
? eol
- sp
: size
- (sp
- buf
);
103 fwrite(sp
, len
, 1, stdout
);
115 static int list_tags(const char *pattern
, int lines
)
117 struct tag_filter filter
;
122 filter
.pattern
= pattern
;
123 filter
.lines
= lines
;
125 for_each_tag_ref(show_reference
, (void *) &filter
);
130 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
131 const unsigned char *sha1
);
133 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
138 unsigned char sha1
[20];
140 for (p
= argv
; *p
; p
++) {
141 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
143 error("tag name too long: %.*s...", 50, *p
);
147 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
148 error("tag '%s' not found.", *p
);
152 if (fn(*p
, ref
, sha1
))
158 static int delete_tag(const char *name
, const char *ref
,
159 const unsigned char *sha1
)
161 if (delete_ref(ref
, sha1
))
163 printf("Deleted tag '%s'\n", name
);
167 static int verify_tag(const char *name
, const char *ref
,
168 const unsigned char *sha1
)
170 const char *argv_verify_tag
[] = {"git-verify-tag",
171 "-v", "SHA1_HEX", NULL
};
172 argv_verify_tag
[2] = sha1_to_hex(sha1
);
174 if (run_command_v_opt(argv_verify_tag
, 0))
175 return error("could not verify the tag '%s'", name
);
179 static int do_sign(struct strbuf
*buffer
)
181 struct child_process gpg
;
187 if (strlcpy(signingkey
, git_committer_info(1),
188 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
189 return error("committer info too long.");
190 bracket
= strchr(signingkey
, '>');
195 /* When the username signingkey is bad, program could be terminated
196 * because gpg exits without reading and then write gets SIGPIPE. */
197 signal(SIGPIPE
, SIG_IGN
);
199 memset(&gpg
, 0, sizeof(gpg
));
205 args
[2] = signingkey
;
208 if (start_command(&gpg
))
209 return error("could not run gpg.");
211 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
213 finish_command(&gpg
);
214 return error("gpg did not accept the tag data");
218 len
= strbuf_read(buffer
, gpg
.out
, 1024);
220 if (finish_command(&gpg
) || !len
|| len
< 0)
221 return error("gpg failed to sign the tag");
224 return error("could not read the entire signature from gpg.");
229 static const char tag_template
[] =
232 "# Write a tag message\n"
235 static int git_tag_config(const char *var
, const char *value
)
237 if (!strcmp(var
, "user.signingkey")) {
239 die("user.signingkey without value");
240 if (strlcpy(signingkey
, value
, sizeof(signingkey
))
241 >= sizeof(signingkey
))
242 die("user.signingkey value too long");
246 return git_default_config(var
, value
);
249 static void create_tag(const unsigned char *object
, const char *tag
,
250 struct strbuf
*buf
, int message
, int sign
,
251 unsigned char *result
)
253 enum object_type type
;
254 char header_buf
[1024];
257 type
= sha1_object_info(object
, NULL
);
258 if (type
<= OBJ_NONE
)
259 die("bad object type.");
261 header_len
= snprintf(header_buf
, sizeof(header_buf
),
269 git_committer_info(1));
271 if (header_len
> sizeof(header_buf
) - 1)
272 die("tag header too big.");
278 /* write the template message before editing: */
279 path
= xstrdup(git_path("TAG_EDITMSG"));
280 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
282 die("could not create file '%s': %s",
283 path
, strerror(errno
));
284 write_or_die(fd
, tag_template
, strlen(tag_template
));
287 launch_editor(path
, buf
);
295 if (!message
&& !buf
->len
)
296 die("no tag message?");
298 strbuf_insert(buf
, 0, header_buf
, header_len
);
300 if (sign
&& do_sign(buf
) < 0)
301 die("unable to sign the tag");
302 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
303 die("unable to write tag file");
306 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
309 unsigned char object
[20], prev
[20];
310 int annotate
= 0, sign
= 0, force
= 0, lines
= 0, message
= 0;
312 const char *object_ref
, *tag
;
314 struct ref_lock
*lock
;
316 git_config(git_tag_config
);
317 strbuf_init(&buf
, 0);
319 for (i
= 1; i
< argc
; i
++) {
320 const char *arg
= argv
[i
];
324 if (!strcmp(arg
, "-a")) {
328 if (!strcmp(arg
, "-s")) {
333 if (!strcmp(arg
, "-f")) {
337 if (!strcmp(arg
, "-n")) {
338 if (i
+ 1 == argc
|| *argv
[i
+ 1] == '-')
342 lines
= isdigit(*argv
[++i
]) ?
346 if (!strcmp(arg
, "-m")) {
350 die("option -m needs an argument.");
352 die("only one -F or -m option is allowed.");
353 strbuf_addstr(&buf
, argv
[i
]);
357 if (!strcmp(arg
, "-F")) {
361 die("option -F needs an argument.");
363 die("only one -F or -m option is allowed.");
365 if (!strcmp(argv
[i
], "-")) {
366 if (strbuf_read(&buf
, 0, 1024) < 0)
367 die("cannot read %s", argv
[i
]);
369 if (strbuf_read_file(&buf
, argv
[i
], 1024) < 0)
370 die("could not open or read '%s': %s",
371 argv
[i
], strerror(errno
));
376 if (!strcmp(arg
, "-u")) {
381 die("option -u needs an argument.");
382 if (strlcpy(signingkey
, argv
[i
], sizeof(signingkey
))
383 >= sizeof(signingkey
))
384 die("argument to option -u too long");
387 if (!strcmp(arg
, "-l"))
388 return list_tags(argv
[i
+ 1], lines
);
389 if (!strcmp(arg
, "-d"))
390 return for_each_tag_name(argv
+ i
+ 1, delete_tag
);
391 if (!strcmp(arg
, "-v"))
392 return for_each_tag_name(argv
+ i
+ 1, verify_tag
);
393 usage(builtin_tag_usage
);
398 usage(builtin_tag_usage
);
399 return list_tags(NULL
, lines
);
403 object_ref
= i
< argc
? argv
[i
] : "HEAD";
405 die("too many params");
407 if (get_sha1(object_ref
, object
))
408 die("Failed to resolve '%s' as a valid ref.", object_ref
);
410 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
411 die("tag name too long: %.*s...", 50, tag
);
412 if (check_ref_format(ref
))
413 die("'%s' is not a valid tag name.", tag
);
415 if (!resolve_ref(ref
, prev
, 1, NULL
))
418 die("tag '%s' already exists", tag
);
421 create_tag(object
, tag
, &buf
, message
, sign
, object
);
423 lock
= lock_any_ref_for_update(ref
, prev
, 0);
425 die("%s: cannot lock the ref", ref
);
426 if (write_ref_sha1(lock
, object
, NULL
) < 0)
427 die("%s: cannot update the ref", ref
);
429 strbuf_release(&buf
);