From patchwork Mon Jul 29 13:27:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13744952 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 9E2F514A62A for ; Mon, 29 Jul 2024 13:27:27 +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=1722259649; cv=none; b=CmCaOdaBNa/IwBh115WcPHWWld7O+MTjLyTlKABC5f/ZEvtB0YYpbAYpmAhPD5m2pfk8diyullEKkeKhH+HEeNwTeG/OQRhEV0ogh0ZQwanANnkNMHMd1jTF4IQls0HQKRRv2xH9XUKx2MGGwYOivOjjDh4vbLA7D+7Dz6uIKi4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722259649; c=relaxed/simple; bh=8RwBGEBIjAWJ5XZJa1Rxbs3RHzkteUTrsYqIsGr0VRg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DgyoKRlboySPL1i0SOrqyo5AglhccIK4werKUOOHA5/EMYcJp0oJsRuoRCI3LacxzOfql2E63ubLGhV4aK42x0Po7hA50RB6rr66De3izjPp5o/E84WTZklMO+FKhrgxdx6OLmbYpV5dlY0+vpq38djFSXihkaizeoEVIttUSY4= 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=cPJ3hkME; 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="cPJ3hkME" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-70d1c8d7d95so1894671b3a.2 for ; Mon, 29 Jul 2024 06:27:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722259646; x=1722864446; 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=vSh3qqL5fVqO1XY1IPTM3TeR42nes8tLSFfDSjMkCBo=; b=cPJ3hkMEpYOXN+jf9B8b20c3xXZuSIC0FHI1K6paMzc1SHE+SvEl6zzSvy8PbJMet8 ivY+6VxRBZvpIlDK6G9MNbeXluIt0PA/8v4tmGeWfaFcjMqpPRhZYqtNYxfz41tJ/9BE 2FUT9Hv98r2Ac5FM6XSdMGza35bnRkj0vcR1Q6BsEDtpw8DYlPX1CmaxereM1zMeI++r 08gCs6Ps3nm2C0edfll9S76z/rOGvLRobYaRi1h+ctJ1zB7SonDeWQK/xZ+d3S2frveP 0h3eWXYF4/PdO7o/HWOiZATUMS/j8z7tD5cPvRw/JbUXr0s28nmW5hpvY9CiaHBxzKyt /2hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722259646; x=1722864446; 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=vSh3qqL5fVqO1XY1IPTM3TeR42nes8tLSFfDSjMkCBo=; b=KYL1sGkPsLitwV9ifmm+qS5v3MTrLzCOwHAJ4rrlcdgZG7/KRZ6fvEgMS972POn5WA e3AAPzU5nyeW3jUkRofUGctSEfpc+j5ukqjgKqHhOZ453c82+sPJHaMcBKtv4gNBs8eT kDcedngRX386RuTYSfVdZybWWJAmKiclBiu9Y5v0z+4Z+wpnFlokeZDhKM0f4AF+m7FB INXpKeJ2uS+FcgE/aaaT7HokJYsh4dafOYVqNTFk8ZdK83c/C+K62qT52Z6M+tBlavT8 TsXRzDJqxxG6ySGhLGdg37OIVUjpN+TV9H2+szxCIhRaL6O0eRkCjMhlCo/lGc168wBD zGrQ== X-Gm-Message-State: AOJu0Ywz5ReQZGTHBBmtqnq4jku2JC398X0ARht1YQQYv34Yv2uVzqfW L2lbTfKZWE2UjHehEQp5U+RyBwyCpNj2fFev8FQxTDrjR9xJ2WUOnGVojg== X-Google-Smtp-Source: AGHT+IEgf0JAgMJluB65QZ34Tw6d/B+ho2LEHzlgPQ/0FFM5URM+kLcAUJ0Ftcsw+3Pnuy0su0e/yg== X-Received: by 2002:a05:6a20:7fa6:b0:1bd:288f:e8b4 with SMTP id adf61e73a8af0-1c4a117c459mr6371469637.7.1722259646044; Mon, 29 Jul 2024 06:27:26 -0700 (PDT) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead7115a1sm6804865b3a.46.2024.07.29.06.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 06:27:25 -0700 (PDT) Date: Mon, 29 Jul 2024 21:27:45 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano , Eric Sunshine , Justin Tobler Subject: [GSoC][PATCH v13 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 | 22 ++++++++ t/t0602-reffiles-fsck.sh | 101 ++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100755 t/t0602-reffiles-fsck.sh diff --git a/Documentation/fsck-msgids.txt b/Documentation/fsck-msgids.txt index f643585a34..d8e437a043 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 an invalid format. + `badTagName`:: (INFO) A tag has an invalid format. diff --git a/fsck.h b/fsck.h index b03dba442e..ce56ce4bef 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 cb184953c1..0d4fc27768 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3419,6 +3419,27 @@ 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; + struct fsck_refs_info info; + int ret = 0; + + if (check_refname_format(iter->basename, REFNAME_ALLOW_ONELEVEL)) { + strbuf_addf(&sb, "%s/%s", refs_check_dir, iter->relative_path); + info.path = sb.buf; + ret = fsck_refs_report(o, NULL, &info, + 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, @@ -3469,6 +3490,7 @@ static int files_fsck_refs(struct ref_store *ref_store, struct fsck_options *o) { 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