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