Skip to content

Commit ad6f04c

Browse files
committed
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 4f81a8e + 7e78c87 commit ad6f04c

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
@@ -89,29 +89,38 @@ static void setup_enlistment_directory(int argc, const char **argv,
8989

9090
static int git_retries = 3;
9191

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

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

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

116125
strvec_clear(&argv);
117126
return res;
@@ -775,6 +784,7 @@ static int cmd_clone(int argc, const char **argv)
775784
const char *cache_server_url = NULL, *local_cache_root = NULL;
776785
char *default_cache_server_url = NULL, *local_cache_root_abs = NULL;
777786
int gvfs_protocol = -1;
787+
const char *ref_format = NULL;
778788

779789
struct option clone_options[] = {
780790
OPT_STRING('b', "branch", &branch, N_("<branch>"),
@@ -798,18 +808,22 @@ static int cmd_clone(int argc, const char **argv)
798808
OPT_STRING(0, "local-cache-path", &local_cache_root,
799809
N_("<path>"),
800810
N_("override the path for the local Scalar cache")),
811+
OPT_STRING(0, "ref-format", &ref_format, N_("format"),
812+
N_("specify the reference format to use")),
801813
OPT_HIDDEN_BOOL(0, "no-fetch-commits-and-trees",
802814
&dummy, N_("no longer used")),
803815
OPT_END(),
804816
};
805817
const char * const clone_usage[] = {
806818
N_("scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]\n"
807-
"\t[--[no-]src] [--[no-]tags] [--[no-]maintenance] <url> [<enlistment>]"),
819+
"\t[--[no-]src] [--[no-]tags] [--[no-]maintenance] [--ref-format <format>]\n"
820+
"\t<url> [<enlistment>]"),
808821
NULL
809822
};
810823
const char *url;
811824
char *enlistment = NULL, *dir = NULL;
812825
struct strbuf buf = STRBUF_INIT;
826+
struct strvec init_argv = STRVEC_INIT;
813827
int res;
814828

815829
argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
@@ -857,16 +871,26 @@ static int cmd_clone(int argc, const char **argv)
857871
if (!local_cache_root)
858872
die(_("could not determine local cache root"));
859873

860-
strbuf_reset(&buf);
874+
strvec_clear(&init_argv);
875+
strvec_pushf(&init_argv, "-c");
861876
if (branch)
862-
strbuf_addf(&buf, "init.defaultBranch=%s", branch);
877+
strvec_pushf(&init_argv, "init.defaultBranch=%s", branch);
863878
else {
864879
char *b = repo_default_branch_name(the_repository, 1);
865-
strbuf_addf(&buf, "init.defaultBranch=%s", b);
880+
strvec_pushf(&init_argv, "init.defaultBranch=%s", b);
866881
free(b);
867882
}
868883

869-
if ((res = run_git("-c", buf.buf, "init", "--", dir, NULL)))
884+
strvec_push(&init_argv, "init");
885+
886+
if (ref_format) {
887+
strvec_push(&init_argv, "--ref-format");
888+
strvec_push(&init_argv, ref_format);
889+
}
890+
891+
strvec_push(&init_argv, "--");
892+
strvec_push(&init_argv, dir);
893+
if ((res = run_git_argv(&init_argv)))
870894
goto cleanup;
871895

872896
if (chdir(dir) < 0) {
@@ -1016,6 +1040,7 @@ static int cmd_clone(int argc, const char **argv)
10161040
free(enlistment);
10171041
free(dir);
10181042
strbuf_release(&buf);
1043+
strvec_clear(&init_argv);
10191044
free(default_cache_server_url);
10201045
free(local_cache_root_abs);
10211046
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)