skip t5512 because remote does not yet work
[git/platforms/storm.git] / builtin-tag.c
blobb5997915453241c5022bd9f262e2f4d6c0bb61da
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 "refs.h"
12 #include "tag.h"
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;
24 const char *args[3];
26 editor = getenv("GIT_EDITOR");
27 if (!editor && editor_program)
28 editor = editor_program;
29 if (!editor)
30 editor = getenv("VISUAL");
31 if (!editor)
32 editor = getenv("EDITOR");
34 terminal = getenv("TERM");
35 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
36 fprintf(stderr,
37 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
38 "Please supply the message using either -m or -F option.\n");
39 exit(1);
42 if (!editor)
43 editor = "vi";
45 memset(&child, 0, sizeof(child));
46 child.argv = args;
47 args[0] = editor;
48 args[1] = path;
49 args[2] = NULL;
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));
59 struct tag_filter {
60 const char *pattern;
61 int lines;
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)) {
72 int i;
73 unsigned long size;
74 enum object_type type;
75 char *buf, *sp, *eol;
76 size_t len;
78 if (!filter->lines) {
79 printf("%s\n", refname);
80 return 0;
82 printf("%-15s ", refname);
84 buf = read_sha1_file(sha1, &type, &size);
85 if (!buf || !size)
86 return 0;
88 /* skip header */
89 sp = strstr(buf, "\n\n");
90 if (!sp) {
91 free(buf);
92 return 0;
94 /* only take up to "lines" lines, and strip the signature */
95 for (i = 0, sp += 2;
96 i < filter->lines && sp < buf + size &&
97 prefixcmp(sp, PGP_SIGNATURE "\n");
98 i++) {
99 if (i)
100 printf("\n ");
101 eol = memchr(sp, '\n', size - (sp - buf));
102 len = eol ? eol - sp : size - (sp - buf);
103 fwrite(sp, len, 1, stdout);
104 if (!eol)
105 break;
106 sp = eol + 1;
108 putchar('\n');
109 free(buf);
112 return 0;
115 static int list_tags(const char *pattern, int lines)
117 struct tag_filter filter;
119 if (pattern == NULL)
120 pattern = "*";
122 filter.pattern = pattern;
123 filter.lines = lines;
125 for_each_tag_ref(show_reference, (void *) &filter);
127 return 0;
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)
135 const char **p;
136 char ref[PATH_MAX];
137 int had_error = 0;
138 unsigned char sha1[20];
140 for (p = argv; *p; p++) {
141 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
142 >= sizeof(ref)) {
143 error("tag name too long: %.*s...", 50, *p);
144 had_error = 1;
145 continue;
147 if (!resolve_ref(ref, sha1, 1, NULL)) {
148 error("tag '%s' not found.", *p);
149 had_error = 1;
150 continue;
152 if (fn(*p, ref, sha1))
153 had_error = 1;
155 return had_error;
158 static int delete_tag(const char *name, const char *ref,
159 const unsigned char *sha1)
161 if (delete_ref(ref, sha1))
162 return 1;
163 printf("Deleted tag '%s'\n", name);
164 return 0;
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);
176 return 0;
179 static int do_sign(struct strbuf *buffer)
181 struct child_process gpg;
182 const char *args[4];
183 char *bracket;
184 int len;
186 if (!*signingkey) {
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, '>');
191 if (bracket)
192 bracket[1] = '\0';
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));
200 gpg.argv = args;
201 gpg.in = -1;
202 gpg.out = -1;
203 args[0] = "gpg";
204 args[1] = "-bsau";
205 args[2] = signingkey;
206 args[3] = NULL;
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) {
212 close(gpg.in);
213 finish_command(&gpg);
214 return error("gpg did not accept the tag data");
216 close(gpg.in);
217 gpg.close_in = 0;
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");
223 if (len < 0)
224 return error("could not read the entire signature from gpg.");
226 #ifdef __MINGW32__
227 /* strip CR from the line endings */
229 int i, j;
230 for (i = j = 0; i < buffer->len; i++)
231 if (buffer->buf[i] != '\r') {
232 if (i != j)
233 buffer->buf[j] = buffer->buf[i];
234 j++;
236 strbuf_setlen(buffer, j);
238 #endif
240 return 0;
243 static const char tag_template[] =
244 "\n"
245 "#\n"
246 "# Write a tag message\n"
247 "#\n";
249 static int git_tag_config(const char *var, const char *value)
251 if (!strcmp(var, "user.signingkey")) {
252 if (!value)
253 die("user.signingkey without value");
254 if (strlcpy(signingkey, value, sizeof(signingkey))
255 >= sizeof(signingkey))
256 die("user.signingkey value too long");
257 return 0;
260 return git_default_config(var, value);
263 static void write_tag_body(int fd, const unsigned char *sha1)
265 unsigned long size;
266 enum object_type type;
267 char *buf, *sp, *eob;
268 size_t len;
270 buf = read_sha1_file(sha1, &type, &size);
271 if (!buf)
272 return;
273 /* skip header */
274 sp = strstr(buf, "\n\n");
276 if (!sp || !size || type != OBJ_TAG) {
277 free(buf);
278 return;
280 sp += 2; /* skip the 2 LFs */
281 eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
282 if (eob)
283 len = eob - sp;
284 else
285 len = buf + size - sp;
286 write_or_die(fd, sp, len);
288 free(buf);
291 static void create_tag(const unsigned char *object, const char *tag,
292 struct strbuf *buf, int message, int sign,
293 unsigned char *prev, unsigned char *result)
295 enum object_type type;
296 char header_buf[1024];
297 int header_len;
299 type = sha1_object_info(object, NULL);
300 if (type <= OBJ_NONE)
301 die("bad object type.");
303 header_len = snprintf(header_buf, sizeof(header_buf),
304 "object %s\n"
305 "type %s\n"
306 "tag %s\n"
307 "tagger %s\n\n",
308 sha1_to_hex(object),
309 typename(type),
310 tag,
311 git_committer_info(1));
313 if (header_len > sizeof(header_buf) - 1)
314 die("tag header too big.");
316 if (!message) {
317 char *path;
318 int fd;
320 /* write the template message before editing: */
321 path = xstrdup(git_path("TAG_EDITMSG"));
322 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
323 if (fd < 0)
324 die("could not create file '%s': %s",
325 path, strerror(errno));
327 if (!is_null_sha1(prev))
328 write_tag_body(fd, prev);
329 else
330 write_or_die(fd, tag_template, strlen(tag_template));
331 close(fd);
333 launch_editor(path, buf);
335 unlink(path);
336 free(path);
339 stripspace(buf, 1);
341 if (!message && !buf->len)
342 die("no tag message?");
344 strbuf_insert(buf, 0, header_buf, header_len);
346 if (sign && do_sign(buf) < 0)
347 die("unable to sign the tag");
348 if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
349 die("unable to write tag file");
352 int cmd_tag(int argc, const char **argv, const char *prefix)
354 struct strbuf buf;
355 unsigned char object[20], prev[20];
356 int annotate = 0, sign = 0, force = 0, lines = 0, message = 0;
357 char ref[PATH_MAX];
358 const char *object_ref, *tag;
359 int i;
360 struct ref_lock *lock;
362 git_config(git_tag_config);
363 strbuf_init(&buf, 0);
365 for (i = 1; i < argc; i++) {
366 const char *arg = argv[i];
368 if (arg[0] != '-')
369 break;
370 if (!strcmp(arg, "-a")) {
371 annotate = 1;
372 continue;
374 if (!strcmp(arg, "-s")) {
375 annotate = 1;
376 sign = 1;
377 continue;
379 if (!strcmp(arg, "-f")) {
380 force = 1;
381 continue;
383 if (!strcmp(arg, "-n")) {
384 if (i + 1 == argc || *argv[i + 1] == '-')
385 /* no argument */
386 lines = 1;
387 else
388 lines = isdigit(*argv[++i]) ?
389 atoi(argv[i]) : 1;
390 continue;
392 if (!strcmp(arg, "-m")) {
393 annotate = 1;
394 i++;
395 if (i == argc)
396 die("option -m needs an argument.");
397 if (message)
398 die("only one -F or -m option is allowed.");
399 strbuf_addstr(&buf, argv[i]);
400 message = 1;
401 continue;
403 if (!strcmp(arg, "-F")) {
404 annotate = 1;
405 i++;
406 if (i == argc)
407 die("option -F needs an argument.");
408 if (message)
409 die("only one -F or -m option is allowed.");
411 if (!strcmp(argv[i], "-")) {
412 if (strbuf_read(&buf, 0, 1024) < 0)
413 die("cannot read %s", argv[i]);
414 } else {
415 if (strbuf_read_file(&buf, argv[i], 1024) < 0)
416 die("could not open or read '%s': %s",
417 argv[i], strerror(errno));
419 message = 1;
420 continue;
422 if (!strcmp(arg, "-u")) {
423 annotate = 1;
424 sign = 1;
425 i++;
426 if (i == argc)
427 die("option -u needs an argument.");
428 if (strlcpy(signingkey, argv[i], sizeof(signingkey))
429 >= sizeof(signingkey))
430 die("argument to option -u too long");
431 continue;
433 if (!strcmp(arg, "-l"))
434 return list_tags(argv[i + 1], lines);
435 if (!strcmp(arg, "-d"))
436 return for_each_tag_name(argv + i + 1, delete_tag);
437 if (!strcmp(arg, "-v"))
438 return for_each_tag_name(argv + i + 1, verify_tag);
439 usage(builtin_tag_usage);
442 if (i == argc) {
443 if (annotate)
444 usage(builtin_tag_usage);
445 return list_tags(NULL, lines);
447 tag = argv[i++];
449 object_ref = i < argc ? argv[i] : "HEAD";
450 if (i + 1 < argc)
451 die("too many params");
453 if (get_sha1(object_ref, object))
454 die("Failed to resolve '%s' as a valid ref.", object_ref);
456 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
457 die("tag name too long: %.*s...", 50, tag);
458 if (check_ref_format(ref))
459 die("'%s' is not a valid tag name.", tag);
461 if (!resolve_ref(ref, prev, 1, NULL))
462 hashclr(prev);
463 else if (!force)
464 die("tag '%s' already exists", tag);
466 if (annotate)
467 create_tag(object, tag, &buf, message, sign, prev, object);
469 lock = lock_any_ref_for_update(ref, prev, 0);
470 if (!lock)
471 die("%s: cannot lock the ref", ref);
472 if (write_ref_sha1(lock, object, NULL) < 0)
473 die("%s: cannot update the ref", ref);
475 strbuf_release(&buf);
476 return 0;