Rewrite convert_to_{git,working_tree} to use strbuf's.
[git/dscho.git] / builtin-tag.c
blob9f293421da389e5a0a50edf8ef269c5065483746
1 /*
2 * Builtin "git tag"
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.
7 */
9 #include "cache.h"
10 #include "builtin.h"
11 #include "strbuf.h"
12 #include "refs.h"
13 #include "tag.h"
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;
25 const char *args[3];
26 int fd;
28 editor = getenv("GIT_EDITOR");
29 if (!editor && editor_program)
30 editor = editor_program;
31 if (!editor)
32 editor = getenv("VISUAL");
33 if (!editor)
34 editor = getenv("EDITOR");
36 terminal = getenv("TERM");
37 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
38 fprintf(stderr,
39 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
40 "Please supply the message using either -m or -F option.\n");
41 exit(1);
44 if (!editor)
45 editor = "vi";
47 memset(&child, 0, sizeof(child));
48 child.argv = args;
49 args[0] = editor;
50 args[1] = path;
51 args[2] = NULL;
53 if (run_command(&child))
54 die("There was a problem with the editor %s.", editor);
56 fd = open(path, O_RDONLY);
57 if (fd < 0)
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));
62 close(fd);
65 struct tag_filter {
66 const char *pattern;
67 int lines;
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)) {
78 int i;
79 unsigned long size;
80 enum object_type type;
81 char *buf, *sp, *eol;
82 size_t len;
84 if (!filter->lines) {
85 printf("%s\n", refname);
86 return 0;
88 printf("%-15s ", refname);
90 sp = buf = read_sha1_file(sha1, &type, &size);
91 if (!buf)
92 return 0;
93 if (!size) {
94 free(buf);
95 return 0;
97 /* skip header */
98 while (sp + 1 < buf + size &&
99 !(sp[0] == '\n' && sp[1] == '\n'))
100 sp++;
101 /* only take up to "lines" lines, and strip the signature */
102 for (i = 0, sp += 2;
103 i < filter->lines && sp < buf + size &&
104 prefixcmp(sp, PGP_SIGNATURE "\n");
105 i++) {
106 if (i)
107 printf("\n ");
108 eol = memchr(sp, '\n', size - (sp - buf));
109 len = eol ? eol - sp : size - (sp - buf);
110 fwrite(sp, len, 1, stdout);
111 if (!eol)
112 break;
113 sp = eol + 1;
115 putchar('\n');
116 free(buf);
119 return 0;
122 static int list_tags(const char *pattern, int lines)
124 struct tag_filter filter;
126 if (pattern == NULL)
127 pattern = "*";
129 filter.pattern = pattern;
130 filter.lines = lines;
132 for_each_tag_ref(show_reference, (void *) &filter);
134 return 0;
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)
142 const char **p;
143 char ref[PATH_MAX];
144 int had_error = 0;
145 unsigned char sha1[20];
147 for (p = argv; *p; p++) {
148 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
149 >= sizeof(ref)) {
150 error("tag name too long: %.*s...", 50, *p);
151 had_error = 1;
152 continue;
154 if (!resolve_ref(ref, sha1, 1, NULL)) {
155 error("tag '%s' not found.", *p);
156 had_error = 1;
157 continue;
159 if (fn(*p, ref, sha1))
160 had_error = 1;
162 return had_error;
165 static int delete_tag(const char *name, const char *ref,
166 const unsigned char *sha1)
168 if (delete_ref(ref, sha1))
169 return 1;
170 printf("Deleted tag '%s'\n", name);
171 return 0;
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);
183 return 0;
186 static int do_sign(struct strbuf *buffer)
188 struct child_process gpg;
189 const char *args[4];
190 char *bracket;
191 int len;
193 if (!*signingkey) {
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, '>');
198 if (bracket)
199 bracket[1] = '\0';
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));
207 gpg.argv = args;
208 gpg.in = -1;
209 gpg.out = -1;
210 args[0] = "gpg";
211 args[1] = "-bsau";
212 args[2] = signingkey;
213 args[3] = NULL;
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) {
219 close(gpg.in);
220 finish_command(&gpg);
221 return error("gpg did not accept the tag data");
223 close(gpg.in);
224 gpg.close_in = 0;
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");
230 if (len < 0)
231 return error("could not read the entire signature from gpg.");
233 return 0;
236 static const char tag_template[] =
237 "\n"
238 "#\n"
239 "# Write a tag message\n"
240 "#\n";
242 static int git_tag_config(const char *var, const char *value)
244 if (!strcmp(var, "user.signingkey")) {
245 if (!value)
246 die("user.signingkey without value");
247 if (strlcpy(signingkey, value, sizeof(signingkey))
248 >= sizeof(signingkey))
249 die("user.signingkey value too long");
250 return 0;
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];
262 int header_len;
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),
269 "object %s\n"
270 "type %s\n"
271 "tag %s\n"
272 "tagger %s\n\n",
273 sha1_to_hex(object),
274 typename(type),
275 tag,
276 git_committer_info(1));
278 if (header_len > sizeof(header_buf) - 1)
279 die("tag header too big.");
281 if (!message) {
282 char *path;
283 int fd;
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);
288 if (fd < 0)
289 die("could not create file '%s': %s",
290 path, strerror(errno));
291 write_or_die(fd, tag_template, strlen(tag_template));
292 close(fd);
294 launch_editor(path, buf);
296 unlink(path);
297 free(path);
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: */
306 if (buf->len)
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)
318 struct strbuf buf;
319 unsigned char object[20], prev[20];
320 int annotate = 0, sign = 0, force = 0, lines = 0, message = 0;
321 char ref[PATH_MAX];
322 const char *object_ref, *tag;
323 int i;
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];
332 if (arg[0] != '-')
333 break;
334 if (!strcmp(arg, "-a")) {
335 annotate = 1;
336 continue;
338 if (!strcmp(arg, "-s")) {
339 annotate = 1;
340 sign = 1;
341 continue;
343 if (!strcmp(arg, "-f")) {
344 force = 1;
345 continue;
347 if (!strcmp(arg, "-n")) {
348 if (i + 1 == argc || *argv[i + 1] == '-')
349 /* no argument */
350 lines = 1;
351 else
352 lines = isdigit(*argv[++i]) ?
353 atoi(argv[i]) : 1;
354 continue;
356 if (!strcmp(arg, "-m")) {
357 annotate = 1;
358 i++;
359 if (i == argc)
360 die("option -m needs an argument.");
361 if (message)
362 die("only one -F or -m option is allowed.");
363 strbuf_addstr(&buf, argv[i]);
364 message = 1;
365 continue;
367 if (!strcmp(arg, "-F")) {
368 int fd;
370 annotate = 1;
371 i++;
372 if (i == argc)
373 die("option -F needs an argument.");
374 if (message)
375 die("only one -F or -m option is allowed.");
377 if (!strcmp(argv[i], "-"))
378 fd = 0;
379 else {
380 fd = open(argv[i], O_RDONLY);
381 if (fd < 0)
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]);
388 message = 1;
389 continue;
391 if (!strcmp(arg, "-u")) {
392 annotate = 1;
393 sign = 1;
394 i++;
395 if (i == argc)
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");
400 continue;
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);
411 if (i == argc) {
412 if (annotate)
413 usage(builtin_tag_usage);
414 return list_tags(NULL, lines);
416 tag = argv[i++];
418 object_ref = i < argc ? argv[i] : "HEAD";
419 if (i + 1 < argc)
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))
431 hashclr(prev);
432 else if (!force)
433 die("tag '%s' already exists", tag);
435 if (annotate)
436 create_tag(object, tag, &buf, message, sign, object);
438 lock = lock_any_ref_for_update(ref, prev, 0);
439 if (!lock)
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);
445 return 0;