Skip to content

Commit f5f0842

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c76a53e commit f5f0842

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

contrib/scalar/scalar.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ static int cmd_register(int argc, const char **argv)
269269
return register_dir();
270270
}
271271

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+
272290
static int cmd_unregister(int argc, const char **argv)
273291
{
274292
struct option options[] = {
@@ -282,6 +300,34 @@ static int cmd_unregister(int argc, const char **argv)
282300
argc = parse_options(argc, argv, NULL, options,
283301
usage, 0);
284302

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+
285331
setup_enlistment_directory(argc, argv, usage, options, NULL);
286332

287333
return unregister_dir();

contrib/scalar/t/t9099-scalar.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,19 @@ test_expect_success 'scalar shows a usage' '
1414
test_expect_code 129 scalar -h
1515
'
1616

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+
1732
test_done

0 commit comments

Comments
 (0)