scalar: let 'unregister' handle a deleted enlistment directory gracefully

When a user deleted an enlistment manually, let's be generous and _still_ unregister it. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 3, 2021 at 13:34 UTC f5f0842d0b53cfd9e1dd0707f230b4fe1af12720
2 files changed +61
contrib/scalar/scalar.c
+46
@@ -269,6 +269,24 @@ static int cmd_register(int argc, const char **argv)
269 return register_dir();
270 }
271
272 +static int remove_deleted_enlistment(struct strbuf *path)
273 +{
274 + int res = 0;
275 + strbuf_realpath_forgiving(path, path->buf, 1);
276 +
277 + if (run_git("config", "--global",
278 + "--unset", "--fixed-value",
279 + "scalar.repo", path->buf, NULL) < 0)
280 + res = -1;
281 +
282 + if (run_git("config", "--global",
283 + "--unset", "--fixed-value",
284 + "maintenance.repo", path->buf, NULL) < 0)
285 + res = -1;
286 +
287 + return res;
288 +}
289 +
290 static int cmd_unregister(int argc, const char **argv)
291 {
292 struct option options[] = {
@@ -282,6 +300,34 @@ static int cmd_unregister(int argc, const char **argv)
300 argc = parse_options(argc, argv, NULL, options,
301 usage, 0);
302
303 + /*
304 + * Be forgiving when the enlistment or worktree does not even exist any
305 + * longer; This can be the case if a user deleted the worktree by
306 + * mistake and _still_ wants to unregister the thing.
307 + */
308 + if (argc == 1) {
309 + struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
310 +
311 + strbuf_addf(&src_path, "%s/src/.git", argv[0]);
312 + strbuf_addf(&workdir_path, "%s/.git", argv[0]);
313 + if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
314 + /* remove possible matching registrations */
315 + int res = -1;
316 +
317 + strbuf_strip_suffix(&src_path, "/.git");
318 + res = remove_deleted_enlistment(&src_path) && res;
319 +
320 + strbuf_strip_suffix(&workdir_path, "/.git");
321 + res = remove_deleted_enlistment(&workdir_path) && res;
322 +
323 + strbuf_release(&src_path);
324 + strbuf_release(&workdir_path);
325 + return res;
326 + }
327 + strbuf_release(&src_path);
328 + strbuf_release(&workdir_path);
329 + }
330 +
331 setup_enlistment_directory(argc, argv, usage, options, NULL);
332
333 return unregister_dir();
contrib/scalar/t/t9099-scalar.sh
+15
@@ -14,4 +14,19 @@ test_expect_success 'scalar shows a usage' '
14 test_expect_code 129 scalar -h
15 '
16
17 +test_expect_success 'scalar unregister' '
18 + git init vanish/src &&
19 + scalar register vanish/src &&
20 + git config --get --global --fixed-value \
21 + maintenance.repo "$(pwd)/vanish/src" &&
22 + scalar list >scalar.repos &&
23 + grep -F "$(pwd)/vanish/src" scalar.repos &&
24 + rm -rf vanish/src/.git &&
25 + scalar unregister vanish &&
26 + test_must_fail git config --get --global --fixed-value \
27 + maintenance.repo "$(pwd)/vanish/src" &&
28 + scalar list >scalar.repos &&
29 + ! grep -F "$(pwd)/vanish/src" scalar.repos
30 +'
31 +
32 test_done