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