Skip to content

worktree repair: avoid breaking unrelated .git file and gitdir - #2225

Open
yoichi wants to merge 2 commits into
gitgitgadget:masterfrom
yoichi:worktree-repair-keep-unrelated-gitfile
Open

worktree repair: avoid breaking unrelated .git file and gitdir#2225
yoichi wants to merge 2 commits into
gitgitgadget:masterfrom
yoichi:worktree-repair-keep-unrelated-gitfile

Conversation

@yoichi

@yoichi yoichi commented Sep 11, 2026

Copy link
Copy Markdown

'git worktree repair' does not sufficiently validate the cross-references
between a linked working tree and its administrative data before
repairing them. This can cause the repair to modify the wrong .git file
or gitdir in certain situations.

This series first refactors the code to read the .git file once and extract
the worktree ID, then uses that information to validate the repair target
before modifying the cross-references.

  • [1/2] Refactor the code without changing functionality before making the fix
  • [2/2] Validate the worktree ID and inferred gitdir path before repairing

cc: Eric Sunshine sunshine@sunshineco.com

Remove the file reading and trimming logic from `infer_backlink()`,
and instead read the .git file once in its caller,
`repair_worktree_at_path()`, using `read_gitfile_raw()`. Since
`read_gitfile_gently()` is replaced with `read_gitfile_raw()`, restore
the logic for constructing the absolute path and replace the
READ_GITFILE_ERR_NOT_A_REPO handling with a check using
`is_git_directory()`. Simplify the logic for prioritizing
'inferred_backlink' over 'backlink'.

Extract `get_worktree_id()` to get the worktree ID from the contents
of the .git file. We are going to modify and use this function in
subsequent commits.

Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Currently, `repair_gitfile()` does not verify whether the worktree ID
recorded in the .git file matches the worktree being repaired, which
can result in an unrelated .git file being corrupted. For instance,
if two worktree directories are swapped without using 'git worktree
move', running 'git worktree repair' in the main worktree accidentally
swaps the links between their .git files and gitdirs.

`repair_worktree_at_path()` proceeds even if it fails to infer the
gitdir path. This can result in the corruption of an unrelated
gitdir. For instance, if we copied a linked worktree to a new location
X, running 'git worktree repair X' in a working tree which does not
belong to the original repository can accidentally overwrite the
gitdir in the original repository (the scope of impact should be
limited to the repository where the command was executed).

Resolve these issues by validating the worktree ID and stopping the
repair when the ID does not match or the gitdir path cannot be
inferred.

Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
@yoichi
yoichi force-pushed the worktree-repair-keep-unrelated-gitfile branch from 920147e to 99aa341 Compare September 13, 2026 03:13
@yoichi

yoichi commented Sep 13, 2026

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Sep 13, 2026

Copy link
Copy Markdown

Submitted as pull.2225.git.1789269613.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2225/yoichi/worktree-repair-keep-unrelated-gitfile-v1

To fetch this version to local tag pr-2225/yoichi/worktree-repair-keep-unrelated-gitfile-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2225/yoichi/worktree-repair-keep-unrelated-gitfile-v1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant