Skip to content

Commit f7415b4

Browse files
stefanbellergitster
authored andcommitted
clone: implement optional references
In a later patch we want to try to create alternates for submodules, but they might not exist in the referenced superproject. So add a way to skip the non existing references and report them. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e40800 commit f7415b4

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

Documentation/git-clone.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,16 @@ If you want to break the dependency of a repository cloned with `-s` on
9090
its source repository, you can simply run `git repack -a` to copy all
9191
objects from the source repository into a pack in the cloned repository.
9292

93-
--reference <repository>::
93+
--reference[-if-able] <repository>::
9494
If the reference repository is on the local machine,
9595
automatically setup `.git/objects/info/alternates` to
9696
obtain objects from the reference repository. Using
9797
an already existing repository as an alternate will
9898
require fewer objects to be copied from the repository
9999
being cloned, reducing network and local storage costs.
100+
When using the `--reference-if-able`, a non existing
101+
directory is skipped with a warning instead of aborting
102+
the clone.
100103
+
101104
*NOTE*: see the NOTE for the `--shared` option, and also the
102105
`--dissociate` option.

builtin/clone.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static int option_progress = -1;
5151
static enum transport_family family;
5252
static struct string_list option_config = STRING_LIST_INIT_NODUP;
5353
static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
54+
static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
5455
static int option_dissociate;
5556
static int max_jobs = -1;
5657

@@ -81,6 +82,8 @@ static struct option builtin_clone_options[] = {
8182
N_("directory from which templates will be used")),
8283
OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
8384
N_("reference repository")),
85+
OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
86+
N_("repo"), N_("reference repository")),
8487
OPT_BOOL(0, "dissociate", &option_dissociate,
8588
N_("use --reference only while cloning")),
8689
OPT_STRING('o', "origin", &option_origin, N_("name"),
@@ -283,24 +286,36 @@ static void strip_trailing_slashes(char *dir)
283286
static int add_one_reference(struct string_list_item *item, void *cb_data)
284287
{
285288
struct strbuf err = STRBUF_INIT;
286-
struct strbuf sb = STRBUF_INIT;
289+
int *required = cb_data;
287290
char *ref_git = compute_alternate_path(item->string, &err);
288291

289-
if (!ref_git)
290-
die("%s", err.buf);
291-
292-
strbuf_addf(&sb, "%s/objects", ref_git);
293-
add_to_alternates_file(sb.buf);
292+
if (!ref_git) {
293+
if (*required)
294+
die("%s", err.buf);
295+
else
296+
fprintf(stderr,
297+
_("info: Could not add alternate for '%s': %s\n"),
298+
item->string, err.buf);
299+
} else {
300+
struct strbuf sb = STRBUF_INIT;
301+
strbuf_addf(&sb, "%s/objects", ref_git);
302+
add_to_alternates_file(sb.buf);
303+
strbuf_release(&sb);
304+
}
294305

295-
free(ref_git);
296306
strbuf_release(&err);
297-
strbuf_release(&sb);
307+
free(ref_git);
298308
return 0;
299309
}
300310

301311
static void setup_reference(void)
302312
{
303-
for_each_string_list(&option_required_reference, add_one_reference, NULL);
313+
int required = 1;
314+
for_each_string_list(&option_required_reference,
315+
add_one_reference, &required);
316+
required = 0;
317+
for_each_string_list(&option_optional_reference,
318+
add_one_reference, &required);
304319
}
305320

306321
static void copy_alternates(struct strbuf *src, struct strbuf *dst,
@@ -952,7 +967,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
952967
git_config_set(key.buf, repo);
953968
strbuf_reset(&key);
954969

955-
if (option_required_reference.nr)
970+
if (option_required_reference.nr || option_optional_reference.nr)
956971
setup_reference();
957972

958973
fetch_pattern = value.buf;

0 commit comments

Comments
 (0)