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.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
  • Loading branch information
derrickstolee committed Sep 30, 2022
1 parent 0489e9e commit 50727bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
7 changes: 6 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 @@ -84,6 +84,11 @@ 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::
Specify if the repository should be created within a `src` directory
within `<enlistment>`. This is the default behavior, so use
`--no-src` to opt-out of the creation of the `src` directory.

--[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 @@ -811,6 +811,8 @@ static int cmd_clone(int argc, const char **argv)
int full_clone = 0, single_branch = 0, dummy = 0;
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 src = 1;
struct option clone_options[] = {
OPT_STRING('b', "branch", &branch, N_("<branch>"),
N_("branch to checkout after clone")),
Expand All @@ -819,6 +821,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, "src", &src,
N_("create repository within '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 @@ -869,7 +873,13 @@ static int cmd_clone(int argc, const char **argv)

ensure_absolute_path(enlistment, &enlistment);

dir = xstrfmt("%s/src", enlistment);
if (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 @@ -910,7 +920,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/t9099-scalar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,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 50727bb

Please sign in to comment.