summaryrefslogtreecommitdiff
path: root/builtin/worktree.c
diff options
context:
space:
mode:
authorEric Sunshine <sunshine@sunshineco.com>2020-08-31 06:57:57 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-08-31 18:47:45 (GMT)
commitbdd1f3e4da716012a52194cdfbc9c80dfdce87d4 (patch)
tree4a54786205851b904bcbdf55b67d4678f97db6d9 /builtin/worktree.c
parente8e1ff24c5cf885c8d0ac208b58ccaf3760e8bfa (diff)
downloadgit-bdd1f3e4da716012a52194cdfbc9c80dfdce87d4.zip
git-bdd1f3e4da716012a52194cdfbc9c80dfdce87d4.tar.gz
git-bdd1f3e4da716012a52194cdfbc9c80dfdce87d4.tar.bz2
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>
Diffstat (limited to 'builtin/worktree.c')
-rw-r--r--builtin/worktree.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 88af412..68b0032 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -1030,6 +1030,17 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
return ret;
}
+static void report_repair(int iserr, const char *path, const char *msg, void *cb_data)
+{
+ if (!iserr) {
+ printf_ln(_("repair: %s: %s"), msg, path);
+ } else {
+ int *exit_status = (int *)cb_data;
+ fprintf_ln(stderr, _("error: %s: %s"), msg, path);
+ *exit_status = 1;
+ }
+}
+
static int repair(int ac, const char **av, const char *prefix)
{
struct option options[] = {
@@ -1040,6 +1051,7 @@ static int repair(int ac, const char **av, const char *prefix)
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
if (ac)
usage_with_options(worktree_usage, options);
+ repair_worktrees(report_repair, &rc);
return rc;
}