Skip to content

Commit 8bee93d

Browse files
mhaggergitster
authored andcommitted
Change fetch_pack() and friends to take string_list arguments
Instead of juggling <nr_heads,heads> (sometimes called <nr_match,match>), pass around the list of references to be sought in a single string_list variable called "sought". Future commits will make more use of string_list functionality. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 63c6945 commit 8bee93d

File tree

3 files changed

+51
-60
lines changed

3 files changed

+51
-60
lines changed

builtin/fetch-pack.c

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -525,27 +525,27 @@ static void mark_recent_complete_commits(unsigned long cutoff)
525525
}
526526
}
527527

528-
static void filter_refs(struct ref **refs, int nr_match, char **match)
528+
static void filter_refs(struct ref **refs, struct string_list *sought)
529529
{
530530
struct ref **return_refs;
531531
struct ref *newlist = NULL;
532532
struct ref **newtail = &newlist;
533533
struct ref *ref, *next;
534534
struct ref *fastarray[32];
535-
int match_pos;
535+
int sought_pos;
536536

537-
if (nr_match && !args.fetch_all) {
538-
if (ARRAY_SIZE(fastarray) < nr_match)
539-
return_refs = xcalloc(nr_match, sizeof(struct ref *));
537+
if (sought->nr && !args.fetch_all) {
538+
if (ARRAY_SIZE(fastarray) < sought->nr)
539+
return_refs = xcalloc(sought->nr, sizeof(struct ref *));
540540
else {
541541
return_refs = fastarray;
542-
memset(return_refs, 0, sizeof(struct ref *) * nr_match);
542+
memset(return_refs, 0, sizeof(struct ref *) * sought->nr);
543543
}
544544
}
545545
else
546546
return_refs = NULL;
547547

548-
match_pos = 0;
548+
sought_pos = 0;
549549
for (ref = *refs; ref; ref = next) {
550550
next = ref->next;
551551
if (!memcmp(ref->name, "refs/", 5) &&
@@ -560,17 +560,17 @@ static void filter_refs(struct ref **refs, int nr_match, char **match)
560560
}
561561
else {
562562
int cmp = -1;
563-
while (match_pos < nr_match) {
564-
cmp = strcmp(ref->name, match[match_pos]);
563+
while (sought_pos < sought->nr) {
564+
cmp = strcmp(ref->name, sought->items[sought_pos].string);
565565
if (cmp < 0) /* definitely do not have it */
566566
break;
567567
else if (cmp == 0) { /* definitely have it */
568-
match[match_pos][0] = '\0';
569-
return_refs[match_pos] = ref;
568+
sought->items[sought_pos].string[0] = '\0';
569+
return_refs[sought_pos] = ref;
570570
break;
571571
}
572572
else /* might have it; keep looking */
573-
match_pos++;
573+
sought_pos++;
574574
}
575575
if (!cmp)
576576
continue; /* we will link it later */
@@ -580,7 +580,7 @@ static void filter_refs(struct ref **refs, int nr_match, char **match)
580580

581581
if (!args.fetch_all) {
582582
int i;
583-
for (i = 0; i < nr_match; i++) {
583+
for (i = 0; i < sought->nr; i++) {
584584
ref = return_refs[i];
585585
if (ref) {
586586
*newtail = ref;
@@ -599,7 +599,7 @@ static void mark_alternate_complete(const struct ref *ref, void *unused)
599599
mark_complete(NULL, ref->old_sha1, 0, NULL);
600600
}
601601

602-
static int everything_local(struct ref **refs, int nr_match, char **match)
602+
static int everything_local(struct ref **refs, struct string_list *sought)
603603
{
604604
struct ref *ref;
605605
int retval;
@@ -650,7 +650,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
650650
}
651651
}
652652

653-
filter_refs(refs, nr_match, match);
653+
filter_refs(refs, sought);
654654

655655
for (retval = 1, ref = *refs; ref ; ref = ref->next) {
656656
const unsigned char *remote = ref->old_sha1;
@@ -781,8 +781,7 @@ static int get_pack(int xd[2], char **pack_lockfile)
781781

782782
static struct ref *do_fetch_pack(int fd[2],
783783
const struct ref *orig_ref,
784-
int nr_match,
785-
char **match,
784+
struct string_list *sought,
786785
char **pack_lockfile)
787786
{
788787
struct ref *ref = copy_ref_list(orig_ref);
@@ -839,7 +838,7 @@ static struct ref *do_fetch_pack(int fd[2],
839838
agent_len, agent_feature);
840839
}
841840

842-
if (everything_local(&ref, nr_match, match)) {
841+
if (everything_local(&ref, sought)) {
843842
packet_flush(fd[1]);
844843
goto all_done;
845844
}
@@ -859,16 +858,16 @@ static struct ref *do_fetch_pack(int fd[2],
859858
return ref;
860859
}
861860

862-
static int remove_duplicates(int nr_heads, char **heads)
861+
static int remove_duplicates(struct string_list *sought)
863862
{
864863
int src, dst;
865864

866-
if (!nr_heads)
865+
if (!sought->nr)
867866
return 0;
868867

869-
for (src = dst = 1; src < nr_heads; src++)
870-
if (strcmp(heads[src], heads[dst-1]))
871-
heads[dst++] = heads[src];
868+
for (src = dst = 1; src < sought->nr; src++)
869+
if (strcmp(sought->items[src].string, sought->items[dst-1].string))
870+
sought->items[dst++] = sought->items[src];
872871
return dst;
873872
}
874873

@@ -922,8 +921,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
922921
int i, ret;
923922
struct ref *ref = NULL;
924923
const char *dest = NULL;
925-
int alloc_heads = 0, nr_heads = 0;
926-
char **heads = NULL;
924+
struct string_list sought = STRING_LIST_INIT_DUP;
927925
int fd[2];
928926
char *pack_lockfile = NULL;
929927
char **pack_lockfile_ptr = NULL;
@@ -1000,9 +998,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
1000998
* Copy refs from cmdline to growable list, then append any
1001999
* refs from the standard input:
10021000
*/
1003-
ALLOC_GROW(heads, argc - i, alloc_heads);
10041001
for (; i < argc; i++)
1005-
heads[nr_heads++] = xstrdup(argv[i]);
1002+
string_list_append(&sought, xstrdup(argv[i]));
10061003
if (args.stdin_refs) {
10071004
if (args.stateless_rpc) {
10081005
/* in stateless RPC mode we use pkt-line to read
@@ -1015,17 +1012,14 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
10151012
break;
10161013
if (line[n-1] == '\n')
10171014
n--;
1018-
ALLOC_GROW(heads, nr_heads + 1, alloc_heads);
1019-
heads[nr_heads++] = xmemdupz(line, n);
1015+
string_list_append(&sought, xmemdupz(line, n));
10201016
}
10211017
}
10221018
else {
10231019
/* read from stdin one ref per line, until EOF */
10241020
struct strbuf line = STRBUF_INIT;
1025-
while (strbuf_getline(&line, stdin, '\n') != EOF) {
1026-
ALLOC_GROW(heads, nr_heads + 1, alloc_heads);
1027-
heads[nr_heads++] = strbuf_detach(&line, NULL);
1028-
}
1021+
while (strbuf_getline(&line, stdin, '\n') != EOF)
1022+
string_list_append(&sought, strbuf_detach(&line, NULL));
10291023
strbuf_release(&line);
10301024
}
10311025
}
@@ -1042,7 +1036,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
10421036
get_remote_heads(fd[0], &ref, 0, NULL);
10431037

10441038
ref = fetch_pack(&args, fd, conn, ref, dest,
1045-
nr_heads, heads, pack_lockfile_ptr);
1039+
&sought, pack_lockfile_ptr);
10461040
if (pack_lockfile) {
10471041
printf("lock %s\n", pack_lockfile);
10481042
fflush(stdout);
@@ -1053,17 +1047,19 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
10531047
ref = NULL;
10541048
ret = !ref;
10551049

1056-
if (!ret && nr_heads) {
1050+
if (!ret && sought.nr) {
10571051
/* If the heads to pull were given, we should have
10581052
* consumed all of them by matching the remote.
10591053
* Otherwise, 'git fetch remote no-such-ref' would
10601054
* silently succeed without issuing an error.
10611055
*/
1062-
for (i = 0; i < nr_heads; i++)
1063-
if (heads[i] && heads[i][0]) {
1064-
error("no such remote ref %s", heads[i]);
1056+
for (i = 0; i < sought.nr; i++) {
1057+
char *s = sought.items[i].string;
1058+
if (s && s[0]) {
1059+
error("no such remote ref %s", s);
10651060
ret = 1;
10661061
}
1062+
}
10671063
}
10681064
while (ref) {
10691065
printf("%s %s\n",
@@ -1074,17 +1070,11 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
10741070
return ret;
10751071
}
10761072

1077-
static int compare_heads(const void *a, const void *b)
1078-
{
1079-
return strcmp(*(const char **)a, *(const char **)b);
1080-
}
1081-
10821073
struct ref *fetch_pack(struct fetch_pack_args *my_args,
10831074
int fd[], struct child_process *conn,
10841075
const struct ref *ref,
10851076
const char *dest,
1086-
int nr_heads,
1087-
char **heads,
1077+
struct string_list *sought,
10881078
char **pack_lockfile)
10891079
{
10901080
struct stat st;
@@ -1098,16 +1088,16 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
10981088
st.st_mtime = 0;
10991089
}
11001090

1101-
if (heads && nr_heads) {
1102-
qsort(heads, nr_heads, sizeof(*heads), compare_heads);
1103-
nr_heads = remove_duplicates(nr_heads, heads);
1091+
if (sought->nr) {
1092+
sort_string_list(sought);
1093+
remove_duplicates(sought);
11041094
}
11051095

11061096
if (!ref) {
11071097
packet_flush(fd[1]);
11081098
die("no matching remote head");
11091099
}
1110-
ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);
1100+
ref_cpy = do_fetch_pack(fd, ref, sought, pack_lockfile);
11111101

11121102
if (args.depth > 0) {
11131103
struct cache_time mtime;

fetch-pack.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef FETCH_PACK_H
22
#define FETCH_PACK_H
33

4+
#include "string-list.h"
5+
46
struct fetch_pack_args {
57
const char *uploadpack;
68
int unpacklimit;
@@ -21,8 +23,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
2123
int fd[], struct child_process *conn,
2224
const struct ref *ref,
2325
const char *dest,
24-
int nr_heads,
25-
char **heads,
26+
struct string_list *sought,
2627
char **pack_lockfile);
2728

2829
#endif

transport.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ static int fetch_refs_via_pack(struct transport *transport,
518518
int nr_heads, struct ref **to_fetch)
519519
{
520520
struct git_transport_data *data = transport->data;
521-
char **heads = xmalloc(nr_heads * sizeof(*heads));
522-
char **origh = xmalloc(nr_heads * sizeof(*origh));
521+
struct string_list orig_sought = STRING_LIST_INIT_DUP;
522+
struct string_list sought = STRING_LIST_INIT_NODUP;
523523
const struct ref *refs;
524524
char *dest = xstrdup(transport->url);
525525
struct fetch_pack_args args;
@@ -537,8 +537,10 @@ static int fetch_refs_via_pack(struct transport *transport,
537537
args.no_progress = !transport->progress;
538538
args.depth = data->options.depth;
539539

540-
for (i = 0; i < nr_heads; i++)
541-
origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
540+
for (i = 0; i < nr_heads; i++) {
541+
string_list_append(&orig_sought, to_fetch[i]->name);
542+
string_list_append(&sought, orig_sought.items[orig_sought.nr - 1].string);
543+
}
542544

543545
if (!data->got_remote_heads) {
544546
connect_setup(transport, 0, 0);
@@ -548,7 +550,7 @@ static int fetch_refs_via_pack(struct transport *transport,
548550

549551
refs = fetch_pack(&args, data->fd, data->conn,
550552
refs_tmp ? refs_tmp : transport->remote_refs,
551-
dest, nr_heads, heads, &transport->pack_lockfile);
553+
dest, &sought, &transport->pack_lockfile);
552554
close(data->fd[0]);
553555
close(data->fd[1]);
554556
if (finish_connect(data->conn))
@@ -558,10 +560,8 @@ static int fetch_refs_via_pack(struct transport *transport,
558560

559561
free_refs(refs_tmp);
560562

561-
for (i = 0; i < nr_heads; i++)
562-
free(origh[i]);
563-
free(origh);
564-
free(heads);
563+
string_list_clear(&sought, 0);
564+
string_list_clear(&orig_sought, 0);
565565
free(dest);
566566
return (refs ? 0 : -1);
567567
}

0 commit comments

Comments
 (0)