blob: e93aee7225818a0801df0f7e51dfd895ff0529b1 [file] [log] [blame]
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001#include "builtin.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00002#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00003#include "hex.h"
Ævar Arnfjörð Bjarmason3f390a32021-01-05 20:42:50 +01004#include "parse-options.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02005#include "tag.h"
Stefan Beller47f351e2018-04-11 17:21:06 -07006#include "replace-object.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07007#include "object-store.h"
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +01008#include "fsck.h"
Ævar Arnfjörð Bjarmasonacfc0132021-01-05 20:42:48 +01009#include "config.h"
10
Ævar Arnfjörð Bjarmason3f390a32021-01-05 20:42:50 +010011static char const * const builtin_mktag_usage[] = {
Jean-Noël Avila959d6702022-01-31 22:07:48 +000012 "git mktag",
Ævar Arnfjörð Bjarmason3f390a32021-01-05 20:42:50 +010013 NULL
14};
Ævar Arnfjörð Bjarmason06ce7912021-01-06 12:47:27 +010015static int option_strict = 1;
Ævar Arnfjörð Bjarmason3f390a32021-01-05 20:42:50 +010016
Ævar Arnfjörð Bjarmasonacfc0132021-01-05 20:42:48 +010017static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
18
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010019static int mktag_fsck_error_func(struct fsck_options *o,
20 const struct object_id *oid,
21 enum object_type object_type,
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +020022 enum fsck_msg_type msg_type,
Ævar Arnfjörð Bjarmason394d5d32021-03-28 15:15:46 +020023 enum fsck_msg_id msg_id,
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +020024 const char *message)
Linus Torvaldsec4465a2005-04-25 12:07:44 -070025{
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010026 switch (msg_type) {
27 case FSCK_WARN:
Ævar Arnfjörð Bjarmason06ce7912021-01-06 12:47:27 +010028 if (!option_strict) {
29 fprintf_ln(stderr, _("warning: tag input does not pass fsck: %s"), message);
30 return 0;
31
32 }
33 /* fallthrough */
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010034 case FSCK_ERROR:
35 /*
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.
39 */
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +010040 fprintf_ln(stderr, _("error: tag input does not pass fsck: %s"), message);
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010041 return 1;
42 default:
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +010043 BUG(_("%d (FSCK_IGNORE?) should never trigger this callback"),
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010044 msg_type);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070045 }
Linus Torvaldsec4465a2005-04-25 12:07:44 -070046}
47
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010048static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type)
Linus Torvaldsec4465a2005-04-25 12:07:44 -070049{
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010050 int ret;
51 enum object_type type;
52 unsigned long size;
53 void *buffer;
54 const struct object_id *repl;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070055
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010056 buffer = read_object_file(tagged_oid, &type, &size);
57 if (!buffer)
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +010058 die(_("could not read tagged object '%s'"),
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010059 oid_to_hex(tagged_oid));
60 if (type != *tagged_type)
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +010061 die(_("object '%s' tagged as '%s', but is a '%s' type"),
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010062 oid_to_hex(tagged_oid),
63 type_name(*tagged_type), type_name(type));
Björn Engelmanncfba0452006-05-23 20:20:09 +020064
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010065 repl = lookup_replace_object(the_repository, tagged_oid);
Ævar Arnfjörð Bjarmason0f156db2022-02-05 00:48:30 +010066 ret = check_object_signature(the_repository, repl, buffer, size,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +010067 *tagged_type);
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010068 free(buffer);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070069
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010070 return ret;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070071}
72
Linus Torvalds112dd512010-01-22 07:34:44 -080073int cmd_mktag(int argc, const char **argv, const char *prefix)
Linus Torvaldsec4465a2005-04-25 12:07:44 -070074{
Ævar Arnfjörð Bjarmason3f390a32021-01-05 20:42:50 +010075 static struct option builtin_mktag_options[] = {
Ævar Arnfjörð Bjarmason06ce7912021-01-06 12:47:27 +010076 OPT_BOOL(0, "strict", &option_strict,
77 N_("enable more strict checking")),
Ævar Arnfjörð Bjarmason3f390a32021-01-05 20:42:50 +010078 OPT_END(),
79 };
Brandon Caseyf285a2d2008-10-09 14:12:12 -050080 struct strbuf buf = STRBUF_INIT;
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010081 struct object_id tagged_oid;
82 int tagged_type;
Patryk Obaraa09c9852018-01-28 01:13:19 +010083 struct object_id result;
Linus Torvaldsec4465a2005-04-25 12:07:44 -070084
Ævar Arnfjörð Bjarmason3f390a32021-01-05 20:42:50 +010085 argc = parse_options(argc, argv, NULL,
86 builtin_mktag_options,
87 builtin_mktag_usage, 0);
Linus Torvaldsec4465a2005-04-25 12:07:44 -070088
Ævar Arnfjörð Bjarmasondfe39482021-01-05 20:42:44 +010089 if (strbuf_read(&buf, 0, 0) < 0)
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +010090 die_errno(_("could not read from stdin"));
Linus Torvaldsec4465a2005-04-25 12:07:44 -070091
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010092 fsck_options.error_func = mktag_fsck_error_func;
Ævar Arnfjörð Bjarmason53692df2021-03-28 15:15:47 +020093 fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
94 FSCK_WARN);
Ævar Arnfjörð Bjarmasonacfc0132021-01-05 20:42:48 +010095 /* config might set fsck.extraHeaderEntry=* again */
Ævar Arnfjörð Bjarmasonfb79f5b2021-03-17 19:20:36 +010096 git_config(git_fsck_config, &fsck_options);
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010097 if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options,
98 &tagged_oid, &tagged_type))
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +010099 die(_("tag on stdin did not pass our strict fsck check"));
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +0100100
Ævar Arnfjörð Bjarmasonee213de2022-02-05 00:48:29 +0100101 if (verify_object_in_tag(&tagged_oid, &tagged_type) < 0)
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +0100102 die(_("tag on stdin did not refer to a valid object"));
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700103
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100104 if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0)
Ævar Arnfjörð Bjarmason2aa94252021-01-05 20:42:51 +0100105 die(_("unable to write tag file"));
Björn Engelmanne7332f92006-05-23 20:19:04 +0200106
Pierre Habouzitfd17f5b2007-09-10 12:35:09 +0200107 strbuf_release(&buf);
Ævar Arnfjörð Bjarmason40ef0152021-01-05 20:42:45 +0100108 puts(oid_to_hex(&result));
Linus Torvaldsec4465a2005-04-25 12:07:44 -0700109 return 0;
110}