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.
14 #include "run-command.h"
16 static const char builtin_tag_usage
[] =
17 "git-tag [-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg> | -F <file>] <tagname> [<head>]";
19 static char signingkey
[1000];
21 static void launch_editor(const char *path
, struct strbuf
*buffer
)
23 const char *editor
, *terminal
;
24 struct child_process child
;
28 editor
= getenv("GIT_EDITOR");
29 if (!editor
&& editor_program
)
30 editor
= editor_program
;
32 editor
= getenv("VISUAL");
34 editor
= getenv("EDITOR");
36 terminal
= getenv("TERM");
37 if (!editor
&& (!terminal
|| !strcmp(terminal
, "dumb"))) {
39 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
40 "Please supply the message using either -m or -F option.\n");
47 memset(&child
, 0, sizeof(child
));
53 if (run_command(&child
))
54 die("There was a problem with the editor %s.", editor
);
56 fd
= open(path
, O_RDONLY
);
58 die("could not open '%s': %s", path
, strerror(errno
));
59 if (strbuf_read(buffer
, fd
, 0) < 0) {
60 die("could not read message file '%s': %s", path
, strerror(errno
));
70 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
72 static int show_reference(const char *refname
, const unsigned char *sha1
,
73 int flag
, void *cb_data
)
75 struct tag_filter
*filter
= cb_data
;
77 if (!fnmatch(filter
->pattern
, refname
, 0)) {
80 enum object_type type
;
85 printf("%s\n", refname
);
88 printf("%-15s ", refname
);
90 sp
= buf
= read_sha1_file(sha1
, &type
, &size
);
98 while (sp
+ 1 < buf
+ size
&&
99 !(sp
[0] == '\n' && sp
[1] == '\n'))
101 /* only take up to "lines" lines, and strip the signature */
103 i
< filter
->lines
&& sp
< buf
+ size
&&
104 prefixcmp(sp
, PGP_SIGNATURE
"\n");
108 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
109 len
= eol
? eol
- sp
: size
- (sp
- buf
);
110 fwrite(sp
, len
, 1, stdout
);
122 static int list_tags(const char *pattern
, int lines
)
124 struct tag_filter filter
;
129 filter
.pattern
= pattern
;
130 filter
.lines
= lines
;
132 for_each_tag_ref(show_reference
, (void *) &filter
);
137 typedef int (*each_tag_name_fn
)(const char *name
, const char *ref
,
138 const unsigned char *sha1
);
140 static int for_each_tag_name(const char **argv
, each_tag_name_fn fn
)
145 unsigned char sha1
[20];
147 for (p
= argv
; *p
; p
++) {
148 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", *p
)
150 error("tag name too long: %.*s...", 50, *p
);
154 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
155 error("tag '%s' not found.", *p
);
159 if (fn(*p
, ref
, sha1
))
165 static int delete_tag(const char *name
, const char *ref
,
166 const unsigned char *sha1
)
168 if (delete_ref(ref
, sha1
))
170 printf("Deleted tag '%s'\n", name
);
174 static int verify_tag(const char *name
, const char *ref
,
175 const unsigned char *sha1
)
177 const char *argv_verify_tag
[] = {"git-verify-tag",
178 "-v", "SHA1_HEX", NULL
};
179 argv_verify_tag
[2] = sha1_to_hex(sha1
);
181 if (run_command_v_opt(argv_verify_tag
, 0))
182 return error("could not verify the tag '%s'", name
);
186 static int do_sign(struct strbuf
*buffer
)
188 struct child_process gpg
;
194 if (strlcpy(signingkey
, git_committer_info(1),
195 sizeof(signingkey
)) > sizeof(signingkey
) - 1)
196 return error("committer info too long.");
197 bracket
= strchr(signingkey
, '>');
202 /* When the username signingkey is bad, program could be terminated
203 * because gpg exits without reading and then write gets SIGPIPE. */
204 signal(SIGPIPE
, SIG_IGN
);
206 memset(&gpg
, 0, sizeof(gpg
));
212 args
[2] = signingkey
;
215 if (start_command(&gpg
))
216 return error("could not run gpg.");
218 if (write_in_full(gpg
.in
, buffer
->buf
, buffer
->len
) != buffer
->len
) {
220 finish_command(&gpg
);
221 return error("gpg did not accept the tag data");
225 len
= strbuf_read(buffer
, gpg
.out
, 1024);
227 if (finish_command(&gpg
) || !len
|| len
< 0)
228 return error("gpg failed to sign the tag");
231 return error("could not read the entire signature from gpg.");
236 static const char tag_template
[] =
239 "# Write a tag message\n"
242 static int git_tag_config(const char *var
, const char *value
)
244 if (!strcmp(var
, "user.signingkey")) {
246 die("user.signingkey without value");
247 if (strlcpy(signingkey
, value
, sizeof(signingkey
))
248 >= sizeof(signingkey
))
249 die("user.signingkey value too long");
253 return git_default_config(var
, value
);
256 static void create_tag(const unsigned char *object
, const char *tag
,
257 struct strbuf
*buf
, int message
, int sign
,
258 unsigned char *result
)
260 enum object_type type
;
261 char header_buf
[1024];
264 type
= sha1_object_info(object
, NULL
);
265 if (type
<= OBJ_NONE
)
266 die("bad object type.");
268 header_len
= snprintf(header_buf
, sizeof(header_buf
),
276 git_committer_info(1));
278 if (header_len
> sizeof(header_buf
) - 1)
279 die("tag header too big.");
285 /* write the template message before editing: */
286 path
= xstrdup(git_path("TAG_EDITMSG"));
287 fd
= open(path
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
289 die("could not create file '%s': %s",
290 path
, strerror(errno
));
291 write_or_die(fd
, tag_template
, strlen(tag_template
));
294 launch_editor(path
, buf
);
300 strbuf_setlen(buf
, stripspace(buf
->buf
, buf
->len
, 1));
302 if (!message
&& !buf
->len
)
303 die("no tag message?");
305 /* insert the header and add the '\n' if needed: */
307 strbuf_addch(buf
, '\n');
308 strbuf_insert(buf
, 0, header_buf
, header_len
);
310 if (sign
&& do_sign(buf
) < 0)
311 die("unable to sign the tag");
312 if (write_sha1_file(buf
->buf
, buf
->len
, tag_type
, result
) < 0)
313 die("unable to write tag file");
316 int cmd_tag(int argc
, const char **argv
, const char *prefix
)
319 unsigned char object
[20], prev
[20];
320 int annotate
= 0, sign
= 0, force
= 0, lines
= 0, message
= 0;
322 const char *object_ref
, *tag
;
324 struct ref_lock
*lock
;
326 git_config(git_tag_config
);
327 strbuf_init(&buf
, 0);
329 for (i
= 1; i
< argc
; i
++) {
330 const char *arg
= argv
[i
];
334 if (!strcmp(arg
, "-a")) {
338 if (!strcmp(arg
, "-s")) {
343 if (!strcmp(arg
, "-f")) {
347 if (!strcmp(arg
, "-n")) {
348 if (i
+ 1 == argc
|| *argv
[i
+ 1] == '-')
352 lines
= isdigit(*argv
[++i
]) ?
356 if (!strcmp(arg
, "-m")) {
360 die("option -m needs an argument.");
362 die("only one -F or -m option is allowed.");
363 strbuf_addstr(&buf
, argv
[i
]);
367 if (!strcmp(arg
, "-F")) {
373 die("option -F needs an argument.");
375 die("only one -F or -m option is allowed.");
377 if (!strcmp(argv
[i
], "-"))
380 fd
= open(argv
[i
], O_RDONLY
);
382 die("could not open '%s': %s",
383 argv
[i
], strerror(errno
));
385 if (strbuf_read(&buf
, fd
, 1024) < 0) {
386 die("cannot read %s", argv
[i
]);
391 if (!strcmp(arg
, "-u")) {
396 die("option -u needs an argument.");
397 if (strlcpy(signingkey
, argv
[i
], sizeof(signingkey
))
398 >= sizeof(signingkey
))
399 die("argument to option -u too long");
402 if (!strcmp(arg
, "-l"))
403 return list_tags(argv
[i
+ 1], lines
);
404 if (!strcmp(arg
, "-d"))
405 return for_each_tag_name(argv
+ i
+ 1, delete_tag
);
406 if (!strcmp(arg
, "-v"))
407 return for_each_tag_name(argv
+ i
+ 1, verify_tag
);
408 usage(builtin_tag_usage
);
413 usage(builtin_tag_usage
);
414 return list_tags(NULL
, lines
);
418 object_ref
= i
< argc
? argv
[i
] : "HEAD";
420 die("too many params");
422 if (get_sha1(object_ref
, object
))
423 die("Failed to resolve '%s' as a valid ref.", object_ref
);
425 if (snprintf(ref
, sizeof(ref
), "refs/tags/%s", tag
) > sizeof(ref
) - 1)
426 die("tag name too long: %.*s...", 50, tag
);
427 if (check_ref_format(ref
))
428 die("'%s' is not a valid tag name.", tag
);
430 if (!resolve_ref(ref
, prev
, 1, NULL
))
433 die("tag '%s' already exists", tag
);
436 create_tag(object
, tag
, &buf
, message
, sign
, object
);
438 lock
= lock_any_ref_for_update(ref
, prev
, 0);
440 die("%s: cannot lock the ref", ref
);
441 if (write_ref_sha1(lock
, object
, NULL
) < 0)
442 die("%s: cannot update the ref", ref
);
444 strbuf_release(&buf
);