From 4aae3e2f88e0a3dbe0dbc6528ed3cdfb00147862 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Fri, 30 Sep 2022 12:44:27 -0400 Subject: [PATCH] scalar: add --no-src option Some users have strong aversions to Scalar's opinion that the repository should be in a 'src' directory, even though it creates a clean slate for placing build outputs in adjacent directories. The --no-src option allows users to opt-out of the default behavior. The parse-opt logic automatically figures out that '--src' is a possible flag that negates '--no-src'. Signed-off-by: Derrick Stolee --- Documentation/scalar.txt | 5 ++++- scalar.c | 14 ++++++++++++-- t/t9210-scalar.sh | 8 ++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Documentation/scalar.txt b/Documentation/scalar.txt index 0424d020d9b9f4..35f67801e92a98 100644 --- a/Documentation/scalar.txt +++ b/Documentation/scalar.txt @@ -9,7 +9,7 @@ SYNOPSIS -------- [verse] scalar clone [--single-branch] [--branch ] [--full-clone] - [--local-cache-path ] [--cache-server-url ] + [--local-cache-path ] [--cache-server-url ] [--[no-]src] [] scalar list scalar register [] @@ -83,6 +83,9 @@ remote-tracking branch for the branch this option was used for the initial cloning. If the HEAD at the remote did not point at any branch when `--single-branch` clone was made, no remote-tracking branch is created. +--no-src:: + Skip adding a `src` directory within the target enlistment. + --[no-]full-clone:: A sparse-checkout is initialized by default. This behavior can be turned off via `--full-clone`. diff --git a/scalar.c b/scalar.c index ac1defeae754f0..142672976c6584 100644 --- a/scalar.c +++ b/scalar.c @@ -707,6 +707,8 @@ static int cmd_clone(int argc, const char **argv) int full_clone = 0, single_branch = 0, show_progress = isatty(2); const char *cache_server_url = NULL, *local_cache_root = NULL; char *default_cache_server_url = NULL, *local_cache_root_abs = NULL; + const char *enlistment_parent; + int no_src = 0; struct option clone_options[] = { OPT_STRING('b', "branch", &branch, N_(""), N_("branch to checkout after clone")), @@ -715,6 +717,8 @@ static int cmd_clone(int argc, const char **argv) OPT_BOOL(0, "single-branch", &single_branch, N_("only download metadata for the branch that will " "be checked out")), + OPT_BOOL(0, "no-src", &no_src, + N_("skip creating a 'src' directory")), OPT_STRING(0, "cache-server-url", &cache_server_url, N_(""), N_("the url or friendly name of the cache server")), @@ -765,7 +769,13 @@ static int cmd_clone(int argc, const char **argv) ensure_absolute_path(enlistment, &enlistment); - dir = xstrfmt("%s/src", enlistment); + if (!no_src) { + dir = xstrfmt("%s/src", enlistment); + enlistment_parent = "../.."; + } else { + dir = xstrdup(enlistment); + enlistment_parent = ".."; + } if (!local_cache_root) local_cache_root = local_cache_root_abs = @@ -806,7 +816,7 @@ static int cmd_clone(int argc, const char **argv) struct strbuf path = STRBUF_INIT; strbuf_addstr(&path, enlistment); - if (chdir("../..") < 0 || + if (chdir(enlistment_parent) < 0 || remove_dir_recursively(&path, 0) < 0) die(_("'--local-cache-path' cannot be inside the src " "folder;\nCould not remove '%s'"), enlistment); diff --git a/t/t9210-scalar.sh b/t/t9210-scalar.sh index 548bff71aeb094..43c4208cdc31b1 100755 --- a/t/t9210-scalar.sh +++ b/t/t9210-scalar.sh @@ -391,4 +391,12 @@ test_expect_success '`scalar delete` with existing repo' ' test_path_is_missing existing ' +test_expect_success '`scalar clone --no-src`' ' + scalar clone --src "file://$(pwd)" with-src && + scalar clone --no-src "file://$(pwd)" without-src && + + test_path_is_dir with-src/src && + test_path_is_missing without-src/src +' + test_done