@matheusbsbs / matheus-bagliano / commits / b214ab5aa5
Fork 0

worktree: teach "repair" to fix outgoing links to worktrees

The .git/worktrees/<id>/gitdir file points at the location of a linked worktree's .git file. Its content must be of the form /path/to/worktree/.git (from which the location of the worktree itself can be derived by stripping the "/.git" suffix). If the gitdir file is deleted or becomes corrupted or outdated, then Git will be unable to find the linked worktree. An easy way for the gitdir file to become outdated is for the user to move the worktree manually (without using "git worktree move"). Although it is possible to manually update the gitdir file to reflect the new linked worktree 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 .git/worktrees/<id>/gitdir files automatically. (For this to work, the command must either be invoked from within the worktree whose gitdir file requires repair, or from within the main or any linked worktree by providing the path of the broken worktree as an argument to "git worktree repair".) Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Aug 31, 2020 at 02:57 UTC b214ab5aa597e748c228e657d4eb7c18960e6a67
5 files changed +188 -5
Documentation/git-worktree.txt
+11 -3
@@ -15,7 +15,7 @@ SYNOPSIS
15 'git worktree move' <worktree> <new-path>
16 'git worktree prune' [-n] [-v] [--expire <expire>]
17 'git worktree remove' [-f] <worktree>
18 -'git worktree repair'
18 +'git worktree repair' [<path>...]
19 'git worktree unlock' <worktree>
20
21 DESCRIPTION
@@ -114,7 +114,7 @@ and no modification in tracked files) can be removed. Unclean working
114 trees or ones with submodules can be removed with `--force`. The main
115 working tree cannot be removed.
116
117 -repair::
117 +repair [<path>...]::
118
119 Repair working tree administrative files, if possible, if they have
120 become corrupted or outdated due to external factors.
@@ -123,6 +123,13 @@ For instance, if the main working tree (or bare repository) is moved,
123 linked working trees will be unable to locate it. Running `repair` in
124 the main working tree will reestablish the connection from linked
125 working trees back to the main working tree.
126 ++
127 +Similarly, if a linked working tree is moved without using `git worktree
128 +move`, the main working tree (or bare repository) will be unable to
129 +locate it. Running `repair` within the recently-moved working tree will
130 +reestablish the connection. If multiple linked working trees are moved,
131 +running `repair` from any working tree with each tree's new `<path>` as
132 +an argument, will reestablish the connection to all the specified paths.
133
134 unlock::
135
@@ -317,7 +324,8 @@ in the entry's directory. For example, if a linked working tree is moved
324 to `/newpath/test-next` and its `.git` file points to
325 `/path/main/.git/worktrees/test-next`, then update
326 `/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next`
320 -instead.
327 +instead. Better yet, run `git worktree repair` to reestablish the connection
328 +automatically.
329
330 To prevent a `$GIT_DIR/worktrees` entry from being pruned (which
331 can be useful in some situations, such as when the
builtin/worktree.c
+5 -2
@@ -1043,15 +1043,18 @@ static void report_repair(int iserr, const char *path, const char *msg, void *cb
1043
1044 static int repair(int ac, const char **av, const char *prefix)
1045 {
1046 + const char **p;
1047 + const char *self[] = { ".", NULL };
1048 struct option options[] = {
1049 OPT_END()
1050 };
1051 int rc = 0;
1052
1053 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
1052 - if (ac)
1053 - usage_with_options(worktree_usage, options);
1054 repair_worktrees(report_repair, &rc);
1055 + p = ac > 0 ? av : self;
1056 + for (; *p; p++)
1057 + repair_worktree_at_path(*p, report_repair, &rc);
1058 return rc;
1059 }
1060
t/t2406-worktree-repair.sh
+86
@@ -90,4 +90,90 @@ test_expect_success 'repair .git file from bare.git' '
90 test_cmp expect actual
91 '
92
93 +test_expect_success 'invalid worktree path' '
94 + test_must_fail git worktree repair /notvalid >out 2>err &&
95 + test_must_be_empty out &&
96 + test_i18ngrep "not a valid path" err
97 +'
98 +
99 +test_expect_success 'repo not found; .git not file' '
100 + test_when_finished "rm -rf not-a-worktree" &&
101 + test_create_repo not-a-worktree &&
102 + test_must_fail git worktree repair not-a-worktree >out 2>err &&
103 + test_must_be_empty out &&
104 + test_i18ngrep ".git is not a file" err
105 +'
106 +
107 +test_expect_success 'repo not found; .git file broken' '
108 + test_when_finished "rm -rf orig moved && git worktree prune" &&
109 + git worktree add --detach orig &&
110 + echo /invalid >orig/.git &&
111 + mv orig moved &&
112 + test_must_fail git worktree repair moved >out 2>err &&
113 + test_must_be_empty out &&
114 + test_i18ngrep ".git file broken" err
115 +'
116 +
117 +test_expect_success 'repair broken gitdir' '
118 + test_when_finished "rm -rf orig moved && git worktree prune" &&
119 + git worktree add --detach orig &&
120 + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
121 + rm .git/worktrees/orig/gitdir &&
122 + mv orig moved &&
123 + git worktree repair moved >out 2>err &&
124 + test_cmp expect .git/worktrees/orig/gitdir &&
125 + test_i18ngrep "gitdir unreadable" out &&
126 + test_must_be_empty err
127 +'
128 +
129 +test_expect_success 'repair incorrect gitdir' '
130 + test_when_finished "rm -rf orig moved && git worktree prune" &&
131 + git worktree add --detach orig &&
132 + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
133 + mv orig moved &&
134 + git worktree repair moved >out 2>err &&
135 + test_cmp expect .git/worktrees/orig/gitdir &&
136 + test_i18ngrep "gitdir incorrect" out &&
137 + test_must_be_empty err
138 +'
139 +
140 +test_expect_success 'repair gitdir (implicit) from linked worktree' '
141 + test_when_finished "rm -rf orig moved && git worktree prune" &&
142 + git worktree add --detach orig &&
143 + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
144 + mv orig moved &&
145 + git -C moved worktree repair >out 2>err &&
146 + test_cmp expect .git/worktrees/orig/gitdir &&
147 + test_i18ngrep "gitdir incorrect" out &&
148 + test_must_be_empty err
149 +'
150 +
151 +test_expect_success 'unable to repair gitdir (implicit) from main worktree' '
152 + test_when_finished "rm -rf orig moved && git worktree prune" &&
153 + git worktree add --detach orig &&
154 + cat .git/worktrees/orig/gitdir >expect &&
155 + mv orig moved &&
156 + git worktree repair >out 2>err &&
157 + test_cmp expect .git/worktrees/orig/gitdir &&
158 + test_must_be_empty out &&
159 + test_must_be_empty err
160 +'
161 +
162 +test_expect_success 'repair multiple gitdir files' '
163 + test_when_finished "rm -rf orig1 orig2 moved1 moved2 &&
164 + git worktree prune" &&
165 + git worktree add --detach orig1 &&
166 + git worktree add --detach orig2 &&
167 + sed s,orig1/\.git$,moved1/.git, .git/worktrees/orig1/gitdir >expect1 &&
168 + sed s,orig2/\.git$,moved2/.git, .git/worktrees/orig2/gitdir >expect2 &&
169 + mv orig1 moved1 &&
170 + mv orig2 moved2 &&
171 + git worktree repair moved1 moved2 >out 2>err &&
172 + test_cmp expect1 .git/worktrees/orig1/gitdir &&
173 + test_cmp expect2 .git/worktrees/orig2/gitdir &&
174 + test_i18ngrep "gitdir incorrect:.*orig1/gitdir$" out &&
175 + test_i18ngrep "gitdir incorrect:.*orig2/gitdir$" out &&
176 + test_must_be_empty err
177 +'
178 +
179 test_done
worktree.c
+74
@@ -632,3 +632,77 @@ void repair_worktrees(worktree_repair_fn fn, void *cb_data)
632 repair_gitfile(*wt, fn, cb_data);
633 free_worktrees(worktrees);
634 }
635 +
636 +static int is_main_worktree_path(const char *path)
637 +{
638 + struct strbuf target = STRBUF_INIT;
639 + struct strbuf maindir = STRBUF_INIT;
640 + int cmp;
641 +
642 + strbuf_add_real_path(&target, path);
643 + strbuf_strip_suffix(&target, "/.git");
644 + strbuf_add_real_path(&maindir, get_git_common_dir());
645 + strbuf_strip_suffix(&maindir, "/.git");
646 + cmp = fspathcmp(maindir.buf, target.buf);
647 +
648 + strbuf_release(&maindir);
649 + strbuf_release(&target);
650 + return !cmp;
651 +}
652 +
653 +/*
654 + * Repair <repo>/worktrees/<id>/gitdir if missing, corrupt, or not pointing at
655 + * the worktree's path.
656 + */
657 +void repair_worktree_at_path(const char *path,
658 + worktree_repair_fn fn, void *cb_data)
659 +{
660 + struct strbuf dotgit = STRBUF_INIT;
661 + struct strbuf realdotgit = STRBUF_INIT;
662 + struct strbuf gitdir = STRBUF_INIT;
663 + struct strbuf olddotgit = STRBUF_INIT;
664 + char *backlink = NULL;
665 + const char *repair = NULL;
666 + int err;
667 +
668 + if (!fn)
669 + fn = repair_noop;
670 +
671 + if (is_main_worktree_path(path))
672 + goto done;
673 +
674 + strbuf_addf(&dotgit, "%s/.git", path);
675 + if (!strbuf_realpath(&realdotgit, dotgit.buf, 0)) {
676 + fn(1, path, _("not a valid path"), cb_data);
677 + goto done;
678 + }
679 +
680 + backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
681 + if (err == READ_GITFILE_ERR_NOT_A_FILE) {
682 + fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
683 + goto done;
684 + } else if (err) {
685 + fn(1, realdotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
686 + goto done;
687 + }
688 +
689 + strbuf_addf(&gitdir, "%s/gitdir", backlink);
690 + if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
691 + repair = _("gitdir unreadable");
692 + else {
693 + strbuf_rtrim(&olddotgit);
694 + if (fspathcmp(olddotgit.buf, realdotgit.buf))
695 + repair = _("gitdir incorrect");
696 + }
697 +
698 + if (repair) {
699 + fn(0, gitdir.buf, repair, cb_data);
700 + write_file(gitdir.buf, "%s", realdotgit.buf);
701 + }
702 +done:
703 + free(backlink);
704 + strbuf_release(&olddotgit);
705 + strbuf_release(&gitdir);
706 + strbuf_release(&realdotgit);
707 + strbuf_release(&dotgit);
708 +}
worktree.h
+12
@@ -100,6 +100,18 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path,
100 */
101 void repair_worktrees(worktree_repair_fn, void *cb_data);
102
103 +/*
104 + * Repair administrative files corresponding to the worktree at the given path.
105 + * The worktree's .git file pointing at the repository must be intact for the
106 + * repair to succeed. Useful for re-associating an orphaned worktree with the
107 + * repository if the worktree has been moved manually (without using "git
108 + * worktree move"). For each repair made or error encountered while attempting
109 + * a repair, the callback function, if non-NULL, is called with the path of the
110 + * worktree and a description of the repair or error, along with the callback
111 + * user-data.
112 + */
113 +void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
114 +
115 /*
116 * Free up the memory for worktree(s)
117 */