Skip to content

Commit fdf3820

Browse files
shejialuogitster
authored andcommitted
builtin/refs: get worktrees without reading head information
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 would parse the "HEAD" information. If the referent of the "HEAD" is inside the "packed-ref", we will call "create_snapshot" function to parse the "packed-ref" to get the information. No matter whether the entry of "HEAD" in "packed-ref" is correct, "create_snapshot" would call "verify_buffer_safe" to check whether there is a newline in the last line of the file. If not, 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 should avoid reading the head information, which may execute the read operation in packed backend with stricter checks to die the program. Instead, we should continue to check other parts of the "packed-refs" file completely. 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 information. 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 320f206 commit fdf3820

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ struct worktree {
3030
*/
3131
struct worktree **get_worktrees(void);
3232

33+
/*
34+
* Like `get_worktrees`, but does not read HEAD. Skip reading HEAD allows to
35+
* get the worktree without worrying about failures pertaining to parsing
36+
* the HEAD ref. This is useful in contexts where it is assumed that the
37+
* refdb may not be in a consistent state.
38+
*/
39+
struct worktree **get_worktrees_without_reading_head(void);
40+
3341
/*
3442
* Returns 1 if linked worktrees exist, 0 otherwise.
3543
*/

0 commit comments

Comments
 (0)