From patchwork Thu Jun 27 15:12:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714625 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C995198E81 for ; Thu, 27 Jun 2024 15:12:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501156; cv=none; b=HhLyXnqj68UOiGra9RgzAG4V/t+zXC/ryD71O0rr0OIpO3XZnjO99NI/vQw7uo2n+wCyEMCB7KDznR/7Xr8twA9SpwZMQEC4ZEI1WSahrYPSqD+31rR/A9ORmGfUY3ofrLdNhKwMhXxQYOmam8xWTXGL6l5YLDt33d2OdvB2GnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501156; c=relaxed/simple; bh=zox0c4KGmr9lebECFTULNLuTpUPPozai+chXeBgameI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DS1jCmSYJ7/WAnOBalMwzynL+dZbEnEpbSjtql54y0PN6v+uQ1HZPFhL1kR69bJBA2tRQGod0CBSLlgC5eLhvV7LkhZ1f69oH5QMZjILxT9B1L5/fuvDAbSp7rLJgOEXIv4ro5uzLqZ/fSwgn2ZUn8ItCD9yUBBc72YJgBgcp98= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lzlNNrrf; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lzlNNrrf" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-7067272245aso3984189b3a.1 for ; Thu, 27 Jun 2024 08:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501153; x=1720105953; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=JULW6pmNy2frx5+4Mr26gRe6+cg2Wj6Q0bMRxQ/Agc4=; b=lzlNNrrfnOZpqXIr+SLL812rkL454N6RfnLbibhWnBFMPECGW0iNQif7CWccwetjFZ /bn4m06e7c5SK+Hqm0ehbFuki42O5jGgI+P+5qfECuBUgYEcdM9ZQ2XCseV2/rzdqfdo snfIvW8Sm4Bkkv1TQJEYA7iyaQibEfsKM+aGcz6diwSW8c9V5gWxVt01OKVagle1EsbX Cpkg/oaXmOeLUxo4hSZJXnw0VkD01D9vRlOyeog6l3D+I0fSSijQE/SVk5XF45Fr3l5n AxVFk0tDstTAGcH57uF9RBct6GiouT0TO4KrdTb2lq9GUi3FVzOas7MNkVDtsXP9dBah 4nXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501153; x=1720105953; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JULW6pmNy2frx5+4Mr26gRe6+cg2Wj6Q0bMRxQ/Agc4=; b=Y28JwBDvVoBh8AUoH2O+NAmcijeFAAUBK5YyePvl5BilOOzzdRThZOtLhbleQn01uC T489YNHF073wjliG3exRtEA8jR5inmK79BAW7gm9T8KfaDNC464nDBsvZI7M6WOclrBF qmIa/lSKdVb9bxcU7AOb9FntA5YCDh38YNYgXH8Hgv4jpuAS5eF7G930lVLDfSwshmfm QUxLQU9jjJ5KeOonubM8CpF733dPuFePOXKqCaRxR7Y1a/j24iUgnSAqar5l1rXOwDIF V4MsC2SKMGXAyAZf8B2kcQl4REJav6SgzEfV5UsnZF8mYn7k3zYO7aZJPteMYTtMf3jE Vq3Q== X-Gm-Message-State: AOJu0YyyYCY3JXTLAuepZkrzi0CnsHkCEQrpg1v8s0kwxTP/WXgW2d2u IGuMKrPRvG3/liO+tG77TU3EUGcD/rpZHCnahtHnzQGcpu3i+8QfmCGxkA== X-Google-Smtp-Source: AGHT+IECSGEUFWF3aoIrcglzEyBb1DGNcaepFDjsPjGGiKB+3I8FWQMnUgIapYTAJ0NjwBke+TfOqA== X-Received: by 2002:a05:6a00:1990:b0:705:9ba5:f3e9 with SMTP id d2e1a72fcca58-7067471bf99mr17174383b3a.30.1719501152942; Thu, 27 Jun 2024 08:12:32 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706b4a3438bsm1468187b3a.151.2024.06.27.08.12.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:12:32 -0700 (PDT) Date: Thu, 27 Jun 2024 23:12:29 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 01/12] fsck: rename "fsck_options" to "fsck_objects_options" Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The git-fsck(1) focuses on object database consistency check. It relies on the "fsck_options" to interact with fsck error levels. However, "fsck_options" aims at checking the object database which contains a lot of fields only related to object database. In order to provide git-fsck(1) with refs consistency check. Rename "fsck_options" to "fsck_objects_options" to explicitly indicate this structure is used to check the object database. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- builtin/fsck.c | 13 ++++---- builtin/index-pack.c | 18 +++++------ builtin/mktag.c | 13 ++++---- builtin/unpack-objects.c | 14 ++++----- fetch-pack.c | 12 ++++---- fsck.c | 64 ++++++++++++++++++++++------------------ fsck.h | 43 ++++++++++++++------------- object-file.c | 4 +-- 8 files changed, 95 insertions(+), 86 deletions(-) diff --git a/builtin/fsck.c b/builtin/fsck.c index d13a226c2e..ec3220880d 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -42,8 +42,8 @@ static int check_full = 1; static int connectivity_only; static int check_strict; static int keep_cache_objects; -static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT; -static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT; +static struct fsck_objects_options fsck_walk_options = FSCK_OBJECTS_OPTIONS_DEFAULT; +static struct fsck_objects_options fsck_obj_options = FSCK_OBJECTS_OPTIONS_DEFAULT; static int errors_found; static int write_lost_and_found; static int verbose; @@ -89,7 +89,7 @@ static int objerror(struct object *obj, const char *err) return -1; } -static int fsck_error_func(struct fsck_options *o UNUSED, +static int fsck_error_func(struct fsck_objects_options *o UNUSED, const struct object_id *oid, enum object_type object_type, enum fsck_msg_type msg_type, @@ -118,7 +118,7 @@ static int fsck_error_func(struct fsck_options *o UNUSED, static struct object_array pending; static int mark_object(struct object *obj, enum object_type type, - void *data, struct fsck_options *options UNUSED) + void *data, struct fsck_objects_options *options UNUSED) { struct object *parent = data; @@ -204,7 +204,8 @@ static int traverse_reachable(void) } static int mark_used(struct object *obj, enum object_type type UNUSED, - void *data UNUSED, struct fsck_options *options UNUSED) + void *data UNUSED, + struct fsck_objects_options *options UNUSED) { if (!obj) return 1; @@ -214,7 +215,7 @@ static int mark_used(struct object *obj, enum object_type type UNUSED, static void mark_unreachable_referents(const struct object_id *oid) { - struct fsck_options options = FSCK_OPTIONS_DEFAULT; + struct fsck_objects_options options = FSCK_OBJECTS_OPTIONS_DEFAULT; struct object *obj = lookup_object(the_repository, oid); if (!obj || !(obj->flags & HAS_OBJ)) diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 856428fef9..08ebeedfd3 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -127,7 +127,7 @@ static int nr_threads; static int from_stdin; static int strict; static int do_fsck_object; -static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES; +static struct fsck_objects_options fsck_objects_options = FSCK_OBJECTS_OPTIONS_MISSING_GITMODULES; static int verbose; static const char *progress_title; static int show_resolving_progress; @@ -220,7 +220,7 @@ static void cleanup_thread(void) static int mark_link(struct object *obj, enum object_type type, void *data UNUSED, - struct fsck_options *options UNUSED) + struct fsck_objects_options *options UNUSED) { if (!obj) return -1; @@ -852,7 +852,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry, else die(_("invalid blob object %s"), oid_to_hex(oid)); if (do_fsck_object && - fsck_object(&blob->object, (void *)data, size, &fsck_options)) + fsck_object(&blob->object, (void *)data, size, &fsck_objects_options)) die(_("fsck error in packed object")); } else { struct object *obj; @@ -871,9 +871,9 @@ static void sha1_object(const void *data, struct object_entry *obj_entry, if (!obj) die(_("invalid %s"), type_name(type)); if (do_fsck_object && - fsck_object(obj, buf, size, &fsck_options)) + fsck_object(obj, buf, size, &fsck_objects_options)) die(_("fsck error in packed object")); - if (strict && fsck_walk(obj, NULL, &fsck_options)) + if (strict && fsck_walk(obj, NULL, &fsck_objects_options)) die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid)); if (obj->type == OBJ_TREE) { @@ -1746,7 +1746,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) usage(index_pack_usage); disable_replace_refs(); - fsck_options.walk = mark_link; + fsck_objects_options.walk = mark_link; reset_pack_idx_option(&opts); opts.flags |= WRITE_REV; @@ -1770,13 +1770,13 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) } else if (skip_to_optional_arg(arg, "--strict", &arg)) { strict = 1; do_fsck_object = 1; - fsck_set_msg_types(&fsck_options, arg); + fsck_set_msg_types(&fsck_objects_options, arg); } else if (!strcmp(arg, "--check-self-contained-and-connected")) { strict = 1; check_self_contained_and_connected = 1; } else if (skip_to_optional_arg(arg, "--fsck-objects", &arg)) { do_fsck_object = 1; - fsck_set_msg_types(&fsck_options, arg); + fsck_set_msg_types(&fsck_objects_options, arg); } else if (!strcmp(arg, "--verify")) { verify = 1; } else if (!strcmp(arg, "--verify-stat")) { @@ -1943,7 +1943,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) else close(input_fd); - if (do_fsck_object && fsck_finish(&fsck_options)) + if (do_fsck_object && fsck_finish(&fsck_objects_options)) die(_("fsck error in pack objects")); free(opts.anomaly); diff --git a/builtin/mktag.c b/builtin/mktag.c index 4767f1a97e..be2abc71d8 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -15,9 +15,9 @@ static char const * const builtin_mktag_usage[] = { }; static int option_strict = 1; -static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; +static struct fsck_objects_options fsck_objects_options = FSCK_OBJECTS_OPTIONS_STRICT; -static int mktag_fsck_error_func(struct fsck_options *o UNUSED, +static int mktag_fsck_error_func(struct fsck_objects_options *o UNUSED, const struct object_id *oid UNUSED, enum object_type object_type UNUSED, enum fsck_msg_type msg_type, @@ -91,12 +91,13 @@ int cmd_mktag(int argc, const char **argv, const char *prefix) if (strbuf_read(&buf, 0, 0) < 0) die_errno(_("could not read from stdin")); - fsck_options.error_func = mktag_fsck_error_func; - fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY, + fsck_objects_options.error_func = mktag_fsck_error_func; + fsck_set_msg_type_from_ids(&fsck_objects_options, + FSCK_MSG_EXTRA_HEADER_ENTRY, FSCK_WARN); /* config might set fsck.extraHeaderEntry=* again */ - git_config(git_fsck_config, &fsck_options); - if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options, + git_config(git_fsck_config, &fsck_objects_options); + if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_objects_options, &tagged_oid, &tagged_type)) die(_("tag on stdin did not pass our strict fsck check")); diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index f1c85a00ae..c59e330db9 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -25,7 +25,7 @@ static unsigned int offset, len; static off_t consumed_bytes; static off_t max_input_size; static git_hash_ctx ctx; -static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; +static struct fsck_objects_options fsck_objects_options = FSCK_OBJECTS_OPTIONS_STRICT; static struct progress *progress; /* @@ -212,7 +212,7 @@ static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf) */ static int check_object(struct object *obj, enum object_type type, void *data UNUSED, - struct fsck_options *options UNUSED) + struct fsck_objects_options *options UNUSED) { struct obj_buffer *obj_buf; @@ -237,10 +237,10 @@ static int check_object(struct object *obj, enum object_type type, obj_buf = lookup_object_buffer(obj); if (!obj_buf) die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid)); - if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options)) + if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_objects_options)) die("fsck error in packed object"); - fsck_options.walk = check_object; - if (fsck_walk(obj, NULL, &fsck_options)) + fsck_objects_options.walk = check_object; + if (fsck_walk(obj, NULL, &fsck_objects_options)) die("Error on reachable objects of %s", oid_to_hex(&obj->oid)); write_cached_object(obj, obj_buf); return 0; @@ -635,7 +635,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED) } if (skip_prefix(arg, "--strict=", &arg)) { strict = 1; - fsck_set_msg_types(&fsck_options, arg); + fsck_set_msg_types(&fsck_objects_options, arg); continue; } if (starts_with(arg, "--pack_header=")) { @@ -671,7 +671,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED) the_hash_algo->final_oid_fn(&oid, &tmp_ctx); if (strict) { write_rest(); - if (fsck_finish(&fsck_options)) + if (fsck_finish(&fsck_objects_options)) die(_("fsck error in pack objects")); } if (!hasheq(fill(the_hash_algo->rawsz), oid.hash)) diff --git a/fetch-pack.c b/fetch-pack.c index eba9e420ea..7d1e4a5087 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -46,7 +46,7 @@ static int server_supports_filtering; static int advertise_sid; static struct shallow_lock shallow_lock; static const char *alternate_shallow_file; -static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES; +static struct fsck_objects_options fsck_objects_options = FSCK_OBJECTS_OPTIONS_MISSING_GITMODULES; static struct strbuf fsck_msg_types = STRBUF_INIT; static struct string_list uri_protocols = STRING_LIST_INIT_DUP; @@ -1220,9 +1220,9 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args, } else alternate_shallow_file = NULL; if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought, - &fsck_options.gitmodules_found)) + &fsck_objects_options.gitmodules_found)) die(_("git fetch-pack: fetch failed.")); - if (fsck_finish(&fsck_options)) + if (fsck_finish(&fsck_objects_options)) die("fsck failed"); all_done: @@ -1780,7 +1780,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, if (get_pack(args, fd, pack_lockfiles, packfile_uris.nr ? &index_pack_args : NULL, - sought, nr_sought, &fsck_options.gitmodules_found)) + sought, nr_sought, &fsck_objects_options.gitmodules_found)) die(_("git fetch-pack: fetch failed.")); do_check_stateless_delimiter(args->stateless_rpc, &reader); @@ -1823,7 +1823,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, packname[the_hash_algo->hexsz] = '\0'; - parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found); + parse_gitmodules_oids(cmd.out, &fsck_objects_options.gitmodules_found); close(cmd.out); @@ -1844,7 +1844,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, string_list_clear(&packfile_uris, 0); strvec_clear(&index_pack_args); - if (fsck_finish(&fsck_options)) + if (fsck_finish(&fsck_objects_options)) die("fsck failed"); if (negotiator) diff --git a/fsck.c b/fsck.c index e193930ae7..c24a0f9fae 100644 --- a/fsck.c +++ b/fsck.c @@ -99,7 +99,7 @@ void list_config_fsck_msg_ids(struct string_list *list, const char *prefix) } static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id, - struct fsck_options *options) + struct fsck_objects_options *options) { assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); @@ -134,7 +134,7 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type) return 1; } -void fsck_set_msg_type_from_ids(struct fsck_options *options, +void fsck_set_msg_type_from_ids(struct fsck_objects_options *options, enum fsck_msg_id msg_id, enum fsck_msg_type msg_type) { @@ -150,7 +150,7 @@ void fsck_set_msg_type_from_ids(struct fsck_options *options, options->msg_type[msg_id] = msg_type; } -void fsck_set_msg_type(struct fsck_options *options, +void fsck_set_msg_type(struct fsck_objects_options *options, const char *msg_id_str, const char *msg_type_str) { int msg_id = parse_msg_id(msg_id_str); @@ -179,7 +179,7 @@ void fsck_set_msg_type(struct fsck_options *options, free(to_free); } -void fsck_set_msg_types(struct fsck_options *options, const char *values) +void fsck_set_msg_types(struct fsck_objects_options *options, const char *values) { char *buf = xstrdup(values), *to_free = buf; int done = 0; @@ -217,14 +217,14 @@ void fsck_set_msg_types(struct fsck_options *options, const char *values) free(to_free); } -static int object_on_skiplist(struct fsck_options *opts, +static int object_on_skiplist(struct fsck_objects_options *opts, const struct object_id *oid) { return opts && oid && oidset_contains(&opts->skiplist, oid); } __attribute__((format (printf, 5, 6))) -static int report(struct fsck_options *options, +static int report(struct fsck_objects_options *options, const struct object_id *oid, enum object_type object_type, enum fsck_msg_id msg_id, const char *fmt, ...) { @@ -257,13 +257,13 @@ static int report(struct fsck_options *options, return result; } -void fsck_enable_object_names(struct fsck_options *options) +void fsck_enable_object_names(struct fsck_objects_options *options) { if (!options->object_names) options->object_names = kh_init_oid_map(); } -const char *fsck_get_object_name(struct fsck_options *options, +const char *fsck_get_object_name(struct fsck_objects_options *options, const struct object_id *oid) { khiter_t pos; @@ -275,7 +275,7 @@ const char *fsck_get_object_name(struct fsck_options *options, return kh_value(options->object_names, pos); } -void fsck_put_object_name(struct fsck_options *options, +void fsck_put_object_name(struct fsck_objects_options *options, const struct object_id *oid, const char *fmt, ...) { @@ -296,7 +296,7 @@ void fsck_put_object_name(struct fsck_options *options, va_end(ap); } -const char *fsck_describe_object(struct fsck_options *options, +const char *fsck_describe_object(struct fsck_objects_options *options, const struct object_id *oid) { static struct strbuf bufs[] = { @@ -316,7 +316,8 @@ const char *fsck_describe_object(struct fsck_options *options, return buf->buf; } -static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options) +static int fsck_walk_tree(struct tree *tree, void *data, + struct fsck_objects_options *options) { struct tree_desc desc; struct name_entry entry; @@ -364,7 +365,8 @@ static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *op return res; } -static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options) +static int fsck_walk_commit(struct commit *commit, void *data, + struct fsck_objects_options *options) { int counter = 0, generation = 0, name_prefix_len = 0; struct commit_list *parents; @@ -433,7 +435,8 @@ static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_optio return res; } -static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options) +static int fsck_walk_tag(struct tag *tag, void *data, + struct fsck_objects_options *options) { const char *name = fsck_get_object_name(options, &tag->object.oid); @@ -444,7 +447,8 @@ static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *optio return options->walk(tag->tagged, OBJ_ANY, data, options); } -int fsck_walk(struct object *obj, void *data, struct fsck_options *options) +int fsck_walk(struct object *obj, void *data, + struct fsck_objects_options *options) { if (!obj) return -1; @@ -580,7 +584,7 @@ static int verify_ordered(unsigned mode1, const char *name1, static int fsck_tree(const struct object_id *tree_oid, const char *buffer, unsigned long size, - struct fsck_options *options) + struct fsck_objects_options *options) { int retval = 0; int has_null_sha1 = 0; @@ -793,7 +797,7 @@ static int fsck_tree(const struct object_id *tree_oid, */ static int verify_headers(const void *data, unsigned long size, const struct object_id *oid, enum object_type type, - struct fsck_options *options) + struct fsck_objects_options *options) { const char *buffer = (const char *)data; unsigned long i; @@ -825,7 +829,7 @@ static int verify_headers(const void *data, unsigned long size, static int fsck_ident(const char **ident, const struct object_id *oid, enum object_type type, - struct fsck_options *options) + struct fsck_objects_options *options) { const char *p = *ident; char *end; @@ -885,7 +889,7 @@ static int fsck_ident(const char **ident, static int fsck_commit(const struct object_id *oid, const char *buffer, unsigned long size, - struct fsck_options *options) + struct fsck_objects_options *options) { struct object_id tree_oid, parent_oid; unsigned author_count; @@ -946,7 +950,7 @@ static int fsck_commit(const struct object_id *oid, } static int fsck_tag(const struct object_id *oid, const char *buffer, - unsigned long size, struct fsck_options *options) + unsigned long size, struct fsck_objects_options *options) { struct object_id tagged_oid; int tagged_type; @@ -955,7 +959,7 @@ static int fsck_tag(const struct object_id *oid, const char *buffer, } int fsck_tag_standalone(const struct object_id *oid, const char *buffer, - unsigned long size, struct fsck_options *options, + unsigned long size, struct fsck_objects_options *options, struct object_id *tagged_oid, int *tagged_type) { @@ -1050,7 +1054,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer, struct fsck_gitmodules_data { const struct object_id *oid; - struct fsck_options *options; + struct fsck_objects_options *options; int ret; }; @@ -1100,7 +1104,8 @@ static int fsck_gitmodules_fn(const char *var, const char *value, } static int fsck_blob(const struct object_id *oid, const char *buf, - unsigned long size, struct fsck_options *options) + unsigned long size, + struct fsck_objects_options *options) { int ret = 0; @@ -1170,7 +1175,7 @@ static int fsck_blob(const struct object_id *oid, const char *buf, } int fsck_object(struct object *obj, void *data, unsigned long size, - struct fsck_options *options) + struct fsck_objects_options *options) { if (!obj) return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); @@ -1180,7 +1185,7 @@ int fsck_object(struct object *obj, void *data, unsigned long size, int fsck_buffer(const struct object_id *oid, enum object_type type, const void *data, unsigned long size, - struct fsck_options *options) + struct fsck_objects_options *options) { if (type == OBJ_BLOB) return fsck_blob(oid, data, size, options); @@ -1197,7 +1202,7 @@ int fsck_buffer(const struct object_id *oid, enum object_type type, type); } -int fsck_error_function(struct fsck_options *o, +int fsck_error_function(struct fsck_objects_options *o, const struct object_id *oid, enum object_type object_type UNUSED, enum fsck_msg_type msg_type, @@ -1214,7 +1219,8 @@ int fsck_error_function(struct fsck_options *o, static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done, enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type, - struct fsck_options *options, const char *blob_type) + struct fsck_objects_options *options, + const char *blob_type) { int ret = 0; struct oidset_iter iter; @@ -1253,7 +1259,7 @@ static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done, return ret; } -int fsck_finish(struct fsck_options *options) +int fsck_finish(struct fsck_objects_options *options) { int ret = 0; @@ -1270,7 +1276,7 @@ int fsck_finish(struct fsck_options *options) int git_fsck_config(const char *var, const char *value, const struct config_context *ctx, void *cb) { - struct fsck_options *options = cb; + struct fsck_objects_options *options = cb; const char *msg_id; if (strcmp(var, "fsck.skiplist") == 0) { @@ -1300,7 +1306,7 @@ int git_fsck_config(const char *var, const char *value, * Custom error callbacks that are used in more than one place. */ -int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o, +int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *o, const struct object_id *oid, enum object_type object_type, enum fsck_msg_type msg_type, diff --git a/fsck.h b/fsck.h index 6085a384f6..b64164db17 100644 --- a/fsck.h +++ b/fsck.h @@ -92,15 +92,15 @@ enum fsck_msg_id { }; #undef MSG_ID -struct fsck_options; +struct fsck_objects_options; struct object; -void fsck_set_msg_type_from_ids(struct fsck_options *options, +void fsck_set_msg_type_from_ids(struct fsck_objects_options *options, enum fsck_msg_id msg_id, enum fsck_msg_type msg_type); -void fsck_set_msg_type(struct fsck_options *options, +void fsck_set_msg_type(struct fsck_objects_options *options, const char *msg_id, const char *msg_type); -void fsck_set_msg_types(struct fsck_options *options, const char *values); +void fsck_set_msg_types(struct fsck_objects_options *options, const char *values); int is_valid_msg_type(const char *msg_id, const char *msg_type); /* @@ -112,26 +112,26 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type); * >0 error signaled and do not abort */ typedef int (*fsck_walk_func)(struct object *obj, enum object_type object_type, - void *data, struct fsck_options *options); + void *data, struct fsck_objects_options *options); /* callback for fsck_object, type is FSCK_ERROR or FSCK_WARN */ -typedef int (*fsck_error)(struct fsck_options *o, +typedef int (*fsck_error)(struct fsck_objects_options *o, const struct object_id *oid, enum object_type object_type, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); -int fsck_error_function(struct fsck_options *o, +int fsck_error_function(struct fsck_objects_options *o, const struct object_id *oid, enum object_type object_type, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); -int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o, +int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *o, const struct object_id *oid, enum object_type object_type, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); -struct fsck_options { +struct fsck_objects_options { fsck_walk_func walk; fsck_error error_func; unsigned strict:1; @@ -144,7 +144,7 @@ struct fsck_options { kh_oid_map_t *object_names; }; -#define FSCK_OPTIONS_DEFAULT { \ +#define FSCK_OBJECTS_OPTIONS_DEFAULT { \ .skiplist = OIDSET_INIT, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ @@ -152,7 +152,7 @@ struct fsck_options { .gitattributes_done = OIDSET_INIT, \ .error_func = fsck_error_function \ } -#define FSCK_OPTIONS_STRICT { \ +#define FSCK_OBJECTS_OPTIONS_STRICT { \ .strict = 1, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ @@ -160,7 +160,7 @@ struct fsck_options { .gitattributes_done = OIDSET_INIT, \ .error_func = fsck_error_function, \ } -#define FSCK_OPTIONS_MISSING_GITMODULES { \ +#define FSCK_OBJECTS_OPTIONS_MISSING_GITMODULES { \ .strict = 1, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ @@ -176,14 +176,15 @@ struct fsck_options { * >0 return value of the first signaled error >0 (in the case of no other errors) * 0 everything OK */ -int fsck_walk(struct object *obj, void *data, struct fsck_options *options); +int fsck_walk(struct object *obj, void *data, + struct fsck_objects_options *options); /* * Blob objects my pass a NULL data pointer, which indicates they are too large * to fit in memory. All other types must pass a real buffer. */ int fsck_object(struct object *obj, void *data, unsigned long size, - struct fsck_options *options); + struct fsck_objects_options *options); /* * Same as fsck_object(), but for when the caller doesn't have an object @@ -191,14 +192,14 @@ int fsck_object(struct object *obj, void *data, unsigned long size, */ int fsck_buffer(const struct object_id *oid, enum object_type, const void *data, unsigned long size, - struct fsck_options *options); + struct fsck_objects_options *options); /* * fsck a tag, and pass info about it back to the caller. This is * exposed fsck_object() internals for git-mktag(1). */ int fsck_tag_standalone(const struct object_id *oid, const char *buffer, - unsigned long size, struct fsck_options *options, + unsigned long size, struct fsck_objects_options *options, struct object_id *tagged_oid, int *tag_type); @@ -207,7 +208,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer, * after completing all fsck_object() calls in order to resolve any remaining * checks. */ -int fsck_finish(struct fsck_options *options); +int fsck_finish(struct fsck_objects_options *options); /* * Subsystem for storing human-readable names for each object. @@ -224,14 +225,14 @@ int fsck_finish(struct fsck_options *options); * points to a rotating array of static buffers, and may be invalidated by a * subsequent call. */ -void fsck_enable_object_names(struct fsck_options *options); -const char *fsck_get_object_name(struct fsck_options *options, +void fsck_enable_object_names(struct fsck_objects_options *options); +const char *fsck_get_object_name(struct fsck_objects_options *options, const struct object_id *oid); __attribute__((format (printf,3,4))) -void fsck_put_object_name(struct fsck_options *options, +void fsck_put_object_name(struct fsck_objects_options *options, const struct object_id *oid, const char *fmt, ...); -const char *fsck_describe_object(struct fsck_options *options, +const char *fsck_describe_object(struct fsck_objects_options *options, const struct object_id *oid); struct key_value_info; diff --git a/object-file.c b/object-file.c index d3cf4b8b2e..ec44ac3d82 100644 --- a/object-file.c +++ b/object-file.c @@ -2472,7 +2472,7 @@ int repo_has_object_file(struct repository *r, * report the minimal fsck error here, and rely on the caller to * give more context. */ -static int hash_format_check_report(struct fsck_options *opts UNUSED, +static int hash_format_check_report(struct fsck_objects_options *opts UNUSED, const struct object_id *oid UNUSED, enum object_type object_type UNUSED, enum fsck_msg_type msg_type UNUSED, @@ -2507,7 +2507,7 @@ static int index_mem(struct index_state *istate, } } if (flags & HASH_FORMAT_CHECK) { - struct fsck_options opts = FSCK_OPTIONS_DEFAULT; + struct fsck_objects_options opts = FSCK_OBJECTS_OPTIONS_DEFAULT; opts.strict = 1; opts.error_func = hash_format_check_report; From patchwork Thu Jun 27 15:13:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714626 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3340198A2E for ; Thu, 27 Jun 2024 15:13:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501190; cv=none; b=MaWhtgpcOahxLxqhq/mkBvh7vRLRXg8k3mEXVfGB/JtVUj6MTdjlUvphbUTg0zW2c5XlhXYX9+VV0AX8OzfMkEG4Jy4UaXYJ0MU23aBGPtzbAU9CJzS0dOfKnxGZuMDcCwcilIt08N8SL4anRWAH4GrpiK/Mkj5Yp88ElLn61BA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501190; c=relaxed/simple; bh=RTqiPnxywsUyIdghyVHF5hRKhzUpLCWlKayt8SVcRHM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=G4p0eODZJVRwXopJeLKIv3mRODGrCf7vb1ADLktUdCmzxJjbnDXKTpN8IYPNkIYLw0bfsjqaFTVV9OrsNv6OXCwBSd9wPeFjS255cNyuYPEAx3H7l3V/Ol45GMjeBQcNrGDwV4G7BEtK5ZTmWXsm+RwkRw7mAIiGWDyS7uhpUdM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NOTP02q/; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NOTP02q/" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-707f9c3bd02so349897b3a.0 for ; Thu, 27 Jun 2024 08:13:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501187; x=1720105987; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=pq6SCM1rjT/Vc0WW6DOriW1hZ8h0R5z5wRIJH2w85dM=; b=NOTP02q/VSEi4HSywD5jE3+wrvf1wDSYS1bVsLXDGnLSXdqvOI6tDMqbK2ZhZKDneP 3OfBC48ywlo2RtE1xhcGuoOafcap6eQPfEXp/ex0hhhawdCvg5439MUBc395RaLrjX+y IPwTwuqEp6CQdUNrAGBFyOUs1YOrbP5VnOOHjnuH0Z5blJ8alAuDqV8qhpg0YEY4MGvn XIy7reD5AybFDqet13Cuoxs3MvyzTskIse6Z19F8EY0rZW+H32xYzY6LzVrdP+/mCS59 n+X9DTS89dkW1qiclYNJj9OM7i8Jr5H0eH+RklQI7Rq5orBUPOhvJ5ARZI8tNcVXhbAN 9IpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501187; x=1720105987; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pq6SCM1rjT/Vc0WW6DOriW1hZ8h0R5z5wRIJH2w85dM=; b=oKv/P1r4jwUJeIGyeiief1rpFWLFX8KSeVMW85majiklpOLIX5Wf4O6Tcs3t2oOSMn bv+skw8Svt92dRHLtTcjBiAFIqXwd2lhyazQVBXcXIhwyfrN2zonmZBC/Lj2s0t+HZSm 5ZFH7kU1BHRo59F9nufN5TVNlgPjbSjb7srQEOA3LSkj+uCKbgwPdagRoRviJsd+WDXT Qgmxu3SY21DK80Ts1woCk7LfA6p7ftNkN5Eh4Cjwpc514bAw5jQJyM6WSsuVlMTf7mwj Ajr6a/3DdKDEGmzXnNLrEnp2ZCkNbJMVLwrKoxK9Z9pHAL97jGrGUfBLO7YRF9Bf5xpT EDag== X-Gm-Message-State: AOJu0YyNKQkiN6Cfcv6eI5luwKYYdhfJ9qZyShQrp+1cwuhiAuH8FoaJ j4Thun9Ewx77er3xlOPmwZ1xi0vGY6+CZuNR/W4HBcBaIQOdamEmkG4/zg== X-Google-Smtp-Source: AGHT+IGirkj5rKtiTu4f1qhbQzunQnv52knQsuYgIVQWWMgDdQqpuxZajdyiLp323ZdfebwMCbihpg== X-Received: by 2002:a05:6a00:1715:b0:706:6bf8:bd2 with SMTP id d2e1a72fcca58-706746b5c30mr12608251b3a.21.1719501187354; Thu, 27 Jun 2024 08:13:07 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706b49104acsm1473115b3a.46.2024.06.27.08.13.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:13:06 -0700 (PDT) Date: Thu, 27 Jun 2024 23:13:04 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 02/12] fsck: use "fsck_configs" to set up configs Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Some fields such as "msg_type" and "skiplist" in "fsck_objects_options" are not options, these fields are related to "git-config(1)" which are initialized using "git_fsck_config" function. Create a static variable named "fsck_configs" in "fsck.c" which aims at handling configs. Thus we don't need to reply on the "fsck_objects_options" to set up the fsck error message severity. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- fsck.c | 29 ++++++++++++++++++----------- fsck.h | 3 --- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/fsck.c b/fsck.c index c24a0f9fae..81b93f02fc 100644 --- a/fsck.c +++ b/fsck.c @@ -24,6 +24,14 @@ static ssize_t max_tree_entry_len = 4096; +static struct { + enum fsck_msg_type *msg_type; + struct oidset oid_skiplist; +} fsck_configs = { + .msg_type = NULL, + .oid_skiplist = OIDSET_INIT +}; + #define STR(x) #x #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type }, static struct { @@ -103,7 +111,7 @@ static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id, { assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); - if (!options->msg_type) { + if (!fsck_configs.msg_type) { enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type; if (options->strict && msg_type == FSCK_WARN) @@ -111,7 +119,7 @@ static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id, return msg_type; } - return options->msg_type[msg_id]; + return fsck_configs.msg_type[msg_id]; } static enum fsck_msg_type parse_msg_type(const char *str) @@ -138,16 +146,16 @@ void fsck_set_msg_type_from_ids(struct fsck_objects_options *options, enum fsck_msg_id msg_id, enum fsck_msg_type msg_type) { - if (!options->msg_type) { + if (!fsck_configs.msg_type) { int i; enum fsck_msg_type *severity; ALLOC_ARRAY(severity, FSCK_MSG_MAX); for (i = 0; i < FSCK_MSG_MAX; i++) severity[i] = fsck_msg_type(i, options); - options->msg_type = severity; + fsck_configs.msg_type = severity; } - options->msg_type[msg_id] = msg_type; + fsck_configs.msg_type[msg_id] = msg_type; } void fsck_set_msg_type(struct fsck_objects_options *options, @@ -203,7 +211,7 @@ void fsck_set_msg_types(struct fsck_objects_options *options, const char *values if (!strcmp(buf, "skiplist")) { if (equal == len) die("skiplist requires a path"); - oidset_parse_file(&options->skiplist, buf + equal + 1); + oidset_parse_file(&fsck_configs.oid_skiplist, buf + equal + 1); buf += len + 1; continue; } @@ -217,10 +225,9 @@ void fsck_set_msg_types(struct fsck_objects_options *options, const char *values free(to_free); } -static int object_on_skiplist(struct fsck_objects_options *opts, - const struct object_id *oid) +static int object_on_skiplist(const struct object_id *oid) { - return opts && oid && oidset_contains(&opts->skiplist, oid); + return oid && oidset_contains(&fsck_configs.oid_skiplist, oid); } __attribute__((format (printf, 5, 6))) @@ -236,7 +243,7 @@ static int report(struct fsck_objects_options *options, if (msg_type == FSCK_IGNORE) return 0; - if (object_on_skiplist(options, oid)) + if (object_on_skiplist(oid)) return 0; if (msg_type == FSCK_FATAL) @@ -1109,7 +1116,7 @@ static int fsck_blob(const struct object_id *oid, const char *buf, { int ret = 0; - if (object_on_skiplist(options, oid)) + if (object_on_skiplist(oid)) return 0; if (oidset_contains(&options->gitmodules_found, oid)) { diff --git a/fsck.h b/fsck.h index b64164db17..37deadc4bd 100644 --- a/fsck.h +++ b/fsck.h @@ -135,8 +135,6 @@ struct fsck_objects_options { fsck_walk_func walk; fsck_error error_func; unsigned strict:1; - enum fsck_msg_type *msg_type; - struct oidset skiplist; struct oidset gitmodules_found; struct oidset gitmodules_done; struct oidset gitattributes_found; @@ -145,7 +143,6 @@ struct fsck_objects_options { }; #define FSCK_OBJECTS_OPTIONS_DEFAULT { \ - .skiplist = OIDSET_INIT, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ From patchwork Thu Jun 27 15:13:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714627 Received: from mail-il1-f177.google.com (mail-il1-f177.google.com [209.85.166.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CF9719884D for ; Thu, 27 Jun 2024 15:13:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501221; cv=none; b=py5Hmvki9Rpq6ybXAbWWc9qvWp7imQOnonFF5aE0mEmxIy2aKa3QtNdXYJiA5uuDrmslp6HWxB+Mrvupcg6xz7bCrWBbaMJy/wrs+kbN6LmmkNctEctnNLbh08+D8zKCcNtvjn5mAfsO7ONvJcBm5RIOaPw5tzSBPBeIdbHMZnY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501221; c=relaxed/simple; bh=B13UYPZYulP1IRnaO4q4bGWZ/+KAwxWLbfcDd6gVR1k=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=srBjUHSQrriqc5nxrxJTaH85ulKyMIN3VAfCYdDqXHvMTuttWGprjZnRpjJHJE6QU3KrARU3ST+8d2f1SXxXMGrEgSxwfIEHphR2yeo7/71EcDmfrfGYK+kTAIc6NjD82xKAjjCqAbqlx9MPiScQEqm2wVqlU0bYzUVVAu87nnU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Rx3HfQpq; arc=none smtp.client-ip=209.85.166.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Rx3HfQpq" Received: by mail-il1-f177.google.com with SMTP id e9e14a558f8ab-3737dc4a669so32459615ab.0 for ; Thu, 27 Jun 2024 08:13:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501218; x=1720106018; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=4TX0U3b8jimiqgeX357u7GUJDhVKKzDM/foxrwVdpbY=; b=Rx3HfQpq95hxICmsq4lJOa05X5EF9EG++c8lDN+GzcdDF5s22wOhucQQEGuSNXF3Gl DDSLwVaqnUfpnhUEk66KXdQqJg3YT8TQsyclCl+9c+tEVOjKZnaSVHPM636tg6kwYzk9 CW5ty+DlOHgd3IjLswgbslJmQhHLucI5lM9bbrHaxNWwtT0A4mOviAL9WQen/uoEE+aa iSlbPAfsdTrOn/WrkLyhbo387SVc69+58hRbft5lkkE1yWqnipkibqmYP5g6vqm15j7M Ud8b1l6+2+wue71PMkMqErqv9CIKydYg02Tq0TSfyPe+JA4/ovlOwjvVw8rLd+MyPAF8 0LvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501218; x=1720106018; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=4TX0U3b8jimiqgeX357u7GUJDhVKKzDM/foxrwVdpbY=; b=K7lgRKydsaygIAPv+DVNsEr917U1BwlYzumv3YrDUj6qw9jXBJsvkaO+laotfQkswX dSRtIqtiwXosVqLRs9RTvzfOLX4NxaaHy7l8KlsGya9UfmCR76slJHq5i4WE8ygkQqSO WRSPSjlUc94wzfgt5bacZFuH4jGswbKwG1z3miHGU4IM7XuUdEyQ4lXv8M3La5Yrb+hC 5iq+jA+wPqzXX6peWxYMuZphMsUDoxgpBgLRL1b6hTcOzbh2rBaSTv+L56yp9pfv4cpC euXSUPi6VIduG2tPPiGbjAu9AOzIhZBV6DYIIR1o/QdZejmlai1NQ5uo0d1AFhKJORMQ tOlA== X-Gm-Message-State: AOJu0YyH9IZUmQ61uYkKqtbHzbcUOYW9G7EODdhgrjkH8ijT0ATWfkHR 0i32oM8+rpadfFm2G+JG1qL3qYw/Kj3j/4WOrXi6GXCm5uOwEKbXyHEkKw== X-Google-Smtp-Source: AGHT+IFh8JIzP8I/wUNs+VVemS0VbJwUYftxYOZiny1wDT7SmoHiD3A24eUzi15kLKeQ4DR0fyCPwA== X-Received: by 2002:a05:6e02:188a:b0:375:ca62:a418 with SMTP id e9e14a558f8ab-3763f6cd3a9mr203177865ab.22.1719501217817; Thu, 27 Jun 2024 08:13:37 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-72749854a9fsm1173462a12.92.2024.06.27.08.13.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:13:37 -0700 (PDT) Date: Thu, 27 Jun 2024 23:13:34 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 03/12] fsck: abstract common options for reusing Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Create "fsck_options" struct to contain the general fields. Change "fsck_objects_options" to incorporate the general "fsck_options" and object-speicifc options. The fsck message will use "strict" option to set the corresponding "msg_type". Rename the parameter name from "struct fsck_objects_options *" to "struct fsck_options *". Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- builtin/fsck.c | 6 +++--- builtin/index-pack.c | 4 ++-- builtin/mktag.c | 6 +++--- builtin/unpack-objects.c | 2 +- fsck.c | 25 ++++++++++++++++--------- fsck.h | 32 ++++++++++++++++++++++---------- object-file.c | 4 ++-- 7 files changed, 49 insertions(+), 30 deletions(-) diff --git a/builtin/fsck.c b/builtin/fsck.c index ec3220880d..c383125027 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -939,9 +939,9 @@ int cmd_fsck(int argc, const char **argv, const char *prefix) fsck_walk_options.walk = mark_object; fsck_obj_options.walk = mark_used; - fsck_obj_options.error_func = fsck_error_func; + fsck_obj_options.fsck_options.error_func = fsck_error_func; if (check_strict) - fsck_obj_options.strict = 1; + fsck_obj_options.fsck_options.strict = 1; if (show_progress == -1) show_progress = isatty(2); @@ -956,7 +956,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix) if (name_objects) fsck_enable_object_names(&fsck_walk_options); - git_config(git_fsck_config, &fsck_obj_options); + git_config(git_fsck_config, &fsck_obj_options.fsck_options); prepare_repo_settings(the_repository); if (connectivity_only) { diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 08ebeedfd3..360106b0c8 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1770,13 +1770,13 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) } else if (skip_to_optional_arg(arg, "--strict", &arg)) { strict = 1; do_fsck_object = 1; - fsck_set_msg_types(&fsck_objects_options, arg); + fsck_set_msg_types(&fsck_objects_options.fsck_options, arg); } else if (!strcmp(arg, "--check-self-contained-and-connected")) { strict = 1; check_self_contained_and_connected = 1; } else if (skip_to_optional_arg(arg, "--fsck-objects", &arg)) { do_fsck_object = 1; - fsck_set_msg_types(&fsck_objects_options, arg); + fsck_set_msg_types(&fsck_objects_options.fsck_options, arg); } else if (!strcmp(arg, "--verify")) { verify = 1; } else if (!strcmp(arg, "--verify-stat")) { diff --git a/builtin/mktag.c b/builtin/mktag.c index be2abc71d8..76860f4c7c 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -91,12 +91,12 @@ int cmd_mktag(int argc, const char **argv, const char *prefix) if (strbuf_read(&buf, 0, 0) < 0) die_errno(_("could not read from stdin")); - fsck_objects_options.error_func = mktag_fsck_error_func; - fsck_set_msg_type_from_ids(&fsck_objects_options, + fsck_objects_options.fsck_options.error_func = mktag_fsck_error_func; + fsck_set_msg_type_from_ids(&fsck_objects_options.fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY, FSCK_WARN); /* config might set fsck.extraHeaderEntry=* again */ - git_config(git_fsck_config, &fsck_objects_options); + git_config(git_fsck_config, &fsck_objects_options.fsck_options); if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_objects_options, &tagged_oid, &tagged_type)) die(_("tag on stdin did not pass our strict fsck check")); diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index c59e330db9..d8d0b14018 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -635,7 +635,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED) } if (skip_prefix(arg, "--strict=", &arg)) { strict = 1; - fsck_set_msg_types(&fsck_objects_options, arg); + fsck_set_msg_types(&fsck_objects_options.fsck_options, arg); continue; } if (starts_with(arg, "--pack_header=")) { diff --git a/fsck.c b/fsck.c index 81b93f02fc..7ac6e4587c 100644 --- a/fsck.c +++ b/fsck.c @@ -107,7 +107,7 @@ void list_config_fsck_msg_ids(struct string_list *list, const char *prefix) } static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id, - struct fsck_objects_options *options) + struct fsck_options *options) { assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); @@ -142,7 +142,7 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type) return 1; } -void fsck_set_msg_type_from_ids(struct fsck_objects_options *options, +void fsck_set_msg_type_from_ids(struct fsck_options *options, enum fsck_msg_id msg_id, enum fsck_msg_type msg_type) { @@ -158,7 +158,7 @@ void fsck_set_msg_type_from_ids(struct fsck_objects_options *options, fsck_configs.msg_type[msg_id] = msg_type; } -void fsck_set_msg_type(struct fsck_objects_options *options, +void fsck_set_msg_type(struct fsck_options *options, const char *msg_id_str, const char *msg_type_str) { int msg_id = parse_msg_id(msg_id_str); @@ -187,7 +187,7 @@ void fsck_set_msg_type(struct fsck_objects_options *options, free(to_free); } -void fsck_set_msg_types(struct fsck_objects_options *options, const char *values) +void fsck_set_msg_types(struct fsck_options *options, const char *values) { char *buf = xstrdup(values), *to_free = buf; int done = 0; @@ -237,9 +237,16 @@ static int report(struct fsck_objects_options *options, { va_list ap; struct strbuf sb = STRBUF_INIT; - enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options); + struct fsck_options *fsck_options; + enum fsck_msg_type msg_type; int result; + if (options) + fsck_options = &options->fsck_options; + else + BUG("fsck_options is not set"); + + msg_type = fsck_msg_type(msg_id, fsck_options); if (msg_type == FSCK_IGNORE) return 0; @@ -256,8 +263,8 @@ static int report(struct fsck_objects_options *options, va_start(ap, fmt); strbuf_vaddf(&sb, fmt, ap); - result = options->error_func(options, oid, object_type, - msg_type, msg_id, sb.buf); + result = fsck_options->error_func(options, oid, object_type, + msg_type, msg_id, sb.buf); strbuf_release(&sb); va_end(ap); @@ -711,7 +718,7 @@ static int fsck_tree(const struct object_id *tree_oid, * bits.. */ case S_IFREG | 0664: - if (!options->strict) + if (!options->fsck_options.strict) break; /* fallthrough */ default: @@ -1283,7 +1290,7 @@ int fsck_finish(struct fsck_objects_options *options) int git_fsck_config(const char *var, const char *value, const struct config_context *ctx, void *cb) { - struct fsck_objects_options *options = cb; + struct fsck_options *options = cb; const char *msg_id; if (strcmp(var, "fsck.skiplist") == 0) { diff --git a/fsck.h b/fsck.h index 37deadc4bd..e531b44a66 100644 --- a/fsck.h +++ b/fsck.h @@ -92,15 +92,16 @@ enum fsck_msg_id { }; #undef MSG_ID +struct fsck_options; struct fsck_objects_options; struct object; -void fsck_set_msg_type_from_ids(struct fsck_objects_options *options, +void fsck_set_msg_type_from_ids(struct fsck_options *options, enum fsck_msg_id msg_id, enum fsck_msg_type msg_type); -void fsck_set_msg_type(struct fsck_objects_options *options, +void fsck_set_msg_type(struct fsck_options *options, const char *msg_id, const char *msg_type); -void fsck_set_msg_types(struct fsck_objects_options *options, const char *values); +void fsck_set_msg_types(struct fsck_options *options, const char *values); int is_valid_msg_type(const char *msg_id, const char *msg_type); /* @@ -131,10 +132,15 @@ int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *o, enum fsck_msg_id msg_id, const char *message); +struct fsck_options { + fsck_error error_func; + unsigned verbose:1, + strict:1; +}; + struct fsck_objects_options { + struct fsck_options fsck_options; fsck_walk_func walk; - fsck_error error_func; - unsigned strict:1; struct oidset gitmodules_found; struct oidset gitmodules_done; struct oidset gitattributes_found; @@ -143,27 +149,33 @@ struct fsck_objects_options { }; #define FSCK_OBJECTS_OPTIONS_DEFAULT { \ + .fsck_options = { \ + .error_func = fsck_error_function, \ + }, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ .gitattributes_done = OIDSET_INIT, \ - .error_func = fsck_error_function \ } #define FSCK_OBJECTS_OPTIONS_STRICT { \ - .strict = 1, \ + .fsck_options = { \ + .error_func = fsck_error_function, \ + .strict = 1, \ + }, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ .gitattributes_done = OIDSET_INIT, \ - .error_func = fsck_error_function, \ } #define FSCK_OBJECTS_OPTIONS_MISSING_GITMODULES { \ - .strict = 1, \ + .fsck_options = { \ + .error_func = fsck_error_cb_print_missing_gitmodules, \ + .strict = 1, \ + }, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ .gitattributes_done = OIDSET_INIT, \ - .error_func = fsck_error_cb_print_missing_gitmodules, \ } /* descend in all linked child objects diff --git a/object-file.c b/object-file.c index ec44ac3d82..9eda05ee01 100644 --- a/object-file.c +++ b/object-file.c @@ -2509,8 +2509,8 @@ static int index_mem(struct index_state *istate, if (flags & HASH_FORMAT_CHECK) { struct fsck_objects_options opts = FSCK_OBJECTS_OPTIONS_DEFAULT; - opts.strict = 1; - opts.error_func = hash_format_check_report; + opts.fsck_options.strict = 1; + opts.fsck_options.error_func = hash_format_check_report; if (fsck_buffer(null_oid(), type, buf, size, &opts)) die(_("refusing to create malformed object")); fsck_finish(&opts); From patchwork Thu Jun 27 15:13:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714628 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4FDEC15EFA4 for ; Thu, 27 Jun 2024 15:14:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501243; cv=none; b=Er1nLEONRrGprN1vlZIcc4ylCgrEHxKi7vHKIb9SuPxOuiO+GoUQ36YdU4YXugDs36Ex1TQ5TFWmR8eyk3QZmtjrBKje/CBJWJfb4cfFsIk+tCVxAJITHRDIIVg+BL32hj0F9eSeaHWz8Kk1nX0NFruscQ+qQFlIrJ3PPIAHq94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501243; c=relaxed/simple; bh=79S8d/XIojCM3+KUc5uwvC1NwVpR+a+PQmBgMZpxLQY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jQvZdVqhJPCrxBLbiSArpeWrZOKByGMkw3yy/iMdgKFU6kq5cXqqPlkzjb5XuAS7U+bEo3rXEFgs9sMMf/0w67QktQMAVtUCgo//iMaj9F/IibDDzWRdKJBmaOhz26GOcr1RtZHw6k4WME0rTibvuY7bW2+PFHNUWGYl116vBNw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VFjKPv9o; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VFjKPv9o" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1f9d9b57b90so54523575ad.0 for ; Thu, 27 Jun 2024 08:14:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501241; x=1720106041; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=If9jABsSdMpep5nQbosl5yEDT0JsxmJqEjUT7xQd328=; b=VFjKPv9oCR/Co0s38s6wQRnuQWw27ofxBUL7vSc0BBPOGTSdsAvCCkJkB7kasTGvnG +gOH4ly88fZ7cCRXwyx2X3tqkWRSLt7sbLzjvpuZ9QenFqyijHayiVeXLLdDlug0Ckfk G9cTw8oTnBtPUM146NUB6FdN7ilYq1NecS86jhECQLqAth7kHYk9pZFoQW8xBDH4f6eT nhkuje/K9CJBQA5C8l0Dwn0yM8zm1ns+njGyRnwu+26+ARWYNbNWePvMjldWU4vN16wR ZWv+/i2cYpV2i0UyPHjWDCPFU56R2PX0wS6CFEuIORsTpRkWBbdAMQLcFPlow9cyPbC7 TRxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501241; x=1720106041; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=If9jABsSdMpep5nQbosl5yEDT0JsxmJqEjUT7xQd328=; b=BfiEYjd+ZNlBMJCkuJz4D5OQg5An+N9oBI2oP0BEOJLfuHF5x3Da1oaFMumlx8sRs+ YGq0H3d78Eo442O1tkpDG2j3C4PbBgs7nzcyzR7BmBZHNqwf1Y+1o0cI83OFCDUHc/Zq S0uwE8eEi/i94SajAQ6j7/kZ/tpkcIRgYST3hzydxOoazewIM58hBU3X/Bo+WgQ0pDmF qTP7+InAyAmzlKHcwlLKHvlyHV/iXOpejXj7dyoK2yZX22sRuvHiVJQeSd+yPplqtgw5 ypuWi6S8UbcUqU6wV+Qe/KmP2tXCmUAp49aLl5mcyeBkr2wgCF6Ou1ek27SxmCPgdzXY dJ2w== X-Gm-Message-State: AOJu0YzQa+okwE5/9De9K6BpZs54bxssVWUmx2YFYmBbt7DXBPo5eqvw wwd6uoo2dSC4y8X11r/GvSNHIeRRhhe+Smd1AdVgNkhaHyDipXcmXhileQ== X-Google-Smtp-Source: AGHT+IHcubCvTpzNvSXYKQI3jURp6S0i460ph7zV5rYl1kqEnnXqE3CdySpQdzRBm3T0yHHFuooC5A== X-Received: by 2002:a17:902:f650:b0:1fa:2dcc:7d94 with SMTP id d9443c01a7336-1fa2dcc7e46mr171194665ad.33.1719501240793; Thu, 27 Jun 2024 08:14:00 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1faac9c570bsm14414325ad.292.2024.06.27.08.13.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:14:00 -0700 (PDT) Date: Thu, 27 Jun 2024 23:13:58 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 04/12] fsck: add "fsck_refs_options" struct Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Add "fsck_refs_options" to support ref consistency checks. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- fsck.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fsck.h b/fsck.h index e531b44a66..a17fee30b4 100644 --- a/fsck.h +++ b/fsck.h @@ -138,6 +138,10 @@ struct fsck_options { strict:1; }; +struct fsck_refs_options { + struct fsck_options fsck_options; +}; + struct fsck_objects_options { struct fsck_options fsck_options; fsck_walk_func walk; From patchwork Thu Jun 27 15:14:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714629 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58CFF15EFA4 for ; Thu, 27 Jun 2024 15:14:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501268; cv=none; b=nyRlD/uOqoC1XJXGmDsdRqa+nlxmDVmd/SXrE2cDbkkswDY7RBHGkwIXjw6SFKMl6S/spNgr1YTnUVK+s9D8YI+YBVhB1i45Uzijc7WuSNOQ5UgKe7Agk8TcAgS+6fV1JDgP2SKGQkAcEEhcWhYovkeQWzpmr7fEwAB2fWzrGxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501268; c=relaxed/simple; bh=NtDlJ5yB9MRzU7FDHUytlNDi3DKpPtkIMakZ2KVgngU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=LwhnNqlhxCFpaKTBGZPipU1X8FllZl7oBXfsH0PdsLizeLAvFMEsjNWtp0bptvFl2sUwUHKme9XgYNRgte8K9cKNpDzEvsGE1YXRUpmdSGtK7c7m7AEY8Ktf1uDF6C6dSPOSXrR7y0GMm+yek0cJHnAqG2TDPM4aulAKWq9FG8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Q3v1vK3h; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Q3v1vK3h" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-70670188420so4290009b3a.2 for ; Thu, 27 Jun 2024 08:14:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501265; x=1720106065; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=GnpUu6Vr4AQMBbj2weRVsZlDo0DFAbZ8DNczzdgCKYU=; b=Q3v1vK3hAcwbVc5KmhxUCLlULsCzyHn3AoDe5BabHnOEkaYXZJiSkI0FHj37Esus2F uabKfaXIrWllYC09m3uBmT89pLKKgbCen8aMG4+WnDaYAKx8NrcqUzeEYLFaYu2VUDT3 XV+QPcntrEKWKr7DpT+JQHI8jcwUBbxemVbzRQtfNtAUaqUQxXkSFfWrRSrbhz2DlRU0 87j2hN7oP47Qb64rjBIhwrT8YEW9Am+ydTD/Xihpp9Ch6F5hkHWo4+cNh4L3/PCUgm2q cNBKYPONAh2XVaz9f1QF+EhrZBR4LnzMur+SwoARTl7OT6oPpn83mLQ1S2JzPfr6xTqQ okQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501265; x=1720106065; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GnpUu6Vr4AQMBbj2weRVsZlDo0DFAbZ8DNczzdgCKYU=; b=xGyHz8+gTF4UVpT+TjxIMjvSade+BMsKwJwCY8P9y+b+VzrujSEOkSbfwQfvwGCZKq ZZa3/56ZKalvoxu525PgM1XwyWguCsFHFGCVldHBCD8KICCyknaPRvfeyKLwmpo2slOe Vv82FgQ2Z+mz0W4+lycAU+xh4A5RN4fgQ5ItZfvmhtF2Bef7nKRTg2aQiZELQrDlyJtl qbZQNM90RSeCfCoMGrKCwqZ7GPrM8BiINqyHJh5xzgAullo+zUu2dPyKzeu4lgLOU2EJ Ro9Tdzqo61PWh7XhKTwSv01fGYRjxNdaq9EYksTeORdjIbRWkSMz8hJbJS7Y4kn2n+Yl M+OA== X-Gm-Message-State: AOJu0Yw9TFBcL55xamupTciAgxwBaPVTYcpSjBRJCCa63Mw9DLgTZ8Dn HqyEN9GbmUdJgogB4bmDKWApJBTx+fw44W5AYFLvAdSc1rkMOd20876Cjw== X-Google-Smtp-Source: AGHT+IGh6rw/GdLkmUPF6HxMJ5TvWsZDTwZOMBJmwEzhwqM9xJqyC7RaMMLadqwy2/OXM7AK5LUPAw== X-Received: by 2002:a05:6a00:2f17:b0:704:2d7f:b61c with SMTP id d2e1a72fcca58-7066e4e69c5mr13996273b3a.7.1719501264863; Thu, 27 Jun 2024 08:14:24 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706b4a58ebfsm1466847b3a.196.2024.06.27.08.14.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:14:24 -0700 (PDT) Date: Thu, 27 Jun 2024 23:14:21 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 05/12] fsck: add a unified interface for reporting fsck messages Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The static function "report" provided by "fsck.c" aims at reporting the problems related to object database which cannot be reused for refs. In order to provide a unified interface which can report either objects or refs, create a new function "fsck_report" by adding two parameters "refs_options" and "checked_ref_name" following the "report" prototype. However, instead of using "...", provide "va_list" to allow more flexibility. The "vfsck_report" function will use "error_func" registered in "fsck_options" function to report customized messages. Change "error_func" prototype to align with the "vfsck_report". Change "report" function to make it use "vfsck_report" to report objects-related messages. Add a new function called "fsck_refs_report" to use "vfsck_report" to report refs-related messages. Also, create a general function "fsck_report" for future use. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- builtin/fsck.c | 4 ++- builtin/mktag.c | 4 ++- fsck.c | 88 ++++++++++++++++++++++++++++++++++++++++--------- fsck.h | 39 +++++++++++++++++++--- object-file.c | 14 ++++---- 5 files changed, 122 insertions(+), 27 deletions(-) diff --git a/builtin/fsck.c b/builtin/fsck.c index c383125027..2a3b536c1b 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -89,9 +89,11 @@ static int objerror(struct object *obj, const char *err) return -1; } -static int fsck_error_func(struct fsck_objects_options *o UNUSED, +static int fsck_error_func(struct fsck_objects_options *objects_options UNUSED, + struct fsck_refs_options *refs_options UNUSED, const struct object_id *oid, enum object_type object_type, + const char *checked_ref_name UNUSED, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id UNUSED, const char *message) diff --git a/builtin/mktag.c b/builtin/mktag.c index 76860f4c7c..0779a778e9 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -17,9 +17,11 @@ static int option_strict = 1; static struct fsck_objects_options fsck_objects_options = FSCK_OBJECTS_OPTIONS_STRICT; -static int mktag_fsck_error_func(struct fsck_objects_options *o UNUSED, +static int mktag_fsck_error_func(struct fsck_objects_options *objects_options UNUSED, + struct fsck_refs_options *refs_options UNUSED, const struct object_id *oid UNUSED, enum object_type object_type UNUSED, + const char *checked_ref_name UNUSED, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id UNUSED, const char *message) diff --git a/fsck.c b/fsck.c index 7ac6e4587c..5184d17736 100644 --- a/fsck.c +++ b/fsck.c @@ -230,19 +230,23 @@ static int object_on_skiplist(const struct object_id *oid) return oid && oidset_contains(&fsck_configs.oid_skiplist, oid); } -__attribute__((format (printf, 5, 6))) -static int report(struct fsck_objects_options *options, - const struct object_id *oid, enum object_type object_type, - enum fsck_msg_id msg_id, const char *fmt, ...) +static int vfsck_report(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, + const struct object_id *oid, + enum object_type object_type, + const char *checked_ref_name, + enum fsck_msg_id msg_id, const char *fmt, va_list ap) { - va_list ap; + va_list ap_copy; struct strbuf sb = STRBUF_INIT; struct fsck_options *fsck_options; enum fsck_msg_type msg_type; int result; - if (options) - fsck_options = &options->fsck_options; + if (objects_options) + fsck_options = &objects_options->fsck_options; + else if (refs_options) + fsck_options = &refs_options->fsck_options; else BUG("fsck_options is not set"); @@ -261,9 +265,10 @@ static int report(struct fsck_objects_options *options, prepare_msg_ids(); strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased); - va_start(ap, fmt); - strbuf_vaddf(&sb, fmt, ap); - result = fsck_options->error_func(options, oid, object_type, + va_copy(ap_copy, ap); + strbuf_vaddf(&sb, fmt, ap_copy); + result = fsck_options->error_func(objects_options, NULL, + oid, object_type, checked_ref_name, msg_type, msg_id, sb.buf); strbuf_release(&sb); va_end(ap); @@ -271,6 +276,51 @@ static int report(struct fsck_objects_options *options, return result; } +__attribute__((format (printf, 5, 6))) +static int report(struct fsck_objects_options *objects_options, + const struct object_id *oid, enum object_type object_type, + enum fsck_msg_id msg_id, const char *fmt, ...) +{ + va_list ap; + int result; + va_start(ap, fmt); + result = vfsck_report(objects_options, NULL, oid, object_type, "", + msg_id, fmt, ap); + va_end(ap); + return result; +} + +int fsck_report(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, + const struct object_id *oid, + enum object_type object_type, + const char *checked_ref_name, + enum fsck_msg_id msg_id, const char *fmt, ...) +{ + va_list ap; + int result; + va_start(ap, fmt); + result = vfsck_report(objects_options, refs_options, oid, object_type, + checked_ref_name, msg_id, fmt, ap); + va_end(ap); + return result; + +} + +int fsck_refs_report(struct fsck_refs_options *refs_options, + const struct object_id *oid, + const char *checked_ref_name, + enum fsck_msg_id msg_id, const char *fmt, ...) +{ + va_list ap; + int result; + va_start(ap, fmt); + result = fsck_report(NULL, refs_options, oid, OBJ_NONE, + checked_ref_name, msg_id, fmt, ap); + va_end(ap); + return result; +} + void fsck_enable_object_names(struct fsck_objects_options *options) { if (!options->object_names) @@ -1216,18 +1266,22 @@ int fsck_buffer(const struct object_id *oid, enum object_type type, type); } -int fsck_error_function(struct fsck_objects_options *o, +int fsck_error_function(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options UNUSED, const struct object_id *oid, enum object_type object_type UNUSED, + const char *checked_ref_name UNUSED, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id UNUSED, const char *message) { if (msg_type == FSCK_WARN) { - warning("object %s: %s", fsck_describe_object(o, oid), message); + warning("object %s: %s", + fsck_describe_object(objects_options, oid), message); return 0; } - error("object %s: %s", fsck_describe_object(o, oid), message); + error("object %s: %s", + fsck_describe_object(objects_options, oid), message); return 1; } @@ -1320,9 +1374,11 @@ int git_fsck_config(const char *var, const char *value, * Custom error callbacks that are used in more than one place. */ -int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *o, +int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, const struct object_id *oid, enum object_type object_type, + const char *checked_ref_name, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message) @@ -1331,5 +1387,7 @@ int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *o, puts(oid_to_hex(oid)); return 0; } - return fsck_error_function(o, oid, object_type, msg_type, msg_id, message); + return fsck_error_function(objects_options, refs_options, + oid, object_type, checked_ref_name, + msg_type, msg_id, message); } diff --git a/fsck.h b/fsck.h index a17fee30b4..70d5e78ae6 100644 --- a/fsck.h +++ b/fsck.h @@ -93,6 +93,7 @@ enum fsck_msg_id { #undef MSG_ID struct fsck_options; +struct fsck_refs_options; struct fsck_objects_options; struct object; @@ -115,19 +116,27 @@ int is_valid_msg_type(const char *msg_id, const char *msg_type); typedef int (*fsck_walk_func)(struct object *obj, enum object_type object_type, void *data, struct fsck_objects_options *options); -/* callback for fsck_object, type is FSCK_ERROR or FSCK_WARN */ -typedef int (*fsck_error)(struct fsck_objects_options *o, +/* + * callback function for reporting errors when checking either objects or refs + */ +typedef int (*fsck_error)(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, const struct object_id *oid, enum object_type object_type, + const char *checked_ref_name, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); -int fsck_error_function(struct fsck_objects_options *o, +int fsck_error_function(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, const struct object_id *oid, enum object_type object_type, + const char *checked_ref_name, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); -int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *o, +int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, const struct object_id *oid, enum object_type object_type, + const char *checked_ref_name, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); @@ -223,6 +232,28 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer, */ int fsck_finish(struct fsck_objects_options *options); +/* + * Provide a unified interface for either fscking refs or objects. + * It will get the current msg error type and call the error_func callback + * which is registered in the "fsck_options" struct. For refs, the caller + * should pass NULL for "objs_options". For objects, the caller should pass + * NULL for "refs_options". + */ +__attribute__((format (printf, 7, 8))) +int fsck_report(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, + const struct object_id *oid, + enum object_type object_type, + const char *checked_ref_name, + enum fsck_msg_id msg_id, const char *fmt, ...); + +__attribute__((format (printf, 5, 6))) +int fsck_refs_report(struct fsck_refs_options *refs_options, + const struct object_id *oid, + const char *checked_ref_name, + enum fsck_msg_id msg_id, + const char *fmt, ...); + /* * Subsystem for storing human-readable names for each object. * diff --git a/object-file.c b/object-file.c index 9eda05ee01..5cb9117fc4 100644 --- a/object-file.c +++ b/object-file.c @@ -2472,12 +2472,14 @@ int repo_has_object_file(struct repository *r, * report the minimal fsck error here, and rely on the caller to * give more context. */ -static int hash_format_check_report(struct fsck_objects_options *opts UNUSED, - const struct object_id *oid UNUSED, - enum object_type object_type UNUSED, - enum fsck_msg_type msg_type UNUSED, - enum fsck_msg_id msg_id UNUSED, - const char *message) +static int hash_format_check_report(struct fsck_objects_options *objects_options UNUSED, + struct fsck_refs_options *refs_options UNUSED, + const struct object_id *oid UNUSED, + enum object_type object_type UNUSED, + const char *chekced_ref_name UNUSED, + enum fsck_msg_type msg_type UNUSED, + enum fsck_msg_id msg_id UNUSED, + const char *message) { error(_("object fails fsck: %s"), message); return 1; From patchwork Thu Jun 27 15:14:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714630 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 599FF198A21 for ; Thu, 27 Jun 2024 15:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501299; cv=none; b=aQeky73OjPnBPwGa37FnoWn+I2WZ8XL8suXBYAR6GJm5BihBcpygiENUbCsiRytFFZaLeGANmpmcFH3awBb+YY90ueSln1pfNEWfZ/UPlCJw/Z3yiQb9mNnGACSkrrRwx5jSxDM2Y1OC5JosEwgezDlvxABwieDtEbb5TAz1a5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501299; c=relaxed/simple; bh=gAYs8CyXA1H3bGEqVclb+J0L1NDMKEeYcPjei11IgsE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=hZCvyjom8+Hib63ROl45eWKl5sVMXdNHAjutgsrke5PH9D2aejSyHHEFELiSU9VFTmawbKEai4xrgDExspsl1fzVUMYIcLtF1F47NTamCb70c07fxTtYhnMB6zVd8HDWoTYICHyUGDV8qIv7G2KX2QnRoS71WncF86EHgj6OjT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MDTbyO2E; arc=none smtp.client-ip=209.85.215.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MDTbyO2E" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-710437d0affso5996562a12.3 for ; Thu, 27 Jun 2024 08:14:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501297; x=1720106097; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=lPBTS4gkLF2FTufbmj7RqHbt4e8xUZlgv4Z+cV2JT9E=; b=MDTbyO2EbTUBZNqMU87HJEgmqLPR9efeFnVxUAmQXu32IE8GUUYSqLrHyWwLEAYtci kVRVBJk4Qpaud1qAPc6PI46nuA3sSpee9QN9dQLY3G61qjJumuOIA9JI4aJ0xjOUsj3D /54yXc+jCoaneF38OGuXrMrAeC43jQHStz9GyF9NMgqW3brmxulY+wBl6LkDua3r99Ev yQhrtTg4fP5JQRC0fuY2ZwFfIcX5dUUcT9VwdENTBI3QvcNPQHKa6IOglbX4Rj7AkLa7 byzP81tfewkfaGAOOIz/u1Joz8mapjXDWXmWR6j6UI4mNbBg2JMb2p1JDZrvUHOx4/pK dWNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501297; x=1720106097; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=lPBTS4gkLF2FTufbmj7RqHbt4e8xUZlgv4Z+cV2JT9E=; b=onBKnLFJ35W6HUqP0qOefWd3ouL0MulFvz+cc7jQ57KBb1nX/7uFSeIfUk5IKLWZf/ AAZwsUOQ8zkqdYvKdPoh3hGsGnfGcOzdxhLVGt1X2OFqSJarH9adv9IgnStxNTsjENul Aq6OVP2yUzA2QWv95dmJ03QISk1kkIWH4h3NpRmJUe92/M2/v3R/r+HiOg3i4k4FJc+F aqhD5Lf5O8lnUzRtQl3F4eZkaAn0GQlvIx5GYOUc50APAJjMMBPWhSuayKUfD2P+x+Zu E68pnyxYH6Ps6bLH1Kzk7pwFlt+m6pwPPDmC7e1N+2fudBKH/TGNk8UvbljsvRxNI9bg f2fQ== X-Gm-Message-State: AOJu0YyyUpok2v0r6j+7W63D/OH4r9m/DdDOUfDsclm2eLGNxlUaW5yT UjnxKeO7bKLtjM2x/R1MY7GXiKlnrq+HedLYbu6QyEBKiSgMDUPAXxadzQ== X-Google-Smtp-Source: AGHT+IG0Fy26pP3SLzEBN7Y4Yu5/ujsC7LwABn1znQR8B48ejLU1IWJ9O5N8LVDFDCxBD9cb7anwfA== X-Received: by 2002:a17:90a:c24a:b0:2c8:6a66:32bf with SMTP id 98e67ed59e1d1-2c86a663328mr10645247a91.39.1719501297055; Thu, 27 Jun 2024 08:14:57 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c8fe8ce765sm1598238a91.21.2024.06.27.08.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:14:56 -0700 (PDT) Date: Thu, 27 Jun 2024 23:14:54 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 06/12] fsck: add "fsck_refs_options" initialization macros Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Add "FSCK_REFS_OPTIONS_DEFAULT" and "FSCK_REFS_OPTIONS_STRICT" macros to create the "fsck_refs_options" easily. Add refs-specific "error_func" callback "fsck_refs_error_function". "fsck_refs_error_function" will use the "oid" parameter. When the caller passed the oid, it will use "oid_to_hex" to get the corresponding hex value to report to the user. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- fsck.c | 23 +++++++++++++++++++++++ fsck.h | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/fsck.c b/fsck.c index 5184d17736..4869566d19 100644 --- a/fsck.c +++ b/fsck.c @@ -1285,6 +1285,29 @@ int fsck_error_function(struct fsck_objects_options *objects_options, return 1; } +int fsck_refs_error_function(struct fsck_objects_options *objects_options UNUSED, + struct fsck_refs_options *refs_options, + const struct object_id *oid, + enum object_type object_type UNUSED, + const char *checked_ref_name, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id UNUSED, + const char *message) +{ + static struct strbuf sb = STRBUF_INIT; + + strbuf_addstr(&sb, checked_ref_name); + if (oid) + strbuf_addf(&sb, " -> (%s)", oid_to_hex(oid)); + + if (msg_type == FSCK_WARN) { + warning("%s: %s", sb.buf, message); + return 0; + } + error("%s: %s", sb.buf, message); + return 1; +} + static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done, enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type, struct fsck_objects_options *options, diff --git a/fsck.h b/fsck.h index 70d5e78ae6..e903845690 100644 --- a/fsck.h +++ b/fsck.h @@ -140,6 +140,14 @@ int fsck_error_cb_print_missing_gitmodules(struct fsck_objects_options *objects_ enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); +int fsck_refs_error_function(struct fsck_objects_options *objects_options, + struct fsck_refs_options *refs_options, + const struct object_id *oid, + enum object_type object_type, + const char *checked_ref_name, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id, + const char *message); struct fsck_options { fsck_error error_func; @@ -150,6 +158,17 @@ struct fsck_options { struct fsck_refs_options { struct fsck_options fsck_options; }; +#define FSCK_REFS_OPTIONS_DEFAULT { \ + .fsck_options = { \ + .error_func = fsck_refs_error_function, \ + }, \ +} +#define FSCK_REFS_OPTIONS_STRICT { \ + .fsck_options = { \ + .error_func = fsck_refs_error_function, \ + .strict = 1, \ + }, \ +} struct fsck_objects_options { struct fsck_options fsck_options; From patchwork Thu Jun 27 15:15:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714631 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D720A197A61 for ; Thu, 27 Jun 2024 15:15:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501320; cv=none; b=Ak/86yoqE5QeEPHJrIQmbNHWCLxwbxoYztKcN33Hob63/6upJy3dd7KAjPamZkztBSpQ44uxQiptUn7WvaNt5L8dq4/GjJ4GlGYoyEShWs2f82XVsoIGfHo+e9RW/Dd7pZDVVYwbXA2XQAr4cksN+YRA37zRaB56/xZKYl+RTTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501320; c=relaxed/simple; bh=MMLgikjjCWZJpa62DV+t8V+bsKzXMjmlEfnYr0BN4Ic=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BURWXYJ9wz0tDm9Ee+xijduM7SvC0LqIugQaf84MzPTf4JNRBrYpdU4tN5CxtInvgVwapadsqBB8tR8e6RbAnV1hChDjPqjm534kYyOk/fvcGwwIhONM+CCXYPXirt/RsQFxj//YCkyNq3+xhm54PzsauE0xaWOYFWfzW2N5XDE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=h0byfTTv; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="h0byfTTv" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-718354c17e4so4399066a12.1 for ; Thu, 27 Jun 2024 08:15:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501317; x=1720106117; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=DTdwjzRv4a3DZpn0+6TZLo6M300hXmSeHagjQKRIRWU=; b=h0byfTTvXzwUvTnFRLQdrbmN1s52t7302hUx9XnDmz/CAnAwpLtcO8cZRWeeiMrhBZ hZdQCjTC789UtvZlC/dICur9Izyb2EUr+owhQyqWWhivjejlaqnPPn3rBxl2BVvEgiq2 2o5KS+5ZB5H0TMExVGCB5H9ynXCjPtj2ZUghRec6zjaxaOqFVtZUxyp13zagwE+zdR85 FegaC7kjb1rBEmEQvwMUXB6rDzlC4GkoS2j7E0giCX495Qt0YzcuRB/ch2uxqzPPrUdH eOlSAehK+1mOb6LX3zcw7ooJBmAu0FQH6ph+R+my1cjyaLHy8Ck8v2iMu+BY0aOvo23a uJhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501317; x=1720106117; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=DTdwjzRv4a3DZpn0+6TZLo6M300hXmSeHagjQKRIRWU=; b=CPZ2dI5kobNQTMU3DQlvcRsZod2ONe4II4gZ6EUsgfowmwxQ5yja6aMveDzbSKbHyB ECerDXR0yRSCHxPDcLtR4UnoK6LkHFTp20HJPMZqbxCnM0ak47rq0eunPmvoRHHZk03Q luHJnzmCPe9s12w4jhnkD6gmTyz+qswjRDObGUbqlIdCxTkqzsJjnzfpp3gFfgJQHuSM lRuHIop0R0USn6YnEhAGekErDI8SYQZI2iQtbrymSq3fWPala6KcBv/xuYRoNh3Il/Yy 3lrXyIGTj0fe3D3EiZn+hOiuHRYyn8EZVCV/17xSQQyVPeyEgiFxgRl3X4WNnZAqhh2L fX0w== X-Gm-Message-State: AOJu0YxlJ/Eg1R6hSxsp6/+t7BsDeg1XqWgHZ5pHzxMlepHPgC138vzM 4LweyiN8zk29aMoA6kb1pjWSl0UlOM8fT8C96wKlTLDeFmJvpaHEa3JI9Q== X-Google-Smtp-Source: AGHT+IF4gvlRUUSPyIAxDNsqh/0J+pHgCOPq22Bwswo8F1+qF3wTQkqzpS60/SOSZBTlZ8TSlu/SzA== X-Received: by 2002:a05:6a20:3016:b0:1be:c551:b74f with SMTP id adf61e73a8af0-1bec551ba09mr3846504637.27.1719501317468; Thu, 27 Jun 2024 08:15:17 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c8d7e54699sm3830485a91.4.2024.06.27.08.15.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:15:16 -0700 (PDT) Date: Thu, 27 Jun 2024 23:15:14 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 07/12] refs: set up ref consistency check infrastructure Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The interfaces defined in the `ref_storage_be` are carefully structured in semantic. It's organized as the five parts: 1. The name and the initialization interfaces. 2. The ref transaction interfaces. 3. The ref internal interfaces (pack, rename and copy). 4. The ref filesystem interfaces. 5. The reflog related interfaces. To keep consistent with the git-fsck(1), add a new interface named "fsck_refs_fn" to the end of "ref_storage_be". This semantic cannot be grouped into any above five categories. Explicitly add blank line to make it different from others. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- refs.c | 5 +++++ refs.h | 8 ++++++++ refs/debug.c | 11 +++++++++++ refs/files-backend.c | 15 ++++++++++++++- refs/packed-backend.c | 8 ++++++++ refs/refs-internal.h | 6 ++++++ refs/reftable-backend.c | 8 ++++++++ 7 files changed, 60 insertions(+), 1 deletion(-) diff --git a/refs.c b/refs.c index dd0d9c360f..646fdfaf22 100644 --- a/refs.c +++ b/refs.c @@ -316,6 +316,11 @@ int check_refname_format(const char *refname, int flags) return check_or_sanitize_refname(refname, flags, NULL); } +int refs_fsck(struct ref_store *refs, struct fsck_refs_options *o) +{ + return refs->be->fsck(refs, o); +} + void sanitize_refname_component(const char *refname, struct strbuf *out) { if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out)) diff --git a/refs.h b/refs.h index df8cbf6124..a8f4deb108 100644 --- a/refs.h +++ b/refs.h @@ -3,6 +3,7 @@ #include "commit.h" +struct fsck_refs_options; struct object_id; struct ref_store; struct repository; @@ -547,6 +548,13 @@ int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_dat */ int check_refname_format(const char *refname, int flags); +/* + * Check the reference database for consistency. Return 0 if refs and + * reflogs are consistent, and non-zero otherwise. The errors will be + * written to stderr. + */ +int refs_fsck(struct ref_store *refs, struct fsck_refs_options *o); + /* * Apply the rules from check_refname_format, but mutate the result until it * is acceptable, and place the result in "out". diff --git a/refs/debug.c b/refs/debug.c index 547d9245b9..6306c25fd3 100644 --- a/refs/debug.c +++ b/refs/debug.c @@ -419,6 +419,15 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname, return res; } +static int debug_fsck(struct ref_store *ref_store, + struct fsck_refs_options *o) +{ + struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; + int res = drefs->refs->be->fsck(drefs->refs, o); + trace_printf_key(&trace_refs, "fsck: %d\n", res); + return res; +} + struct ref_storage_be refs_be_debug = { .name = "debug", .init = NULL, @@ -451,4 +460,6 @@ struct ref_storage_be refs_be_debug = { .create_reflog = debug_create_reflog, .delete_reflog = debug_delete_reflog, .reflog_expire = debug_reflog_expire, + + .fsck = debug_fsck, }; diff --git a/refs/files-backend.c b/refs/files-backend.c index 44c5c3b201..7257fffb77 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3406,6 +3406,17 @@ static int files_ref_store_remove_on_disk(struct ref_store *ref_store, return ret; } +static int files_fsck(struct ref_store *ref_store, + struct fsck_refs_options *o) +{ + int ret; + struct files_ref_store *refs = + files_downcast(ref_store, REF_STORE_READ, "fsck"); + + ret = refs->packed_ref_store->be->fsck(refs->packed_ref_store, o); + return ret; +} + struct ref_storage_be refs_be_files = { .name = "files", .init = files_ref_store_init, @@ -3432,5 +3443,7 @@ struct ref_storage_be refs_be_files = { .reflog_exists = files_reflog_exists, .create_reflog = files_create_reflog, .delete_reflog = files_delete_reflog, - .reflog_expire = files_reflog_expire + .reflog_expire = files_reflog_expire, + + .fsck = files_fsck, }; diff --git a/refs/packed-backend.c b/refs/packed-backend.c index c4c1e36aa2..db152053f8 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -1733,6 +1733,12 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s return empty_ref_iterator_begin(); } +static int packed_fsck(struct ref_store *ref_store, + struct fsck_refs_options *o) +{ + return 0; +} + struct ref_storage_be refs_be_packed = { .name = "packed", .init = packed_ref_store_init, @@ -1760,4 +1766,6 @@ struct ref_storage_be refs_be_packed = { .create_reflog = NULL, .delete_reflog = NULL, .reflog_expire = NULL, + + .fsck = packed_fsck, }; diff --git a/refs/refs-internal.h b/refs/refs-internal.h index fa975d69aa..ecc082baf8 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -4,6 +4,7 @@ #include "refs.h" #include "iterator.h" +struct fsck_options; struct ref_transaction; /* @@ -650,6 +651,9 @@ typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname, typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname, struct strbuf *referent); +typedef int fsck_fn(struct ref_store *ref_store, + struct fsck_refs_options *o); + struct ref_storage_be { const char *name; ref_store_init_fn *init; @@ -677,6 +681,8 @@ struct ref_storage_be { create_reflog_fn *create_reflog; delete_reflog_fn *delete_reflog; reflog_expire_fn *reflog_expire; + + fsck_fn *fsck; }; extern struct ref_storage_be refs_be_files; diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 7d872a32ac..2a358b40ca 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -2292,6 +2292,12 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store, return ret; } +static int reftable_be_fsck(struct ref_store *ref_store, + struct fsck_refs_options *o) +{ + return 0; +} + struct ref_storage_be refs_be_reftable = { .name = "reftable", .init = reftable_be_init, @@ -2319,4 +2325,6 @@ struct ref_storage_be refs_be_reftable = { .create_reflog = reftable_be_create_reflog, .delete_reflog = reftable_be_delete_reflog, .reflog_expire = reftable_be_reflog_expire, + + .fsck = reftable_be_fsck, }; From patchwork Thu Jun 27 15:15:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714632 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 802C1198A17 for ; Thu, 27 Jun 2024 15:15:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501345; cv=none; b=PyDlCw2gE2OLPQRLihf6SzbWHcGmfhE8LqN2nLUAtpWsxw46aQN2J/40HNBCMksxbdyL1eJi0s0/iX1KZpIxISGdNgOmLPZeZZJ+LMYuOYmJMtMW39hFzfuTbbrDGU8sY4Qhxu3ZdpmS59YTuWL9uHYfFfL/MMna9aWzLmHDOpw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501345; c=relaxed/simple; bh=voQLTpFo2dHENNDdW2w0eqZZTUbIcksiE+cLN6MF6cU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rFvslyDSpBHuZJ8bUCo3Z5AUVg0zVa4tSkRj+lmHMHyK8DUgLA7GJuAdTgDpRA0/o5ExEN/Dbgp7nzDO5siJxe/l237+M59XECjysBg8TMpz/g2q2SMDV6xr8k81vPJ8LrTHosTj3rAlBsR0nU5LioOZp5LPXStdBVn4YHpakSc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hoxxJmoW; arc=none smtp.client-ip=209.85.210.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hoxxJmoW" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7066c9741b7so3935542b3a.1 for ; Thu, 27 Jun 2024 08:15:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501343; x=1720106143; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=DLPTV5SATk82eS+2oWbSqq87eYYCga3DwhqGe9F+Vxo=; b=hoxxJmoWuIc5WzUAW/39bKscpH18EqcB1iV3fNa/9+303V0MVQLgxCyjyk0iU7kF9/ VCwL3suK/Whb1VTQCEV63NOMo/DyqQfSPsNG426UTsPaqM8wzZange2YCa6y8CeB1R1/ //zk6XnIwE880EpSyUlmyvLsqJTjm4+VC3P0Kn763G8x9/Dx3+eEIq/QU7qB2w59O4Ih tMx8CrSNATD+DR1e+a2/CEI0pZt6bYzvZ2r0IWId12okzMOuowLsVMuZUlwIEUmP4EoZ 3pu7wob4iSJgtCGYBMl/zUtnbArWrmqScB7pwg342thlq5fcdVqYQPRBHOetnd6GDbLf 4zAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501343; x=1720106143; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=DLPTV5SATk82eS+2oWbSqq87eYYCga3DwhqGe9F+Vxo=; b=FIKkNA3k20rInxUYlERm1330IA/v10ij+h6Q6Qe0hTPdiEBlvKQwhjEdj78mUDawXh u5KkAfsGK7QQ1Mw5HrWIPfKooh57jc1nowY22U1K+GSzFww8EUBMtqiym9gY4Firw5e8 8MqUyDPKcvDGiBxXQ+WSwtXYspwuAZrpYv7z6AHXMVd7sID6qW+ri9cUtLkkvavJNwWX QGCmw6dCs7mzS9UlkJe/KK4ZGeah6ralD+DB4W9m2EmnOrMOEvrZ63+nr4/2LpZw251l ztD0ov1FfHx02GBHP0qU+PW66PZDzyDqzhB/ucfFMZUwWKgjTIXVtk1e6njz2NhvCA1X i4VA== X-Gm-Message-State: AOJu0YwvuMV9WUETITmT2kC3OJfqWa75wehbzfQuPYFEsqBdBpyy1wp0 vF9rdJoDIW70FJH9DRVC0v4j8M8iVE7uGnbPtYXprGVBJ8T/dVKChGxMHQ== X-Google-Smtp-Source: AGHT+IG9noX02MslNUH6hBSXEasumLVVkR6WZfw3rAPnWQcrKTZg/VUjVxgLTVxRHJ7DIFfWn8MIHg== X-Received: by 2002:a05:6a21:339b:b0:1b6:dffa:d6f8 with SMTP id adf61e73a8af0-1bcf7e6b981mr15834840637.3.1719501343141; Thu, 27 Jun 2024 08:15:43 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706b4a5892esm1469308b3a.190.2024.06.27.08.15.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:15:42 -0700 (PDT) Date: Thu, 27 Jun 2024 23:15:40 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 08/12] builtin/refs: add verify subcommand Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Introduce a new subcommand "verify" in git-refs(1) to allow the user to check the reference database consistency. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- Documentation/git-refs.txt | 13 +++++++++++++ builtin/refs.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/Documentation/git-refs.txt b/Documentation/git-refs.txt index 5b99e04385..1244a85b64 100644 --- a/Documentation/git-refs.txt +++ b/Documentation/git-refs.txt @@ -10,6 +10,7 @@ SYNOPSIS -------- [verse] 'git refs migrate' --ref-format= [--dry-run] +'git refs verify' [--strict] [--verbose] DESCRIPTION ----------- @@ -22,6 +23,9 @@ COMMANDS migrate:: Migrate ref store between different formats. +verify:: + Verify reference database consistency. + OPTIONS ------- @@ -39,6 +43,15 @@ include::ref-storage-format.txt[] can be used to double check that the migration works as expected before performing the actual migration. +The following options are specific to 'git refs verify': + +--strict:: + Enable more strict checking, every WARN severity for the `Fsck Messages` + be seen as ERROR. See linkgit:git-fsck[1]. + +--verbose:: + When verifying the reference database consistency, be chatty. + KNOWN LIMITATIONS ----------------- diff --git a/builtin/refs.c b/builtin/refs.c index 46dcd150d4..511b0d0f8b 100644 --- a/builtin/refs.c +++ b/builtin/refs.c @@ -1,4 +1,6 @@ #include "builtin.h" +#include "config.h" +#include "fsck.h" #include "parse-options.h" #include "refs.h" #include "repository.h" @@ -7,6 +9,9 @@ #define REFS_MIGRATE_USAGE \ N_("git refs migrate --ref-format= [--dry-run]") +#define REFS_VERIFY_USAGE \ + N_("git refs verify [--strict] [--verbose]") + static int cmd_refs_migrate(int argc, const char **argv, const char *prefix) { const char * const migrate_usage[] = { @@ -58,15 +63,46 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix) return err; } +static int cmd_refs_verify(int argc, const char **argv, const char *prefix) +{ + struct fsck_refs_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT; + const char * const verify_usage[] = { + REFS_VERIFY_USAGE, + NULL, + }; + unsigned int verbose = 0, strict = 0; + struct option options[] = { + OPT__VERBOSE(&verbose, N_("be verbose")), + OPT_BOOL(0, "strict", &strict, N_("enable strict checking")), + OPT_END(), + }; + + argc = parse_options(argc, argv, prefix, options, verify_usage, 0); + if (argc) + usage(_("too many arguments")); + + if (verbose) + fsck_refs_options.fsck_options.verbose = 1; + if (strict) + fsck_refs_options.fsck_options.strict = 1; + + git_config(git_fsck_config, &fsck_refs_options.fsck_options); + prepare_repo_settings(the_repository); + + return refs_fsck(get_main_ref_store(the_repository), &fsck_refs_options); +} + int cmd_refs(int argc, const char **argv, const char *prefix) { const char * const refs_usage[] = { REFS_MIGRATE_USAGE, + REFS_VERIFY_USAGE, NULL, }; parse_opt_subcommand_fn *fn = NULL; struct option opts[] = { OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate), + OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify), OPT_END(), }; From patchwork Thu Jun 27 15:16:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714633 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F8E823BE for ; Thu, 27 Jun 2024 15:16:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501366; cv=none; b=joulbRHMO2zZuwpcEvtepRgVcqqewBhMvhow1ezsWcQp+7G8gV11CQgZe4FC9Fpzcihook67mdWvof8VinjSJa4ORRadLzhJRZh9ZCueFeMXHjZbVWySJcdETm3b7QGkZxQyuke5oc/X1nljawlr65BrpiQt6EiJN2N+Vt6P6XI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501366; c=relaxed/simple; bh=hh+J/5GCe/Id7LcjCcoKvp2sfb6YWfYz1lKq/a8Hos0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ahCqlGS5xz5ikchBLYNHC0uRonz3q9OwDl+V5Lo0rqZWZwfHX3mmOluWhzTUhRDHALgeNMQhdP//Huhw08UxA8EO3jq+l7Z/VcTC55rJ3OwegXycW8dZzQAgVx+lPQ2XbBgbbtRdXV/7LapBvBKborASgT6iiQogCxpvuc5a4vI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SgJVS29w; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SgJVS29w" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-706b539fcaeso536086b3a.0 for ; Thu, 27 Jun 2024 08:16:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501364; x=1720106164; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MJgm0No5QtyeJVi0CV5nn90CiCsprEpBKvdo1r2AKt0=; b=SgJVS29w5tEN8682h8jau6YADWK66qMeSuwzgORhVR7PmQrRdjlTBHCP9bZ2XHstfD lxqfX+zmChF75xA7UtRTFe36tnhv4GyrqND+TD1beqWnpnf8bbp4KPEVWNmJ6aSULA/Z UbGhQ06z1I4ZefdCFp5JgKgboYWOdh/O/Vnr52K+oh2O7FhDl0Pn+Iz3zAysocDHfn4K zS3h/gXXt/TEStSue+WfGSOPiTYCfrPR/kurkS6AEPI6KUT9w0Rx9e91hcFvWt+TG3AX q9hPmPWR/nrQNL2FzjuZ/ywfdnGS4N6fZ3EPoy5B5rSi+68Mj6GtYvVopUebFpg1gGJQ m9JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501364; x=1720106164; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=MJgm0No5QtyeJVi0CV5nn90CiCsprEpBKvdo1r2AKt0=; b=cNRWKGYP9vGHfmkeP94AFyA7lvggw+9vnIbV8S2bWmlkNPb1YBkKgQxBtxlRqYMieK orz9RnRxvWBFB6eLLtZeMiNUQfAaGWdR735t+CENexpIK+rDhUj4tFMFMgFUdz3joPIU ghy7VEuwb5EQV/SVeB9MqX9Bi7f6hUlvsFl/mtNKcNNksNfGCcctfECQOcpgW6YUOZE/ RrR+7M3UH8yZdp6FRL4SXASfSwS6VKX2Ukwhd4sL4Q7JQvseUwVuwdX0pPnTKpdcYE3B hSIHF6iPn+YqnxDyEbznRffQXUZzFKGLUKny7FWD335i0CFC0wLHaQIDEY9U0/Tq86yp 6UKQ== X-Gm-Message-State: AOJu0YxKJRyiZ+r3KlI50kiic7X39xOQCMp0UyEpeCHQKiTc7IuRZP7p lWrS5fz2lLYX2yTd9oITn7cnZmzMldwzCcjl4lgckFqjTk2fm6TCPw4HXA== X-Google-Smtp-Source: AGHT+IG06Rng5iBIXEQekYvynOvClHW4+KsFnkbHL0WCsVGDq0ZNg9gaigyV1ncGfvU57Zp9OJC5Qw== X-Received: by 2002:a05:6a21:998f:b0:1be:c166:6445 with SMTP id adf61e73a8af0-1bec1666fd8mr7027580637.4.1719501363650; Thu, 27 Jun 2024 08:16:03 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706b49103a9sm1480005b3a.66.2024.06.27.08.16.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:16:03 -0700 (PDT) Date: Thu, 27 Jun 2024 23:16:01 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 09/12] builtin/fsck: add `git-refs verify` child process Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Introduce a new function "fsck_refs" that initializes and runs a child process to execute the "git-refs verify" command. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- builtin/fsck.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/builtin/fsck.c b/builtin/fsck.c index 2a3b536c1b..a5654da62b 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -899,6 +899,21 @@ static int check_pack_rev_indexes(struct repository *r, int show_progress) return res; } +static void fsck_refs(void) +{ + struct child_process refs_verify = CHILD_PROCESS_INIT; + child_process_init(&refs_verify); + refs_verify.git_cmd = 1; + strvec_pushl(&refs_verify.args, "refs", "verify", NULL); + if (verbose) + strvec_push(&refs_verify.args, "--verbose"); + if (check_strict) + strvec_push(&refs_verify.args, "--strict"); + + if (run_command(&refs_verify)) + errors_found |= ERROR_REFS; +} + static char const * const fsck_usage[] = { N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n" " [--[no-]full] [--strict] [--verbose] [--lost-found]\n" @@ -1068,6 +1083,8 @@ int cmd_fsck(int argc, const char **argv, const char *prefix) check_connectivity(); + fsck_refs(); + if (the_repository->settings.core_commit_graph) { struct child_process commit_graph_verify = CHILD_PROCESS_INIT; From patchwork Thu Jun 27 15:16:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714642 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BD9823BE for ; Thu, 27 Jun 2024 15:16:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501415; cv=none; b=ILS1Y6RugIhSzw4Egnd1v18EDdEx6C861EI4a+rEZvd5fy73XfDJAz1GrLBvJxAozEpyyCwzqtKcIgw6/EQto//keedVnXqvVvrNbo+a6EZpZZFhBsn0ahPo5tWsEF0QXsZkAFvi7dCq2SSAW5jxYfmalR8zG0dEg+9VFjwUrvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501415; c=relaxed/simple; bh=mfI5Kwv03fKfrSBuBGIt1iX3SSfHhcv2tfByPa/ZUGI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uGzNFaGUV8KsR2D/MTJbnZGgs3YJgpVndOUOr+SOpLM+/aD8VOoNrRtNtc060kra04Wvr05HN4Y6Dyc0/otEGID1mlo1moQxj3wf3Z/Fkzr20itZIrNwUmDB6K7fbx8Ob1urcXQnsgu887zAsqbR36Cp3ek/uB8bQkQFZwyVIH4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DitM/Pjq; arc=none smtp.client-ip=209.85.215.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DitM/Pjq" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-6c4926bf9baso6571692a12.2 for ; Thu, 27 Jun 2024 08:16:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501413; x=1720106213; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=fwnJTzOkZxdXLhBypnuUx/TOt+tOGgh3//QJ3Nj/i5A=; b=DitM/PjqtPyY+xxGAd7hnc9tWGDtahaDFzjy9XVFntso4hhE6rRZebvWZF04rE4JZ7 16qUq4O3NoQ3Zs+Ei9ALPTF6dYfroKbiChSwNxwTzzALsamHJIjGbRhXLI0MVgLEti5Q N3hzGsG9ie0BjbeBGVFiCPb1IUhO1CTsnMJ3pOHvZFOPV9DwoLvOLMRmNymnAZKX/xRy HGFT3V3qAIshU2kwg5F3NL/Nfh4kJprK6juZ86SyhMbhJD+ZghmrQQJLUhj7K8QfwWyY tVt/PN59XqHXlTUYtojsur7apw67s5sH6N5m04QZ4k/e7bdG48grVUHjHm/1Cy73csxl 413A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501413; x=1720106213; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fwnJTzOkZxdXLhBypnuUx/TOt+tOGgh3//QJ3Nj/i5A=; b=hVgWa7SjTf5SE7vA4SiDCW0JHa60r/zj2gM9fa+jlaXmgj8MlxsXiI+5isb5nlTUK1 6cED+Wm/NVgn6uTmAd8b0iEhmGYax1VES2giPHbtcBr4qILmEHlp/82ZMk3hh2v7S4HP 0COq7cBb0mrnDD2XcTA5gSPsapcPzAIqn95m4O2WhePHNDIGLu9oOmCPM2JAsASRUdBI UEyXAy/csL6BRvyOsMoX5nx7nhaOe1Z6G9e22jJmRSDrtfBwvrigap22Y7sIc+64iAvH Z3mfKQtwofVV+ibk8ThqdHel59XFgH8Qj9yxA4Vx7/ZEm5MTQ5hY3hddcWtXe6Lqzx9y czRQ== X-Gm-Message-State: AOJu0YwdQiojzYcbrjZc0bRSSDEmGDKDIoEGV16Qlz5K1gOWIx8BMkdK Cv6rSmYdwBfZh+rBeP90iBF0OBVl9nWwmnYxfuszg3jVwQ61RvynJlgwkw== X-Google-Smtp-Source: AGHT+IEcKYimI/M74yiPlCyWX3uHGJIpVY6AN9KXO4s9wXlK7EBpSn2T2mhdJm/TH8Nd5y8h3tmzMQ== X-Received: by 2002:a05:6a20:5a81:b0:1bd:2894:a0ca with SMTP id adf61e73a8af0-1bd2894a15dmr6489976637.41.1719501413331; Thu, 27 Jun 2024 08:16:53 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706b48caecfsm1476841b3a.24.2024.06.27.08.16.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:16:52 -0700 (PDT) Date: Thu, 27 Jun 2024 23:16:50 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 10/12] files-backend: add unified interface for refs scanning Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: For refs and reflogs, we need to scan its corresponding directories to check every regular file or symbolic link which shares the same pattern. Introduce a unified interface for scanning directories for files-backend. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- refs/files-backend.c | 77 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 7257fffb77..aefc947fc3 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -4,6 +4,7 @@ #include "../gettext.h" #include "../hash.h" #include "../hex.h" +#include "../fsck.h" #include "../refs.h" #include "refs-internal.h" #include "ref-cache.h" @@ -3406,6 +3407,78 @@ static int files_ref_store_remove_on_disk(struct ref_store *ref_store, return ret; } +/* + * For refs and reflogs, they share a unified interface when scanning + * the whole directory. This function is used as the callback for each + * regular file or symlink in the directory. + */ +typedef int (*files_fsck_refs_fn)(struct fsck_refs_options *o, + const char *gitdir, + const char *refs_check_dir, + struct dir_iterator *iter); + +static int files_fsck_refs_dir(struct ref_store *ref_store, + struct fsck_refs_options *o, + const char *refs_check_dir, + files_fsck_refs_fn *fsck_refs_fns) +{ + const char *gitdir = ref_store->gitdir; + struct strbuf sb = STRBUF_INIT; + struct dir_iterator *iter; + int iter_status; + int ret = 0; + + strbuf_addf(&sb, "%s/%s", gitdir, refs_check_dir); + + iter = dir_iterator_begin(sb.buf, 0); + + if (!iter) { + ret = error_errno("cannot open directory %s", sb.buf); + goto out; + } + + while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) { + if (S_ISDIR(iter->st.st_mode)) { + continue; + } else if (S_ISREG(iter->st.st_mode) || + S_ISLNK(iter->st.st_mode)) { + if (o->fsck_options.verbose) + fprintf_ln(stderr, "Checking %s/%s", + refs_check_dir, iter->relative_path); + for (size_t i = 0; fsck_refs_fns[i]; i++) { + if (fsck_refs_fns[i](o, gitdir, refs_check_dir, iter)) + ret = -1; + } + } else { + ret = error(_("unexpected file type for '%s'"), + iter->basename); + } + } + + if (iter_status != ITER_DONE) + ret = error(_("failed to iterate over '%s'"), sb.buf); + +out: + strbuf_release(&sb); + return ret; +} + +static int files_fsck_refs(struct ref_store *ref_store, + struct fsck_refs_options *o) +{ + int ret; + files_fsck_refs_fn fsck_refs_fns[]= { + NULL + }; + + if (o->fsck_options.verbose) + fprintf_ln(stderr, "Checking references consistency"); + + ret = files_fsck_refs_dir(ref_store, o, "refs", fsck_refs_fns); + + return ret; +} + static int files_fsck(struct ref_store *ref_store, struct fsck_refs_options *o) { @@ -3413,7 +3486,9 @@ static int files_fsck(struct ref_store *ref_store, struct files_ref_store *refs = files_downcast(ref_store, REF_STORE_READ, "fsck"); - ret = refs->packed_ref_store->be->fsck(refs->packed_ref_store, o); + ret = refs->packed_ref_store->be->fsck(refs->packed_ref_store, o) + | files_fsck_refs(ref_store, o); + return ret; } From patchwork Thu Jun 27 15:17:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714643 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF59015EFA4 for ; Thu, 27 Jun 2024 15:17:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501481; cv=none; b=plyjZ1QKlgZyNPy5LW0471u7ICzJ7kRsg59LNXgw/xbTncGUIUViuXM7Pk9uwXEtp+2ADezRkA6H2b/QdMdwBlr0YuIWHGo7CJgoLZzmX426aUGf+s70IKBnYfkx/QhDHsfyCHXN16L2FaD0mlS98q0qDcRnjbc0CZZwVPlBj5U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501481; c=relaxed/simple; bh=2Jorx0PwXWOhrxGBMlzhRs0RbK2JjykDd5pUimbqdYw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DhKRAagdgvISArNUp689L6qtV1QPcJLpp5XhHMvbevcpsHvM9eM5l7lTzZZMzsuvEF7WLeqtDHRLrC5tiZKvkt6hbAxbRI8bUO8j5xt4AxhbBkM+1jYK9HwAY/3vyW3ZM6Lu2zHNLvQS5ki4VL8FtYvQ8bc2kAERARifPXlkpz8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=D2TmmhXi; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D2TmmhXi" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-7065a2f4573so4861061b3a.2 for ; Thu, 27 Jun 2024 08:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501478; x=1720106278; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=XQn820o0Xm8Tr+akXnLCY1hSNh1cPHx8kVQVfuo4geI=; b=D2TmmhXiCpQMeEwAoihk3D8Q8t/JR5zJVTd2zDxeGXkEpEjyHIe0aVAmdT9VrWgK2y /M8TUvG43+hIR82uVnbxfuoVzidsGbFyRdPRqmrbIq/WGhW8QnVYYHphnjy5wSa1Gch+ f2boQfmtZmGQ2ur9kTouZvQMwxNX4dBbbqBDaZMycWXHSlQlCK/1jM27PPEyqjUrGwPu HZArC+ZBp+lv8E5iQNYbz0vxPfHt9+GMqLckGtKZvCphpTx+wsYNeX04J5zk+cqKlHuT heRydZgBJbVkIPT/XB5iQN4d5kMqgfjiPNqfAUf9V6DoTvk3VHR9oo4VTgz5g35EmwJX XAMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501478; x=1720106278; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XQn820o0Xm8Tr+akXnLCY1hSNh1cPHx8kVQVfuo4geI=; b=gm0riUQ6HsBls+h+Hhmefu+uALXumcFswKGm4QQHcW29KDFO9eLWVy0Zj/t9Pnv+7I 6/0Ayw5jbvIR1CR/PPRvUh8v7iW7OJdI2fav7GZriSwmug759OAtjeCMlRsq00SmIhfD micfyOoEzXFpScwjoGekOo/znqOfnB52s+02+3hLGDKOgdrHWSyGHFuxC7KCZyB2KayR h5oPeW1tsWiYu7otptnX+edMsQH3u5e+U0Ftm1F2l3ueQTNZ0F1akeq8m5MFnIJJWvmJ KykWVeDqwcSZAgQhfDB8Yq58AEURmFaVApn7Qyr/ckDWsSCoRJwJKIOxo4RS5IePO9bT ZhGA== X-Gm-Message-State: AOJu0YyovzKt1hZQJRIJNXShbNeVGnD9UNTZ8UKNuBTOQbnDwzCX/XYL sfdFEoKXPUyBQtPdp2ywM1O3zGvvUxnzZRVEnzMTRAcyw8sBgpPlbBa4aA== X-Google-Smtp-Source: AGHT+IHEG9sq97eEhyKrIE7BlUNytPaAHpovvS1pqBsF45Ci5FaplNPPK7Z7OYn9F8ixv/xRSS+FDw== X-Received: by 2002:a05:6a20:3d8b:b0:1be:c4e6:ed40 with SMTP id adf61e73a8af0-1bec4e6ee22mr5390997637.51.1719501478509; Thu, 27 Jun 2024 08:17:58 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1faac8f5a71sm14516505ad.109.2024.06.27.08.17.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:17:58 -0700 (PDT) Date: Thu, 27 Jun 2024 23:17:55 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 11/12] fsck: add ref name check for files backend Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The git-fsck(1) only implicitly checks the reference, it does not fully check refs with bad format name such as standalone "@" and name ending with ".lock". In order to provide such checks, add a new fsck message id "badRefName" with default ERROR type. Use existing "check_refname_format" to explicit check the ref name. And add a new unit test to verify the functionality. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- Documentation/fsck-msgids.txt | 3 + fsck.h | 1 + refs/files-backend.c | 20 +++++++ t/t0602-reffiles-fsck.sh | 101 ++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100755 t/t0602-reffiles-fsck.sh diff --git a/Documentation/fsck-msgids.txt b/Documentation/fsck-msgids.txt index f643585a34..dab4012246 100644 --- a/Documentation/fsck-msgids.txt +++ b/Documentation/fsck-msgids.txt @@ -19,6 +19,9 @@ `badParentSha1`:: (ERROR) A commit object has a bad parent sha1. +`badRefName`:: + (ERROR) A ref has a bad name. + `badTagName`:: (INFO) A tag has an invalid format. diff --git a/fsck.h b/fsck.h index e903845690..aad2211bff 100644 --- a/fsck.h +++ b/fsck.h @@ -31,6 +31,7 @@ enum fsck_msg_type { FUNC(BAD_NAME, ERROR) \ FUNC(BAD_OBJECT_SHA1, ERROR) \ FUNC(BAD_PARENT_SHA1, ERROR) \ + FUNC(BAD_REF_NAME, ERROR) \ FUNC(BAD_TIMEZONE, ERROR) \ FUNC(BAD_TREE, ERROR) \ FUNC(BAD_TREE_SHA1, ERROR) \ diff --git a/refs/files-backend.c b/refs/files-backend.c index aefc947fc3..e840a768b4 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3417,6 +3417,25 @@ typedef int (*files_fsck_refs_fn)(struct fsck_refs_options *o, const char *refs_check_dir, struct dir_iterator *iter); +static int files_fsck_refs_name(struct fsck_refs_options *o, + const char *gitdir UNUSED, + const char *refs_check_dir, + struct dir_iterator *iter) +{ + struct strbuf sb = STRBUF_INIT; + int ret = 0; + + if (check_refname_format(iter->basename, REFNAME_ALLOW_ONELEVEL)) { + strbuf_addf(&sb, "%s/%s", refs_check_dir, iter->relative_path); + ret = fsck_refs_report(o, NULL, sb.buf, + FSCK_MSG_BAD_REF_NAME, + "invalid refname format"); + } + + strbuf_release(&sb); + return ret; +} + static int files_fsck_refs_dir(struct ref_store *ref_store, struct fsck_refs_options *o, const char *refs_check_dir, @@ -3468,6 +3487,7 @@ static int files_fsck_refs(struct ref_store *ref_store, { int ret; files_fsck_refs_fn fsck_refs_fns[]= { + files_fsck_refs_name, NULL }; diff --git a/t/t0602-reffiles-fsck.sh b/t/t0602-reffiles-fsck.sh new file mode 100755 index 0000000000..b2db58d2c6 --- /dev/null +++ b/t/t0602-reffiles-fsck.sh @@ -0,0 +1,101 @@ +#!/bin/sh + +test_description='Test reffiles backend consistency check' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +GIT_TEST_DEFAULT_REF_FORMAT=files +export GIT_TEST_DEFAULT_REF_FORMAT + +. ./test-lib.sh + +test_expect_success 'ref name should be checked' ' + test_when_finished "rm -rf repo" && + git init repo && + branch_dir_prefix=.git/refs/heads && + tag_dir_prefix=.git/refs/tags && + ( + cd repo && + git commit --allow-empty -m initial && + git checkout -b branch-1 && + git tag tag-1 && + git commit --allow-empty -m second && + git checkout -b branch-2 && + git tag tag-2 && + git tag multi_hierarchy/tag-2 + ) && + ( + cd repo && + cp $branch_dir_prefix/branch-1 $branch_dir_prefix/.branch-1 && + test_must_fail git fsck 2>err && + cat >expect <<-EOF && + error: refs/heads/.branch-1: badRefName: invalid refname format + EOF + rm $branch_dir_prefix/.branch-1 && + test_cmp expect err + ) && + ( + cd repo && + cp $tag_dir_prefix/tag-1 $tag_dir_prefix/tag-1.lock && + test_must_fail git fsck 2>err && + cat >expect <<-EOF && + error: refs/tags/tag-1.lock: badRefName: invalid refname format + EOF + rm $tag_dir_prefix/tag-1.lock && + test_cmp expect err + ) && + ( + cd repo && + cp $branch_dir_prefix/branch-1 $branch_dir_prefix/@ && + test_must_fail git fsck 2>err && + cat >expect <<-EOF && + error: refs/heads/@: badRefName: invalid refname format + EOF + rm $branch_dir_prefix/@ && + test_cmp expect err + ) && + ( + cd repo && + cp $tag_dir_prefix/multi_hierarchy/tag-2 $tag_dir_prefix/multi_hierarchy/@ && + test_must_fail git fsck 2>err && + cat >expect <<-EOF && + error: refs/tags/multi_hierarchy/@: badRefName: invalid refname format + EOF + rm $tag_dir_prefix/multi_hierarchy/@ && + test_cmp expect err + ) +' + +test_expect_success 'ref name check should be adapted into fsck messages' ' + test_when_finished "rm -rf repo" && + git init repo && + branch_dir_prefix=.git/refs/heads && + tag_dir_prefix=.git/refs/tags && + ( + cd repo && + git commit --allow-empty -m initial && + git checkout -b branch-1 && + git tag tag-1 && + git commit --allow-empty -m second && + git checkout -b branch-2 && + git tag tag-2 + ) && + ( + cd repo && + cp $branch_dir_prefix/branch-1 $branch_dir_prefix/.branch-1 && + git -c fsck.badRefName=warn fsck 2>err && + cat >expect <<-EOF && + warning: refs/heads/.branch-1: badRefName: invalid refname format + EOF + rm $branch_dir_prefix/.branch-1 && + test_cmp expect err + ) && + ( + cd repo && + cp $branch_dir_prefix/branch-1 $branch_dir_prefix/@ && + git -c fsck.badRefName=ignore fsck 2>err && + test_must_be_empty err + ) +' + +test_done From patchwork Thu Jun 27 15:18:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13714644 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 195FE198E96 for ; Thu, 27 Jun 2024 15:18:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501499; cv=none; b=QLNizX4EZF8jzT0Tgn+3LJSgQYEhKB2lVLl1Qhec94b0wEPbzSf8sKCPYGfln5ymiCYHNW6/gA5CdaRKybxHnH9BFQBQ+OD+LLl4bOD/wFSMcjir4Nv6+VzUP5vGKrsrXeYO3LRpMUx79t8M4WIYjhjDYVnmNFzmWohBlvLu36Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719501499; c=relaxed/simple; bh=R0T8JzUr6AWivVyvt04qZZJbnMKptSOL2CtE1u0dSNo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=H3RmER3ajwkt0B24zVce+hPcniysUw/7qjWpcjQy0uPb3kXIqpGyMSZGUv14REZv3HYRY6NxMQSr2Jmm+VC1Yb2ZSelLDnRKlsJfwbLvp8uBt3H/7U1wybfFj4b6u6LQaHuDAeXLcPqiLqxmQxiysNUD52yhd7i0ZY7Q9ZpCBMY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bG+mUhe4; arc=none smtp.client-ip=209.85.216.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bG+mUhe4" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2c7dbdc83bfso6042481a91.1 for ; Thu, 27 Jun 2024 08:18:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719501497; x=1720106297; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=8BdknIzx21ifR9ywklaprptCBuVEqPxc3M9t4bqVngc=; b=bG+mUhe4iGSqNFOF9T1bwGCKGKWQOmhAZUanCKQPX4kxeFvFX4BhJ2KivxMpiKhazO 9PeQ6cUImM31AXxgoU3hKIXtoQp8M19Osv4t9+VfcW4z3sTaHRjR6pVcz8qtmMoHh2KS E6ttsQOblXAoi0TnKV4miHMY+MZsqS6AOXhOOnPI1fJqmfMS/7oxQfQ0GDAKPyOayNy/ HyZqnXwivbgWC3FD8DVKFcswZOpcwjiWQXVviXmwe07KXnvbYfW6fPVwV11/lMfX7fKC n1RlO9wHbI754uOiGSTZDwtDRMCKR5DSN3sE84v1zsIcMY9yDmsVWAK26hucfcSCe6hr oR0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719501497; x=1720106297; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=8BdknIzx21ifR9ywklaprptCBuVEqPxc3M9t4bqVngc=; b=qduo20kDkhoU3+aIrzCGCU78s4SGZWyuYoH6csBtpy/zOSA+MsQq2STw4noEVVLb2H UqPolGvH7W+wihiYJcR+E7trmXGZogKsvLP8IvaJCuYJHFi25BB0Ck7gXF+DY31TNj05 zAtreNZ6pg4GgY/AAuDubeI2Q7ei1oWR8psX0EpFT8/eg5mxv2jKb8WoqsxSmQ7XEw+p J8YMdMpE8VUQ6SslV0TYnzZcJ+HROWcjtXzC+y78+WwSDDONxMTUGVGw5yLgLPBO+cE+ mm6TEj6pDoxvIMoGaRmf2cV8JYt9m1b5sEGal2JjsaG9jsFh1iiM6DKuBGN79uK1962R vXVA== X-Gm-Message-State: AOJu0YyzxuGquDixyD2LdPozne8qATxN+P+/U1ARpjGSza5uS+bf3hNh UnpBCxFWo6gnjqHCKH2IrP1+jnOwbDVG8wbUm6A+u0NQPNwEto7PPfmdrQ== X-Google-Smtp-Source: AGHT+IHYIrOl9niyrtm7hzoY/uBbpYCFenjskG43GHEi67Bgo+MLCuVahMSekBrW/h/vi/CX4lFUiw== X-Received: by 2002:a17:90b:34ce:b0:2c2:d243:e8b with SMTP id 98e67ed59e1d1-2c8614ac695mr10330711a91.47.1719501496655; Thu, 27 Jun 2024 08:18:16 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c8d7e6354csm3778128a91.20.2024.06.27.08.18.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 08:18:16 -0700 (PDT) Date: Thu, 27 Jun 2024 23:18:13 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine Subject: [GSoC][PATCH v5 12/12] fsck: add ref content check for files backend Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Enhance the git-fsck(1) command by adding a check for reference content in the files backend. The new functionality ensures that symrefs, real symbolic link and regular refs are validated correctly. In order to check the trailing content of the regular refs, add a new parameter `trailing` to `parse_loose_ref_contents`. For symrefs, `parse_loose_ref_contents` will set the "referent". However, symbolic link could be either absolute or relative. Use "strbuf_add_real_path" to read the symbolic link and convert the relative path to absolute path. Then use "skip_prefix" to make it align with symref "referent". Thus, the symrefs and symbolic links could share the same interface. Add a new function "files_fsck_symref_target" which aims at checking the following things: 1. whether the pointee is under the `refs/` directory. 2. whether the pointee name is correct. 3. whether the pointee path is a wrong type in filesystem. Last, add the following FSCK MESSAGEs: 1. "badRefContent(ERROR)": A ref has a bad content 2. "badSymrefPointee(ERROR)": The pointee of a symref is bad. 3. "trailingRefContent(WARN)": A ref content has trailing contents. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- Documentation/fsck-msgids.txt | 9 +++ fsck.h | 3 + refs.c | 2 +- refs/files-backend.c | 145 +++++++++++++++++++++++++++++++++- refs/refs-internal.h | 5 +- t/t0602-reffiles-fsck.sh | 110 ++++++++++++++++++++++++++ 6 files changed, 269 insertions(+), 5 deletions(-) diff --git a/Documentation/fsck-msgids.txt b/Documentation/fsck-msgids.txt index dab4012246..b1630a478b 100644 --- a/Documentation/fsck-msgids.txt +++ b/Documentation/fsck-msgids.txt @@ -19,9 +19,15 @@ `badParentSha1`:: (ERROR) A commit object has a bad parent sha1. +`badRefContent`:: + (ERROR) A ref has a bad content. + `badRefName`:: (ERROR) A ref has a bad name. +`badSymrefPointee`:: + (ERROR) The pointee of a symref is bad. + `badTagName`:: (INFO) A tag has an invalid format. @@ -167,6 +173,9 @@ `nullSha1`:: (WARN) Tree contains entries pointing to a null sha1. +`trailingRefContent`:: + (WARN) A ref content has trailing contents. + `treeNotSorted`:: (ERROR) A tree is not properly sorted. diff --git a/fsck.h b/fsck.h index aad2211bff..5c1b113674 100644 --- a/fsck.h +++ b/fsck.h @@ -32,6 +32,8 @@ enum fsck_msg_type { FUNC(BAD_OBJECT_SHA1, ERROR) \ FUNC(BAD_PARENT_SHA1, ERROR) \ FUNC(BAD_REF_NAME, ERROR) \ + FUNC(BAD_REF_CONTENT, ERROR) \ + FUNC(BAD_SYMREF_POINTEE, ERROR) \ FUNC(BAD_TIMEZONE, ERROR) \ FUNC(BAD_TREE, ERROR) \ FUNC(BAD_TREE_SHA1, ERROR) \ @@ -72,6 +74,7 @@ enum fsck_msg_type { FUNC(HAS_DOTDOT, WARN) \ FUNC(HAS_DOTGIT, WARN) \ FUNC(NULL_SHA1, WARN) \ + FUNC(TRAILING_REF_CONTENT, WARN) \ FUNC(ZERO_PADDED_FILEMODE, WARN) \ FUNC(NUL_IN_COMMIT, WARN) \ FUNC(LARGE_PATHNAME, WARN) \ diff --git a/refs.c b/refs.c index 646fdfaf22..55d581ab12 100644 --- a/refs.c +++ b/refs.c @@ -1758,7 +1758,7 @@ static int refs_read_special_head(struct ref_store *ref_store, } result = parse_loose_ref_contents(content.buf, oid, referent, type, - failure_errno); + failure_errno, NULL); done: strbuf_release(&full_path); diff --git a/refs/files-backend.c b/refs/files-backend.c index e840a768b4..0e6b6d3f6a 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1,4 +1,5 @@ #include "../git-compat-util.h" +#include "../abspath.h" #include "../copy.h" #include "../environment.h" #include "../gettext.h" @@ -551,7 +552,7 @@ static int read_ref_internal(struct ref_store *ref_store, const char *refname, strbuf_rtrim(&sb_contents); buf = sb_contents.buf; - ret = parse_loose_ref_contents(buf, oid, referent, type, &myerr); + ret = parse_loose_ref_contents(buf, oid, referent, type, &myerr, NULL); out: if (ret && !myerr) @@ -587,7 +588,7 @@ static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refn int parse_loose_ref_contents(const char *buf, struct object_id *oid, struct strbuf *referent, unsigned int *type, - int *failure_errno) + int *failure_errno, const char **trailing) { const char *p; if (skip_prefix(buf, "ref:", &buf)) { @@ -609,6 +610,10 @@ int parse_loose_ref_contents(const char *buf, struct object_id *oid, *failure_errno = EINVAL; return -1; } + + if (trailing) + *trailing = p; + return 0; } @@ -3436,6 +3441,141 @@ static int files_fsck_refs_name(struct fsck_refs_options *o, return ret; } +/* + * Check the symref "pointee_name" and "pointee_path". The caller should + * make sure that "pointee_path" is absolute. For symbolic ref, "pointee_name" + * would be the content after "refs:". For symblic link, "pointee_name" would + * be the relative path agaignst "gitdir". + */ +static int files_fsck_symref_target(struct fsck_refs_options *o, + const char *refname, + const char *pointee_name, + const char *pointee_path) +{ + const char *p = NULL; + struct stat st; + int ret = 0; + + if (!skip_prefix(pointee_name, "refs/", &p)) { + + ret = fsck_refs_report(o, NULL, refname, + FSCK_MSG_BAD_SYMREF_POINTEE, + "point to target out of refs hierarchy"); + goto out; + } + + if (check_refname_format(pointee_name, 0)) { + ret = fsck_refs_report(o, NULL, refname, + FSCK_MSG_BAD_SYMREF_POINTEE, + "point to invalid refname"); + } + + if (lstat(pointee_path, &st) < 0) + goto out; + + if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) { + ret = fsck_refs_report(o, NULL, refname, + FSCK_MSG_BAD_SYMREF_POINTEE, + "point to invalid target"); + goto out; + } +out: + return ret; +} + +static int files_fsck_refs_content(struct fsck_refs_options *o, + const char *gitdir, + const char *refs_check_dir, + struct dir_iterator *iter) +{ + struct strbuf pointee_path = STRBUF_INIT, + ref_content = STRBUF_INIT, + abs_gitdir = STRBUF_INIT, + referent = STRBUF_INIT, + refname = STRBUF_INIT; + const char *trailing = NULL; + int failure_errno = 0; + unsigned int type = 0; + struct object_id oid; + int ret = 0; + + strbuf_addf(&refname, "%s/%s", refs_check_dir, iter->relative_path); + + /* + * If the file is a symlink, we need to only check the connectivity + * of the destination object. + */ + if (S_ISLNK(iter->st.st_mode)) { + const char *pointee_name = NULL; + + strbuf_add_real_path(&pointee_path, iter->path.buf); + + strbuf_add_absolute_path(&abs_gitdir, gitdir); + strbuf_normalize_path(&abs_gitdir); + if (!is_dir_sep(abs_gitdir.buf[abs_gitdir.len - 1])) + strbuf_addch(&abs_gitdir, '/'); + + if (!skip_prefix(pointee_path.buf, + abs_gitdir.buf, &pointee_name)) { + ret = fsck_refs_report(o, NULL, refname.buf, + FSCK_MSG_BAD_SYMREF_POINTEE, + "point to target outside gitdir"); + goto clean; + } + + ret = files_fsck_symref_target(o, refname.buf, pointee_name, + pointee_path.buf); + goto clean; + } + + if (strbuf_read_file(&ref_content, iter->path.buf, 0) < 0) { + ret = error_errno(_("%s/%s: unable to read the ref"), + refs_check_dir, iter->relative_path); + goto clean; + } + + if (parse_loose_ref_contents(ref_content.buf, &oid, + &referent, &type, + &failure_errno, &trailing)) { + ret = fsck_refs_report(o, NULL, refname.buf, + FSCK_MSG_BAD_REF_CONTENT, + "invalid ref content"); + goto clean; + } + + /* + * If the ref is a symref, we need to check the destination name and + * connectivity. + */ + if (referent.len && (type & REF_ISSYMREF)) { + strbuf_addf(&pointee_path, "%s/%s", gitdir, referent.buf); + strbuf_rtrim(&referent); + + ret = files_fsck_symref_target(o, refname.buf, referent.buf, + pointee_path.buf); + goto clean; + } else { + /* + * Only regular refs could have a trailing garbage. Should + * be reported as a warning. + */ + if (trailing && (*trailing != '\0' && *trailing != '\n')) { + ret = fsck_refs_report(o, NULL, refname.buf, + FSCK_MSG_TRAILING_REF_CONTENT, + "trailing garbage in ref"); + goto clean; + } + } + +clean: + strbuf_release(&abs_gitdir); + strbuf_release(&pointee_path); + strbuf_release(&refname); + strbuf_release(&ref_content); + strbuf_release(&referent); + return ret; +} + static int files_fsck_refs_dir(struct ref_store *ref_store, struct fsck_refs_options *o, const char *refs_check_dir, @@ -3488,6 +3628,7 @@ static int files_fsck_refs(struct ref_store *ref_store, int ret; files_fsck_refs_fn fsck_refs_fns[]= { files_fsck_refs_name, + files_fsck_refs_content, NULL }; diff --git a/refs/refs-internal.h b/refs/refs-internal.h index ecc082baf8..915d0d1f20 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -709,11 +709,12 @@ struct ref_store { /* * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for - * invalid contents. + * invalid contents. Also *trailing is set to the first character after the + * refname or NULL if the referent is not empty. */ int parse_loose_ref_contents(const char *buf, struct object_id *oid, struct strbuf *referent, unsigned int *type, - int *failure_errno); + int *failure_errno, const char **trailing); /* * Fill in the generic part of refs and add it to our collection of diff --git a/t/t0602-reffiles-fsck.sh b/t/t0602-reffiles-fsck.sh index b2db58d2c6..35bf40ee64 100755 --- a/t/t0602-reffiles-fsck.sh +++ b/t/t0602-reffiles-fsck.sh @@ -98,4 +98,114 @@ test_expect_success 'ref name check should be adapted into fsck messages' ' ) ' +test_expect_success 'regular ref content should be checked' ' + test_when_finished "rm -rf repo" && + git init repo && + branch_dir_prefix=.git/refs/heads && + tag_dir_prefix=.git/refs/tags && + ( + cd repo && + git commit --allow-empty -m initial && + git checkout -b branch-1 && + git tag tag-1 && + git commit --allow-empty -m second && + git checkout -b branch-2 && + git tag tag-2 && + git checkout -b a/b/tag-2 + ) && + ( + cd repo && + printf "%s garbage" "$(git rev-parse branch-1)" > $branch_dir_prefix/branch-1-garbage && + git fsck 2>err && + cat >expect <<-EOF && + warning: refs/heads/branch-1-garbage: trailingRefContent: trailing garbage in ref + EOF + rm $branch_dir_prefix/branch-1-garbage && + test_cmp expect err + ) && + ( + cd repo && + printf "%s garbage" "$(git rev-parse tag-1)" > $tag_dir_prefix/tag-1-garbage && + test_must_fail git -c fsck.trailingRefContent=error fsck 2>err && + cat >expect <<-EOF && + error: refs/tags/tag-1-garbage: trailingRefContent: trailing garbage in ref + EOF + rm $tag_dir_prefix/tag-1-garbage && + test_cmp expect err + ) && + ( + cd repo && + printf "%s " "$(git rev-parse tag-2)" > $tag_dir_prefix/tag-2-garbage && + git fsck 2>err && + cat >expect <<-EOF && + warning: refs/tags/tag-2-garbage: trailingRefContent: trailing garbage in ref + EOF + rm $tag_dir_prefix/tag-2-garbage && + test_cmp expect err + ) && + ( + cd repo && + printf "xfsazqfxcadas" > $tag_dir_prefix/tag-2-bad && + test_must_fail git refs verify 2>err && + cat >expect <<-EOF && + error: refs/tags/tag-2-bad: badRefContent: invalid ref content + EOF + rm $tag_dir_prefix/tag-2-bad && + test_cmp expect err + ) && + ( + cd repo && + printf "xfsazqfxcadas" > $branch_dir_prefix/a/b/branch-2-bad && + test_must_fail git refs verify 2>err && + cat >expect <<-EOF && + error: refs/heads/a/b/branch-2-bad: badRefContent: invalid ref content + EOF + rm $branch_dir_prefix/a/b/branch-2-bad && + test_cmp expect err + ) +' + +test_expect_success 'symbolic ref content should be checked' ' + test_when_finished "rm -rf repo" && + git init repo && + branch_dir_prefix=.git/refs/heads && + tag_dir_prefix=.git/refs/tags && + ( + cd repo && + git commit --allow-empty -m initial && + git checkout -b branch-1 && + git tag tag-1 + ) && + ( + cd repo && + printf "ref: refs/heads/.branch" > $branch_dir_prefix/branch-2-bad && + test_must_fail git refs verify 2>err && + cat >expect <<-EOF && + error: refs/heads/branch-2-bad: badSymrefPointee: point to invalid refname + EOF + rm $branch_dir_prefix/branch-2-bad && + test_cmp expect err + ) && + ( + cd repo && + printf "ref: refs/heads" > $branch_dir_prefix/branch-2-bad && + test_must_fail git refs verify 2>err && + cat >expect <<-EOF && + error: refs/heads/branch-2-bad: badSymrefPointee: point to invalid target + EOF + rm $branch_dir_prefix/branch-2-bad && + test_cmp expect err + ) && + ( + cd repo && + printf "ref: logs/maint-v2.45" > $branch_dir_prefix/branch-2-bad && + test_must_fail git refs verify 2>err && + cat >expect <<-EOF && + error: refs/heads/branch-2-bad: badSymrefPointee: point to target out of refs hierarchy + EOF + rm $branch_dir_prefix/branch-2-bad && + test_cmp expect err + ) +' + test_done