worktree: teach "repair" to fix worktree back-links to main worktree
The .git file in a linked worktree is a "gitfile" which points back to the .git/worktrees/<id> entry in the main worktree or bare repository. If a worktree's .git file is deleted or becomes corrupted or outdated, then the linked worktree won't know how to find the repository or any of its own administrative files (such as 'index', 'HEAD', etc.). An easy way for the .git file to become outdated is for the user to move the main worktree or bare repository. Although it is possible to manually update each linked worktree's .git file to reflect the new repository location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand. Therefore, teach "git worktree repair" how to repair broken or outdated worktree .git files automatically. (For this to work, the command must be invoked from within the main worktree or bare repository, or from within a worktree which has not become disconnected from the repository -- such as one which was created after the repository was moved.) Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
e8e1ff24c5
commit
bdd1f3e4da
61
worktree.c
61
worktree.c
@ -571,3 +571,64 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
|
||||
free_worktrees(worktrees);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not
|
||||
* pointing at <repo>/worktrees/<id>.
|
||||
*/
|
||||
static void repair_gitfile(struct worktree *wt,
|
||||
worktree_repair_fn fn, void *cb_data)
|
||||
{
|
||||
struct strbuf dotgit = STRBUF_INIT;
|
||||
struct strbuf repo = STRBUF_INIT;
|
||||
char *backlink;
|
||||
const char *repair = NULL;
|
||||
int err;
|
||||
|
||||
/* missing worktree can't be repaired */
|
||||
if (!file_exists(wt->path))
|
||||
return;
|
||||
|
||||
if (!is_directory(wt->path)) {
|
||||
fn(1, wt->path, _("not a directory"), cb_data);
|
||||
return;
|
||||
}
|
||||
|
||||
strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
|
||||
strbuf_addf(&dotgit, "%s/.git", wt->path);
|
||||
backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
|
||||
|
||||
if (err == READ_GITFILE_ERR_NOT_A_FILE)
|
||||
fn(1, wt->path, _(".git is not a file"), cb_data);
|
||||
else if (err)
|
||||
repair = _(".git file broken");
|
||||
else if (fspathcmp(backlink, repo.buf))
|
||||
repair = _(".git file incorrect");
|
||||
|
||||
if (repair) {
|
||||
fn(0, wt->path, repair, cb_data);
|
||||
write_file(dotgit.buf, "gitdir: %s", repo.buf);
|
||||
}
|
||||
|
||||
free(backlink);
|
||||
strbuf_release(&repo);
|
||||
strbuf_release(&dotgit);
|
||||
}
|
||||
|
||||
static void repair_noop(int iserr, const char *path, const char *msg,
|
||||
void *cb_data)
|
||||
{
|
||||
/* nothing */
|
||||
}
|
||||
|
||||
void repair_worktrees(worktree_repair_fn fn, void *cb_data)
|
||||
{
|
||||
struct worktree **worktrees = get_worktrees();
|
||||
struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
|
||||
|
||||
if (!fn)
|
||||
fn = repair_noop;
|
||||
for (; *wt; wt++)
|
||||
repair_gitfile(*wt, fn, cb_data);
|
||||
free_worktrees(worktrees);
|
||||
}
|
||||
|
Reference in New Issue
Block a user