Skip to content

Commit cf76bae

Browse files
sunshinecogitster
authored andcommitted
worktree: teach repair to fix multi-directional breakage
`git worktree repair` knows how to repair the two-way links between the repository and a worktree as long as a link in one or the other direction is sound. For instance, if a linked worktree is moved (without using `git worktree move`), repair is possible because the worktree still knows the location of the repository even though the repository no longer knows where the worktree is. Similarly, if the repository is moved, repair is possible since the repository still knows the locations of the worktrees even though the worktrees no longer know where the repository is. However, if both the repository and the worktrees are moved, then links are severed in both directions, and no repair is possible. This is the case even when the new worktree locations are specified as arguments to `git worktree repair`. The reason for this limitation is twofold. First, when `repair` consults the worktree's gitfile (/path/to/worktree/.git) to determine the corresponding <repo>/worktrees/<id>/gitdir file to fix, <repo> is the old path to the repository, thus it is unable to fix the `gitdir` file at its new location since it doesn't know where it is. Second, when `repair` consults <repo>/worktrees/<id>/gitdir to find the location of the worktree's gitfile (/path/to/worktree/.git), the path recorded in `gitdir` is the old location of the worktree's gitfile, thus it is unable to repair the gitfile since it doesn't know where it is. Fix these shortcomings by teaching `repair` to attempt to infer the new location of the <repo>/worktrees/<id>/gitdir file when the location recorded in the worktree's gitfile has become stale but the file is otherwise well-formed. The inference is intentionally simple-minded. For each worktree path specified as an argument, `git worktree repair` manually reads the ".git" gitfile at that location and, if it is well-formed, extracts the <id>. It then searches for a corresponding <id> in <repo>/worktrees/ and, if found, concludes that there is a reasonable match and updates <repo>/worktrees/<id>/gitdir to point at the specified worktree path. In order for <repo> to be known, `git worktree repair` must be run in the main worktree or bare repository. `git worktree repair` first attempts to repair each incoming /path/to/worktree/.git gitfile to point at the repository, and then attempts to repair outgoing <repo>/worktrees/<id>/gitdir files to point at the worktrees. This sequence was chosen arbitrarily when originally implemented since the order of fixes is immaterial as long as one side of the two-way link between the repository and a worktree is sound. However, for this new repair technique to work, the order must be reversed. This is because the new inference mechanism, when it is successful, allows the outgoing <repo>/worktrees/<id>/gitdir file to be repaired, thus fixing one side of the two-way link. Once that side is fixed, the other side can be fixed by the existing repair mechanism, hence the order of repairs is now significant. Two safeguards are employed to avoid hijacking a worktree from a different repository if the user accidentally specifies a foreign worktree as an argument. The first, as described above, is that it requires an <id> match between the repository and the worktree. That itself is not foolproof for preventing hijack, so the second safeguard is that the inference will only kick in if the worktree's /path/to/worktree/.git gitfile does not point at a repository. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d3ef5b commit cf76bae

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

Documentation/git-worktree.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ locate it. Running `repair` within the recently-moved working tree will
143143
reestablish the connection. If multiple linked working trees are moved,
144144
running `repair` from any working tree with each tree's new `<path>` as
145145
an argument, will reestablish the connection to all the specified paths.
146+
+
147+
If both the main working tree and linked working trees have been moved
148+
manually, then running `repair` in the main working tree and specifying the
149+
new `<path>` of each linked working tree will reestablish all connections
150+
in both directions.
146151

147152
unlock::
148153

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,10 +1052,10 @@ static int repair(int ac, const char **av, const char *prefix)
10521052
int rc = 0;
10531053

10541054
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1055-
repair_worktrees(report_repair, &rc);
10561055
p = ac > 0 ? av : self;
10571056
for (; *p; p++)
10581057
repair_worktree_at_path(*p, report_repair, &rc);
1058+
repair_worktrees(report_repair, &rc);
10591059
return rc;
10601060
}
10611061

t/t2406-worktree-repair.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ test_expect_success 'repo not found; .git not file' '
104104
test_i18ngrep ".git is not a file" err
105105
'
106106

107+
test_expect_success 'repo not found; .git not referencing repo' '
108+
test_when_finished "rm -rf side not-a-repo && git worktree prune" &&
109+
git worktree add --detach side &&
110+
sed s,\.git/worktrees/side$,not-a-repo, side/.git >side/.newgit &&
111+
mv side/.newgit side/.git &&
112+
mkdir not-a-repo &&
113+
test_must_fail git worktree repair side 2>err &&
114+
test_i18ngrep ".git file does not reference a repository" err
115+
'
116+
107117
test_expect_success 'repo not found; .git file broken' '
108118
test_when_finished "rm -rf orig moved && git worktree prune" &&
109119
git worktree add --detach orig &&
@@ -176,4 +186,20 @@ test_expect_success 'repair multiple gitdir files' '
176186
test_must_be_empty err
177187
'
178188

189+
test_expect_success 'repair moved main and linked worktrees' '
190+
test_when_finished "rm -rf main side mainmoved sidemoved" &&
191+
test_create_repo main &&
192+
test_commit -C main init &&
193+
git -C main worktree add --detach ../side &&
194+
sed "s,side/\.git$,sidemoved/.git," \
195+
main/.git/worktrees/side/gitdir >expect-gitdir &&
196+
sed "s,main/.git/worktrees/side$,mainmoved/.git/worktrees/side," \
197+
side/.git >expect-gitfile &&
198+
mv main mainmoved &&
199+
mv side sidemoved &&
200+
git -C mainmoved worktree repair ../sidemoved &&
201+
test_cmp expect-gitdir mainmoved/.git/worktrees/side/gitdir &&
202+
test_cmp expect-gitfile sidemoved/.git
203+
'
204+
179205
test_done

worktree.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,42 @@ static int is_main_worktree_path(const char *path)
644644
return !cmp;
645645
}
646646

647+
/*
648+
* If both the main worktree and linked worktree have been moved, then the
649+
* gitfile /path/to/worktree/.git won't point into the repository, thus we
650+
* won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
651+
* be able to infer the gitdir by manually reading /path/to/worktree/.git,
652+
* extracting the <id>, and checking if <repo>/worktrees/<id> exists.
653+
*/
654+
static char *infer_backlink(const char *gitfile)
655+
{
656+
struct strbuf actual = STRBUF_INIT;
657+
struct strbuf inferred = STRBUF_INIT;
658+
const char *id;
659+
660+
if (strbuf_read_file(&actual, gitfile, 0) < 0)
661+
goto error;
662+
if (!starts_with(actual.buf, "gitdir:"))
663+
goto error;
664+
if (!(id = find_last_dir_sep(actual.buf)))
665+
goto error;
666+
strbuf_trim(&actual);
667+
id++; /* advance past '/' to point at <id> */
668+
if (!*id)
669+
goto error;
670+
strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
671+
if (!is_directory(inferred.buf))
672+
goto error;
673+
674+
strbuf_release(&actual);
675+
return strbuf_detach(&inferred, NULL);
676+
677+
error:
678+
strbuf_release(&actual);
679+
strbuf_release(&inferred);
680+
return NULL;
681+
}
682+
647683
/*
648684
* Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
649685
* the worktree's path.
@@ -675,6 +711,11 @@ void repair_worktree_at_path(const char *path,
675711
if (err == READ_GITFILE_ERR_NOT_A_FILE) {
676712
fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
677713
goto done;
714+
} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
715+
if (!(backlink = infer_backlink(realdotgit.buf))) {
716+
fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
717+
goto done;
718+
}
678719
} else if (err) {
679720
fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
680721
goto done;

0 commit comments

Comments
 (0)