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