]>
Commit | Line | Data |
---|---|---|
c2e86add | 1 | #include "builtin.h" |
3f390a36 | 2 | #include "parse-options.h" |
8e440259 | 3 | #include "tag.h" |
47f351e9 | 4 | #include "replace-object.h" |
cbd53a21 | 5 | #include "object-store.h" |
acf9de4c | 6 | #include "fsck.h" |
acfc0133 ÆAB |
7 | #include "config.h" |
8 | ||
3f390a36 ÆAB |
9 | static char const * const builtin_mktag_usage[] = { |
10 | N_("git mktag"), | |
11 | NULL | |
12 | }; | |
06ce7915 | 13 | static int option_strict = 1; |
3f390a36 | 14 | |
acfc0133 ÆAB |
15 | static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; |
16 | ||
17 | static int mktag_config(const char *var, const char *value, void *cb) | |
18 | { | |
19 | return fsck_config_internal(var, value, cb, &fsck_options); | |
20 | } | |
ec4465ad | 21 | |
acf9de4c ÆAB |
22 | static int mktag_fsck_error_func(struct fsck_options *o, |
23 | const struct object_id *oid, | |
24 | enum object_type object_type, | |
25 | int msg_type, const char *message) | |
ec4465ad | 26 | { |
acf9de4c ÆAB |
27 | switch (msg_type) { |
28 | case FSCK_WARN: | |
06ce7915 ÆAB |
29 | if (!option_strict) { |
30 | fprintf_ln(stderr, _("warning: tag input does not pass fsck: %s"), message); | |
31 | return 0; | |
32 | ||
33 | } | |
34 | /* fallthrough */ | |
acf9de4c ÆAB |
35 | case FSCK_ERROR: |
36 | /* | |
37 | * We treat both warnings and errors as errors, things | |
38 | * like missing "tagger" lines are "only" warnings | |
39 | * under fsck, we've always considered them an error. | |
40 | */ | |
2aa9425f | 41 | fprintf_ln(stderr, _("error: tag input does not pass fsck: %s"), message); |
acf9de4c ÆAB |
42 | return 1; |
43 | default: | |
2aa9425f | 44 | BUG(_("%d (FSCK_IGNORE?) should never trigger this callback"), |
acf9de4c | 45 | msg_type); |
ec4465ad | 46 | } |
ec4465ad LT |
47 | } |
48 | ||
acf9de4c | 49 | static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) |
ec4465ad | 50 | { |
acf9de4c ÆAB |
51 | int ret; |
52 | enum object_type type; | |
53 | unsigned long size; | |
54 | void *buffer; | |
55 | const struct object_id *repl; | |
56 | ||
57 | buffer = read_object_file(tagged_oid, &type, &size); | |
58 | if (!buffer) | |
2aa9425f | 59 | die(_("could not read tagged object '%s'"), |
acf9de4c ÆAB |
60 | oid_to_hex(tagged_oid)); |
61 | if (type != *tagged_type) | |
2aa9425f | 62 | die(_("object '%s' tagged as '%s', but is a '%s' type"), |
acf9de4c ÆAB |
63 | oid_to_hex(tagged_oid), |
64 | type_name(*tagged_type), type_name(type)); | |
65 | ||
66 | repl = lookup_replace_object(the_repository, tagged_oid); | |
67 | ret = check_object_signature(the_repository, repl, | |
68 | buffer, size, type_name(*tagged_type)); | |
69 | free(buffer); | |
c818566d | 70 | |
acf9de4c | 71 | return ret; |
ec4465ad LT |
72 | } |
73 | ||
112dd514 | 74 | int cmd_mktag(int argc, const char **argv, const char *prefix) |
ec4465ad | 75 | { |
3f390a36 | 76 | static struct option builtin_mktag_options[] = { |
06ce7915 ÆAB |
77 | OPT_BOOL(0, "strict", &option_strict, |
78 | N_("enable more strict checking")), | |
3f390a36 ÆAB |
79 | OPT_END(), |
80 | }; | |
f285a2d7 | 81 | struct strbuf buf = STRBUF_INIT; |
acf9de4c ÆAB |
82 | struct object_id tagged_oid; |
83 | int tagged_type; | |
a09c985e | 84 | struct object_id result; |
ec4465ad | 85 | |
3f390a36 ÆAB |
86 | argc = parse_options(argc, argv, NULL, |
87 | builtin_mktag_options, | |
88 | builtin_mktag_usage, 0); | |
ec4465ad | 89 | |
dfe39487 | 90 | if (strbuf_read(&buf, 0, 0) < 0) |
2aa9425f | 91 | die_errno(_("could not read from stdin")); |
ec4465ad | 92 | |
acf9de4c ÆAB |
93 | fsck_options.error_func = mktag_fsck_error_func; |
94 | fsck_set_msg_type(&fsck_options, "extraheaderentry", "warn"); | |
acfc0133 ÆAB |
95 | /* config might set fsck.extraHeaderEntry=* again */ |
96 | git_config(mktag_config, NULL); | |
acf9de4c ÆAB |
97 | if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options, |
98 | &tagged_oid, &tagged_type)) | |
2aa9425f | 99 | die(_("tag on stdin did not pass our strict fsck check")); |
acf9de4c ÆAB |
100 | |
101 | if (verify_object_in_tag(&tagged_oid, &tagged_type)) | |
2aa9425f | 102 | die(_("tag on stdin did not refer to a valid object")); |
ec4465ad | 103 | |
a09c985e | 104 | if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0) |
2aa9425f | 105 | die(_("unable to write tag file")); |
e7332f96 | 106 | |
fd17f5b5 | 107 | strbuf_release(&buf); |
40ef015a | 108 | puts(oid_to_hex(&result)); |
ec4465ad LT |
109 | return 0; |
110 | } |