]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref: check whether the target of the symref is a ref
authorshejialuo <shejialuo@gmail.com>
Wed, 20 Nov 2024 11:52:09 +0000 (19:52 +0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 Nov 2024 23:21:33 +0000 (08:21 +0900)
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>
Documentation/fsck-msgids.txt
fsck.h
refs/files-backend.c
t/t0602-reffiles-fsck.sh

index dcea05edfc62ba9268bf843247867c5784344084..f82ebc58e84d0c8f11c3cc2c59a1d64055241c56 100644 (file)
        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 5227dfdef2c20d87da46857260b15af856509542..53a47612e67c4d63384dd49551c6a7331c979fb9 100644 (file)
--- 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)
index f4342e3f3e4fb435420c1a2f0798b7448baac65a..c2b99fdf40861f6f75931ba67a420ffaf31d4bd3 100644 (file)
@@ -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);
index ee1e5f2864133a3c9c4285681db39d48b21dd5e6..692b30727a14deb790a5a270fcb979f6860b5047 100755 (executable)
@@ -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 &&