Skip to content

Commit

Permalink
Merge pull request #536: Allow --no-src during clones and git worktre…
Browse files Browse the repository at this point in the history
…e after clones

These are two highly-requested items from an internal team considering a
move to Scalar using Azure Repos.

1. Remove the requirement that we create a `src` directory at clone time.

2. Allow `git worktree` even when using the GVFS protocol.

These are not difficult to implement. The `--no-src` option could even
be submitted upstream (though the commit will need to drop one bit about
an interaction with the local cache path).
  • Loading branch information
dscho authored and vdye committed May 17, 2023
2 parents 0916f37 + db6af15 commit f498850
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 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
6 changes: 3 additions & 3 deletions abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int is_directory(const char *path)
}

/* removes the last path component from 'path' except if 'path' is root */
static void strip_last_component(struct strbuf *path)
void strip_last_path_component(struct strbuf *path)
{
size_t offset = offset_1st_component(path->buf);
size_t len = path->len;
Expand Down Expand Up @@ -119,7 +119,7 @@ static char *strbuf_realpath_1(struct strbuf *resolved, const char *path,
continue; /* '.' component */
} else if (next.len == 2 && !strcmp(next.buf, "..")) {
/* '..' component; strip the last path component */
strip_last_component(resolved);
strip_last_path_component(resolved);
continue;
}

Expand Down Expand Up @@ -171,7 +171,7 @@ static char *strbuf_realpath_1(struct strbuf *resolved, const char *path,
* strip off the last component since it will
* be replaced with the contents of the symlink
*/
strip_last_component(resolved);
strip_last_path_component(resolved);
}

/*
Expand Down
5 changes: 5 additions & 0 deletions abspath.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ char *real_pathdup(const char *path, int die_on_error);
const char *absolute_path(const char *path);
char *absolute_pathdup(const char *path);

/**
* Remove the last path component from 'path' except if 'path' is root.
*/
void strip_last_path_component(struct strbuf *path);

/*
* Concatenate "prefix" (if len is non-zero) and "path", with no
* connecting characters (so "prefix" should end with a "/").
Expand Down
24 changes: 20 additions & 4 deletions scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "trace2.h"
#include "json-parser.h"
#include "remote.h"
#include "path.h"

static int is_unattended(void) {
return git_env_bool("Scalar_UNATTENDED", 0);
Expand Down Expand Up @@ -476,8 +477,13 @@ static char *default_cache_root(const char *root)
{
const char *env;

if (is_unattended())
return xstrfmt("%s/.scalarCache", root);
if (is_unattended()) {
struct strbuf path = STRBUF_INIT;
strbuf_addstr(&path, root);
strip_last_path_component(&path);
strbuf_addstr(&path, "/.scalarCache");
return strbuf_detach(&path, NULL);
}

#ifdef WIN32
(void)env;
Expand Down Expand Up @@ -701,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 @@ -709,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 @@ -759,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 @@ -800,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
10 changes: 9 additions & 1 deletion t/t9210-scalar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ test_expect_success '`scalar clone` with GVFS-enabled server' '
cache_key="url_$(printf "%s" http://$HOST_PORT/ |
tr A-Z a-z |
test-tool sha1)" &&
echo "$(pwd)/using-gvfs/.scalarCache/$cache_key" >expect &&
echo "$(pwd)/.scalarCache/$cache_key" >expect &&
git -C using-gvfs/src config gvfs.sharedCache >actual &&
test_cmp expect actual &&
Expand Down 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 f498850

Please sign in to comment.