From patchwork Sun Jul 14 12:30:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732697 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.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 3ACD61FDD for ; Sun, 14 Jul 2024 12:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960259; cv=none; b=XFu+cDHpwbp8pP5Ks1LbhxMeOUpJ2sfcl5MISjxljZM96LAySgZo+S5GoeEiKURae50bPQ+OWRoSRptEZ7qkh4BucuaMFhqQ8EFhsDaYyRtWUT1oYO1Xy/ZYanNVximIfPVbQP2RZlsiTAY6D62SE77Ck4PybMNVDPloxfFXkpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960259; c=relaxed/simple; bh=4S/eeYa3WSAOASAvQOCp2KZZaz7om+5BeG3DmzjcW/Q=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=id6TW9qpqw/35+6RxvXeb8ZFIygnrD/hwPBsdqhv+/QVveR6A82jT17QAC4yZaunsLkCjXQz9U2ApJxjXrt/S+UwBEj+3KytvhNSBppCz/GDHvcDzS21q1Q4ewsiAtSV7fthGs5PvepEl1CRVdR09r3lL9e8PN+LYOV220Urg/g= 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=XG/Lvpxp; arc=none smtp.client-ip=209.85.167.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="XG/Lvpxp" Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3d9c487b2b5so2081932b6e.3 for ; Sun, 14 Jul 2024 05:30:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960256; x=1721565056; 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=9UDRGCnen2eMRIWeoclWfDlQnsmaMv5ILJOERPNxlb4=; b=XG/Lvpxp59Ib7+7uh1qFZwtdt0QAJyzh9TN6u/TYlvEe+OQm760cUdutBUfkPwP5j6 IlmRAIs/A0p/Lm8Mu5vwDjYgoml+YjZhWthKKrphAOp7y+kXyPeORczDW159fjEpGP/N 8ot2W4jXos3ODJJCiOnP7h/TRxKlitPs3o+MVdjBsD3telBhaqxFwCNa97Ai113MiXSp GV0E4926DTQygsUtQzVRxg6YHnXrCZpGCEQQVa/8cQarg6kcWuTcZEfYKb3RSVRg1pua DMT4U2jVSeBzg5k79Hu5MEMjrJAKy3LQPMXR2C/9H19KUuHaQ16B+oiRF/4ugJ/hvYbE 8cmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960256; x=1721565056; 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=9UDRGCnen2eMRIWeoclWfDlQnsmaMv5ILJOERPNxlb4=; b=e48xnaT3tHP3GREWzG6mOBjHayak6Cph5YvulHPctT1o09g0ikV/GAUYW7gsHnE0vI udQbS8E5vNu/wFrYgEUIt0i0FcwfKFM763Ar6+Ate6QHHSurXoafgUSazlacVoL5XNqk NoomHFEbdl3T9NoXNiPurD+0b5dwgNKPKOyBGqME4XU33z2HuyzUb4ZHmnNNy/AMDNHJ 6KT14ZLlcni4WKtF+dvOVPzA3zs3ytAqYY1jSALArQOJBOGhX6uSK0aZE54+apmeJYID iJX/CcT5zuJt9YnIRcs/XMK0JL8FLYznHZCvk08eOUitsJcuLvyoNbMAvdmsywiRieSu 7ZvQ== X-Gm-Message-State: AOJu0YwbMcc3u5XmEyEhP5nYHaTidqlgjZxGt/6dNycIvRhDn1/SGYfy FyyE5ENImJqLhgkYFdlqTfogWgAqx7cW7VOQzWF4WMUguFWxLB0zSGS0ng== X-Google-Smtp-Source: AGHT+IEiO53LssiQbIs9r/6GVYoPmxGFwDmKuZ9kAf9I3laOWbQTmHvGkgODJsFKaFShFbbG/wSNYg== X-Received: by 2002:a05:6808:189e:b0:3d9:e22a:8fa3 with SMTP id 5614622812f47-3d9e22a94c5mr14721889b6e.32.1720960254794; Sun, 14 Jul 2024 05:30:54 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0bb73ea1sm22955625ad.34.2024.07.14.05.30.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:30:54 -0700 (PDT) Date: Sun, 14 Jul 2024 20:30:59 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 01/10] fsck: rename "skiplist" to "skip_oids" 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 "skiplist" field in "fsck_options" is related to objects. Because we are going to introduce ref consistency check, the "skiplist" name is too general which will make the caller think "skiplist" is related to both the refs and objects. It may seem that for both refs and objects, we should provide a general "skiplist" here. However, the type for "skiplist" is `struct oidset` which is totally unsuitable for refs. To avoid above ambiguity, rename "skiplist" to "skip_oids". Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- fsck.c | 4 ++-- fsck.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fsck.c b/fsck.c index eea7145470..3f32441492 100644 --- a/fsck.c +++ b/fsck.c @@ -205,7 +205,7 @@ void fsck_set_msg_types(struct fsck_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(&options->skip_oids, buf + equal + 1, the_repository->hash_algo); buf += len + 1; continue; @@ -223,7 +223,7 @@ void fsck_set_msg_types(struct fsck_options *options, const char *values) static int object_on_skiplist(struct fsck_options *opts, const struct object_id *oid) { - return opts && oid && oidset_contains(&opts->skiplist, oid); + return opts && oid && oidset_contains(&opts->skip_oids, oid); } __attribute__((format (printf, 5, 6))) diff --git a/fsck.h b/fsck.h index 6085a384f6..bcfb2e34cd 100644 --- a/fsck.h +++ b/fsck.h @@ -136,7 +136,7 @@ struct fsck_options { fsck_error error_func; unsigned strict:1; enum fsck_msg_type *msg_type; - struct oidset skiplist; + struct oidset skip_oids; struct oidset gitmodules_found; struct oidset gitmodules_done; struct oidset gitattributes_found; @@ -145,7 +145,7 @@ struct fsck_options { }; #define FSCK_OPTIONS_DEFAULT { \ - .skiplist = OIDSET_INIT, \ + .skip_oids = OIDSET_INIT, \ .gitmodules_found = OIDSET_INIT, \ .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ From patchwork Sun Jul 14 12:31:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732698 Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) (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 222331FDD for ; Sun, 14 Jul 2024 12:31:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960273; cv=none; b=P4HykM/RXrKwlBLdnhficTge6+lonrxuzp0J9Y+zW4KSfiKlZ/w8AidL83Q0vku9b6IY7rR+/cC4aOgS48HAsAxpTX9El0DyXS7ibTdjhwPAAIqeLIOWQokICykm4MDXLXv4G3zDUnqFveVFgdAqr5TZalsbYlZlO6k3D5Gpikw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960273; c=relaxed/simple; bh=S3EQ2wKKD/ps9kONyyiFhUrB9IDZ53zjMNvGg7ojQDc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RdSLrILi2Vu2R53BCjG6PQ3SqsTwYRqMhRwCjLoODsdwFgQMqXPXvH1MoSbr8cTOnUbE8YEji0/LYsMkAhyqXYOEiBNYSVjvtzslbTPk5baYDg9h2LXNzLegbRCdWZqpTbL+LhueTn3+YEe2nQknzKG1GeAdTBPWCwpOIUMzk1E= 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=ibUgZ4s0; arc=none smtp.client-ip=209.85.167.170 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="ibUgZ4s0" Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3c9cc681ee0so1977314b6e.0 for ; Sun, 14 Jul 2024 05:31:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960270; x=1721565070; 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=Seoqvdll4X7hFWS8fDEbdt/icnhk0Fij2yQ8eCCM4ak=; b=ibUgZ4s0C5vBJqos2Lg0gdZ6F9XvUyQhxfz3mdDzXHQ7RcixBY9HL4siidPcfuQDUW MD7aExh1X078H0KIjU++hD6qgc2fS0cNBKMxMaar8UK9a2i5ogzxMpWhXPzzOKFwqndx ngEU/f6O7IgIIm2Bp7cO2Ho+zFLx657YcEXuGHc+Hd92XJqBFaUdeX1HmZRjLtZSPK9V 5jCPwtoahgEHg4AAhyr4zFQCxditvF4BIToUBuT1Y5hxGF0yiiWQbXeBU7K+IuU7e8i/ BcJ28y9F8IwfawMavcNYf4v9Vlel0WSzfvSEl86XUKKpsS7ZNQATsMXSkt2GhOLa+U4A Lf3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960270; x=1721565070; 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=Seoqvdll4X7hFWS8fDEbdt/icnhk0Fij2yQ8eCCM4ak=; b=nDrNodIMhJwoAlEWr43e8HXrU1UPW2LXt80xMZwYKcW52PT1nVBtO4Xe7mk6ktkTbt rXARs1dbCfjYKj4fZ62F+ILctzmtXIcfrdZ14/ADBl+x51gD+x85ZEUhUwDkaZwa9Cmg icSpYEM+3IZTYAWEimIFg2hdmdSO2a9mpWHX4b5MRaWDpfFEcdSmqkgSGy1cs7gW9aW+ vkczYEG6SejBh0qR7wlhfhkY1bxZYestzaUbLuX7m+VPLwf0HzJpDIXdda1Dt/2e+pCK 65DMzal9DnfLdsTlBZwiQ7+OR6uaykI5IUMIXjLybFaBsGeTnzasiIQlx7TuMI1L6Tcr AP1Q== X-Gm-Message-State: AOJu0YxxJcHvRW1ZuaKjvSf8aP3oruyZDXF4k3HW2Q3wJFFWuu5utEyJ /kRWPWGxBdGdRb0js+50vsTE+AnolBNJ/jsJnex/iBxSAKfstO3gPYTxyQ== X-Google-Smtp-Source: AGHT+IGkpomIIqWqB04UshpUuw+CAOKsdRQEEwGG/cObmfrGYiZt325uNA2KW3VPyb2Sp3f3zGsHPw== X-Received: by 2002:a05:6808:1a2a:b0:3d5:6413:2138 with SMTP id 5614622812f47-3d93c038456mr19841140b6e.12.1720960270001; Sun, 14 Jul 2024 05:31:10 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b7ecc947fsm2538069b3a.195.2024.07.14.05.31.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:31:09 -0700 (PDT) Date: Sun, 14 Jul 2024 20:31:14 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 02/10] 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 checking fsck error type and calling the callback "error_func" to report the message. However, "report" function is only 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_vreport" following the "report" prototype. Instead of using "...", provide "va_list" to allow more flexibility. When checking loose refs and reflogs, we only need to pass the checked name to the fsck error report function. However, for packed-refs and reftable refs, we need to check both the consistency of the file itself and the refs or reflogs contained in the file. In order to provide above checks, add two parameters "ref_checkee" and "sub_ref_checkee" in "fsck_vreport" function. Like "report", the "fsck_vreport" function will use "error_func" registered in "fsck_options" to report customized messages. Change "error_func" prototype to align with the new "fsck_vreport". Then, change "report" function to use "fsck_vreport" to report objects related messages. Add a new function called "fsck_refs_report" to use "fsck_vreport" to report refs related messages. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- builtin/fsck.c | 2 ++ builtin/mktag.c | 2 ++ fsck.c | 60 ++++++++++++++++++++++++++++++++++++++++++------- fsck.h | 19 +++++++++++++++- object-file.c | 12 +++++----- 5 files changed, 81 insertions(+), 14 deletions(-) diff --git a/builtin/fsck.c b/builtin/fsck.c index d13a226c2e..8aeb8b17e2 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -92,6 +92,8 @@ static int objerror(struct object *obj, const char *err) static int fsck_error_func(struct fsck_options *o UNUSED, const struct object_id *oid, enum object_type object_type, + const char *ref_checkee UNUSED, + const char *sub_ref_checkee 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 4767f1a97e..b5f9e108e5 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -20,6 +20,8 @@ static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; static int mktag_fsck_error_func(struct fsck_options *o UNUSED, const struct object_id *oid UNUSED, enum object_type object_type UNUSED, + const char *ref_checkee UNUSED, + const char *sub_ref_checkee 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 3f32441492..7fceecdfae 100644 --- a/fsck.c +++ b/fsck.c @@ -226,12 +226,19 @@ static int object_on_skiplist(struct fsck_options *opts, return opts && oid && oidset_contains(&opts->skip_oids, oid); } -__attribute__((format (printf, 5, 6))) -static int report(struct fsck_options *options, - const struct object_id *oid, enum object_type object_type, - enum fsck_msg_id msg_id, const char *fmt, ...) +/* + * 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. + */ +static int fsck_vreport(struct fsck_options *options, + const struct object_id *oid, + enum object_type object_type, + const char *ref_checkee, + const char *sub_ref_checkee, + enum fsck_msg_id msg_id, const char *fmt, va_list ap) { - va_list ap; + va_list ap_copy; struct strbuf sb = STRBUF_INIT; enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options); int result; @@ -250,9 +257,10 @@ static int report(struct fsck_options *options, prepare_msg_ids(); strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased); - va_start(ap, fmt); - strbuf_vaddf(&sb, fmt, ap); + va_copy(ap_copy, ap); + strbuf_vaddf(&sb, fmt, ap_copy); result = options->error_func(options, oid, object_type, + ref_checkee, sub_ref_checkee, msg_type, msg_id, sb.buf); strbuf_release(&sb); va_end(ap); @@ -260,6 +268,37 @@ static int report(struct fsck_options *options, return result; } +__attribute__((format (printf, 5, 6))) +static int report(struct fsck_options *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 = fsck_vreport(options, oid, object_type, NULL, NULL, + msg_id, fmt, ap); + va_end(ap); + + return result; +} + +int fsck_refs_report(struct fsck_options *options, + const struct object_id *oid, + const char *ref_checkee, + const char *sub_ref_checkee, + enum fsck_msg_id msg_id, const char *fmt, ...) +{ + va_list ap; + int result; + va_start(ap, fmt); + result = fsck_vreport(options, oid, OBJ_NONE, ref_checkee, sub_ref_checkee, + msg_id, fmt, ap); + va_end(ap); + return result; +} + void fsck_enable_object_names(struct fsck_options *options) { if (!options->object_names) @@ -1203,6 +1242,8 @@ int fsck_buffer(const struct object_id *oid, enum object_type type, int fsck_error_function(struct fsck_options *o, const struct object_id *oid, enum object_type object_type UNUSED, + const char *ref_checkee UNUSED, + const char *sub_ref_checkee UNUSED, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id UNUSED, const char *message) @@ -1306,6 +1347,8 @@ int git_fsck_config(const char *var, const char *value, int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o, const struct object_id *oid, enum object_type object_type, + const char *ref_checkee, + const char *sub_ref_checkee, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message) @@ -1314,5 +1357,6 @@ int fsck_error_cb_print_missing_gitmodules(struct fsck_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(o, oid, object_type, ref_checkee, + sub_ref_checkee, msg_type, msg_id, message); } diff --git a/fsck.h b/fsck.h index bcfb2e34cd..61ca38afd6 100644 --- a/fsck.h +++ b/fsck.h @@ -114,19 +114,25 @@ 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_options *options); -/* callback for fsck_object, type is FSCK_ERROR or FSCK_WARN */ +/* + * callback function for reporting errors when checking either objects or refs + */ typedef int (*fsck_error)(struct fsck_options *o, const struct object_id *oid, enum object_type object_type, + const char *ref_checkee, const char *sub_ref_checkee, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); int fsck_error_function(struct fsck_options *o, const struct object_id *oid, enum object_type object_type, + const char *ref_checkee, const char *sub_ref_checkee, 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, const struct object_id *oid, enum object_type object_type, + const char *ref_checkee, + const char *sub_ref_checkee, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); @@ -209,6 +215,17 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer, */ int fsck_finish(struct fsck_options *options); +/* + * Report an error or warning for refs. + */ +__attribute__((format (printf, 6, 7))) +int fsck_refs_report(struct fsck_options *options, + const struct object_id *oid, + const char *ref_checkee, + const char *sub_ref_checkee, + 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 065103be3e..bc63b80c48 100644 --- a/object-file.c +++ b/object-file.c @@ -2470,11 +2470,13 @@ int repo_has_object_file(struct repository *r, * give more context. */ static int hash_format_check_report(struct fsck_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) + const struct object_id *oid UNUSED, + enum object_type object_type UNUSED, + const char *ref_checkee UNUSED, + const char *sub_ref_checkee 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 Sun Jul 14 12:31:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732699 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 CBE0C49635 for ; Sun, 14 Jul 2024 12:31:22 +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=1720960284; cv=none; b=spZ25nf4AaxljB6QZzo6KZZJ4G+++mCHFz4RM9K+FCDBxZ5GRYbGAEyfdjk0sXwG/TbgPUAxka5MseYyNJ9zmf5P+Vv6++x+3aNTueGDZoIKzwcdOlsdmTOsqKnNzW8U3kHt+hv3GuksYY2hKZPB7JlHOAqzou02sdje1gr4pO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960284; c=relaxed/simple; bh=MxwAzjlqVNuoxZlxmFEYKA8A5mjus1wveytMqEV4ywY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=H+skhg76aiS9vIlquiCpQNM241574ULNX1rXrrLdsKzyVQN4BDy2MRdAkCWi7UbDdxIpOpyj509CJ9NcyBs2yCAjFidcGZsKnrBybT+yxguH3CPO+BjAz1z6UneULt9MVk976xE2Qux70hPfsbki8TXnEcTkthmhiRGEqGkuKTQ= 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=KOM5GhNL; 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="KOM5GhNL" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-6e7e23b42c3so1935843a12.1 for ; Sun, 14 Jul 2024 05:31:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960281; x=1721565081; 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=zhg4RNNdhtPm4o7yGzVsRbA9V5BekoEg8E1GhQOvOcA=; b=KOM5GhNL397/ln4uqnjCLZbsi67PUgYtHmGRS+fLaGkHBnxBBIVKWpQKUiVhSPX4wj Uc82wv/urg/R3gjwI2QkuFlDC0M7atc8RmY2Ixkw1wkMEq4afZBbhQ28itfFvNrgYVLj bD93LLG70gddJlt1hWpDqpVmBj87pWOakQNQZR0kbm+APqV6ohcOJJKXfUJc0qeorhZp YyEq0XGLmxEqSgiSiJht3yroA6khYh/cj5wfsGX2J139RM+J3THe0JRaYxMuedc6dlVu 96MbTZhmXIO1z6XcwjngJGnN563Y2D/2/qsQ6eYIwQuSYroK8JpB/qXLwngcevAoCdah uDqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960281; x=1721565081; 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=zhg4RNNdhtPm4o7yGzVsRbA9V5BekoEg8E1GhQOvOcA=; b=QGIrC618m3GfnJDmxmTpBXoi48aX2BDujMo1/vs+bcAso+crbebFlXSVyujPM9BVJ+ RN1b8fFKdKNVvEbFUPOAEQNv9FRWCn5wNnUS9m3bb87TbhlKfBuKqYF8LiARONhHL1zc rD3zMEaoQCUE5KYHaARHK1PweAU0Z9MdqiGjlzXDXnlwveNEknfkFFOxYmcLt1b5xHeP 77SAlvs7A/eE+k1KuFOs+zmXwHRokyXtP3cs6vW3YUUKGQvWpCLIzYvKlVOAP+Sw7bFT RQ0kiS7l74tkRI6py2V0FdrPvNRRLC/huh/FVeWTbCGGLfrorBEJmV6reTWA818duq9F UlMA== X-Gm-Message-State: AOJu0YxbOP+i0GcVx2BAxKLTjVnAklhrXLLoTGuEpp590iM6wFdGkK34 xsHcgw0zm0CY2vyUzTJlKR/r6qTOe8ajdgSGx86yuvoQT8ZzlqPwgMpGbw== X-Google-Smtp-Source: AGHT+IGDqcbgA8YVT3HNNLmvQ93KpZ0ziIP+cyBPQCR5uRMj5wpY2eIQZbdPgIM3R3+SQHoNGFD8tQ== X-Received: by 2002:a05:6a21:9997:b0:1c2:905c:dc2 with SMTP id adf61e73a8af0-1c29822dabemr18208710637.15.1720960281266; Sun, 14 Jul 2024 05:31:21 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0bb6fd90sm23073015ad.59.2024.07.14.05.31.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:31:20 -0700 (PDT) Date: Sun, 14 Jul 2024 20:31:25 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 03/10] fsck: rename objects-related fsck error functions 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 names of objects-related fsck error functions are general. It's OK when there is only object database check. However, we have introduced refs database check report function. To avoid ambiguity, rename object-related fsck error functions to explicitly indicate these functions are used to report objects-related messages. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- builtin/fsck.c | 18 +++++++++--------- fsck.c | 37 +++++++++++++++++++------------------ fsck.h | 32 ++++++++++++++++---------------- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/builtin/fsck.c b/builtin/fsck.c index 8aeb8b17e2..d22488c5d0 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -89,14 +89,14 @@ static int objerror(struct object *obj, const char *err) return -1; } -static int fsck_error_func(struct fsck_options *o UNUSED, - const struct object_id *oid, - enum object_type object_type, - const char *ref_checkee UNUSED, - const char *sub_ref_checkee UNUSED, - enum fsck_msg_type msg_type, - enum fsck_msg_id msg_id UNUSED, - const char *message) +static int fsck_objects_error_func(struct fsck_options *o UNUSED, + const struct object_id *oid, + enum object_type object_type, + const char *ref_checkee UNUSED, + const char *sub_ref_checkee UNUSED, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id UNUSED, + const char *message) { switch (msg_type) { case FSCK_WARN: @@ -940,7 +940,7 @@ 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.error_func = fsck_objects_error_func; if (check_strict) fsck_obj_options.strict = 1; diff --git a/fsck.c b/fsck.c index 7fceecdfae..d66ea4ef44 100644 --- a/fsck.c +++ b/fsck.c @@ -1239,14 +1239,14 @@ int fsck_buffer(const struct object_id *oid, enum object_type type, type); } -int fsck_error_function(struct fsck_options *o, - const struct object_id *oid, - enum object_type object_type UNUSED, - const char *ref_checkee UNUSED, - const char *sub_ref_checkee UNUSED, - enum fsck_msg_type msg_type, - enum fsck_msg_id msg_id UNUSED, - const char *message) +int fsck_objects_error_function(struct fsck_options *o, + const struct object_id *oid, + enum object_type object_type UNUSED, + const char *ref_checkee UNUSED, + const char *sub_ref_checkee 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); @@ -1344,19 +1344,20 @@ 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, - const struct object_id *oid, - enum object_type object_type, - const char *ref_checkee, - const char *sub_ref_checkee, - enum fsck_msg_type msg_type, - enum fsck_msg_id msg_id, - const char *message) +int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o, + const struct object_id *oid, + enum object_type object_type, + const char *ref_checkee, + const char *sub_ref_checkee, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id, + const char *message) { if (msg_id == FSCK_MSG_GITMODULES_MISSING) { puts(oid_to_hex(oid)); return 0; } - return fsck_error_function(o, oid, object_type, ref_checkee, - sub_ref_checkee, msg_type, msg_id, message); + return fsck_objects_error_function(o, oid, object_type, ref_checkee, + sub_ref_checkee, msg_type, msg_id, + message); } diff --git a/fsck.h b/fsck.h index 61ca38afd6..0b40d9ec28 100644 --- a/fsck.h +++ b/fsck.h @@ -123,19 +123,19 @@ typedef int (*fsck_error)(struct fsck_options *o, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); -int fsck_error_function(struct fsck_options *o, - const struct object_id *oid, enum object_type object_type, - const char *ref_checkee, const char *sub_ref_checkee, - 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, - const struct object_id *oid, - enum object_type object_type, - const char *ref_checkee, - const char *sub_ref_checkee, - enum fsck_msg_type msg_type, - enum fsck_msg_id msg_id, - const char *message); +int fsck_objects_error_function(struct fsck_options *o, + const struct object_id *oid, enum object_type object_type, + const char *ref_checkee, const char *sub_ref_checkee, + enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, + const char *message); +int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o, + const struct object_id *oid, + enum object_type object_type, + const char *ref_checkee, + const char *sub_ref_checkee, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id, + const char *message); struct fsck_options { fsck_walk_func walk; @@ -156,7 +156,7 @@ struct fsck_options { .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ .gitattributes_done = OIDSET_INIT, \ - .error_func = fsck_error_function \ + .error_func = fsck_objects_error_function \ } #define FSCK_OPTIONS_STRICT { \ .strict = 1, \ @@ -164,7 +164,7 @@ struct fsck_options { .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ .gitattributes_done = OIDSET_INIT, \ - .error_func = fsck_error_function, \ + .error_func = fsck_objects_error_function, \ } #define FSCK_OPTIONS_MISSING_GITMODULES { \ .strict = 1, \ @@ -172,7 +172,7 @@ struct fsck_options { .gitmodules_done = OIDSET_INIT, \ .gitattributes_found = OIDSET_INIT, \ .gitattributes_done = OIDSET_INIT, \ - .error_func = fsck_error_cb_print_missing_gitmodules, \ + .error_func = fsck_objects_error_cb_print_missing_gitmodules, \ } /* descend in all linked child objects From patchwork Sun Jul 14 12:31:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732700 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 072B21FDD for ; Sun, 14 Jul 2024 12:31:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960294; cv=none; b=nuRcQvvf/BDdNogYgkfNw5wamQgUv92AxHAnvWHPzff2TRnj2I88pGzR1KwD7JmbAoL7ou28syPj7CQPrF+dW9zBLAJYzpxQChBFPO7troDzndebraI/u6dDOs2EoCNIid7DjkkCfWQMYgYjjAVrpJahuP/yf2lhpcYWs8bK0ZQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960294; c=relaxed/simple; bh=eIWq4uYDutHwHrFgl+3hACJFVYTFmRT7RgAU63MYuZE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WN9LjRo7TzVVaPXSrWlLavso6ojDO+/FycxPOBig7xDVvA7PffE2Z8Ork96JrB2i/FrUy4NDLAQJZAeYLA43SKJZeC7nVaEf0RPMjJoefYYLFpDk34xiEP1CbgHO+XFqg1GzmiACWQdkpV2KLc1hhSEnoGYs5XE+i3OVcWrExiw= 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=cA2s75nF; arc=none smtp.client-ip=209.85.210.180 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="cA2s75nF" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-70b03ffbb3aso2760881b3a.0 for ; Sun, 14 Jul 2024 05:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960292; x=1721565092; 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=Lfu5IeCtwZoudyufnZa16NZFu/pUa0f5LKBXqywK6C4=; b=cA2s75nFX1KYo29vlWgkDaEsJ3Wbz7Raqua8UZ2gXk2jZbs4NvIfoNpc3eqsQZaT4x 8YmI+FO+lAbmiwtILIfHhn9GPkPC0vER9+doNySjIxOeqNMZTiobQBIkBaNlyvBcPyx5 DzEV9C3FqlJWAuVkjOshqgh/6u/cFVE92emorLrC8G8vFp84NZ2BvbYlzhJo+hccd5Kv eUqt/vOxhkTqbyAyhTH4AYzDy42Wsz6su0yYtVO2PfVAl8PxWSCrO5Z/ncDXy5jWSV4Z FxBpawYURvawwsg53mxQ/h848q3/+feR52w2PFD6fYNIR2HALl8RBsXGNFjLakoDAqTL iNMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960292; x=1721565092; 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=Lfu5IeCtwZoudyufnZa16NZFu/pUa0f5LKBXqywK6C4=; b=qJhQ7keleLgxvb+UrlK21qZWeskcbhDLLxtVXQ2P6NQCKRAUElHtVLCRY0axZxySKG Z9KrSayfw7DtbDDJqMP90Uw6yT1/q74VFFn3UshSxEoSsLgDxZdnOUSx4cDAugi5OQjP h3hqdPJ7gKa42nXZkDHBbHPMx3D5cBZQMjdDrOlFzIcI+ec2wqZibJDCS1lMNNU0uIWP oDI0iNiz5t6SKjMZa5KOffF1g8cufKEoIIjX8Ph7aA6fKqWvayeRVhgMRcPy9PbDRG6C xSmetaT/0/vLgJp4KZnoKYpE94Zk4Wy92lXD4dTN2JLi9hFOZ2sFDzoHlvWtBnv5VRA4 p+zg== X-Gm-Message-State: AOJu0YycKu7P3dd19N1mpZxYGjojNcRubb5G5YK1AffS/tKr8W0+csE7 MQ9fblf6+koXWi/UtfvtNzMnNLkoKodQQltEZ12ggqWg7wFEwVs5vukp6A== X-Google-Smtp-Source: AGHT+IExjdsKspg+tc4xoh637+htDzqhX7t0JMiirae2EERi6gipYH7mvnTn542PO9jU0iALy4tJjg== X-Received: by 2002:a05:6a00:21cd:b0:706:6af8:e08a with SMTP id d2e1a72fcca58-70b434f3493mr21093541b3a.2.1720960291734; Sun, 14 Jul 2024 05:31:31 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b81972f56sm2282876b3a.84.2024.07.14.05.31.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:31:31 -0700 (PDT) Date: Sun, 14 Jul 2024 20:31:36 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 04/10] fsck: add refs-related error report function 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 refs-specific "error_func" callback "fsck_refs_error_function" which could provide the following report messages. 1. "ref_checkee": "fsck error name": "user message". 2. "ref_checkee.sub_ref_checkee": "fsck error name": "user message". 3. "ref_checkee -> (oid hex)": "fsck error name": "user message". 4. "ref_checkee.sub_ref_checkee -> (oid hex)": "fsck error name": "user message". "fsck_refs_error_function" uses the "ref_checkee" and "sub_ref_checkee" parameters to indicate the information of the checked refs. For loose ref and reflog, it only uses the "ref_checkee" parameter. For packed refs and reftable refs, when checking the consistency of the file itself, it still only uses "ref_checkee" parameter. However, when checking the consistency of the ref or reflog contained in the file, it will use "sub_ref_checkee" parameter to indicate that we are not checking the file but the incorporated ref or reflog. "fsck_refs_error_function" will use the "oid" parameter if the caller passes the oid, it will use "oid_to_hex" to get the corresponding hex value to report to the caller. Last, add "FSCK_REFS_OPTIONS_DEFAULT" and "FSCK_REFS_OPTIONS_STRICT" macros to create refs options easily. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- fsck.c | 29 +++++++++++++++++++++++++++++ fsck.h | 15 +++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/fsck.c b/fsck.c index d66ea4ef44..4d18f20b09 100644 --- a/fsck.c +++ b/fsck.c @@ -1256,6 +1256,35 @@ int fsck_objects_error_function(struct fsck_options *o, return 1; } +int fsck_refs_error_function(struct fsck_options *options UNUSED, + const struct object_id *oid, + enum object_type object_type UNUSED, + const char *ref_checkee, + const char *sub_ref_checkee, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id UNUSED, + const char *message) +{ + struct strbuf sb = STRBUF_INIT; + int ret = 0; + + if (sub_ref_checkee) + strbuf_addf(&sb, "%s.%s", ref_checkee, sub_ref_checkee); + else + strbuf_addstr(&sb, ref_checkee); + + if (oid) + strbuf_addf(&sb, " -> (%s)", oid_to_hex(oid)); + + if (msg_type == FSCK_WARN) + warning("%s: %s", sb.buf, message); + else + ret = error("%s: %s", sb.buf, message); + + strbuf_release(&sb); + return ret; +} + 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) diff --git a/fsck.h b/fsck.h index 0b40d9ec28..7ae640ac6c 100644 --- a/fsck.h +++ b/fsck.h @@ -136,6 +136,14 @@ int fsck_objects_error_cb_print_missing_gitmodules(struct fsck_options *o, enum fsck_msg_type msg_type, enum fsck_msg_id msg_id, const char *message); +int fsck_refs_error_function(struct fsck_options *options, + const struct object_id *oid, + enum object_type object_type, + const char *ref_checkee, + const char *sub_ref_checkee, + enum fsck_msg_type msg_type, + enum fsck_msg_id msg_id, + const char *message); struct fsck_options { fsck_walk_func walk; @@ -174,6 +182,13 @@ struct fsck_options { .gitattributes_done = OIDSET_INIT, \ .error_func = fsck_objects_error_cb_print_missing_gitmodules, \ } +#define FSCK_REFS_OPTIONS_DEFAULT { \ + .error_func = fsck_refs_error_function, \ +} +#define FSCK_REFS_OPTIONS_STRICT { \ + .strict = 1, \ + .error_func = fsck_refs_error_function, \ +} /* descend in all linked child objects * the return value is: From patchwork Sun Jul 14 12:31:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732701 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 089F41FDD for ; Sun, 14 Jul 2024 12:31:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960305; cv=none; b=WwzRGtE8dOQA4IFhaVgMt1Exa4TnAIl61shMJhhL1PxyQXrywb+/3OxiXGEiNzG+GXAO2/goS/BiZZeNjUHLGG16hDVj6rRw9E6khuHYobgj7TwlwIZE7dwOmY9vX9d6Dwy3A8CyF26pQUlrE50NDDKWbouBxiFbAAdqJyEmUi8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960305; c=relaxed/simple; bh=Xaf9riBzozmCKz1H0udrYwbdPauR82bQALqKOtzPJvU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lHIxf2/Iv7Cv/LoMvhvZegjvFRwHyL1NBgdGZChExjzsElu7dG7bL6Gk0eaPxGGhuplz5/iTLzFHjioON6FT2zKRNyiRXcXZ8M/Y977sf4Cs1tbc3+VxjI1ZstT1fsbV+XxCD4Y/ZpeM8d6R+8CZZ0wksBMIQIy94tsADaY51sw= 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=hAFTxNZu; arc=none smtp.client-ip=209.85.214.170 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="hAFTxNZu" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1fb8781ef1bso25486335ad.3 for ; Sun, 14 Jul 2024 05:31:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960303; x=1721565103; 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=laa4K3fMvS0I93jB3AnHiR4n7++3zAPc3WP2RAf/kko=; b=hAFTxNZu/W04NOEjNQ+hGePZN+t3KBmjO1hQ+O+6W5eE+8eK/NBcKieyY4iFEqloA0 Hv3sXyPcAPcxgQl6lw1u/QYUnCdBVEwatxW16TtI7SuN6fDnPIif6qVoPsIGoh1Xg6/c fhl6xKvO0aE8bEEf4AiD/wGTpycGhxygoZd6vP9OnFGd62JrNVm9b3CWzxYQb1n6mGjD FdaCyBOa6Ylw3iGn6LBiR4POKvPgCa4VTMkwPTUyjiogXUdr3GvsXIpSKacE6hPq4D+B H2vTS6Gz91JsCOIbaNMF75D4bP0jHORKZuHXZj+4P1vvzKJXLPi7ic9nB3rOz+e15brR dZlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960303; x=1721565103; 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=laa4K3fMvS0I93jB3AnHiR4n7++3zAPc3WP2RAf/kko=; b=ue1vKIyxJf4wtRB6s0+UTC0YuNI82OS/MU6DditQUbIMAdSS3Mgp6SxA2wemymGGO8 7iBXZnl8EDf8HWB+RBRk4ojRHFSo/gZ0hxgpHbNMYDGQFuJ/XSM06rgwpwjy8n9mh/ca 3vpU2GSaOLAkgX/w8Z2KczIAQErZLWTbVNXVW84ObyUWpBf7iSdh5x9waDy6XSlh7O9c TuUGfoGrCFu8nYCiym3yDYPTz1eeNo2TDsdW+hGd4ROVENwxMElP41cymDl9N1GYkUFt Olk/SeDj+CthYivTI+CCzYDMYdX4BGWaWZ5bD1vwfA9JyQ5+XWBQ/Nzx++i8rxjyYHiq fYkQ== X-Gm-Message-State: AOJu0Yzt2RoY3Bhbq1KCdbo4yfk+wPk2HMIHt09sxayNgs+3PfyKD3zE KJ5hG2UlpUYo1l3hDIHXww/KLQm7pChzNO7Y2MAmkCBL1tHnK7wShKmWEg== X-Google-Smtp-Source: AGHT+IFzUhKpp1eaNFGeCwBv/6jajAn52EsUHKJSmR20BQMEVLn/sS6YPcrgIo6B8khy7F9D4gNWxA== X-Received: by 2002:a17:902:ea0f:b0:1fb:5f82:6a53 with SMTP id d9443c01a7336-1fbb6ea44e0mr121339655ad.43.1720960302739; Sun, 14 Jul 2024 05:31:42 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0bc2712csm23095625ad.176.2024.07.14.05.31.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:31:42 -0700 (PDT) Date: Sun, 14 Jul 2024 20:31:47 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 05/10] 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. Last, implement placeholder functions for each ref backends. 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 bb90a18875..410919246b 100644 --- a/refs.c +++ b/refs.c @@ -318,6 +318,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_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 0ecba21b4a..804d6a7fce 100644 --- a/refs.h +++ b/refs.h @@ -4,6 +4,7 @@ #include "commit.h" #include "repository.h" +struct fsck_options; struct object_id; struct ref_store; struct strbuf; @@ -541,6 +542,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_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..45e2e784a0 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_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 aa52d9be7c..d89eeda8ef 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3408,6 +3408,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_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, @@ -3434,5 +3445,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 a0666407cd..5209b0b212 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -1735,6 +1735,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_options *o) +{ + return 0; +} + struct ref_storage_be refs_be_packed = { .name = "packed", .init = packed_ref_store_init, @@ -1762,4 +1768,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..a905e187cd 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_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 fbe74c239d..b5a1a526df 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -2303,6 +2303,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_options *o) +{ + return 0; +} + struct ref_storage_be refs_be_reftable = { .name = "reftable", .init = reftable_be_init, @@ -2330,4 +2336,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 Sun Jul 14 12:31:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732702 Received: from mail-oi1-f172.google.com (mail-oi1-f172.google.com [209.85.167.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 7EF4C1FDD for ; Sun, 14 Jul 2024 12:31:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960318; cv=none; b=S73aeOf1vtR9xjZ2cjteEx28mMARBjLTZtw7gkm7BxiEyVc44umJzPZJshMGV6wHbGHxHMefKvE82dYrR1F9UZdgBTmLbhm4hnzcztbr1OiTgDdsPPKCtWQgzqVWQo2DF0udSOdW+3Z95/qwlddPATN8j+F+6yzyFFA8uy4o18U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960318; c=relaxed/simple; bh=MvrTPTFCaPjB4z+T0TKXZJ6Vw3UwsNdtgG5CIawLIKE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mklpIY5lZ+/Be5wSIZAvx9I+h+XpPNXKtHvhBg0krQPu6fHOBByzFe1BYL4VpF6dSHH5cDNVQ5t8zcBMq9tlRNXFhaIF0eZYUIy2/EK8HQNZ1UbvLNwPasLbgbbWaDSTm2Kxt40UIhus7hUXAEUxIXbmjCV6yAJQvJewxnyVSKI= 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=UD4A9qAj; arc=none smtp.client-ip=209.85.167.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="UD4A9qAj" Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3d9c487b2b5so2082184b6e.3 for ; Sun, 14 Jul 2024 05:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960314; x=1721565114; 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=Mj8OCpVWbf9VjpMdoJtJCpUGm+FdBMREXJ+eS6y5kW0=; b=UD4A9qAjkLBifesQT5Y1Fy4awTvhxSAG155Q1czF1YctsF8BUAiv2F14Y90dFHYb/P r43r1RweUFthOJbd7c+NcRLPnNzkLIHYpmjYZ8/THm9Fx0R4r+u4Y8leHazv8Whl+b0A fADb9UCVntMNv4dYvaYTsd2RbV56Nor2WFUn+8nF/TWNNE43NLr/xo/mqcT2SK9pSufD iWOyvAPWfcdfFqfxZaDwvxgseoHauAZx53Sl356C6C1d8M2WXz4hF9uOKU6Jm84zj15q l37W8OaWm7VWg3T6IhAc2rnhdoj5pkCKYcCTuEpoIbG/hOKIlf91TFJJ7sfZj4MaR95E H80Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960314; x=1721565114; 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=Mj8OCpVWbf9VjpMdoJtJCpUGm+FdBMREXJ+eS6y5kW0=; b=wAZNg5A2FeuFH+is5VorR+QOL7bQ4PANAo5uAJMBwtM69U5QEzEg4cOWVdHHbCSmZ+ 1lF0rHS3U9BCiq0Gp3KWSRzyo5cIaP3fBQBhEldfiOsRRJYPeXSfza8aDaS86FjEAWyx lBAiqsmVJGb1+85f/Qnvroipn6pMVsedQ/Mhp2s2mSEWkMldpNMInbj5u1KBD+dYS5WQ M/6lHxyAoArxK78LwoNPfLcFG6Yv0Fu7Rss6e/aq8WqtXq0iyuXhHlJoKiLrte7FtrJW 6TWV98Syp4TC8BZKQuPlCTPnLcXjU/rPl1/hS6fCUU3sckM28DiAJQ95cB0GLNtuEgx+ 6a/Q== X-Gm-Message-State: AOJu0YxO9pGW1HdI2qYGW4+6piMVc+zmhTspit2B8oErXEjoCwMtwjUx Sj2B9wX9cdMaZHrKh5R7LlFzaLtziEgzyUSzFvkaLTiBd+oXur9aVIhA1w== X-Google-Smtp-Source: AGHT+IEpEutluAnapNHz4vIwOpBJMtnt6uHNIOCdZMs+geNxIyYj+lEy9PfIV1FIH0inLC1e9JkIrw== X-Received: by 2002:a05:6808:bcb:b0:3d9:384a:3674 with SMTP id 5614622812f47-3d93c05efa9mr20096247b6e.33.1720960314606; Sun, 14 Jul 2024 05:31:54 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0bc38cd8sm23027975ad.213.2024.07.14.05.31.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:31:54 -0700 (PDT) Date: Sun, 14 Jul 2024 20:31:59 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 06/10] git 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 and also this subcommand will be used as the entry point of checking refs for "git-fsck(1)". Last, add "verbose" field into "fsck_options" to indicate whether we should print verbose messages when checking refs and objects consistency. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- Documentation/git-refs.txt | 13 +++++++++++ builtin/refs.c | 44 ++++++++++++++++++++++++++++++++++++++ fsck.h | 1 + 3 files changed, 58 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..4831c9e28e 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,54 @@ 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_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(), + }; + int ret; + + argc = parse_options(argc, argv, prefix, options, verify_usage, 0); + if (argc) + usage(_("'git refs verify' takes no arguments")); + + if (verbose) + fsck_refs_options.verbose = 1; + if (strict) + fsck_refs_options.strict = 1; + + git_config(git_fsck_config, &fsck_refs_options); + prepare_repo_settings(the_repository); + + ret = refs_fsck(get_main_ref_store(the_repository), &fsck_refs_options); + + /* + * Explicitly free the allocated array and "skip_oids" set + */ + free(fsck_refs_options.msg_type); + oidset_clear(&fsck_refs_options.skip_oids); + return ret; +} + 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(), }; diff --git a/fsck.h b/fsck.h index 7ae640ac6c..6803696f68 100644 --- a/fsck.h +++ b/fsck.h @@ -149,6 +149,7 @@ struct fsck_options { fsck_walk_func walk; fsck_error error_func; unsigned strict:1; + unsigned verbose:1; enum fsck_msg_type *msg_type; struct oidset skip_oids; struct oidset gitmodules_found; From patchwork Sun Jul 14 12:32:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732703 Received: from mail-oa1-f48.google.com (mail-oa1-f48.google.com [209.85.160.48]) (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 CF7B61FDD for ; Sun, 14 Jul 2024 12:32:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960327; cv=none; b=UETIQpn+p7dTzsIDbLPA/X1LCL90oRjN76c+v+QxLur0VKY+j61twm2uT9xKuBQNxWllpKh70/HeZVRhgiMPM8zIClEBfgzEkI9Y7VJhfEgv9m9IFPQ9iF5Q8kJm378kHvh0HpTdTkfgXzQMCxemLnqijse2M5GPB2z4kCay/AQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960327; c=relaxed/simple; bh=ek9E8IClAA5Pug9rfdoQVLLDaCJUaD2cNG50fTVw0ok=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OAKWKzQ0xYlG49zepZJEL3QpOLDEempBY0kAcG09mZjzETHrXj8e+Vkz6N4Z6rdHqytLZr9hB99YCdkiooTcRvUtViNgr0KDyvNaooGnEOJ/kGKNbPkIZrlqVu5yYsjEher8L3/VufRKoMWO4VKfA/aCIRjTk02lJMFOxq6K/9M= 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=fq9MdpzQ; arc=none smtp.client-ip=209.85.160.48 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="fq9MdpzQ" Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-25e15406855so2144805fac.0 for ; Sun, 14 Jul 2024 05:32:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960324; x=1721565124; 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=uvuzcrY6ir+rp6T0QqF0tlZePMzXH1J6N3QzRgz8UW8=; b=fq9MdpzQzEDoiTXpSMXgVniEcFbW1BrGxf2Am5ap9OIKeS1PVXzS9QhnVNrohhoxHi M5n1igUCfcd5f/wMeHwRPM35FtxePpT/G4EsFPfm3eya/H9KuTiFuLoRUhKAkShzyqsa rj9D3HwM1sgRyWye8z4ANpR15nUwe/cRnWDA+XF7lcvY+L0569Hcm2N4XqJv7nGbvQVu U2uYQuUAQB5XxYrYHHlc1mmhqjYOmMjQzQJuAqS5atm8XJ6qTV+sGlZ/s2elaAT2GO8m rgC7Tw4DcS1IxWCTDdlisrRPsmz+WYLUF0Wc5btNF4LrqTLG92Ng8s92Il2nYKPxp6Cv Zq7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960324; x=1721565124; 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=uvuzcrY6ir+rp6T0QqF0tlZePMzXH1J6N3QzRgz8UW8=; b=WmBTzoQlBerjKKj9BGR3Om7kMFlLH/GhePKf8eAE3CQeOO4DNB7IEKLHLLPLcZavmA LH+lG24Cb16sPzmQFU+Q/hqcZZJhwKVQmJJ5sfVsTc6KcmfRJzFxcqfmSjgqshHp4ivI rbIK2TU49/jqbf/lylMQ3n9ePKAAbnW8rhGQewye1DcdNsswz52N/e20U2GvFHrizWEW vikrssr0T9rvq1HtSisCDFtG4lS8iT/wuaTIAKX5s7ZFQMMwMYC/Vo5yIW2CHtMSNmyv uW6ZcKlxF/pKWUgD81QFwlZHJXgcrgA0k26kZ4ktG+tGZoV7nY+R0lcqiBHcjuEocBKH 9nzg== X-Gm-Message-State: AOJu0YxnqDXi8iioec2GY9B5ydrF+htwbrSKpZM6ohNucdVYa//PmHlc CghoyvdqVl9allE3jbdWEy2wuXhIpuwCtMQJ/uwMeWW1cTwzLtf4BqyEWQ== X-Google-Smtp-Source: AGHT+IEyrDzBp9W/WtAeKZmkqc5SbBNvKgz8aeElrFErjEFLmzA06G0jJ2AtVIXkfwdGOPeNFW1XqA== X-Received: by 2002:a05:6870:c112:b0:259:8b2f:8d76 with SMTP id 586e51a60fabf-25eae7efc0amr13624967fac.20.1720960324577; Sun, 14 Jul 2024 05:32:04 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b7ebd113csm2612527b3a.91.2024.07.14.05.32.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:32:04 -0700 (PDT) Date: Sun, 14 Jul 2024 20:32:09 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 07/10] 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 d22488c5d0..d3b466b84e 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -898,6 +898,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" @@ -1067,6 +1082,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 Sun Jul 14 12:32:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732704 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 D4B3E1FDD for ; Sun, 14 Jul 2024 12:32:21 +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=1720960343; cv=none; b=ZZY+T+QmuMht/+htPJ+RnJpNqBZELcD0qJpF58mMIN5G/gcELIX2DWtzD3PtpHEROj78macDxETB7Bj4rrqCG+PcNa/J3vRHCGAM/WDnMWG8vVCJhAsvIiu9EYOHUW00mchrW/i53DXqBjIvfoR6dr+KIp+14CmjLscLK7HO28g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960343; c=relaxed/simple; bh=74LojJWDbW9OcMNx9RyPKjMV5xMTrr3rTVDU9xheogw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lwKzcV/mqVtR/mZt0r2svK8n+Hlti82ctJidM0R3tQ3/R/kJUlrIxw/Pxj+mIIXX/wkRJaVC8I9k/S4FUS5XmeDBU4g4Ol1LN/CX/6ZmHR9nbNQ4oTzyOZbfuqusgQvOCChXqEMEJfFevqe6yadRQD/jEjW5aZQeUSpr15jEDIA= 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=SECrwK2m; 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="SECrwK2m" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70b0d0a7a56so2628295b3a.0 for ; Sun, 14 Jul 2024 05:32:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960341; x=1721565141; 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=OS2CkskgLXdpVTeUgcsIyFkMp5SUUYLcibIkpuXSC38=; b=SECrwK2mu+450IU1Y0vMrK7dPjmPuD1udDyrgyH5amTaaFuCU3ubXYwlLihxkngjH1 T+8pJmfoAWcFmjxEKvkr7g/iO+O4Lt2J4CbnFmUE4ugDaiI3StC9nbkocH6XlzARFWzi cj3DyM9alEvgbXKh8fox5IDmijmjt2IqsLChzMZ/AHbRbrHpXfeqR2gOJhNx+BueNQSg Y/sVkERqRB9fK/I8T+AXZYh2TG4Aze7yuhxmyvFTRP1bOKg+aShy15GiG9qW7SwgVtgr dSHRBvybPSYPL3iXyARZmlOyuQEV6krI/OuV+UwB/yhrQfwVODoUimuiM6IXnF1AubaW 5ZJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960341; x=1721565141; 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=OS2CkskgLXdpVTeUgcsIyFkMp5SUUYLcibIkpuXSC38=; b=AcDU24/n88XyyxfbJMraCho2ohR/x1zQhbpJ7BSbgwlCItW9AXHkIlpfLA04c5aUwn JKzPkVyNGS5yyc4ki0Bk8y6pmjLLPmBn57mSd75VU4k/8V4hm+T/rZpKEGdpwgw8GIB5 J0IPE+aVgZdGCIdFAMCbFXKZ6OyW1njzRIhrdMrBBgvE8T99rWeXaC+LPQJ6HlHZfYwS QacFXy24QqT39OFPJwyOH+KkjizxqJFNaxpZUdJgsEGNICP5ToLI83MqHWk4K2lgGDJ7 nF0UvmzapDu5EectyBkyeQ+Ul4Q4YsMedhTP4m+PSbkMqMk2bWRMs0MbrIVvTaYOXZIF e6wA== X-Gm-Message-State: AOJu0YxBia9UiHLVvGzGRQXhAJ3pBsbi5FIUK4ertQc0o0NJvNMF9fz5 V+jaioTY6hbGWu1qGiV9CfsO1Bvk3N4YOqgYut1Dk7QXfc5AUKAhep2CJw== X-Google-Smtp-Source: AGHT+IH4lbuBVsbV7S3AKZPZjY+ByqRS+mXH+kEpICS24LA8PTPdH6GvQgZlOQEIKPKZHbUcHHuO3g== X-Received: by 2002:a05:6a21:2d04:b0:1c2:a55d:88bd with SMTP id adf61e73a8af0-1c2a55d8b5cmr13753133637.53.1720960340671; Sun, 14 Jul 2024 05:32:20 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0ee8ad03sm22228815ad.246.2024.07.14.05.32.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:32:20 -0700 (PDT) Date: Sun, 14 Jul 2024 20:32:24 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 08/10] 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 d89eeda8ef..794e9f3f2e 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -6,6 +6,7 @@ #include "../gettext.h" #include "../hash.h" #include "../hex.h" +#include "../fsck.h" #include "../refs.h" #include "refs-internal.h" #include "ref-cache.h" @@ -3408,6 +3409,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_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_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->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_options *o) +{ + int ret; + files_fsck_refs_fn fsck_refs_fns[]= { + NULL + }; + + if (o->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_options *o) { @@ -3415,7 +3488,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 Sun Jul 14 12:32:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732705 Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) (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 80FA61FDD for ; Sun, 14 Jul 2024 12:32:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960353; cv=none; b=UYXnoH91wcn+a92HCvBGEZV30ASJesrKY8U5U5OLdf2bKSnB84KjzehU+uOONjm+zLvQC2LR49RhTj4WAOBFMNtGIN7hX9UABog21RoTArOOiYSKLHCxzFThYnb8roQyPDON/jteSBUqJoQCLFLiGw7ZunLZtxD228Xo+z1DcQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960353; c=relaxed/simple; bh=wsqsZEHpPQs92DiNkHtKNCMDA7I0W7O92N1x4tIgW2E=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XRmN7H+Q+7uInDFmM3zfFvjVoWIcEp4wjerbB7zqdu4dfKWFJFIjCzMp0zDehyxVzcb/iP+DALoEzc8+Npf2JKMVTc9MvwBeXpVOKajwcpBzboqp3VLTaGaDJ2WKhyDeamXj1EDl90dJhiuWYShtBTZ7ANBziRILb3Fgea7TcKg= 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=bJ5boa/T; arc=none smtp.client-ip=209.85.167.171 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="bJ5boa/T" Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3d94293f12fso2023077b6e.3 for ; Sun, 14 Jul 2024 05:32:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960350; x=1721565150; 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=+KX53qJYVd5hhbCXfYhdrcexu+ISyYIJvPzHHP2rzpA=; b=bJ5boa/THtH3eYC1XRp5odiIEdX46XgK/7pF4yiF3UknBKjhqEiwFBBWiLVBJ4jkVK 7yItC7dNpgjSjpIBZgZSCeSmyKL1SBF9wb9xHQfSTs2o+0SnbxKMHvWlTF3sm1KRMo7U pGuneQOm0s6ffdIMOjL/Ozsqx3y9IaLaBOltN1Cnt/WDj3mADa95UwL/e0eNnO/T4iu4 XRYblORMw4OuJJ+edeoj4RXH7PRqe6KLN+3QVrG0XbB9cMRkah2HDRy0ckKnpdAYAqN+ tKVFSUF0swDGFFunQZrIUsMtmwjfhqL94BLKaKTTnN6Wxmg1FcSIlNryn7DCKglQuds0 e48g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960350; x=1721565150; 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=+KX53qJYVd5hhbCXfYhdrcexu+ISyYIJvPzHHP2rzpA=; b=pd8K2hvM9Slq8mzEdjFZ6Vc2Z7mjf55YqAlrTdRjYHFiPdW85wx8e5F9AxSsy/sFb5 gYoff2pp5IRmuOdfpuhTvF8k9AvIp34fBXau33XMUONQxAsRD4SoE5z8KZSuoa1Hc899 xUIqN9eLbfz6zCz6ybplPg3/YKni4BTLlf6fbAL+relJCoRVrTIeuVLlMGjHdF2nZRTw WfLOxhri9k4U4X1PZ+KuzlAk9EneV1E4DgPOZiQKKFjbNyqMWTLG1Uz1M9cyFQjctDxc xR9t0uJs89clJSjp9IYCytJUnQAsLBby0ODnd1B1LrZgSW9y89K+7ZtqrhGp1BucSfC/ 6DBg== X-Gm-Message-State: AOJu0YykHw1WMTeuEhv6TYECiOJ8BvmRSjwgf+989JtSIGEOOy9b1Gvd zhbbrJIhjtk3a/6xAEmSkW2vAMXmKG6r9RajqZXwV4zNXruDHIStwNvJsA== X-Google-Smtp-Source: AGHT+IHhQn9TncRWCerqSEcEmfTXhJFpX9Sjcz5qRP+Gi+n+N2rcTD9EH857/cEr109rhh1spklVrw== X-Received: by 2002:a05:6808:995:b0:3da:ae19:ef0 with SMTP id 5614622812f47-3daae1912e9mr6511688b6e.49.1720960350268; Sun, 14 Jul 2024 05:32:30 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b7ec7d2cfsm2524729b3a.100.2024.07.14.05.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:32:29 -0700 (PDT) Date: Sun, 14 Jul 2024 20:32:34 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 09/10] 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 6803696f68..2a2441e147 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 794e9f3f2e..d20e149214 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3419,6 +3419,25 @@ typedef int (*files_fsck_refs_fn)(struct fsck_options *o, const char *refs_check_dir, struct dir_iterator *iter); +static int files_fsck_refs_name(struct fsck_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, NULL, + 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_options *o, const char *refs_check_dir, @@ -3470,6 +3489,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 Sun Jul 14 12:32:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13732706 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 B0CC41FDD for ; Sun, 14 Jul 2024 12:32:41 +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=1720960363; cv=none; b=GSGPO4pprht9T0/BuPoERWHLXtkpruZ78lzv1kRxlvoDmXdSfFNPVNuQZFvDJKKix3SKxW3ENqmx7i5E5Ta2z90Bls537BslFkSedkAJdtKqPqEAwT4ah2c8tLP8G5eZ8CBeE1D1uemVUN2uT3DUixB/gU/IivPE+WDSWrMjI7w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720960363; c=relaxed/simple; bh=0hi3YqelWSb0d+8YkYQcLVR8wMKbq6TOlT1x8/bsdtc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=faO5cmMmdRfDiUKujj+dDe7+C5gPOqiLUm7QrK7/vs1IAyVgP2estd/8vcM9mGWEFf5+H36cikhPLohBSvepUgTfGqVMGq6xyHSoT4C2lwr+aHx6nVzOMD9P4uCkVWaPLhXOyJiC9gQp7PBioodDc9RDcv3p6Ff3UKW2LTa8ZkE= 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=YZ/9QKAL; 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="YZ/9QKAL" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1fb3cf78fcaso23830275ad.1 for ; Sun, 14 Jul 2024 05:32:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720960360; x=1721565160; 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=2WNNZ+Xn73vAD0rgi99OqnjlURsq9GOq4YBmHNkCkZ0=; b=YZ/9QKALY5R/gZCxF4YlkakRYqV/FeWALJks4O+tfOw1dPsBv5qkKvesO01cs83CdO oMEIgw1k2WAxfZxtlXyLWI0a2HFjd7AZWAe1/OuwvMRVS5Ekw6OjeXijFXQ/8k9FfTuW QMn3HEASVJCyRyJDErWkjRJDM1XLwPd+GKyhPORosF52ocJpzvU9pUBzgOJ6mn338a6h j21lR3fEDND0nov2TGbAkIR34tkj1Mr135A2V5HM7pvmceHkCYvUZ1R90IrZtoffWUKW rpFR5iuxx62Kpbij1Twy5yKtm4TWuX+tBKeDprPpqONY8Y//RVV8Ruu08JSgV4Wui5xy /ccg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720960360; x=1721565160; 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=2WNNZ+Xn73vAD0rgi99OqnjlURsq9GOq4YBmHNkCkZ0=; b=b4x83dgvcW+2fNaNTcQM/H05Pqdrztvu9/rHA3pMhKIt9F9tvdjzjpTePcbWY4qWLb tjVJtT3j22dxTDlD/438SYH0Icr7QIYMGAj4zCoh44YwRM7TSylZNLWSk+pHT59z7DQv T6GcSSOpI7q5qh2y59MXSdHEoaaU623BPHFhGoj2ZtYPn1OuLlZxum4yrWhkmYeG4XR/ GGuMXqIFcNRtgzQ3BqoV7nCgq0H1uDNUS2dYoq2TLMeCRjptZ71U8CkW54X637kYK8iQ k4/lK35htPnR658bpRZw/evPiEycYAzNEHHMUuA550D5oXTjl5cstpmiCdcSRyY++6Up vFBg== X-Gm-Message-State: AOJu0YzY2/SfZXEvs+m8ZwHoiDGC0V5UAjn5JqvXoNGSXM0kxv/FSgUG UBvkiKmB046vUvLnyVz6PQ0yzN7XXr//Wcj9dE/RBmxIBGGUR0A+hvYppA== X-Google-Smtp-Source: AGHT+IFrDCosTGt9S6lnD+hB9E7AduAZFKUsXhqx3mZ/B7zMtfK7K32MKbYMLGuo4mmuQeMCN8WYuQ== X-Received: by 2002:a17:902:ea07:b0:1fb:75b6:a43d with SMTP id d9443c01a7336-1fbb6d4c958mr169299935ad.32.1720960360426; Sun, 14 Jul 2024 05:32:40 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fc0bc46bfasm23067095ad.245.2024.07.14.05.32.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 05:32:39 -0700 (PDT) Date: Sun, 14 Jul 2024 20:32:45 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v11 10/10] 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 2a2441e147..e92a5844ae 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 410919246b..eb82fb7d4e 100644 --- a/refs.c +++ b/refs.c @@ -1760,7 +1760,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 d20e149214..42d2f676b9 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1,6 +1,7 @@ #define USE_THE_REPOSITORY_VARIABLE #include "../git-compat-util.h" +#include "../abspath.h" #include "../copy.h" #include "../environment.h" #include "../gettext.h" @@ -553,7 +554,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) @@ -589,7 +590,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)) { @@ -611,6 +612,10 @@ int parse_loose_ref_contents(const char *buf, struct object_id *oid, *failure_errno = EINVAL; return -1; } + + if (trailing) + *trailing = p; + return 0; } @@ -3438,6 +3443,141 @@ static int files_fsck_refs_name(struct fsck_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_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, NULL, + 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, NULL, + 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, NULL, + FSCK_MSG_BAD_SYMREF_POINTEE, + "point to invalid target"); + goto out; + } +out: + return ret; +} + +static int files_fsck_refs_content(struct fsck_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, NULL, + 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, NULL, + 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, NULL, + 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_options *o, const char *refs_check_dir, @@ -3490,6 +3630,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 a905e187cd..2fabf41d14 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