Merge branch 'aj/stash-staged-fix' into maint-2.45
[alt-git.git] / builtin / mktag.c
blob4767f1a97e6df25ca09eba0846feaf404e08fe46
1 #include "builtin.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "parse-options.h"
5 #include "strbuf.h"
6 #include "replace-object.h"
7 #include "object-file.h"
8 #include "object-store-ll.h"
9 #include "fsck.h"
10 #include "config.h"
12 static char const * const builtin_mktag_usage[] = {
13 "git mktag",
14 NULL
16 static int option_strict = 1;
18 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
20 static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
21 const struct object_id *oid UNUSED,
22 enum object_type object_type UNUSED,
23 enum fsck_msg_type msg_type,
24 enum fsck_msg_id msg_id UNUSED,
25 const char *message)
27 switch (msg_type) {
28 case FSCK_WARN:
29 if (!option_strict) {
30 fprintf_ln(stderr, _("warning: tag input does not pass fsck: %s"), message);
31 return 0;
34 /* fallthrough */
35 case FSCK_ERROR:
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.
41 fprintf_ln(stderr, _("error: tag input does not pass fsck: %s"), message);
42 return 1;
43 default:
44 BUG(_("%d (FSCK_IGNORE?) should never trigger this callback"),
45 msg_type);
49 static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type)
51 int ret;
52 enum object_type type;
53 unsigned long size;
54 void *buffer;
55 const struct object_id *repl;
57 buffer = repo_read_object_file(the_repository, tagged_oid, &type,
58 &size);
59 if (!buffer)
60 die(_("could not read tagged object '%s'"),
61 oid_to_hex(tagged_oid));
62 if (type != *tagged_type)
63 die(_("object '%s' tagged as '%s', but is a '%s' type"),
64 oid_to_hex(tagged_oid),
65 type_name(*tagged_type), type_name(type));
67 repl = lookup_replace_object(the_repository, tagged_oid);
68 ret = check_object_signature(the_repository, repl, buffer, size,
69 *tagged_type);
70 free(buffer);
72 return ret;
75 int cmd_mktag(int argc, const char **argv, const char *prefix)
77 static struct option builtin_mktag_options[] = {
78 OPT_BOOL(0, "strict", &option_strict,
79 N_("enable more strict checking")),
80 OPT_END(),
82 struct strbuf buf = STRBUF_INIT;
83 struct object_id tagged_oid;
84 int tagged_type;
85 struct object_id result;
87 argc = parse_options(argc, argv, prefix,
88 builtin_mktag_options,
89 builtin_mktag_usage, 0);
91 if (strbuf_read(&buf, 0, 0) < 0)
92 die_errno(_("could not read from stdin"));
94 fsck_options.error_func = mktag_fsck_error_func;
95 fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
96 FSCK_WARN);
97 /* config might set fsck.extraHeaderEntry=* again */
98 git_config(git_fsck_config, &fsck_options);
99 if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options,
100 &tagged_oid, &tagged_type))
101 die(_("tag on stdin did not pass our strict fsck check"));
103 if (verify_object_in_tag(&tagged_oid, &tagged_type) < 0)
104 die(_("tag on stdin did not refer to a valid object"));
106 if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0)
107 die(_("unable to write tag file"));
109 strbuf_release(&buf);
110 puts(oid_to_hex(&result));
111 return 0;