Skip to content

Commit

Permalink
Merge branch 'sj/ref-consistency-checks-more' into seen
Browse files Browse the repository at this point in the history
"git fsck" becomes more careful when checking the refs.

* sj/ref-consistency-checks-more:
  builtin/fsck: add `git refs verify` child process
  packed-backend: check whether the "packed-refs" is sorted
  packed-backend: add "packed-refs" entry consistency check
  packed-backend: check whether the refname contains NUL characters
  packed-backend: add "packed-refs" header consistency check
  packed-backend: check whether the "packed-refs" is regular
  builtin/refs: get worktrees without reading head info
  t0602: use subshell to ensure working directory unchanged
  • Loading branch information
gitster committed Feb 4, 2025
2 parents 836caf8 + b2c32bf commit 3df2ad0
Show file tree
Hide file tree
Showing 8 changed files with 1,040 additions and 481 deletions.
22 changes: 22 additions & 0 deletions Documentation/fsck-msgids.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
`badObjectSha1`::
(ERROR) An object has a bad sha1.

`badPackedRefEntry`::
(ERROR) The "packed-refs" file contains an invalid entry.

`badPackedRefHeader`::
(ERROR) The "packed-refs" file contains an invalid
header.

`badParentSha1`::
(ERROR) A commit object has a bad parent sha1.

Expand Down Expand Up @@ -176,6 +183,16 @@
`nullSha1`::
(WARN) Tree contains entries pointing to a null sha1.

`packedRefEntryNotTerminated`::
(ERROR) The "packed-refs" file contains an entry that is
not terminated by a newline.

`packedRefMissingHeader`::
(INFO) The "packed-refs" file does not contain the header.

`packedRefUnsorted`::
(ERROR) The "packed-refs" file is not sorted.

`refMissingNewline`::
(INFO) A loose ref that does not end with newline(LF). As
valid implementations of Git never created such a loose ref
Expand Down Expand Up @@ -208,6 +225,11 @@
`treeNotSorted`::
(ERROR) A tree is not properly sorted.

`unknownPackedRefHeader`::
(INFO) The "packed-refs" header starts with "# pack-refs with:"
but the remaining content is not the same as what `git pack-refs`
would write.

`unknownType`::
(ERROR) Found an unknown object type.

Expand Down
30 changes: 30 additions & 0 deletions builtin/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,34 @@ static int check_pack_rev_indexes(struct repository *r, int show_progress)
return res;
}

static void fsck_refs(struct repository *r)
{
struct child_process refs_verify = CHILD_PROCESS_INIT;
struct progress *progress = NULL;
uint64_t progress_num = 1;

if (show_progress)
progress = start_progress(r, _("Checking ref database"),
progress_num);

if (verbose)
fprintf_ln(stderr, _("Checking ref database"));

child_process_init(&refs_verify);
refs_verify.git_cmd = 1;
strvec_pushl(&refs_verify.args, "refs", "verify", NULL);
if (verbose)
strvec_push(&refs_verify.args, "--verbose");
if (check_strict)
strvec_push(&refs_verify.args, "--strict");

if (run_command(&refs_verify))
errors_found |= ERROR_REFS;

display_progress(progress, 1);
stop_progress(&progress);
}

static char const * const fsck_usage[] = {
N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
" [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
Expand Down Expand Up @@ -970,6 +998,8 @@ int cmd_fsck(int argc,
git_config(git_fsck_config, &fsck_obj_options);
prepare_repo_settings(the_repository);

fsck_refs(the_repository);

if (connectivity_only) {
for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
for_each_packed_object(the_repository,
Expand Down
2 changes: 1 addition & 1 deletion builtin/refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
git_config(git_fsck_config, &fsck_refs_options);
prepare_repo_settings(the_repository);

worktrees = get_worktrees();
worktrees = get_worktrees_without_reading_head();
for (size_t i = 0; worktrees[i]; i++)
ret |= refs_fsck(get_worktree_ref_store(worktrees[i]),
&fsck_refs_options, worktrees[i]);
Expand Down
6 changes: 6 additions & 0 deletions fsck.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ enum fsck_msg_type {
FUNC(BAD_EMAIL, ERROR) \
FUNC(BAD_NAME, ERROR) \
FUNC(BAD_OBJECT_SHA1, ERROR) \
FUNC(BAD_PACKED_REF_ENTRY, ERROR) \
FUNC(BAD_PACKED_REF_HEADER, ERROR) \
FUNC(BAD_PARENT_SHA1, ERROR) \
FUNC(BAD_REF_CONTENT, ERROR) \
FUNC(BAD_REF_FILETYPE, ERROR) \
Expand All @@ -53,6 +55,8 @@ enum fsck_msg_type {
FUNC(MISSING_TYPE, ERROR) \
FUNC(MISSING_TYPE_ENTRY, ERROR) \
FUNC(MULTIPLE_AUTHORS, ERROR) \
FUNC(PACKED_REF_ENTRY_NOT_TERMINATED, ERROR) \
FUNC(PACKED_REF_UNSORTED, ERROR) \
FUNC(TREE_NOT_SORTED, ERROR) \
FUNC(UNKNOWN_TYPE, ERROR) \
FUNC(ZERO_PADDED_DATE, ERROR) \
Expand Down Expand Up @@ -90,6 +94,8 @@ enum fsck_msg_type {
FUNC(REF_MISSING_NEWLINE, INFO) \
FUNC(SYMREF_TARGET_IS_NOT_A_REF, INFO) \
FUNC(TRAILING_REF_CONTENT, INFO) \
FUNC(UNKNOWN_PACKED_REF_HEADER, INFO) \
FUNC(PACKED_REF_MISSING_HEADER, INFO) \
/* ignored (elevated when requested) */ \
FUNC(EXTRA_HEADER_ENTRY, IGNORE)

Expand Down
Loading

0 comments on commit 3df2ad0

Please sign in to comment.