]>
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" | |
39686585 CR |
14 | #include "parse-options.h" |
15 | ||
16 | static const char * const git_tag_usage[] = { | |
17 | "git-tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]", | |
18 | "git-tag -d <tagname>...", | |
19 | "git-tag [-n [<num>]] -l [<pattern>]", | |
20 | "git-tag -v <tagname>...", | |
21 | NULL | |
22 | }; | |
62e09ce9 CR |
23 | |
24 | static char signingkey[1000]; | |
25 | ||
fd17f5b5 | 26 | static void launch_editor(const char *path, struct strbuf *buffer) |
62e09ce9 CR |
27 | { |
28 | const char *editor, *terminal; | |
29 | struct child_process child; | |
30 | const char *args[3]; | |
62e09ce9 | 31 | |
4d87b9c5 JS |
32 | editor = getenv("GIT_EDITOR"); |
33 | if (!editor && editor_program) | |
34 | editor = editor_program; | |
35 | if (!editor) | |
36 | editor = getenv("VISUAL"); | |
62e09ce9 CR |
37 | if (!editor) |
38 | editor = getenv("EDITOR"); | |
39 | ||
40 | terminal = getenv("TERM"); | |
41 | if (!editor && (!terminal || !strcmp(terminal, "dumb"))) { | |
42 | fprintf(stderr, | |
43 | "Terminal is dumb but no VISUAL nor EDITOR defined.\n" | |
44 | "Please supply the message using either -m or -F option.\n"); | |
45 | exit(1); | |
46 | } | |
47 | ||
48 | if (!editor) | |
49 | editor = "vi"; | |
50 | ||
51 | memset(&child, 0, sizeof(child)); | |
52 | child.argv = args; | |
53 | args[0] = editor; | |
54 | args[1] = path; | |
55 | args[2] = NULL; | |
56 | ||
57 | if (run_command(&child)) | |
58 | die("There was a problem with the editor %s.", editor); | |
59 | ||
387e7e19 | 60 | if (strbuf_read_file(buffer, path, 0) < 0) |
a9390b9f KH |
61 | die("could not read message file '%s': %s", |
62 | path, strerror(errno)); | |
62e09ce9 CR |
63 | } |
64 | ||
65 | struct tag_filter { | |
66 | const char *pattern; | |
67 | int lines; | |
68 | }; | |
69 | ||
70 | #define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----" | |
71 | ||
72 | static int show_reference(const char *refname, const unsigned char *sha1, | |
73 | int flag, void *cb_data) | |
74 | { | |
75 | struct tag_filter *filter = cb_data; | |
76 | ||
77 | if (!fnmatch(filter->pattern, refname, 0)) { | |
78 | int i; | |
79 | unsigned long size; | |
80 | enum object_type type; | |
81 | char *buf, *sp, *eol; | |
82 | size_t len; | |
83 | ||
84 | if (!filter->lines) { | |
85 | printf("%s\n", refname); | |
86 | return 0; | |
87 | } | |
88 | printf("%-15s ", refname); | |
89 | ||
e1f14cce MH |
90 | buf = read_sha1_file(sha1, &type, &size); |
91 | if (!buf || !size) | |
62e09ce9 | 92 | return 0; |
e1f14cce MH |
93 | |
94 | /* skip header */ | |
95 | sp = strstr(buf, "\n\n"); | |
96 | if (!sp) { | |
e317cfaf CR |
97 | free(buf); |
98 | return 0; | |
99 | } | |
62e09ce9 | 100 | /* only take up to "lines" lines, and strip the signature */ |
e317cfaf CR |
101 | for (i = 0, sp += 2; |
102 | i < filter->lines && sp < buf + size && | |
62e09ce9 CR |
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; | |
62e09ce9 CR |
124 | |
125 | if (pattern == NULL) | |
18e32b5b | 126 | pattern = "*"; |
62e09ce9 | 127 | |
18e32b5b | 128 | filter.pattern = pattern; |
62e09ce9 CR |
129 | filter.lines = lines; |
130 | ||
131 | for_each_tag_ref(show_reference, (void *) &filter); | |
132 | ||
62e09ce9 CR |
133 | return 0; |
134 | } | |
135 | ||
e317cfaf | 136 | typedef int (*each_tag_name_fn)(const char *name, const char *ref, |
62e09ce9 CR |
137 | const unsigned char *sha1); |
138 | ||
e317cfaf | 139 | static int for_each_tag_name(const char **argv, each_tag_name_fn fn) |
62e09ce9 CR |
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 | ||
fd17f5b5 | 185 | static int do_sign(struct strbuf *buffer) |
62e09ce9 CR |
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), | |
e317cfaf | 194 | sizeof(signingkey)) > sizeof(signingkey) - 1) |
62e09ce9 CR |
195 | return error("committer info too long."); |
196 | bracket = strchr(signingkey, '>'); | |
197 | if (bracket) | |
198 | bracket[1] = '\0'; | |
199 | } | |
200 | ||
aba91192 CR |
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 | ||
62e09ce9 CR |
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 | ||
fd17f5b5 | 217 | if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) { |
aba91192 CR |
218 | close(gpg.in); |
219 | finish_command(&gpg); | |
220 | return error("gpg did not accept the tag data"); | |
221 | } | |
62e09ce9 CR |
222 | close(gpg.in); |
223 | gpg.close_in = 0; | |
fd17f5b5 | 224 | len = strbuf_read(buffer, gpg.out, 1024); |
62e09ce9 | 225 | |
aba91192 CR |
226 | if (finish_command(&gpg) || !len || len < 0) |
227 | return error("gpg failed to sign the tag"); | |
62e09ce9 | 228 | |
fd17f5b5 | 229 | if (len < 0) |
62e09ce9 CR |
230 | return error("could not read the entire signature from gpg."); |
231 | ||
fd17f5b5 | 232 | return 0; |
62e09ce9 CR |
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 | ||
bab8118a MH |
255 | static void write_tag_body(int fd, const unsigned char *sha1) |
256 | { | |
257 | unsigned long size; | |
258 | enum object_type type; | |
259 | char *buf, *sp, *eob; | |
260 | size_t len; | |
261 | ||
262 | buf = read_sha1_file(sha1, &type, &size); | |
263 | if (!buf) | |
264 | return; | |
265 | /* skip header */ | |
266 | sp = strstr(buf, "\n\n"); | |
267 | ||
268 | if (!sp || !size || type != OBJ_TAG) { | |
269 | free(buf); | |
270 | return; | |
271 | } | |
272 | sp += 2; /* skip the 2 LFs */ | |
273 | eob = strstr(sp, "\n" PGP_SIGNATURE "\n"); | |
274 | if (eob) | |
275 | len = eob - sp; | |
276 | else | |
277 | len = buf + size - sp; | |
278 | write_or_die(fd, sp, len); | |
279 | ||
280 | free(buf); | |
281 | } | |
282 | ||
62e09ce9 | 283 | static void create_tag(const unsigned char *object, const char *tag, |
fd17f5b5 | 284 | struct strbuf *buf, int message, int sign, |
bab8118a | 285 | unsigned char *prev, unsigned char *result) |
62e09ce9 CR |
286 | { |
287 | enum object_type type; | |
fd17f5b5 PH |
288 | char header_buf[1024]; |
289 | int header_len; | |
62e09ce9 CR |
290 | |
291 | type = sha1_object_info(object, NULL); | |
e317cfaf | 292 | if (type <= OBJ_NONE) |
62e09ce9 CR |
293 | die("bad object type."); |
294 | ||
295 | header_len = snprintf(header_buf, sizeof(header_buf), | |
296 | "object %s\n" | |
297 | "type %s\n" | |
298 | "tag %s\n" | |
299 | "tagger %s\n\n", | |
300 | sha1_to_hex(object), | |
301 | typename(type), | |
302 | tag, | |
303 | git_committer_info(1)); | |
304 | ||
e317cfaf | 305 | if (header_len > sizeof(header_buf) - 1) |
62e09ce9 CR |
306 | die("tag header too big."); |
307 | ||
308 | if (!message) { | |
309 | char *path; | |
310 | int fd; | |
311 | ||
312 | /* write the template message before editing: */ | |
313 | path = xstrdup(git_path("TAG_EDITMSG")); | |
314 | fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); | |
315 | if (fd < 0) | |
316 | die("could not create file '%s': %s", | |
317 | path, strerror(errno)); | |
bab8118a MH |
318 | |
319 | if (!is_null_sha1(prev)) | |
320 | write_tag_body(fd, prev); | |
321 | else | |
322 | write_or_die(fd, tag_template, strlen(tag_template)); | |
62e09ce9 CR |
323 | close(fd); |
324 | ||
fd17f5b5 | 325 | launch_editor(path, buf); |
62e09ce9 CR |
326 | |
327 | unlink(path); | |
328 | free(path); | |
329 | } | |
62e09ce9 | 330 | |
6d69b6f6 | 331 | stripspace(buf, 1); |
62e09ce9 | 332 | |
fd17f5b5 | 333 | if (!message && !buf->len) |
62e09ce9 CR |
334 | die("no tag message?"); |
335 | ||
fd17f5b5 | 336 | strbuf_insert(buf, 0, header_buf, header_len); |
62e09ce9 | 337 | |
fd17f5b5 PH |
338 | if (sign && do_sign(buf) < 0) |
339 | die("unable to sign the tag"); | |
340 | if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0) | |
62e09ce9 | 341 | die("unable to write tag file"); |
62e09ce9 CR |
342 | } |
343 | ||
344 | int cmd_tag(int argc, const char **argv, const char *prefix) | |
345 | { | |
fd17f5b5 | 346 | struct strbuf buf; |
62e09ce9 | 347 | unsigned char object[20], prev[20]; |
62e09ce9 CR |
348 | char ref[PATH_MAX]; |
349 | const char *object_ref, *tag; | |
62e09ce9 CR |
350 | struct ref_lock *lock; |
351 | ||
39686585 CR |
352 | int annotate = 0, sign = 0, force = 0, lines = 0, |
353 | delete = 0, verify = 0; | |
354 | char *list = NULL, *msg = NULL, *msgfile = NULL, *keyid = NULL; | |
355 | const char *no_pattern = "NO_PATTERN"; | |
356 | struct option options[] = { | |
357 | { OPTION_STRING, 'l', NULL, &list, "pattern", "list tag names", | |
358 | PARSE_OPT_OPTARG, NULL, (intptr_t) no_pattern }, | |
359 | { OPTION_INTEGER, 'n', NULL, &lines, NULL, | |
360 | "print n lines of each tag message", | |
361 | PARSE_OPT_OPTARG, NULL, 1 }, | |
362 | OPT_BOOLEAN('d', NULL, &delete, "delete tags"), | |
363 | OPT_BOOLEAN('v', NULL, &verify, "verify tags"), | |
364 | ||
365 | OPT_GROUP("Tag creation options"), | |
366 | OPT_BOOLEAN('a', NULL, &annotate, | |
367 | "annotated tag, needs a message"), | |
368 | OPT_STRING('m', NULL, &msg, "msg", "message for the tag"), | |
369 | OPT_STRING('F', NULL, &msgfile, "file", "message in a file"), | |
370 | OPT_BOOLEAN('s', NULL, &sign, "annotated and GPG-signed tag"), | |
371 | OPT_STRING('u', NULL, &keyid, "key-id", | |
372 | "use another key to sign the tag"), | |
373 | OPT_BOOLEAN('f', NULL, &force, "replace the tag if exists"), | |
374 | OPT_END() | |
375 | }; | |
376 | ||
62e09ce9 CR |
377 | git_config(git_tag_config); |
378 | ||
39686585 | 379 | argc = parse_options(argc, argv, options, git_tag_usage, 0); |
62e09ce9 | 380 | |
39686585 CR |
381 | if (list) |
382 | return list_tags(list == no_pattern ? NULL : list, lines); | |
383 | if (delete) | |
384 | return for_each_tag_name(argv, delete_tag); | |
385 | if (verify) | |
386 | return for_each_tag_name(argv, verify_tag); | |
387 | ||
388 | strbuf_init(&buf, 0); | |
389 | if (msg || msgfile) { | |
390 | if (msg && msgfile) | |
391 | die("only one -F or -m option is allowed."); | |
392 | annotate = 1; | |
393 | if (msg) | |
394 | strbuf_addstr(&buf, msg); | |
395 | else { | |
396 | if (!strcmp(msgfile, "-")) { | |
387e7e19 | 397 | if (strbuf_read(&buf, 0, 1024) < 0) |
39686585 | 398 | die("cannot read %s", msgfile); |
387e7e19 | 399 | } else { |
39686585 | 400 | if (strbuf_read_file(&buf, msgfile, 1024) < 0) |
387e7e19 | 401 | die("could not open or read '%s': %s", |
39686585 | 402 | msgfile, strerror(errno)); |
62e09ce9 | 403 | } |
62e09ce9 | 404 | } |
62e09ce9 CR |
405 | } |
406 | ||
39686585 | 407 | if (argc == 0) { |
62e09ce9 | 408 | if (annotate) |
39686585 | 409 | usage_with_options(git_tag_usage, options); |
62e09ce9 CR |
410 | return list_tags(NULL, lines); |
411 | } | |
39686585 | 412 | tag = argv[0]; |
62e09ce9 | 413 | |
39686585 CR |
414 | object_ref = argc == 2 ? argv[1] : "HEAD"; |
415 | if (argc > 2) | |
62e09ce9 CR |
416 | die("too many params"); |
417 | ||
418 | if (get_sha1(object_ref, object)) | |
419 | die("Failed to resolve '%s' as a valid ref.", object_ref); | |
420 | ||
e317cfaf | 421 | if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1) |
62e09ce9 CR |
422 | die("tag name too long: %.*s...", 50, tag); |
423 | if (check_ref_format(ref)) | |
424 | die("'%s' is not a valid tag name.", tag); | |
425 | ||
426 | if (!resolve_ref(ref, prev, 1, NULL)) | |
427 | hashclr(prev); | |
428 | else if (!force) | |
429 | die("tag '%s' already exists", tag); | |
430 | ||
431 | if (annotate) | |
39686585 | 432 | create_tag(object, tag, &buf, msg || msgfile, sign, prev, object); |
62e09ce9 CR |
433 | |
434 | lock = lock_any_ref_for_update(ref, prev, 0); | |
435 | if (!lock) | |
436 | die("%s: cannot lock the ref", ref); | |
437 | if (write_ref_sha1(lock, object, NULL) < 0) | |
438 | die("%s: cannot update the ref", ref); | |
439 | ||
fd17f5b5 | 440 | strbuf_release(&buf); |
62e09ce9 CR |
441 | return 0; |
442 | } |