1/*
2 * Builtin "git tag"
3 *
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 */
8
9#include "cache.h"
10#include "builtin.h"
11#include "refs.h"
12#include "tag.h"
13#include "run-command.h"
14
15static 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>]";
17
18static char signingkey[1000];
19
20static void launch_editor(const char *path, char **buffer, unsigned long *len)
21{
22 const char *editor, *terminal;
23 struct child_process child;
24 const char *args[3];
25 int fd;
26
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");
34
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);
41 }
42
43 if (!editor)
44 editor = "vi";
45
46 memset(&child, 0, sizeof(child));
47 child.argv = args;
48 args[0] = editor;
49 args[1] = path;
50 args[2] = NULL;
51
52 if (run_command(&child))
53 die("There was a problem with the editor %s.", editor);
54
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));
62 }
63 close(fd);
64}
65
66struct tag_filter {
67 const char *pattern;
68 int lines;
69};
70
71#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
72
73static int show_reference(const char *refname, const unsigned char *sha1,
74 int flag, void *cb_data)
75{
76 struct tag_filter *filter = cb_data;
77
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;
84
85 if (!filter->lines) {
86 printf("%s\n", refname);
87 return 0;
88 }
89 printf("%-15s ", refname);
90
91 sp = buf = read_sha1_file(sha1, &type, &size);
92 if (!buf || !size)
93 return 0;
94 /* skip header */
95 while (sp + 1 < buf + size &&
96 !(sp[0] == '\n' && sp[1] == '\n'))
97 sp++;
98 /* only take up to "lines" lines, and strip the signature */
99 for (i = 0, sp += 2; i < filter->lines && sp < buf + size &&
100 prefixcmp(sp, PGP_SIGNATURE "\n");
101 i++) {
102 if (i)
103 printf("\n ");
104 eol = memchr(sp, '\n', size - (sp - buf));
105 len = eol ? eol - sp : size - (sp - buf);
106 fwrite(sp, len, 1, stdout);
107 if (!eol)
108 break;
109 sp = eol + 1;
110 }
111 putchar('\n');
112 free(buf);
113 }
114
115 return 0;
116}
117
118static int list_tags(const char *pattern, int lines)
119{
120 struct tag_filter filter;
121 char *newpattern;
122
123 if (pattern == NULL)
124 pattern = "";
125
126 /* prepend/append * to the shell pattern: */
127 newpattern = xmalloc(strlen(pattern) + 3);
128 sprintf(newpattern, "*%s*", pattern);
129
130 filter.pattern = newpattern;
131 filter.lines = lines;
132
133 for_each_tag_ref(show_reference, (void *) &filter);
134
135 free(newpattern);
136
137 return 0;
138}
139
140typedef int (*func_tag)(const char *name, const char *ref,
141 const unsigned char *sha1);
142
143static int do_tag_names(const char **argv, func_tag fn)
144{
145 const char **p;
146 char ref[PATH_MAX];
147 int had_error = 0;
148 unsigned char sha1[20];
149
150 for (p = argv; *p; p++) {
151 if (snprintf(ref, sizeof(ref), "refs/tags/%s", *p)
152 >= sizeof(ref)) {
153 error("tag name too long: %.*s...", 50, *p);
154 had_error = 1;
155 continue;
156 }
157 if (!resolve_ref(ref, sha1, 1, NULL)) {
158 error("tag '%s' not found.", *p);
159 had_error = 1;
160 continue;
161 }
162 if (fn(*p, ref, sha1))
163 had_error = 1;
164 }
165 return had_error;
166}
167
168static int delete_tag(const char *name, const char *ref,
169 const unsigned char *sha1)
170{
171 if (delete_ref(ref, sha1))
172 return 1;
173 printf("Deleted tag '%s'\n", name);
174 return 0;
175}
176
177static int verify_tag(const char *name, const char *ref,
178 const unsigned char *sha1)
179{
180 const char *argv_verify_tag[] = {"git-verify-tag",
181 "-v", "SHA1_HEX", NULL};
182 argv_verify_tag[2] = sha1_to_hex(sha1);
183
184 if (run_command_v_opt(argv_verify_tag, 0))
185 return error("could not verify the tag '%s'", name);
186 return 0;
187}
188
189static ssize_t do_sign(char *buffer, size_t size, size_t max)
190{
191 struct child_process gpg;
192 const char *args[4];
193 char *bracket;
194 int len;
195
196 if (!*signingkey) {
197 if (strlcpy(signingkey, git_committer_info(1),
198 sizeof(signingkey)) >= sizeof(signingkey))
199 return error("committer info too long.");
200 bracket = strchr(signingkey, '>');
201 if (bracket)
202 bracket[1] = '\0';
203 }
204
205 memset(&gpg, 0, sizeof(gpg));
206 gpg.argv = args;
207 gpg.in = -1;
208 gpg.out = -1;
209 args[0] = "gpg";
210 args[1] = "-bsau";
211 args[2] = signingkey;
212 args[3] = NULL;
213
214 if (start_command(&gpg))
215 return error("could not run gpg.");
216
217 write_or_die(gpg.in, buffer, size);
218 close(gpg.in);
219 gpg.close_in = 0;
220 len = read_in_full(gpg.out, buffer + size, max - size);
221
222 finish_command(&gpg);
223
224 if (len == max - size)
225 return error("could not read the entire signature from gpg.");
226
227 return size + len;
228}
229
230static const char tag_template[] =
231 "\n"
232 "#\n"
233 "# Write a tag message\n"
234 "#\n";
235
236static int git_tag_config(const char *var, const char *value)
237{
238 if (!strcmp(var, "user.signingkey")) {
239 if (!value)
240 die("user.signingkey without value");
241 if (strlcpy(signingkey, value, sizeof(signingkey))
242 >= sizeof(signingkey))
243 die("user.signingkey value too long");
244 return 0;
245 }
246
247 return git_default_config(var, value);
248}
249
250#define MAX_SIGNATURE_LENGTH 1024
251/* message must be NULL or allocated, it will be reallocated and freed */
252static void create_tag(const unsigned char *object, const char *tag,
253 char *message, int sign, unsigned char *result)
254{
255 enum object_type type;
256 char header_buf[1024], *buffer = NULL;
257 int header_len, max_size;
258 unsigned long size = 0;
259
260 type = sha1_object_info(object, NULL);
261 if (type <= 0)
262 die("bad object type.");
263
264 header_len = snprintf(header_buf, sizeof(header_buf),
265 "object %s\n"
266 "type %s\n"
267 "tag %s\n"
268 "tagger %s\n\n",
269 sha1_to_hex(object),
270 typename(type),
271 tag,
272 git_committer_info(1));
273
274 if (header_len >= sizeof(header_buf))
275 die("tag header too big.");
276
277 if (!message) {
278 char *path;
279 int fd;
280
281 /* write the template message before editing: */
282 path = xstrdup(git_path("TAG_EDITMSG"));
283 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
284 if (fd < 0)
285 die("could not create file '%s': %s",
286 path, strerror(errno));
287 write_or_die(fd, tag_template, strlen(tag_template));
288 close(fd);
289
290 launch_editor(path, &buffer, &size);
291
292 unlink(path);
293 free(path);
294 }
295 else {
296 buffer = message;
297 size = strlen(message);
298 }
299
300 size = stripspace(buffer, size, 1);
301
302 if (!message && !size)
303 die("no tag message?");
304
305 /* insert the header and add the '\n' if needed: */
306 max_size = header_len + size + (sign ? MAX_SIGNATURE_LENGTH : 0) + 1;
307 buffer = xrealloc(buffer, max_size);
308 if (size)
309 buffer[size++] = '\n';
310 memmove(buffer + header_len, buffer, size);
311 memcpy(buffer, header_buf, header_len);
312 size += header_len;
313
314 if (sign) {
315 size = do_sign(buffer, size, max_size);
316 if (size < 0)
317 die("unable to sign the tag");
318 }
319
320 if (write_sha1_file(buffer, size, tag_type, result) < 0)
321 die("unable to write tag file");
322 free(buffer);
323}
324
325int cmd_tag(int argc, const char **argv, const char *prefix)
326{
327 unsigned char object[20], prev[20];
328 int annotate = 0, sign = 0, force = 0, lines = 0;
329 char *message = NULL;
330 char ref[PATH_MAX];
331 const char *object_ref, *tag;
332 int i;
333 struct ref_lock *lock;
334
335 git_config(git_tag_config);
336
337 for (i = 1; i < argc; i++) {
338 const char *arg = argv[i];
339
340 if (arg[0] != '-')
341 break;
342 if (!strcmp(arg, "-a")) {
343 annotate = 1;
344 continue;
345 }
346 if (!strcmp(arg, "-s")) {
347 annotate = 1;
348 sign = 1;
349 continue;
350 }
351 if (!strcmp(arg, "-f")) {
352 force = 1;
353 continue;
354 }
355 if (!strcmp(arg, "-n")) {
356 if (i + 1 == argc || *argv[i + 1] == '-')
357 /* no argument */
358 lines = 1;
359 else
360 lines = isdigit(*argv[++i]) ?
361 atoi(argv[i]) : 1;
362 continue;
363 }
364 if (!strcmp(arg, "-m")) {
365 annotate = 1;
366 i++;
367 if (i == argc)
368 die("option -m needs an argument.");
369 message = xstrdup(argv[i]);
370 continue;
371 }
372 if (!strcmp(arg, "-F")) {
373 unsigned long len;
374 int fd;
375
376 annotate = 1;
377 i++;
378 if (i == argc)
379 die("option -F needs an argument.");
380
381 if (!strcmp(argv[i], "-"))
382 fd = 0;
383 else {
384 fd = open(argv[i], O_RDONLY);
385 if (fd < 0)
386 die("could not open '%s': %s",
387 argv[i], strerror(errno));
388 }
389 len = 1024;
390 message = xmalloc(len);
391 if (read_fd(fd, &message, &len)) {
392 free(message);
393 die("cannot read %s", argv[i]);
394 }
395 continue;
396 }
397 if (!strcmp(arg, "-u")) {
398 annotate = 1;
399 sign = 1;
400 i++;
401 if (i == argc)
402 die("option -u needs an argument.");
403 if (strlcpy(signingkey, argv[i], sizeof(signingkey))
404 >= sizeof(signingkey))
405 die("argument to option -u too long");
406 continue;
407 }
408 if (!strcmp(arg, "-l")) {
409 return list_tags(argv[i + 1], lines);
410 }
411 if (!strcmp(arg, "-d")) {
412 return do_tag_names(argv + i + 1, delete_tag);
413 }
414 if (!strcmp(arg, "-v")) {
415 return do_tag_names(argv + i + 1, verify_tag);
416 }
417 usage(builtin_tag_usage);
418 }
419
420 if (i == argc) {
421 if (annotate)
422 usage(builtin_tag_usage);
423 return list_tags(NULL, lines);
424 }
425 tag = argv[i++];
426
427 object_ref = i < argc ? argv[i] : "HEAD";
428 if (i + 1 < argc)
429 die("too many params");
430
431 if (get_sha1(object_ref, object))
432 die("Failed to resolve '%s' as a valid ref.", object_ref);
433
434 if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) >= sizeof(ref))
435 die("tag name too long: %.*s...", 50, tag);
436 if (check_ref_format(ref))
437 die("'%s' is not a valid tag name.", tag);
438
439 if (!resolve_ref(ref, prev, 1, NULL))
440 hashclr(prev);
441 else if (!force)
442 die("tag '%s' already exists", tag);
443
444 if (annotate)
445 create_tag(object, tag, message, sign, object);
446
447 lock = lock_any_ref_for_update(ref, prev, 0);
448 if (!lock)
449 die("%s: cannot lock the ref", ref);
450 if (write_ref_sha1(lock, object, NULL) < 0)
451 die("%s: cannot update the ref", ref);
452
453 return 0;
454}