Skip to content

Commit 7a52a8c

Browse files
To1negitster
authored andcommitted
clone: introduce struct clone_opts in builtin/clone.c
There is a lot of state stored in global variables in builtin/clone.c. In the long run we'd like to remove many of those. Introduce `struct clone_opts` in this file. This struct will be used to contain all details needed to perform the clone. The struct object can be thrown around to all the functions that need these details. The first field we're adding is `wants_head`. In some scenarios (specifically when both `--single-branch` and `--branch` are given) we are not interested in `HEAD` on the remote. The field `wants_head` in `struct clone_opts` will hold this information. We could have put `option_branch` and `option_single_branch` into that struct instead, but in a following commit we'll be using `wants_head` as well. Signed-off-by: Toon Claes <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2ca67c6 commit 7a52a8c

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

builtin/clone.c

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@
5757
*
5858
*/
5959

60+
struct clone_opts {
61+
int wants_head;
62+
};
63+
#define CLONE_OPTS_INIT { \
64+
.wants_head = 1 /* default enabled */ \
65+
}
66+
6067
static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
6168
static int option_local = -1, option_no_hardlinks, option_shared;
6269
static int option_tags = 1; /* default enabled */
@@ -429,23 +436,24 @@ static struct ref *find_remote_branch(const struct ref *refs, const char *branch
429436
return ref;
430437
}
431438

432-
static struct ref *wanted_peer_refs(const struct ref *refs,
433-
struct refspec *refspec)
439+
static struct ref *wanted_peer_refs(struct clone_opts *opts,
440+
const struct ref *refs,
441+
struct refspec *refspec)
434442
{
435-
struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
436-
struct ref *local_refs = head;
437-
struct ref **tail = local_refs ? &local_refs->next : &local_refs;
443+
struct ref *local_refs = NULL;
444+
struct ref **tail = &local_refs;
438445
struct ref *to_free = NULL;
439446

440-
if (option_single_branch) {
441-
if (!option_branch)
447+
if (opts->wants_head) {
448+
struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
449+
if (head)
450+
tail_link_ref(head, &tail);
451+
if (option_single_branch)
442452
refs = to_free = guess_remote_head(head, refs, 0);
443-
else {
444-
free_one_ref(head);
445-
local_refs = head = NULL;
446-
tail = &local_refs;
447-
refs = to_free = copy_ref(find_remote_branch(refs, option_branch));
448-
}
453+
} else if (option_single_branch) {
454+
local_refs = NULL;
455+
tail = &local_refs;
456+
refs = to_free = copy_ref(find_remote_branch(refs, option_branch));
449457
}
450458

451459
for (size_t i = 0; i < refspec->nr; i++)
@@ -893,6 +901,8 @@ int cmd_clone(int argc,
893901
struct string_list server_options = STRING_LIST_INIT_NODUP;
894902
const char *bundle_uri = NULL;
895903

904+
struct clone_opts opts = CLONE_OPTS_INIT;
905+
896906
struct transport_ls_refs_options transport_ls_refs_options =
897907
TRANSPORT_LS_REFS_OPTIONS_INIT;
898908

@@ -1343,9 +1353,13 @@ int cmd_clone(int argc,
13431353
if (option_not.nr)
13441354
transport_set_option(transport, TRANS_OPT_DEEPEN_NOT,
13451355
(const char *)&option_not);
1346-
if (option_single_branch)
1356+
if (option_single_branch) {
13471357
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
13481358

1359+
if (option_branch)
1360+
opts.wants_head = 0;
1361+
}
1362+
13491363
if (option_upload_pack)
13501364
transport_set_option(transport, TRANS_OPT_UPLOADPACK,
13511365
option_upload_pack);
@@ -1454,7 +1468,7 @@ int cmd_clone(int argc,
14541468
}
14551469

14561470
if (refs)
1457-
mapped_refs = wanted_peer_refs(refs, &remote->fetch);
1471+
mapped_refs = wanted_peer_refs(&opts, refs, &remote->fetch);
14581472

14591473
if (mapped_refs) {
14601474
/*

remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ int count_refspec_match(const char *pattern,
12341234
}
12351235
}
12361236

1237-
static void tail_link_ref(struct ref *ref, struct ref ***tail)
1237+
void tail_link_ref(struct ref *ref, struct ref ***tail)
12381238
{
12391239
**tail = ref;
12401240
while (ref->next)

remote.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ struct ref *alloc_ref(const char *name);
219219
struct ref *copy_ref(const struct ref *ref);
220220
struct ref *copy_ref_list(const struct ref *ref);
221221
int count_refspec_match(const char *, struct ref *refs, struct ref **matched_ref);
222+
/*
223+
* Put a ref in the tail and prepare tail for adding another one.
224+
* *tail is the pointer to the tail of the list of refs.
225+
*/
226+
void tail_link_ref(struct ref *ref, struct ref ***tail);
222227

223228
int check_ref_type(const struct ref *ref, int flags);
224229

0 commit comments

Comments
 (0)