Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame^] | 2 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 3 | #include "hex.h" |
Ævar Arnfjörð Bjarmason | 3f390a3 | 2021-01-05 20:42:50 +0100 | [diff] [blame] | 4 | #include "parse-options.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 5 | #include "tag.h" |
Stefan Beller | 47f351e | 2018-04-11 17:21:06 -0700 | [diff] [blame] | 6 | #include "replace-object.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 7 | #include "object-store.h" |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 8 | #include "fsck.h" |
Ævar Arnfjörð Bjarmason | acfc013 | 2021-01-05 20:42:48 +0100 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
Ævar Arnfjörð Bjarmason | 3f390a3 | 2021-01-05 20:42:50 +0100 | [diff] [blame] | 11 | static char const * const builtin_mktag_usage[] = { |
Jean-Noël Avila | 959d670 | 2022-01-31 22:07:48 +0000 | [diff] [blame] | 12 | "git mktag", |
Ævar Arnfjörð Bjarmason | 3f390a3 | 2021-01-05 20:42:50 +0100 | [diff] [blame] | 13 | NULL |
| 14 | }; |
Ævar Arnfjörð Bjarmason | 06ce791 | 2021-01-06 12:47:27 +0100 | [diff] [blame] | 15 | static int option_strict = 1; |
Ævar Arnfjörð Bjarmason | 3f390a3 | 2021-01-05 20:42:50 +0100 | [diff] [blame] | 16 | |
Ævar Arnfjörð Bjarmason | acfc013 | 2021-01-05 20:42:48 +0100 | [diff] [blame] | 17 | static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; |
| 18 | |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 19 | static int mktag_fsck_error_func(struct fsck_options *o, |
| 20 | const struct object_id *oid, |
| 21 | enum object_type object_type, |
Ævar Arnfjörð Bjarmason | 1b32b59 | 2021-03-28 15:15:40 +0200 | [diff] [blame] | 22 | enum fsck_msg_type msg_type, |
Ævar Arnfjörð Bjarmason | 394d5d3 | 2021-03-28 15:15:46 +0200 | [diff] [blame] | 23 | enum fsck_msg_id msg_id, |
Ævar Arnfjörð Bjarmason | 1b32b59 | 2021-03-28 15:15:40 +0200 | [diff] [blame] | 24 | const char *message) |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 25 | { |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 26 | switch (msg_type) { |
| 27 | case FSCK_WARN: |
Ævar Arnfjörð Bjarmason | 06ce791 | 2021-01-06 12:47:27 +0100 | [diff] [blame] | 28 | 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ð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 34 | 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ð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 40 | fprintf_ln(stderr, _("error: tag input does not pass fsck: %s"), message); |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 41 | return 1; |
| 42 | default: |
Ævar Arnfjörð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 43 | BUG(_("%d (FSCK_IGNORE?) should never trigger this callback"), |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 44 | msg_type); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 45 | } |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 48 | static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 49 | { |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 50 | int ret; |
| 51 | enum object_type type; |
| 52 | unsigned long size; |
| 53 | void *buffer; |
| 54 | const struct object_id *repl; |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 55 | |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 56 | buffer = read_object_file(tagged_oid, &type, &size); |
| 57 | if (!buffer) |
Ævar Arnfjörð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 58 | die(_("could not read tagged object '%s'"), |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 59 | oid_to_hex(tagged_oid)); |
| 60 | if (type != *tagged_type) |
Ævar Arnfjörð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 61 | die(_("object '%s' tagged as '%s', but is a '%s' type"), |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 62 | oid_to_hex(tagged_oid), |
| 63 | type_name(*tagged_type), type_name(type)); |
Björn Engelmann | cfba045 | 2006-05-23 20:20:09 +0200 | [diff] [blame] | 64 | |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 65 | repl = lookup_replace_object(the_repository, tagged_oid); |
Ævar Arnfjörð Bjarmason | 0f156db | 2022-02-05 00:48:30 +0100 | [diff] [blame] | 66 | ret = check_object_signature(the_repository, repl, buffer, size, |
Ævar Arnfjörð Bjarmason | 44439c1 | 2022-02-05 00:48:32 +0100 | [diff] [blame] | 67 | *tagged_type); |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 68 | free(buffer); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 69 | |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 70 | return ret; |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Linus Torvalds | 112dd51 | 2010-01-22 07:34:44 -0800 | [diff] [blame] | 73 | int cmd_mktag(int argc, const char **argv, const char *prefix) |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 74 | { |
Ævar Arnfjörð Bjarmason | 3f390a3 | 2021-01-05 20:42:50 +0100 | [diff] [blame] | 75 | static struct option builtin_mktag_options[] = { |
Ævar Arnfjörð Bjarmason | 06ce791 | 2021-01-06 12:47:27 +0100 | [diff] [blame] | 76 | OPT_BOOL(0, "strict", &option_strict, |
| 77 | N_("enable more strict checking")), |
Ævar Arnfjörð Bjarmason | 3f390a3 | 2021-01-05 20:42:50 +0100 | [diff] [blame] | 78 | OPT_END(), |
| 79 | }; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 80 | struct strbuf buf = STRBUF_INIT; |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 81 | struct object_id tagged_oid; |
| 82 | int tagged_type; |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 83 | struct object_id result; |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 84 | |
Ævar Arnfjörð Bjarmason | 3f390a3 | 2021-01-05 20:42:50 +0100 | [diff] [blame] | 85 | argc = parse_options(argc, argv, NULL, |
| 86 | builtin_mktag_options, |
| 87 | builtin_mktag_usage, 0); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 88 | |
Ævar Arnfjörð Bjarmason | dfe3948 | 2021-01-05 20:42:44 +0100 | [diff] [blame] | 89 | if (strbuf_read(&buf, 0, 0) < 0) |
Ævar Arnfjörð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 90 | die_errno(_("could not read from stdin")); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 91 | |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 92 | fsck_options.error_func = mktag_fsck_error_func; |
Ævar Arnfjörð Bjarmason | 53692df | 2021-03-28 15:15:47 +0200 | [diff] [blame] | 93 | fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY, |
| 94 | FSCK_WARN); |
Ævar Arnfjörð Bjarmason | acfc013 | 2021-01-05 20:42:48 +0100 | [diff] [blame] | 95 | /* config might set fsck.extraHeaderEntry=* again */ |
Ævar Arnfjörð Bjarmason | fb79f5b | 2021-03-17 19:20:36 +0100 | [diff] [blame] | 96 | git_config(git_fsck_config, &fsck_options); |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 97 | if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options, |
| 98 | &tagged_oid, &tagged_type)) |
Ævar Arnfjörð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 99 | die(_("tag on stdin did not pass our strict fsck check")); |
Ævar Arnfjörð Bjarmason | acf9de4 | 2021-01-05 20:42:46 +0100 | [diff] [blame] | 100 | |
Ævar Arnfjörð Bjarmason | ee213de | 2022-02-05 00:48:29 +0100 | [diff] [blame] | 101 | if (verify_object_in_tag(&tagged_oid, &tagged_type) < 0) |
Ævar Arnfjörð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 102 | die(_("tag on stdin did not refer to a valid object")); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 103 | |
Ævar Arnfjörð Bjarmason | c80d226 | 2022-02-05 00:48:26 +0100 | [diff] [blame] | 104 | if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0) |
Ævar Arnfjörð Bjarmason | 2aa9425 | 2021-01-05 20:42:51 +0100 | [diff] [blame] | 105 | die(_("unable to write tag file")); |
Björn Engelmann | e7332f9 | 2006-05-23 20:19:04 +0200 | [diff] [blame] | 106 | |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 107 | strbuf_release(&buf); |
Ævar Arnfjörð Bjarmason | 40ef015 | 2021-01-05 20:42:45 +0100 | [diff] [blame] | 108 | puts(oid_to_hex(&result)); |
Linus Torvalds | ec4465a | 2005-04-25 12:07:44 -0700 | [diff] [blame] | 109 | return 0; |
| 110 | } |