summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohannes Schindelin <johannes.schindelin@gmx.de>2021-12-03 13:34:21 +0000
committerJunio C Hamano <gitster@pobox.com>2021-12-04 21:52:23 -0800
commitf5f0842d0b53cfd9e1dd0707f230b4fe1af12720 (patch)
treeb71d43a8fcf7213295785e706e53d03211dc222a
parentc76a53eb71c5d0d931b1f95a9933913ecef9e4ad (diff)
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>
-rw-r--r--contrib/scalar/scalar.c46
-rwxr-xr-xcontrib/scalar/t/t9099-scalar.sh15
2 files changed, 61 insertions, 0 deletions
diff --git a/contrib/scalar/scalar.c b/contrib/scalar/scalar.c
index 9ab9dffe3a..ec783e72ef 100644
--- a/contrib/scalar/scalar.c
+++ b/contrib/scalar/scalar.c
@@ -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();
diff --git a/contrib/scalar/t/t9099-scalar.sh b/contrib/scalar/t/t9099-scalar.sh
index 16f2b72b12..ef0e8d680d 100755
--- a/contrib/scalar/t/t9099-scalar.sh
+++ b/contrib/scalar/t/t9099-scalar.sh
@@ -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