ref: check whether the target of the symref is a ref

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 <ps@pks.im> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

shejialuo committed Nov 20, 2024 at 19:52 UTC d996b4475c7a49e568c22ffd4217d8dba8352fcb
4 files changed +51 -2
Documentation/fsck-msgids.txt
+9
@@ -183,6 +183,15 @@
183 git@vger.kernel.org mailing list if you see this error, as
184 we need to know what tools created such a file.
185
186 +`symrefTargetIsNotARef`::
187 + (INFO) The target of a symbolic reference points neither to
188 + a root reference nor to a reference starting with "refs/".
189 + Although we allow create a symref pointing to the referent which
190 + is outside the "ref" by using `git symbolic-ref`, we may tighten
191 + the rule in the future. Report to the git@vger.kernel.org
192 + mailing list if you see this error, as we need to know what tools
193 + created such a file.
194 +
195 `trailingRefContent`::
196 (INFO) A loose ref has trailing content. As valid implementations
197 of Git never created such a loose ref file, it may become an
fsck.h
+1
@@ -87,6 +87,7 @@ enum fsck_msg_type {
87 FUNC(BAD_TAG_NAME, INFO) \
88 FUNC(MISSING_TAGGER_ENTRY, INFO) \
89 FUNC(REF_MISSING_NEWLINE, INFO) \
90 + FUNC(SYMREF_TARGET_IS_NOT_A_REF, INFO) \
91 FUNC(TRAILING_REF_CONTENT, INFO) \
92 /* ignored (elevated when requested) */ \
93 FUNC(EXTRA_HEADER_ENTRY, IGNORE)
refs/files-backend.c
+12 -2
@@ -3513,6 +3513,7 @@ static int files_fsck_symref_target(struct fsck_options *o,
3513 struct fsck_ref_report *report,
3514 struct strbuf *referent)
3515 {
3516 + int is_referent_root;
3517 char orig_last_byte;
3518 size_t orig_len;
3519 int ret = 0;
@@ -3521,8 +3522,17 @@ static int files_fsck_symref_target(struct fsck_options *o,
3522 orig_last_byte = referent->buf[orig_len - 1];
3523 strbuf_rtrim(referent);
3524
3524 - if (!is_root_ref(referent->buf) &&
3525 - check_refname_format(referent->buf, 0)) {
3525 + is_referent_root = is_root_ref(referent->buf);
3526 + if (!is_referent_root &&
3527 + !starts_with(referent->buf, "refs/") &&
3528 + !starts_with(referent->buf, "worktrees/")) {
3529 + ret = fsck_report_ref(o, report,
3530 + FSCK_MSG_SYMREF_TARGET_IS_NOT_A_REF,
3531 + "points to non-ref target '%s'", referent->buf);
3532 +
3533 + }
3534 +
3535 + if (!is_referent_root && check_refname_format(referent->buf, 0)) {
3536 ret = fsck_report_ref(o, report,
3537 FSCK_MSG_BAD_REFERENT_NAME,
3538 "points to invalid refname '%s'", referent->buf);
t/t0602-reffiles-fsck.sh
+29
@@ -366,6 +366,35 @@ test_expect_success 'textual symref content should be checked (aggregate)' '
366 test_cmp expect sorted_err
367 '
368
369 +test_expect_success 'the target of the textual symref should be checked' '
370 + test_when_finished "rm -rf repo" &&
371 + git init repo &&
372 + branch_dir_prefix=.git/refs/heads &&
373 + tag_dir_prefix=.git/refs/tags &&
374 + cd repo &&
375 + test_commit default &&
376 + mkdir -p "$branch_dir_prefix/a/b" &&
377 +
378 + for good_referent in "refs/heads/branch" "HEAD" "refs/tags/tag"
379 + do
380 + printf "ref: %s\n" $good_referent >$branch_dir_prefix/branch-good &&
381 + git refs verify 2>err &&
382 + rm $branch_dir_prefix/branch-good &&
383 + test_must_be_empty err || return 1
384 + done &&
385 +
386 + for nonref_referent in "refs-back/heads/branch" "refs-back/tags/tag" "reflogs/refs/heads/branch"
387 + do
388 + printf "ref: %s\n" $nonref_referent >$branch_dir_prefix/branch-bad-1 &&
389 + git refs verify 2>err &&
390 + cat >expect <<-EOF &&
391 + warning: refs/heads/branch-bad-1: symrefTargetIsNotARef: points to non-ref target '\''$nonref_referent'\''
392 + EOF
393 + rm $branch_dir_prefix/branch-bad-1 &&
394 + test_cmp expect err || return 1
395 + done
396 +'
397 +
398 test_expect_success 'ref content checks should work with worktrees' '
399 test_when_finished "rm -rf repo" &&
400 git init repo &&