Skip to content

Commit 1274512

Browse files
shejialuogitster
authored andcommitted
builtin/refs.h: get worktrees without reading head info
In "packed-backend.c", there are some functions such as "create_snapshot" and "next_record" which would check the correctness of the content of the "packed-ref" file. When anything is bad, the program will die. It may seem that we have nothing relevant to above feature, because we are going to read and parse the raw "packed-ref" file without creating the snapshot and using the ref iterator to check the consistency. However, when using "get_worktrees" in "builtin/refs", we will parse the head information. If the referent of the "HEAD" is inside the "packed-ref", we will call "create_snapshot" and "next_record" functions to parse the "packed-ref" to get the head information. And if there are something wrong, the program will die. Although this behavior has no harm for the program, it will short-circuit the program. When the users execute "git refs verify" or "git fsck", we don't want to simply die the program but rather show the warnings or errors as many as possible to info the users. So, we should avoiding reading the head info. Fortunately, in 465a22b (worktree: skip reading HEAD when repairing worktrees, 2023-12-29), we have introduced a function "get_worktrees_internal" which allows us to get worktrees without reading head info. Create a new exposed function "get_worktrees_without_reading_head", then replace the "get_worktrees" in "builtin/refs" with the new created function. 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 32521d5 commit 1274512

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

builtin/refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
8888
git_config(git_fsck_config, &fsck_refs_options);
8989
prepare_repo_settings(the_repository);
9090

91-
worktrees = get_worktrees();
91+
worktrees = get_worktrees_without_reading_head();
9292
for (size_t i = 0; worktrees[i]; i++)
9393
ret |= refs_fsck(get_worktree_ref_store(worktrees[i]),
9494
&fsck_refs_options, worktrees[i]);

worktree.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ struct worktree **get_worktrees(void)
175175
return get_worktrees_internal(0);
176176
}
177177

178+
struct worktree **get_worktrees_without_reading_head(void)
179+
{
180+
return get_worktrees_internal(1);
181+
}
182+
178183
const char *get_worktree_git_dir(const struct worktree *wt)
179184
{
180185
if (!wt)

worktree.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ struct worktree {
3030
*/
3131
struct worktree **get_worktrees(void);
3232

33+
/*
34+
* Like `get_worktrees`, but does not read HEAD. This is useful when checking
35+
* the consistency, as reading HEAD may not be necessary.
36+
*/
37+
struct worktree **get_worktrees_without_reading_head(void);
38+
3339
/*
3440
* Returns 1 if linked worktrees exist, 0 otherwise.
3541
*/

0 commit comments

Comments
 (0)