Skip to content

Commit d996b44

Browse files
shejialuogitster
authored andcommitted
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 <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a6354e6 commit d996b44

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

Documentation/fsck-msgids.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@
183183
[email protected] mailing list if you see this error, as
184184
we need to know what tools created such a file.
185185

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 [email protected]
192+
mailing list if you see this error, as we need to know what tools
193+
created such a file.
194+
186195
`trailingRefContent`::
187196
(INFO) A loose ref has trailing content. As valid implementations
188197
of Git never created such a loose ref file, it may become an

fsck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ enum fsck_msg_type {
8787
FUNC(BAD_TAG_NAME, INFO) \
8888
FUNC(MISSING_TAGGER_ENTRY, INFO) \
8989
FUNC(REF_MISSING_NEWLINE, INFO) \
90+
FUNC(SYMREF_TARGET_IS_NOT_A_REF, INFO) \
9091
FUNC(TRAILING_REF_CONTENT, INFO) \
9192
/* ignored (elevated when requested) */ \
9293
FUNC(EXTRA_HEADER_ENTRY, IGNORE)

refs/files-backend.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,7 @@ static int files_fsck_symref_target(struct fsck_options *o,
35133513
struct fsck_ref_report *report,
35143514
struct strbuf *referent)
35153515
{
3516+
int is_referent_root;
35163517
char orig_last_byte;
35173518
size_t orig_len;
35183519
int ret = 0;
@@ -3521,8 +3522,17 @@ static int files_fsck_symref_target(struct fsck_options *o,
35213522
orig_last_byte = referent->buf[orig_len - 1];
35223523
strbuf_rtrim(referent);
35233524

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)) {
35263536
ret = fsck_report_ref(o, report,
35273537
FSCK_MSG_BAD_REFERENT_NAME,
35283538
"points to invalid refname '%s'", referent->buf);

t/t0602-reffiles-fsck.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,35 @@ test_expect_success 'textual symref content should be checked (aggregate)' '
366366
test_cmp expect sorted_err
367367
'
368368

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+
369398
test_expect_success 'ref content checks should work with worktrees' '
370399
test_when_finished "rm -rf repo" &&
371400
git init repo &&

0 commit comments

Comments
 (0)