Skip to content

Commit c94328f

Browse files
authored
Add --ref-format option to scalar clone (port to vfs-2.52.0) (#832)
Add the `--ref-format` option to the `scalar clone` command. This will allow users to opt-in to creating a Scalar repository using alternative ref storage backends, such as reftable. Example: ```shell scalar clone --ref-format reftable $URL ```
2 parents 98bfb51 + 33c414d commit c94328f

2 files changed

Lines changed: 70 additions & 15 deletions

File tree

scalar.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,38 @@ static void setup_enlistment_directory(int argc, const char **argv,
8888

8989
static int git_retries = 3;
9090

91+
static int run_git_argv(const struct strvec *argv)
92+
{
93+
int res = 0, attempts;
94+
95+
for (attempts = 0, res = 1;
96+
res && attempts < git_retries;
97+
attempts++) {
98+
struct child_process cmd = CHILD_PROCESS_INIT;
99+
100+
cmd.git_cmd = 1;
101+
strvec_pushv(&cmd.args, argv->v);
102+
res = run_command(&cmd);
103+
}
104+
105+
return res;
106+
}
107+
91108
LAST_ARG_MUST_BE_NULL
92109
static int run_git(const char *arg, ...)
93110
{
94111
va_list args;
95112
const char *p;
96113
struct strvec argv = STRVEC_INIT;
97-
int res = 0, attempts;
114+
int res;
98115

99116
va_start(args, arg);
100117
strvec_push(&argv, arg);
101118
while ((p = va_arg(args, const char *)))
102119
strvec_push(&argv, p);
103120
va_end(args);
104121

105-
for (attempts = 0, res = 1;
106-
res && attempts < git_retries;
107-
attempts++) {
108-
struct child_process cmd = CHILD_PROCESS_INIT;
109-
110-
cmd.git_cmd = 1;
111-
strvec_pushv(&cmd.args, argv.v);
112-
res = run_command(&cmd);
113-
}
122+
res = run_git_argv(&argv);
114123

115124
strvec_clear(&argv);
116125
return res;
@@ -765,6 +774,7 @@ static int cmd_clone(int argc, const char **argv)
765774
const char *cache_server_url = NULL, *local_cache_root = NULL;
766775
char *default_cache_server_url = NULL, *local_cache_root_abs = NULL;
767776
int gvfs_protocol = -1;
777+
const char *ref_format = NULL;
768778

769779
struct option clone_options[] = {
770780
OPT_STRING('b', "branch", &branch, N_("<branch>"),
@@ -788,18 +798,22 @@ static int cmd_clone(int argc, const char **argv)
788798
OPT_STRING(0, "local-cache-path", &local_cache_root,
789799
N_("<path>"),
790800
N_("override the path for the local Scalar cache")),
801+
OPT_STRING(0, "ref-format", &ref_format, N_("format"),
802+
N_("specify the reference format to use")),
791803
OPT_HIDDEN_BOOL(0, "no-fetch-commits-and-trees",
792804
&dummy, N_("no longer used")),
793805
OPT_END(),
794806
};
795807
const char * const clone_usage[] = {
796808
N_("scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]\n"
797-
"\t[--[no-]src] [--[no-]tags] [--[no-]maintenance] <url> [<enlistment>]"),
809+
"\t[--[no-]src] [--[no-]tags] [--[no-]maintenance] [--ref-format <format>]\n"
810+
"\t<url> [<enlistment>]"),
798811
NULL
799812
};
800813
const char *url;
801814
char *enlistment = NULL, *dir = NULL;
802815
struct strbuf buf = STRBUF_INIT;
816+
struct strvec init_argv = STRVEC_INIT;
803817
int res;
804818

805819
argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
@@ -847,16 +861,26 @@ static int cmd_clone(int argc, const char **argv)
847861
if (!local_cache_root)
848862
die(_("could not determine local cache root"));
849863

850-
strbuf_reset(&buf);
864+
strvec_clear(&init_argv);
865+
strvec_pushf(&init_argv, "-c");
851866
if (branch)
852-
strbuf_addf(&buf, "init.defaultBranch=%s", branch);
867+
strvec_pushf(&init_argv, "init.defaultBranch=%s", branch);
853868
else {
854869
char *b = repo_default_branch_name(the_repository, 1);
855-
strbuf_addf(&buf, "init.defaultBranch=%s", b);
870+
strvec_pushf(&init_argv, "init.defaultBranch=%s", b);
856871
free(b);
857872
}
858873

859-
if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
874+
strvec_push(&init_argv, "init");
875+
876+
if (ref_format) {
877+
strvec_push(&init_argv, "--ref-format");
878+
strvec_push(&init_argv, ref_format);
879+
}
880+
881+
strvec_push(&init_argv, "--");
882+
strvec_push(&init_argv, dir);
883+
if ((res = run_git_argv(&init_argv)))
860884
goto cleanup;
861885

862886
if (chdir(dir) < 0) {
@@ -1006,6 +1030,7 @@ static int cmd_clone(int argc, const char **argv)
10061030
free(enlistment);
10071031
free(dir);
10081032
strbuf_release(&buf);
1033+
strvec_clear(&init_argv);
10091034
free(default_cache_server_url);
10101035
free(local_cache_root_abs);
10111036
return res;

t/t9211-scalar-clone.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,34 @@ test_expect_success '`scalar clone --no-src`' '
201201
test_cmp with without
202202
'
203203

204+
test_expect_success '`scalar clone --ref-format`' '
205+
scalar clone "file://$(pwd)/to-clone" refs-default &&
206+
scalar clone --ref-format files "file://$(pwd)/to-clone" refs-files &&
207+
scalar clone --ref-format reftable "file://$(pwd)/to-clone" refs-reftable &&
208+
209+
test_path_is_dir refs-default/src &&
210+
test_path_is_dir refs-files/src &&
211+
test_path_is_dir refs-reftable/src &&
212+
213+
(
214+
cd refs-default/src &&
215+
case test_detect_ref_format in
216+
files)
217+
test_must_fail git config --local extensions.refstorage
218+
;;
219+
reftable)
220+
test_cmp_config reftable extensions.refstorage
221+
;;
222+
esac
223+
) &&
224+
(
225+
cd refs-files/src &&
226+
test_must_fail git config --local extensions.refstorage
227+
) &&
228+
(
229+
cd refs-reftable/src &&
230+
test_cmp_config reftable extensions.refstorage
231+
)
232+
'
233+
204234
test_done

0 commit comments

Comments
 (0)