Skip to content

Commit

Permalink
scalar: add --no-src option
Browse files Browse the repository at this point in the history
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 <derrickstolee@github.com>
  • Loading branch information
derrickstolee authored and dscho committed Aug 8, 2023
1 parent 0095742 commit 05c96a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Documentation/scalar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ SYNOPSIS
--------
[verse]
scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]
[--local-cache-path <path>] [--cache-server-url <url>]
[--local-cache-path <path>] [--cache-server-url <url>] [--[no-]src]
<url> [<enlistment>]
scalar list
scalar register [<enlistment>]
Expand Down Expand Up @@ -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`.
Expand Down
14 changes: 12 additions & 2 deletions scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_("<branch>"),
N_("branch to checkout after clone")),
Expand All @@ -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_("<url>"),
N_("the url or friendly name of the cache server")),
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions t/t9210-scalar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 05c96a4

Please sign in to comment.