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