]>
Commit | Line | Data |
---|---|---|
2ae68fcb CR |
1 | /* |
2 | * Builtin "git verify-tag" | |
3 | * | |
4 | * Copyright (c) 2007 Carlos Rica <jasampler@gmail.com> | |
5 | * | |
6 | * Based on git-verify-tag.sh | |
7 | */ | |
8 | #include "cache.h" | |
9 | #include "builtin.h" | |
10 | #include "tag.h" | |
11 | #include "run-command.h" | |
12 | #include <signal.h> | |
4855b2a2 | 13 | #include "parse-options.h" |
2ae68fcb | 14 | |
4855b2a2 SB |
15 | static const char * const verify_tag_usage[] = { |
16 | "git verify-tag [-v|--verbose] <tag>...", | |
17 | NULL | |
18 | }; | |
2ae68fcb | 19 | |
2ae68fcb CR |
20 | static int run_gpg_verify(const char *buf, unsigned long size, int verbose) |
21 | { | |
22 | struct child_process gpg; | |
23 | const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL}; | |
ac58c4c7 | 24 | char path[PATH_MAX]; |
2ae68fcb CR |
25 | size_t len; |
26 | int fd, ret; | |
27 | ||
28 | fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX"); | |
29 | if (fd < 0) | |
30 | return error("could not create temporary file '%s': %s", | |
31 | path, strerror(errno)); | |
32 | if (write_in_full(fd, buf, size) < 0) | |
33 | return error("failed writing temporary file '%s': %s", | |
34 | path, strerror(errno)); | |
35 | close(fd); | |
36 | ||
37 | /* find the length without signature */ | |
ac58c4c7 | 38 | len = parse_signature(buf, size); |
2ae68fcb CR |
39 | if (verbose) |
40 | write_in_full(1, buf, len); | |
41 | ||
42 | memset(&gpg, 0, sizeof(gpg)); | |
43 | gpg.argv = args_gpg; | |
44 | gpg.in = -1; | |
2ae68fcb | 45 | args_gpg[2] = path; |
69fe5ef6 JS |
46 | if (start_command(&gpg)) { |
47 | unlink(path); | |
2ae68fcb | 48 | return error("could not run gpg."); |
69fe5ef6 | 49 | } |
2ae68fcb CR |
50 | |
51 | write_in_full(gpg.in, buf, len); | |
52 | close(gpg.in); | |
2ae68fcb CR |
53 | ret = finish_command(&gpg); |
54 | ||
691f1a28 | 55 | unlink_or_warn(path); |
2ae68fcb CR |
56 | |
57 | return ret; | |
58 | } | |
59 | ||
60 | static int verify_tag(const char *name, int verbose) | |
61 | { | |
62 | enum object_type type; | |
63 | unsigned char sha1[20]; | |
64 | char *buf; | |
65 | unsigned long size; | |
66 | int ret; | |
67 | ||
68 | if (get_sha1(name, sha1)) | |
69 | return error("tag '%s' not found.", name); | |
70 | ||
71 | type = sha1_object_info(sha1, NULL); | |
72 | if (type != OBJ_TAG) | |
73 | return error("%s: cannot verify a non-tag object of type %s.", | |
74 | name, typename(type)); | |
75 | ||
76 | buf = read_sha1_file(sha1, &type, &size); | |
77 | if (!buf) | |
78 | return error("%s: unable to read file.", name); | |
79 | ||
80 | ret = run_gpg_verify(buf, size, verbose); | |
81 | ||
82 | free(buf); | |
83 | return ret; | |
84 | } | |
85 | ||
86 | int cmd_verify_tag(int argc, const char **argv, const char *prefix) | |
87 | { | |
88 | int i = 1, verbose = 0, had_error = 0; | |
4855b2a2 | 89 | const struct option verify_tag_options[] = { |
6e565345 | 90 | OPT__VERBOSE(&verbose, "print tag contents"), |
4855b2a2 SB |
91 | OPT_END() |
92 | }; | |
2ae68fcb | 93 | |
ef90d6d4 | 94 | git_config(git_default_config, NULL); |
2ae68fcb | 95 | |
4855b2a2 SB |
96 | argc = parse_options(argc, argv, prefix, verify_tag_options, |
97 | verify_tag_usage, PARSE_OPT_KEEP_ARGV0); | |
d2761895 | 98 | if (argc <= i) |
4855b2a2 | 99 | usage_with_options(verify_tag_usage, verify_tag_options); |
d2761895 | 100 | |
2ae68fcb CR |
101 | /* sometimes the program was terminated because this signal |
102 | * was received in the process of writing the gpg input: */ | |
103 | signal(SIGPIPE, SIG_IGN); | |
104 | while (i < argc) | |
105 | if (verify_tag(argv[i++], verbose)) | |
106 | had_error = 1; | |
107 | return had_error; | |
108 | } |