summaryrefslogtreecommitdiff
path: root/contrib/scalar/scalar.c
diff options
context:
space:
mode:
authorLibravatar Matthew John Cheetham <mjcheetham@outlook.com>2021-12-03 13:34:28 +0000
committerLibravatar Junio C Hamano <gitster@pobox.com>2021-12-04 21:52:24 -0800
commitd85ada7cbdc7a1ff6b98891cec87e54ea4a303ae (patch)
tree4ba5fa07744dc433b03545ab599eaa00bda966c7 /contrib/scalar/scalar.c
parentscalar: teach 'reconfigure' to optionally handle all registered enlistments (diff)
downloadtgif-d85ada7cbdc7a1ff6b98891cec87e54ea4a303ae.tar.xz
scalar: implement the `delete` command
Delete an enlistment by first unregistering the repository and then deleting the enlistment directory (usually the directory containing the worktree `src/` directory). On Windows, if the current directory is inside the enlistment's directory, change to the parent of the enlistment directory, to allow us to delete the enlistment (directories used by processes e.g. as current working directories cannot be deleted on Windows). Co-authored-by: Victoria Dye <vdye@github.com> Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'contrib/scalar/scalar.c')
-rw-r--r--contrib/scalar/scalar.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/contrib/scalar/scalar.c b/contrib/scalar/scalar.c
index 305b080663..d4303c7c4a 100644
--- a/contrib/scalar/scalar.c
+++ b/contrib/scalar/scalar.c
@@ -8,6 +8,8 @@
#include "config.h"
#include "run-command.h"
#include "refs.h"
+#include "dir.h"
+#include "packfile.h"
/*
* Remove the deepest subdirectory in the provided path string. Path must not
@@ -328,6 +330,33 @@ static char *remote_default_branch(const char *url)
return NULL;
}
+static int delete_enlistment(struct strbuf *enlistment)
+{
+#ifdef WIN32
+ struct strbuf parent = STRBUF_INIT;
+#endif
+
+ if (unregister_dir())
+ die(_("failed to unregister repository"));
+
+#ifdef WIN32
+ /*
+ * Change the current directory to one outside of the enlistment so
+ * that we may delete everything underneath it.
+ */
+ strbuf_addbuf(&parent, enlistment);
+ strbuf_parent_directory(&parent);
+ if (chdir(parent.buf) < 0)
+ die_errno(_("could not switch to '%s'"), parent.buf);
+ strbuf_release(&parent);
+#endif
+
+ if (remove_dir_recursively(enlistment, 0))
+ die(_("failed to delete enlistment directory"));
+
+ return 0;
+}
+
static int cmd_clone(int argc, const char **argv)
{
const char *branch = NULL;
@@ -688,6 +717,39 @@ static int cmd_unregister(int argc, const char **argv)
return unregister_dir();
}
+static int cmd_delete(int argc, const char **argv)
+{
+ char *cwd = xgetcwd();
+ struct option options[] = {
+ OPT_END(),
+ };
+ const char * const usage[] = {
+ N_("scalar delete <enlistment>"),
+ NULL
+ };
+ struct strbuf enlistment = STRBUF_INIT;
+ int res = 0;
+
+ argc = parse_options(argc, argv, NULL, options,
+ usage, 0);
+
+ if (argc != 1)
+ usage_with_options(usage, options);
+
+ setup_enlistment_directory(argc, argv, usage, options, &enlistment);
+
+ if (dir_inside_of(cwd, enlistment.buf) >= 0)
+ res = error(_("refusing to delete current working directory"));
+ else {
+ close_object_store(the_repository->objects);
+ res = delete_enlistment(&enlistment);
+ }
+ strbuf_release(&enlistment);
+ free(cwd);
+
+ return res;
+}
+
static struct {
const char *name;
int (*fn)(int, const char **);
@@ -698,6 +760,7 @@ static struct {
{ "unregister", cmd_unregister },
{ "run", cmd_run },
{ "reconfigure", cmd_reconfigure },
+ { "delete", cmd_delete },
{ NULL, NULL},
};