From patchwork Sun Nov 10 12:10:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shejialuo X-Patchwork-Id: 13869798 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 7ADFB1537C6 for ; Sun, 10 Nov 2024 12:10:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731240625; cv=none; b=HTaJJ1LO9H9eJLIrDASTAXhrFfmDcrXZziFUGfXTkqlpbUVw+IPXQqk9lpkHpDTk65eDz2F+Gix16CPNqIhgduLrjSvEVeXsdICgnoScm4mcg0bpSAraTiry1JpuM0dacKZlJn5LNb6p2TxDQydpMHtfgP99WrU5aNaPC5cgIbY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731240625; c=relaxed/simple; bh=ycach4/Zu/pXj5xhF7w8AKVzUPQtcrGbaxA51kPNzsQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eJV4np8IP7Ntan2vKURcvmvENfD+r2w6BvIVJkYSxF61/O35rsSTfx9Yf20qcaVlA0SvlVK2QpA9+AZtZjxeFHfIcN9BeqM8uYMWcDTjTVETq6vpXlNn+E8Nj8qio6H0OhH7p/HfzMo46c67VnAOmMUAe3GEICwNAiL+siakPx8= 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=FTMRrMW7; arc=none smtp.client-ip=209.85.210.175 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="FTMRrMW7" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-7242f559a9fso566554b3a.1 for ; Sun, 10 Nov 2024 04:10:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731240622; x=1731845422; 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=uJIm0PLKopA0pLigBMFzpW3ZN+UPJ4RZA8VqzbVNzfc=; b=FTMRrMW7rvOunmhHqeucigftxITIT156EHAqRRjTH2b8r7u4X9jBAYmHF/cf5U2UDf ek32SzyAYS8GqJdm4oQLzei6zSx9VbkQWR38SeVz8vPkIxFJ6AD4o6bAPDWpm/uMRuhF /BFC0K0zAQRvGE8kecGNebzsBDPeb/RrAaQvChVfjAQd2PKJKteU+cA6QklaTfTsGYGl TaQL1nHjEhEK5xi05GPdP4z9mzgFBBWMI+Ox3aV9BzemRkhxiRKwKBPcWkGoDdH7qSkj XUUOUF6TQOnyH6Wxov0m5G+gOvmq78XGms0p2f268fra5XTZ5hvzLcsmPKji6cruJhDl d6Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731240622; x=1731845422; 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=uJIm0PLKopA0pLigBMFzpW3ZN+UPJ4RZA8VqzbVNzfc=; b=XjYJJBrRqHeTPLVFyb6ayHMwmqie+/iDz79+MLX36mngYagSUgamDawwMlOJOG3RnE 6oT8b+DL9kCpzxbeSAoE2+jUD/0sz89BS/crwhAczJHX1z2uZ/8ePgNehnKvRuLF90xt Pcz9yQSq7D/bsqwf9swBwoOYmbc+5sEW37WZi+bwU04a7+S6O/wKwqrfPPI3fD+DnxgD uXhjdZry4jL0OlQvEYEwAQTTl6to+YCdmD9NfD3m1L9xjLes/ES7g3S7ba91Edgb0nqs JGwthkcff+EnouRH1TZWSBJ0QCI16AFWl2KApr1qRHpC3QBc+NEDIT9UhZmOnRjGMyiI Nq+g== X-Gm-Message-State: AOJu0YyD5/LDL/fSZXWV8PZCkQTRHtfnu9efHdqjaInC5dpUstq6Spn7 dQUJ68tjBxWmU8IrywmU88bM0T9d+Kh3tggsreJH20PSVX2uTDI4IFthDk3+leU= X-Google-Smtp-Source: AGHT+IFWMRtQX3gVDSz7fct91VlL9Ht916qr/jMm7iN0Z1oS3PtAw63AuvB5Y9urujqEDfFuBOXvJw== X-Received: by 2002:a05:6a00:1307:b0:71e:6728:72d5 with SMTP id d2e1a72fcca58-724132c1a44mr13489473b3a.15.1731240622348; Sun, 10 Nov 2024 04:10:22 -0800 (PST) Received: from localhost ([2605:52c0:1:4cf:6c5a:92ff:fe25:ceff]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72407860a6bsm7000562b3a.6.2024.11.10.04.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 04:10:21 -0800 (PST) Date: Sun, 10 Nov 2024 20:10:20 +0800 From: shejialuo To: git@vger.kernel.org Cc: Patrick Steinhardt , Karthik Nayak , Junio C Hamano Subject: [PATCH v7 8/9] ref: check whether the target of the symref is a ref 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: Ideally, we want to the users use "git symbolic-ref" to create symrefs instead of writing raw contents into the filesystem. However, "git symbolic-ref" is strict with the refname but not strict with the referent. For example, we can make the "referent" located at the "$(gitdir)/logs/aaa" and manually write the content into this where we can still successfully parse this symref by using "git rev-parse". $ git init repo && cd repo && git commit --allow-empty -mx $ git symbolic-ref refs/heads/test logs/aaa $ echo $(git rev-parse HEAD) > .git/logs/aaa $ git rev-parse test We may need to add some restrictions for "referent" parameter when using "git symbolic-ref" to create symrefs because ideally all the nonpseudo-refs should be located under the "refs" directory and we may tighten this in the future. In order to tell the user we may tighten the above situation, create a new fsck message "symrefTargetIsNotARef" to notify the user that this may become an error in the future. Mentored-by: Patrick Steinhardt Mentored-by: Karthik Nayak Signed-off-by: shejialuo --- Documentation/fsck-msgids.txt | 9 +++++++++ fsck.h | 1 + refs/files-backend.c | 14 ++++++++++++-- t/t0602-reffiles-fsck.sh | 29 +++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Documentation/fsck-msgids.txt b/Documentation/fsck-msgids.txt index dcea05edfc..f82ebc58e8 100644 --- a/Documentation/fsck-msgids.txt +++ b/Documentation/fsck-msgids.txt @@ -183,6 +183,15 @@ git@vger.kernel.org mailing list if you see this error, as we need to know what tools created such a file. +`symrefTargetIsNotARef`:: + (INFO) The target of a symbolic reference points neither to + a root reference nor to a reference starting with "refs/". + Although we allow create a symref pointing to the referent which + is outside the "ref" by using `git symbolic-ref`, we may tighten + the rule in the future. Report to the git@vger.kernel.org + mailing list if you see this error, as we need to know what tools + created such a file. + `trailingRefContent`:: (INFO) A loose ref has trailing content. As valid implementations of Git never created such a loose ref file, it may become an diff --git a/fsck.h b/fsck.h index 5227dfdef2..53a47612e6 100644 --- a/fsck.h +++ b/fsck.h @@ -87,6 +87,7 @@ enum fsck_msg_type { FUNC(BAD_TAG_NAME, INFO) \ FUNC(MISSING_TAGGER_ENTRY, INFO) \ FUNC(REF_MISSING_NEWLINE, INFO) \ + FUNC(SYMREF_TARGET_IS_NOT_A_REF, INFO) \ FUNC(TRAILING_REF_CONTENT, INFO) \ /* ignored (elevated when requested) */ \ FUNC(EXTRA_HEADER_ENTRY, IGNORE) diff --git a/refs/files-backend.c b/refs/files-backend.c index 8bc7c6e0c2..b3ec409920 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -3513,6 +3513,7 @@ static int files_fsck_symref_target(struct fsck_options *o, struct fsck_ref_report *report, struct strbuf *referent) { + int is_referent_root; char orig_last_byte; size_t orig_len; int ret = 0; @@ -3521,8 +3522,17 @@ static int files_fsck_symref_target(struct fsck_options *o, orig_last_byte = referent->buf[orig_len - 1]; strbuf_rtrim(referent); - if (!is_root_ref(referent->buf) && - check_refname_format(referent->buf, 0)) { + is_referent_root = is_root_ref(referent->buf); + if (!is_referent_root && + !starts_with(referent->buf, "refs/") && + !starts_with(referent->buf, "worktrees/")) { + ret = fsck_report_ref(o, report, + FSCK_MSG_SYMREF_TARGET_IS_NOT_A_REF, + "points to non-ref target '%s'", referent->buf); + + } + + if (!is_referent_root && check_refname_format(referent->buf, 0)) { ret = fsck_report_ref(o, report, FSCK_MSG_BAD_REFERENT_NAME, "points to invalid refname '%s'", referent->buf); diff --git a/t/t0602-reffiles-fsck.sh b/t/t0602-reffiles-fsck.sh index ee1e5f2864..692b30727a 100755 --- a/t/t0602-reffiles-fsck.sh +++ b/t/t0602-reffiles-fsck.sh @@ -366,6 +366,35 @@ test_expect_success 'textual symref content should be checked (aggregate)' ' test_cmp expect sorted_err ' +test_expect_success 'the target of the textual symref 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 && + test_commit default && + mkdir -p "$branch_dir_prefix/a/b" && + + for good_referent in "refs/heads/branch" "HEAD" "refs/tags/tag" + do + printf "ref: %s\n" $good_referent >$branch_dir_prefix/branch-good && + git refs verify 2>err && + rm $branch_dir_prefix/branch-good && + test_must_be_empty err || return 1 + done && + + for nonref_referent in "refs-back/heads/branch" "refs-back/tags/tag" "reflogs/refs/heads/branch" + do + printf "ref: %s\n" $nonref_referent >$branch_dir_prefix/branch-bad-1 && + git refs verify 2>err && + cat >expect <<-EOF && + warning: refs/heads/branch-bad-1: symrefTargetIsNotARef: points to non-ref target '\''$nonref_referent'\'' + EOF + rm $branch_dir_prefix/branch-bad-1 && + test_cmp expect err || return 1 + done +' + test_expect_success 'ref content checks should work with worktrees' ' test_when_finished "rm -rf repo" && git init repo &&