@matheusbsbs / matheus-bagliano / commits / 4a3ce479ce
Fork 0

worktree: prune duplicate entries referencing same worktree path

A fundamental restriction of linked working trees is that there must only ever be a single worktree associated with a particular path, thus "git worktree add" explicitly disallows creation of a new worktree at the same location as an existing registered worktree. Nevertheless, users can still "shoot themselves in the foot" by mucking with administrative files in .git/worktree/<id>/. Worse, "git worktree move" is careless[1] and allows a worktree to be moved atop a registered but missing worktree (which can happen, for instance, if the worktree is on removable media). For instance: $ git clone foo.git $ cd foo $ git worktree add ../bar $ git worktree add ../baz $ rm -rf ../bar $ git worktree move ../baz ../bar $ git worktree list .../foo beefd00f [master] .../bar beefd00f [bar] .../bar beefd00f [baz] Help users recover from this form of corruption by teaching "git worktree prune" to detect when multiple worktrees are associated with the same path. [1]: A subsequent commit will fix "git worktree move" validation to be more strict. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jun 10, 2020 at 02:30 UTC 4a3ce479ce7c8f268f42b725e471e35967da9b4f
2 files changed +55 -6
builtin/worktree.c
+43 -6
@@ -67,7 +67,12 @@ static void delete_worktrees_dir_if_empty(void)
67 rmdir(git_path("worktrees")); /* ignore failed removal */
68 }
69
70 -static int should_prune_worktree(const char *id, struct strbuf *reason)
70 +/*
71 + * Return true if worktree entry should be pruned, along with the reason for
72 + * pruning. Otherwise, return false and the worktree's path, or NULL if it
73 + * cannot be determined. Caller is responsible for freeing returned path.
74 + */
75 +static int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath)
76 {
77 struct stat st;
78 char *path;
@@ -75,6 +80,7 @@ static int should_prune_worktree(const char *id, struct strbuf *reason)
80 size_t len;
81 ssize_t read_result;
82
83 + *wtpath = NULL;
84 if (!is_directory(git_path("worktrees/%s", id))) {
85 strbuf_addstr(reason, _("not a valid directory"));
86 return 1;
@@ -120,16 +126,17 @@ static int should_prune_worktree(const char *id, struct strbuf *reason)
126 }
127 path[len] = '\0';
128 if (!file_exists(path)) {
123 - free(path);
129 if (stat(git_path("worktrees/%s/index", id), &st) ||
130 st.st_mtime <= expire) {
131 strbuf_addstr(reason, _("gitdir file points to non-existent location"));
132 + free(path);
133 return 1;
134 } else {
135 + *wtpath = path;
136 return 0;
137 }
138 }
132 - free(path);
139 + *wtpath = path;
140 return 0;
141 }
142
@@ -141,22 +148,52 @@ static void prune_worktree(const char *id, const char *reason)
148 delete_git_dir(id);
149 }
150
151 +static int prune_cmp(const void *a, const void *b)
152 +{
153 + const struct string_list_item *x = a;
154 + const struct string_list_item *y = b;
155 + int c;
156 +
157 + if ((c = fspathcmp(x->string, y->string)))
158 + return c;
159 + /* paths same; sort by .git/worktrees/<id> */
160 + return strcmp(x->util, y->util);
161 +}
162 +
163 +static void prune_dups(struct string_list *l)
164 +{
165 + int i;
166 +
167 + QSORT(l->items, l->nr, prune_cmp);
168 + for (i = 1; i < l->nr; i++) {
169 + if (!fspathcmp(l->items[i].string, l->items[i - 1].string))
170 + prune_worktree(l->items[i].util, "duplicate entry");
171 + }
172 +}
173 +
174 static void prune_worktrees(void)
175 {
176 struct strbuf reason = STRBUF_INIT;
177 + struct string_list kept = STRING_LIST_INIT_NODUP;
178 DIR *dir = opendir(git_path("worktrees"));
179 struct dirent *d;
180 if (!dir)
181 return;
182 while ((d = readdir(dir)) != NULL) {
183 + char *path;
184 if (is_dot_or_dotdot(d->d_name))
185 continue;
186 strbuf_reset(&reason);
155 - if (!should_prune_worktree(d->d_name, &reason))
156 - continue;
157 - prune_worktree(d->d_name, reason.buf);
187 + if (should_prune_worktree(d->d_name, &reason, &path))
188 + prune_worktree(d->d_name, reason.buf);
189 + else if (path)
190 + string_list_append(&kept, path)->util = xstrdup(d->d_name);
191 }
192 closedir(dir);
193 +
194 + prune_dups(&kept);
195 + string_list_clear(&kept, 1);
196 +
197 if (!show_only)
198 delete_worktrees_dir_if_empty();
199 strbuf_release(&reason);
t/t2401-worktree-prune.sh
+12
@@ -92,4 +92,16 @@ test_expect_success 'not prune proper checkouts' '
92 test -d .git/worktrees/nop
93 '
94
95 +test_expect_success 'prune duplicate (linked/linked)' '
96 + test_when_finished rm -fr .git/worktrees w1 w2 &&
97 + git worktree add --detach w1 &&
98 + git worktree add --detach w2 &&
99 + sed "s/w2/w1/" .git/worktrees/w2/gitdir >.git/worktrees/w2/gitdir.new &&
100 + mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir &&
101 + git worktree prune --verbose >actual &&
102 + test_i18ngrep "duplicate entry" actual &&
103 + test -d .git/worktrees/w1 &&
104 + ! test -d .git/worktrees/w2
105 +'
106 +
107 test_done