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>
This commit is contained in:
Johannes Schindelin 2021-12-03 13:34:21 +00:00 коммит произвёл Junio C Hamano
Родитель c76a53eb71
Коммит f5f0842d0b
2 изменённых файлов: 61 добавлений и 0 удалений

Просмотреть файл

@ -269,6 +269,24 @@ static int cmd_register(int argc, const char **argv)
return register_dir();
}
static int remove_deleted_enlistment(struct strbuf *path)
{
int res = 0;
strbuf_realpath_forgiving(path, path->buf, 1);
if (run_git("config", "--global",
"--unset", "--fixed-value",
"scalar.repo", path->buf, NULL) < 0)
res = -1;
if (run_git("config", "--global",
"--unset", "--fixed-value",
"maintenance.repo", path->buf, NULL) < 0)
res = -1;
return res;
}
static int cmd_unregister(int argc, const char **argv)
{
struct option options[] = {
@ -282,6 +300,34 @@ static int cmd_unregister(int argc, const char **argv)
argc = parse_options(argc, argv, NULL, options,
usage, 0);
/*
* Be forgiving when the enlistment or worktree does not even exist any
* longer; This can be the case if a user deleted the worktree by
* mistake and _still_ wants to unregister the thing.
*/
if (argc == 1) {
struct strbuf src_path = STRBUF_INIT, workdir_path = STRBUF_INIT;
strbuf_addf(&src_path, "%s/src/.git", argv[0]);
strbuf_addf(&workdir_path, "%s/.git", argv[0]);
if (!is_directory(src_path.buf) && !is_directory(workdir_path.buf)) {
/* remove possible matching registrations */
int res = -1;
strbuf_strip_suffix(&src_path, "/.git");
res = remove_deleted_enlistment(&src_path) && res;
strbuf_strip_suffix(&workdir_path, "/.git");
res = remove_deleted_enlistment(&workdir_path) && res;
strbuf_release(&src_path);
strbuf_release(&workdir_path);
return res;
}
strbuf_release(&src_path);
strbuf_release(&workdir_path);
}
setup_enlistment_directory(argc, argv, usage, options, NULL);
return unregister_dir();

Просмотреть файл

@ -14,4 +14,19 @@ test_expect_success 'scalar shows a usage' '
test_expect_code 129 scalar -h
'
test_expect_success 'scalar unregister' '
git init vanish/src &&
scalar register vanish/src &&
git config --get --global --fixed-value \
maintenance.repo "$(pwd)/vanish/src" &&
scalar list >scalar.repos &&
grep -F "$(pwd)/vanish/src" scalar.repos &&
rm -rf vanish/src/.git &&
scalar unregister vanish &&
test_must_fail git config --get --global --fixed-value \
maintenance.repo "$(pwd)/vanish/src" &&
scalar list >scalar.repos &&
! grep -F "$(pwd)/vanish/src" scalar.repos
'
test_done