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