Merge branch 'pu' of git://repo.or.cz/git-gui into devel
[git/platforms.git] / builtin-tag.c
blob381f05176137c9371f23e9a332e6220a283f0d0f
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, char **buffer, unsigned long *len)
22 const char *editor, *terminal;
23 struct child_process child;
24 const char *args[3];
25 int fd;
27 editor = getenv("GIT_EDITOR");
28 if (!editor && editor_program)
29 editor = editor_program;
30 if (!editor)
31 editor = getenv("VISUAL");
32 if (!editor)
33 editor = getenv("EDITOR");
35 terminal = getenv("TERM");
36 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
37 fprintf(stderr,
38 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
39 "Please supply the message using either -m or -F option.\n");
40 exit(1);
43 if (!editor)
44 editor = "vi";
46 memset(&child, 0, sizeof(child));
47 child.argv = args;
48 args[0] = editor;
49 args[1] = path;
50 args[2] = NULL;
52 if (run_command(&child))
53 die("There was a problem with the editor %s.", editor);
55 fd = open(path, O_RDONLY);
56 if (fd < 0)
57 die("could not open '%s': %s", path, strerror(errno));
58 if (read_fd(fd, buffer, len)) {
59 free(*buffer);
60 die("could not read message file '%s': %s",
61 path, strerror(errno));
63 close(fd);
66 struct tag_filter {
67 const char *pattern;
68 int lines;
71 #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
73 static int show_reference(const char *refname, const unsigned char *sha1,
74 int flag, void *cb_data)
76 struct tag_filter *filter = cb_data;
78 if (!fnmatch(filter->pattern, refname, 0)) {
79 int i;
80 unsigned long size;
81 enum object_type type;
82 char *buf, *sp, *eol;
83 size_t len;
85 if (!filter->lines) {
86 printf("%s\n", refname);
87 return 0;
89 printf("%-15s ", refname);
91 sp = buf = read_sha1_file(sha1, &type, &size);
92 if (!buf)
93 return 0;
94 if (!size) {
95 free(buf);
96 return 0;
98 /* skip header */
99 while (sp + 1 < buf + size &&
100 !(sp[0] == '\n' && sp[1] == '\n'))
101 sp++;
102 /* only take up to "lines" lines, and strip the signature */
103 for (i = 0, sp += 2;
104 i < filter->lines && sp < buf + size &&
105 prefixcmp(sp, PGP_SIGNATURE "\n");
106 i++) {
107 if (i)
108 printf("\n ");
109 eol = memchr(sp, '\n', size - (sp - buf));
110 len = eol ? eol - sp : size - (sp - buf);
111 fwrite(sp, len, 1, stdout);
112 if (!eol)
113 break;
114 sp = eol + 1;
116 putchar('\n');
117 free(buf);
120 return 0;
123 static int list_tags(const char *pattern, int lines)
125 struct tag_filter filter;
127 if (pattern == NULL)
128 pattern = "*";
130 filter.pattern = pattern;
131 filter.lines = lines;
133 for_each_tag_ref(show_reference, (void *) &filter);
135 return 0;
138 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
139 const unsigned char *sha1);
141 static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
143 const char **p;
144 char ref[PATH_MAX];
145 int had_error = 0;
146 unsigned char sha1[20];
148 for (p = argv; *p; p++) {
149 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
150 >= sizeof(ref)) {
151 error("tag name too long: %.*s...", 50, *p);
152 had_error = 1;
153 continue;
155 if (!resolve_ref(ref, sha1, 1, NULL)) {
156 error("tag '%s' not found.", *p);
157 had_error = 1;
158 continue;
160 if (fn(*p, ref, sha1))
161 had_error = 1;
163 return had_error;
166 static int delete_tag(const char *name, const char *ref,
167 const unsigned char *sha1)
169 if (delete_ref(ref, sha1))
170 return 1;
171 printf("Deleted tag '%s'\n", name);
172 return 0;
175 static int verify_tag(const char *name, const char *ref,
176 const unsigned char *sha1)
178 const char *argv_verify_tag[] = {"git-verify-tag",
179 "-v", "SHA1_HEX", NULL};
180 argv_verify_tag[2] = sha1_to_hex(sha1);
182 if (run_command_v_opt(argv_verify_tag, 0))
183 return error("could not verify the tag '%s'", name);
184 return 0;
187 static ssize_t do_sign(char *buffer, size_t size, size_t max)
189 struct child_process gpg;
190 const char *args[4];
191 char *bracket;
192 int len;
194 if (!*signingkey) {
195 if (strlcpy(signingkey, git_committer_info(1),
196 sizeof(signingkey)) > sizeof(signingkey) - 1)
197 return error("committer info too long.");
198 bracket = strchr(signingkey, '>');
199 if (bracket)
200 bracket[1] = '\0';
203 memset(&gpg, 0, sizeof(gpg));
204 gpg.argv = args;
205 gpg.in = -1;
206 gpg.out = -1;
207 args[0] = "gpg";
208 args[1] = "-bsau";
209 args[2] = signingkey;
210 args[3] = NULL;
212 if (start_command(&gpg))
213 return error("could not run gpg.");
215 write_or_die(gpg.in, buffer, size);
216 close(gpg.in);
217 gpg.close_in = 0;
218 len = read_in_full(gpg.out, buffer + size, max - size);
220 finish_command(&gpg);
222 if (len == max - size)
223 return error("could not read the entire signature from gpg.");
225 #ifdef __MINGW32__
226 /* strip CR from the line endings */
228 int i, j;
229 for (i = j = 0; i < len; i++)
230 if (buffer[size + i] != '\r') {
231 if (i != j)
232 buffer[size + j] = buffer[size + i];
233 j++;
235 len = j;
237 #endif
239 return size + len;
242 static const char tag_template[] =
243 "\n"
244 "#\n"
245 "# Write a tag message\n"
246 "#\n";
248 static int git_tag_config(const char *var, const char *value)
250 if (!strcmp(var, "user.signingkey")) {
251 if (!value)
252 die("user.signingkey without value");
253 if (strlcpy(signingkey, value, sizeof(signingkey))
254 >= sizeof(signingkey))
255 die("user.signingkey value too long");
256 return 0;
259 return git_default_config(var, value);
262 #define MAX_SIGNATURE_LENGTH 1024
263 /* message must be NULL or allocated, it will be reallocated and freed */
264 static void create_tag(const unsigned char *object, const char *tag,
265 char *message, int sign, unsigned char *result)
267 enum object_type type;
268 char header_buf[1024], *buffer = NULL;
269 int header_len, max_size;
270 unsigned long size = 0;
272 type = sha1_object_info(object, NULL);
273 if (type <= OBJ_NONE)
274 die("bad object type.");
276 header_len = snprintf(header_buf, sizeof(header_buf),
277 "object %s\n"
278 "type %s\n"
279 "tag %s\n"
280 "tagger %s\n\n",
281 sha1_to_hex(object),
282 typename(type),
283 tag,
284 git_committer_info(1));
286 if (header_len > sizeof(header_buf) - 1)
287 die("tag header too big.");
289 if (!message) {
290 char *path;
291 int fd;
293 /* write the template message before editing: */
294 path = xstrdup(git_path("TAG_EDITMSG"));
295 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
296 if (fd < 0)
297 die("could not create file '%s': %s",
298 path, strerror(errno));
299 write_or_die(fd, tag_template, strlen(tag_template));
300 close(fd);
302 launch_editor(path, &buffer, &size);
304 unlink(path);
305 free(path);
307 else {
308 buffer = message;
309 size = strlen(message);
312 size = stripspace(buffer, size, 1);
314 if (!message && !size)
315 die("no tag message?");
317 /* insert the header and add the '\n' if needed: */
318 max_size = header_len + size + (sign ? MAX_SIGNATURE_LENGTH : 0) + 1;
319 buffer = xrealloc(buffer, max_size);
320 if (size)
321 buffer[size++] = '\n';
322 memmove(buffer + header_len, buffer, size);
323 memcpy(buffer, header_buf, header_len);
324 size += header_len;
326 if (sign) {
327 size = do_sign(buffer, size, max_size);
328 if (size < 0)
329 die("unable to sign the tag");
332 if (write_sha1_file(buffer, size, tag_type, result) < 0)
333 die("unable to write tag file");
334 free(buffer);
337 int cmd_tag(int argc, const char **argv, const char *prefix)
339 unsigned char object[20], prev[20];
340 int annotate = 0, sign = 0, force = 0, lines = 0;
341 char *message = NULL;
342 char ref[PATH_MAX];
343 const char *object_ref, *tag;
344 int i;
345 struct ref_lock *lock;
347 git_config(git_tag_config);
349 for (i = 1; i < argc; i++) {
350 const char *arg = argv[i];
352 if (arg[0] != '-')
353 break;
354 if (!strcmp(arg, "-a")) {
355 annotate = 1;
356 continue;
358 if (!strcmp(arg, "-s")) {
359 annotate = 1;
360 sign = 1;
361 continue;
363 if (!strcmp(arg, "-f")) {
364 force = 1;
365 continue;
367 if (!strcmp(arg, "-n")) {
368 if (i + 1 == argc || *argv[i + 1] == '-')
369 /* no argument */
370 lines = 1;
371 else
372 lines = isdigit(*argv[++i]) ?
373 atoi(argv[i]) : 1;
374 continue;
376 if (!strcmp(arg, "-m")) {
377 annotate = 1;
378 i++;
379 if (i == argc)
380 die("option -m needs an argument.");
381 if (message)
382 die("only one -F or -m option is allowed.");
383 message = xstrdup(argv[i]);
384 continue;
386 if (!strcmp(arg, "-F")) {
387 unsigned long len;
388 int fd;
390 annotate = 1;
391 i++;
392 if (i == argc)
393 die("option -F needs an argument.");
394 if (message)
395 die("only one -F or -m option is allowed.");
397 if (!strcmp(argv[i], "-"))
398 fd = 0;
399 else {
400 fd = open(argv[i], O_RDONLY);
401 if (fd < 0)
402 die("could not open '%s': %s",
403 argv[i], strerror(errno));
405 len = 1024;
406 message = xmalloc(len);
407 if (read_fd(fd, &message, &len)) {
408 free(message);
409 die("cannot read %s", argv[i]);
411 continue;
413 if (!strcmp(arg, "-u")) {
414 annotate = 1;
415 sign = 1;
416 i++;
417 if (i == argc)
418 die("option -u needs an argument.");
419 if (strlcpy(signingkey, argv[i], sizeof(signingkey))
420 >= sizeof(signingkey))
421 die("argument to option -u too long");
422 continue;
424 if (!strcmp(arg, "-l"))
425 return list_tags(argv[i + 1], lines);
426 if (!strcmp(arg, "-d"))
427 return for_each_tag_name(argv + i + 1, delete_tag);
428 if (!strcmp(arg, "-v"))
429 return for_each_tag_name(argv + i + 1, verify_tag);
430 usage(builtin_tag_usage);
433 if (i == argc) {
434 if (annotate)
435 usage(builtin_tag_usage);
436 return list_tags(NULL, lines);
438 tag = argv[i++];
440 object_ref = i < argc ? argv[i] : "HEAD";
441 if (i + 1 < argc)
442 die("too many params");
444 if (get_sha1(object_ref, object))
445 die("Failed to resolve '%s' as a valid ref.", object_ref);
447 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
448 die("tag name too long: %.*s...", 50, tag);
449 if (check_ref_format(ref))
450 die("'%s' is not a valid tag name.", tag);
452 if (!resolve_ref(ref, prev, 1, NULL))
453 hashclr(prev);
454 else if (!force)
455 die("tag '%s' already exists", tag);
457 if (annotate)
458 create_tag(object, tag, message, sign, object);
460 lock = lock_any_ref_for_update(ref, prev, 0);
461 if (!lock)
462 die("%s: cannot lock the ref", ref);
463 if (write_ref_sha1(lock, object, NULL) < 0)
464 die("%s: cannot update the ref", ref);
466 return 0;