From patchwork Sat Jul 20 09:29:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13737762 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.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 38B151B86FD for ; Sat, 20 Jul 2024 09:28:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721467734; cv=none; b=M3EJxajkr/bfpOyIedjNxSThtgWnvpBqNBQvoA11TEMn6EeIWhQ7DOuSthtD6XzwiY1E9HhM72mWtMNbtuP7eDF7gFrlUrNJlmLoEpHnuBSUpd/pQutQsia/jV4U6IRkzTG2cLSJKu0HVjg5TP44IipnA8WZ3UmJAo2tCIL6kr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721467734; c=relaxed/simple; bh=ItctcTAlm4Qeq0fEDyOsp2oQssNALOJYNHxWpwq6KmI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ctLIbjsWB2HKhydfyhjtMgbsBURB+Le58/vjpzdbmyKBzrClnmjS4nlnnnJWQq1IghH8EiEo3nPGboJAS2lRmzPDTOAICJiMfyd5PE//MW6csnmC9KJJ2QHfLpWkgoFcQoARjq9MV3A6Ke/mPVYBGhXedRkfbklVCmtKxyG10HM= 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=CnRUIMTl; arc=none smtp.client-ip=209.85.214.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="CnRUIMTl" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1fc56fd4de1so20360395ad.0 for ; Sat, 20 Jul 2024 02:28:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721467732; x=1722072532; 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=B5kHxxGKk1+5n5peuJEVysHe9LDxchpuRy3QHWJes0I=; b=CnRUIMTlqmI5XKFHcF5Qx8uZjMLzHx/kzqT0V/cngmK0t08gBqMg0b28sugkHNdNjD vmSRz4qXFp6obAiH0NmxayBobaIN13kDBr2l3dX7ZTnEhuoXukAgobLeVxx4v97Bj7b9 3ZsY43M23elpREV4tOxg1mZe9BQMWM1T7LYRxflzsItIGm8aEbH4wg2Zf2v7ZFEYsNHN XnaXFuQqRlCDOjySghdS/I9j8FzhNnFQjDSXaNTTCvIqTQeAJAbp5x+vZ6Qbq+Jw6J/d dCCTgI0LfowMq4xNFNsoHbFXhLpJTE0wM1+RJlgyrSKLSV/5MmWyWcRJW29x+qCCTlIR zDvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721467732; x=1722072532; 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=B5kHxxGKk1+5n5peuJEVysHe9LDxchpuRy3QHWJes0I=; b=FUliXSv4C7K1W5K5kj9k8WVe5vp4mEzNGVY4JmzOe7puL1+4yppY2Qmfln1G1Bwgg4 N9/+8AjtocpCAx6OZ+KKK17Q3N2N01zvsbmHrB0EvNTV9d5axqRU4WORRyk7EDsvr5tr odPI3hUZmnwe4C8YuPpFALxno1k2AaD+4jLHxUFzSM+JDan/FVySuJ3KK219rk4V8YJD H+1vsNwTYRg2QB4GMrQ5UmTMsFNsVnmbRD71GHMuhoE0d5mkHYXpV7lcy3jdd9/Zydje mRKHbtl+dhEAwKbvQr4r1DLCRCPW0E23ZoPMtvCBP8tymMuCFXEjnaE4gSWjHkS/P25f ORnQ== X-Gm-Message-State: AOJu0YzkYM8+yNfwPmVVkGkff4dK3Z+XCY1NHOJg7TnrUMVDfaoT/DSi Gl1bElPqhYZbLJ4xetx37Iha5tYciCPZQ0YMM2ukgDfRmsS06LBuDspbrA== X-Google-Smtp-Source: AGHT+IGiT8qvmX+tN0RO/GpS5X779xnCgy6dOOqXJI+wOUz2goY3jlntW0mS0xRJ6sUXvVAWNsD7dg== X-Received: by 2002:a17:903:2b06:b0:1f9:a69d:4e05 with SMTP id d9443c01a7336-1fc5b5ccf67mr131492235ad.19.1721467731853; Sat, 20 Jul 2024 02:28:51 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fd6f290b0esm18251805ad.78.2024.07.20.02.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 02:28:51 -0700 (PDT) Date: Sat, 20 Jul 2024 17:29:02 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v12 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 | 21 +++++++ t/t0602-reffiles-fsck.sh | 101 ++++++++++++++++++++++++++++++++++ 4 files changed, 126 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 98e2225593..421ec62b8f 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..f93c44c4d1 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3419,6 +3419,26 @@ 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); + o->refs_info.ref_checkee = sb.buf; + ret = fsck_refs_report(o, 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 +3490,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