Skip to content

Commit 6d1700d

Browse files
bmwillgitster
authored andcommitted
fetch: refactor to make function args narrower
Refactor find_non_local_tags and get_ref_map to only take the information they need instead of the entire transport struct. Besides improving code clarity, this also improves their flexibility, allowing for a different set of refs to be used instead of relying on the ones stored in the transport struct. Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 05c4422 commit 6d1700d

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

builtin/fetch.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,17 @@ static int will_fetch(struct ref **head, const unsigned char *sha1)
254254
return 0;
255255
}
256256

257-
static void find_non_local_tags(struct transport *transport,
258-
struct ref **head,
259-
struct ref ***tail)
257+
static void find_non_local_tags(const struct ref *refs,
258+
struct ref **head,
259+
struct ref ***tail)
260260
{
261261
struct string_list existing_refs = STRING_LIST_INIT_DUP;
262262
struct string_list remote_refs = STRING_LIST_INIT_NODUP;
263263
const struct ref *ref;
264264
struct string_list_item *item = NULL;
265265

266266
for_each_ref(add_existing, &existing_refs);
267-
for (ref = transport_get_remote_refs(transport, NULL); ref; ref = ref->next) {
267+
for (ref = refs; ref; ref = ref->next) {
268268
if (!starts_with(ref->name, "refs/tags/"))
269269
continue;
270270

@@ -338,35 +338,20 @@ static void find_non_local_tags(struct transport *transport,
338338
string_list_clear(&remote_refs, 0);
339339
}
340340

341-
static struct ref *get_ref_map(struct transport *transport,
341+
static struct ref *get_ref_map(struct remote *remote,
342+
const struct ref *remote_refs,
342343
struct refspec *rs,
343344
int tags, int *autotags)
344345
{
345346
int i;
346347
struct ref *rm;
347348
struct ref *ref_map = NULL;
348349
struct ref **tail = &ref_map;
349-
struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
350350

351351
/* opportunistically-updated references: */
352352
struct ref *orefs = NULL, **oref_tail = &orefs;
353353

354354
struct string_list existing_refs = STRING_LIST_INIT_DUP;
355-
const struct ref *remote_refs;
356-
357-
if (rs->nr)
358-
refspec_ref_prefixes(rs, &ref_prefixes);
359-
else if (transport->remote && transport->remote->fetch.nr)
360-
refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
361-
362-
if (ref_prefixes.argc &&
363-
(tags == TAGS_SET || (tags == TAGS_DEFAULT && !rs->nr))) {
364-
argv_array_push(&ref_prefixes, "refs/tags/");
365-
}
366-
367-
remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
368-
369-
argv_array_clear(&ref_prefixes);
370355

371356
if (rs->nr) {
372357
struct refspec *fetch_refspec;
@@ -403,15 +388,14 @@ static struct ref *get_ref_map(struct transport *transport,
403388
if (refmap.nr)
404389
fetch_refspec = &refmap;
405390
else
406-
fetch_refspec = &transport->remote->fetch;
391+
fetch_refspec = &remote->fetch;
407392

408393
for (i = 0; i < fetch_refspec->nr; i++)
409394
get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
410395
} else if (refmap.nr) {
411396
die("--refmap option is only meaningful with command-line refspec(s).");
412397
} else {
413398
/* Use the defaults */
414-
struct remote *remote = transport->remote;
415399
struct branch *branch = branch_get(NULL);
416400
int has_merge = branch_has_merge_config(branch);
417401
if (remote &&
@@ -450,7 +434,7 @@ static struct ref *get_ref_map(struct transport *transport,
450434
/* also fetch all tags */
451435
get_fetch_map(remote_refs, tag_refspec, &tail, 0);
452436
else if (tags == TAGS_DEFAULT && *autotags)
453-
find_non_local_tags(transport, &ref_map, &tail);
437+
find_non_local_tags(remote_refs, &ref_map, &tail);
454438

455439
/* Now append any refs to be updated opportunistically: */
456440
*tail = orefs;
@@ -1143,6 +1127,8 @@ static int do_fetch(struct transport *transport,
11431127
struct ref *ref_map;
11441128
int autotags = (transport->remote->fetch_tags == 1);
11451129
int retcode = 0;
1130+
const struct ref *remote_refs;
1131+
struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
11461132

11471133
if (tags == TAGS_DEFAULT) {
11481134
if (transport->remote->fetch_tags == 2)
@@ -1158,7 +1144,21 @@ static int do_fetch(struct transport *transport,
11581144
goto cleanup;
11591145
}
11601146

1161-
ref_map = get_ref_map(transport, rs, tags, &autotags);
1147+
if (rs->nr)
1148+
refspec_ref_prefixes(rs, &ref_prefixes);
1149+
else if (transport->remote && transport->remote->fetch.nr)
1150+
refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
1151+
1152+
if (ref_prefixes.argc &&
1153+
(tags == TAGS_SET || (tags == TAGS_DEFAULT && !rs->nr))) {
1154+
argv_array_push(&ref_prefixes, "refs/tags/");
1155+
}
1156+
1157+
remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
1158+
argv_array_clear(&ref_prefixes);
1159+
1160+
ref_map = get_ref_map(transport->remote, remote_refs, rs,
1161+
tags, &autotags);
11621162
if (!update_head_ok)
11631163
check_not_current_branch(ref_map);
11641164

@@ -1190,7 +1190,7 @@ static int do_fetch(struct transport *transport,
11901190
if (tags == TAGS_DEFAULT && autotags) {
11911191
struct ref **tail = &ref_map;
11921192
ref_map = NULL;
1193-
find_non_local_tags(transport, &ref_map, &tail);
1193+
find_non_local_tags(remote_refs, &ref_map, &tail);
11941194
if (ref_map)
11951195
backfill_tags(transport, ref_map);
11961196
free_refs(ref_map);

0 commit comments

Comments
 (0)