notes.c: use designated initializers for clarity
[git/debian.git] / builtin / mktag.c
blobb3f6d7ea389788610c6ae9351f31ed040b2eb264
1 #include "builtin.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "parse-options.h"
5 #include "tag.h"
6 #include "replace-object.h"
7 #include "object-store.h"
8 #include "fsck.h"
9 #include "config.h"
11 static char const * const builtin_mktag_usage[] = {
12 "git mktag",
13 NULL
15 static int option_strict = 1;
17 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
19 static int mktag_fsck_error_func(struct fsck_options *o,
20 const struct object_id *oid,
21 enum object_type object_type,
22 enum fsck_msg_type msg_type,
23 enum fsck_msg_id msg_id,
24 const char *message)
26 switch (msg_type) {
27 case FSCK_WARN:
28 if (!option_strict) {
29 fprintf_ln(stderr, _("warning: tag input does not pass fsck: %s"), message);
30 return 0;
33 /* fallthrough */
34 case FSCK_ERROR:
36 * We treat both warnings and errors as errors, things
37 * like missing "tagger" lines are "only" warnings
38 * under fsck, we've always considered them an error.
40 fprintf_ln(stderr, _("error: tag input does not pass fsck: %s"), message);
41 return 1;
42 default:
43 BUG(_("%d (FSCK_IGNORE?) should never trigger this callback"),
44 msg_type);
48 static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type)
50 int ret;
51 enum object_type type;
52 unsigned long size;
53 void *buffer;
54 const struct object_id *repl;
56 buffer = repo_read_object_file(the_repository, tagged_oid, &type,
57 &size);
58 if (!buffer)
59 die(_("could not read tagged object '%s'"),
60 oid_to_hex(tagged_oid));
61 if (type != *tagged_type)
62 die(_("object '%s' tagged as '%s', but is a '%s' type"),
63 oid_to_hex(tagged_oid),
64 type_name(*tagged_type), type_name(type));
66 repl = lookup_replace_object(the_repository, tagged_oid);
67 ret = check_object_signature(the_repository, repl, buffer, size,
68 *tagged_type);
69 free(buffer);
71 return ret;
74 int cmd_mktag(int argc, const char **argv, const char *prefix)
76 static struct option builtin_mktag_options[] = {
77 OPT_BOOL(0, "strict", &option_strict,
78 N_("enable more strict checking")),
79 OPT_END(),
81 struct strbuf buf = STRBUF_INIT;
82 struct object_id tagged_oid;
83 int tagged_type;
84 struct object_id result;
86 argc = parse_options(argc, argv, prefix,
87 builtin_mktag_options,
88 builtin_mktag_usage, 0);
90 if (strbuf_read(&buf, 0, 0) < 0)
91 die_errno(_("could not read from stdin"));
93 fsck_options.error_func = mktag_fsck_error_func;
94 fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
95 FSCK_WARN);
96 /* config might set fsck.extraHeaderEntry=* again */
97 git_config(git_fsck_config, &fsck_options);
98 if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options,
99 &tagged_oid, &tagged_type))
100 die(_("tag on stdin did not pass our strict fsck check"));
102 if (verify_object_in_tag(&tagged_oid, &tagged_type) < 0)
103 die(_("tag on stdin did not refer to a valid object"));
105 if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0)
106 die(_("unable to write tag file"));
108 strbuf_release(&buf);
109 puts(oid_to_hex(&result));
110 return 0;