Skip to content

Commit fe9a6b7

Browse files
shejialuogitster
authored andcommitted
packed-backend: check whether the "packed-refs" is regular
Although "git-fsck(1)" and "packed-backend.c" will check some consistency and correctness of "packed-refs" file, they never check the filetype of the "packed-refs". The user should always use "git packed-refs" command to create the raw regular "packed-refs" file, so we need to explicitly check this in "git refs verify". Use "lstat" to check the file mode. If we cannot check the file status, this is OK because there is a chance that there is no "packed-refs" in the repo. Reuse "FSCK_MSG_BAD_REF_FILETYPE" fsck message id to report the error to the user if "packed-refs" is not a regular file. 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 1274512 commit fe9a6b7

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

refs/packed-backend.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../config.h"
66
#include "../dir.h"
77
#include "../gettext.h"
8+
#include "../fsck.h"
89
#include "../hash.h"
910
#include "../hex.h"
1011
#include "../refs.h"
@@ -1748,15 +1749,39 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
17481749
return empty_ref_iterator_begin();
17491750
}
17501751

1751-
static int packed_fsck(struct ref_store *ref_store UNUSED,
1752-
struct fsck_options *o UNUSED,
1752+
static int packed_fsck(struct ref_store *ref_store,
1753+
struct fsck_options *o,
17531754
struct worktree *wt)
17541755
{
1756+
struct packed_ref_store *refs = packed_downcast(ref_store,
1757+
REF_STORE_READ, "fsck");
1758+
struct stat st;
1759+
int ret = 0;
17551760

17561761
if (!is_main_worktree(wt))
1757-
return 0;
1762+
goto cleanup;
17581763

1759-
return 0;
1764+
/*
1765+
* If the packed-refs file doesn't exist, there's nothing to
1766+
* check.
1767+
*/
1768+
if (lstat(refs->path, &st) < 0)
1769+
goto cleanup;
1770+
1771+
if (o->verbose)
1772+
fprintf_ln(stderr, "Checking packed-refs file %s", refs->path);
1773+
1774+
if (!S_ISREG(st.st_mode)) {
1775+
struct fsck_ref_report report = { 0 };
1776+
report.path = "packed-refs";
1777+
1778+
ret = fsck_report_ref(o, &report, FSCK_MSG_BAD_REF_FILETYPE,
1779+
"not a regular file");
1780+
goto cleanup;
1781+
}
1782+
1783+
cleanup:
1784+
return ret;
17601785
}
17611786

17621787
struct ref_storage_be refs_be_packed = {

t/t0602-reffiles-fsck.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,24 @@ test_expect_success 'ref content checks should work with worktrees' '
626626
test_cmp expect err
627627
'
628628

629+
test_expect_success SYMLINKS 'the filetype of packed-refs should be checked' '
630+
test_when_finished "rm -rf repo" &&
631+
git init repo &&
632+
cd repo &&
633+
test_commit default &&
634+
git branch branch-1 &&
635+
git branch branch-2 &&
636+
git branch branch-3 &&
637+
git pack-refs --all &&
638+
639+
mv .git/packed-refs .git/packed-refs-back &&
640+
ln -sf packed-refs-bak .git/packed-refs &&
641+
test_must_fail git refs verify 2>err &&
642+
cat >expect <<-EOF &&
643+
error: packed-refs: badRefFiletype: not a regular file
644+
EOF
645+
rm .git/packed-refs &&
646+
test_cmp expect err
647+
'
648+
629649
test_done

0 commit comments

Comments
 (0)