@@ -525,72 +525,59 @@ static void mark_recent_complete_commits(unsigned long cutoff)
525
525
}
526
526
}
527
527
528
- static void filter_refs (struct ref * * refs , int nr_match , char * * match )
528
+ static int non_matching_ref (struct string_list_item * item , void * unused )
529
+ {
530
+ if (item -> util ) {
531
+ item -> util = NULL ;
532
+ return 0 ;
533
+ }
534
+ else
535
+ return 1 ;
536
+ }
537
+
538
+ static void filter_refs (struct ref * * refs , struct string_list * sought )
529
539
{
530
- struct ref * * return_refs ;
531
540
struct ref * newlist = NULL ;
532
541
struct ref * * newtail = & newlist ;
533
542
struct ref * ref , * next ;
534
- struct ref * fastarray [32 ];
535
- int match_pos ;
536
-
537
- if (nr_match && !args .fetch_all ) {
538
- if (ARRAY_SIZE (fastarray ) < nr_match )
539
- return_refs = xcalloc (nr_match , sizeof (struct ref * ));
540
- else {
541
- return_refs = fastarray ;
542
- memset (return_refs , 0 , sizeof (struct ref * ) * nr_match );
543
- }
544
- }
545
- else
546
- return_refs = NULL ;
543
+ int sought_pos ;
547
544
548
- match_pos = 0 ;
545
+ sought_pos = 0 ;
549
546
for (ref = * refs ; ref ; ref = next ) {
547
+ int keep = 0 ;
550
548
next = ref -> next ;
551
549
if (!memcmp (ref -> name , "refs/" , 5 ) &&
552
550
check_refname_format (ref -> name + 5 , 0 ))
553
551
; /* trash */
554
- else if (args .fetch_all &&
555
- (!args .depth || prefixcmp (ref -> name , "refs/tags/" ) )) {
556
- * newtail = ref ;
557
- ref -> next = NULL ;
558
- newtail = & ref -> next ;
559
- continue ;
560
- }
561
552
else {
562
- int cmp = -1 ;
563
- while (match_pos < nr_match ) {
564
- cmp = strcmp (ref -> name , match [match_pos ]);
565
- if (cmp < 0 ) /* definitely do not have it */
566
- break ;
567
- else if (cmp == 0 ) { /* definitely have it */
568
- match [match_pos ][0 ] = '\0' ;
569
- return_refs [match_pos ] = ref ;
553
+ while (sought_pos < sought -> nr ) {
554
+ int cmp = strcmp (ref -> name , sought -> items [sought_pos ].string );
555
+ if (cmp < 0 )
556
+ break ; /* definitely do not have it */
557
+ else if (cmp == 0 ) {
558
+ keep = 1 ; /* definitely have it */
559
+ sought -> items [sought_pos ++ ].util = "matched" ;
570
560
break ;
571
561
}
572
- else /* might have it; keep looking */
573
- match_pos ++ ;
562
+ else
563
+ sought_pos ++ ; /* might have it; keep looking */
574
564
}
575
- if (!cmp )
576
- continue ; /* we will link it later */
577
565
}
578
- free (ref );
579
- }
580
566
581
- if (!args .fetch_all ) {
582
- int i ;
583
- for (i = 0 ; i < nr_match ; i ++ ) {
584
- ref = return_refs [i ];
585
- if (ref ) {
586
- * newtail = ref ;
587
- ref -> next = NULL ;
588
- newtail = & ref -> next ;
589
- }
567
+ if (! keep && args .fetch_all &&
568
+ (!args .depth || prefixcmp (ref -> name , "refs/tags/" )))
569
+ keep = 1 ;
570
+
571
+ if (keep ) {
572
+ * newtail = ref ;
573
+ ref -> next = NULL ;
574
+ newtail = & ref -> next ;
575
+ } else {
576
+ free (ref );
590
577
}
591
- if (return_refs != fastarray )
592
- free (return_refs );
593
578
}
579
+
580
+ filter_string_list (sought , 0 , non_matching_ref , NULL );
594
581
* refs = newlist ;
595
582
}
596
583
@@ -599,7 +586,7 @@ static void mark_alternate_complete(const struct ref *ref, void *unused)
599
586
mark_complete (NULL , ref -> old_sha1 , 0 , NULL );
600
587
}
601
588
602
- static int everything_local (struct ref * * refs , int nr_match , char * * match )
589
+ static int everything_local (struct ref * * refs , struct string_list * sought )
603
590
{
604
591
struct ref * ref ;
605
592
int retval ;
@@ -650,7 +637,7 @@ static int everything_local(struct ref **refs, int nr_match, char **match)
650
637
}
651
638
}
652
639
653
- filter_refs (refs , nr_match , match );
640
+ filter_refs (refs , sought );
654
641
655
642
for (retval = 1 , ref = * refs ; ref ; ref = ref -> next ) {
656
643
const unsigned char * remote = ref -> old_sha1 ;
@@ -781,8 +768,7 @@ static int get_pack(int xd[2], char **pack_lockfile)
781
768
782
769
static struct ref * do_fetch_pack (int fd [2 ],
783
770
const struct ref * orig_ref ,
784
- int nr_match ,
785
- char * * match ,
771
+ struct string_list * sought ,
786
772
char * * pack_lockfile )
787
773
{
788
774
struct ref * ref = copy_ref_list (orig_ref );
@@ -839,7 +825,7 @@ static struct ref *do_fetch_pack(int fd[2],
839
825
agent_len , agent_feature );
840
826
}
841
827
842
- if (everything_local (& ref , nr_match , match )) {
828
+ if (everything_local (& ref , sought )) {
843
829
packet_flush (fd [1 ]);
844
830
goto all_done ;
845
831
}
@@ -859,19 +845,6 @@ static struct ref *do_fetch_pack(int fd[2],
859
845
return ref ;
860
846
}
861
847
862
- static int remove_duplicates (int nr_heads , char * * heads )
863
- {
864
- int src , dst ;
865
-
866
- if (!nr_heads )
867
- return 0 ;
868
-
869
- for (src = dst = 1 ; src < nr_heads ; src ++ )
870
- if (strcmp (heads [src ], heads [dst - 1 ]))
871
- heads [dst ++ ] = heads [src ];
872
- return dst ;
873
- }
874
-
875
848
static int fetch_pack_config (const char * var , const char * value , void * cb )
876
849
{
877
850
if (strcmp (var , "fetch.unpacklimit" ) == 0 ) {
@@ -922,8 +895,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
922
895
int i , ret ;
923
896
struct ref * ref = NULL ;
924
897
const char * dest = NULL ;
925
- int alloc_heads = 0 , nr_heads = 0 ;
926
- char * * heads = NULL ;
898
+ struct string_list sought = STRING_LIST_INIT_DUP ;
927
899
int fd [2 ];
928
900
char * pack_lockfile = NULL ;
929
901
char * * pack_lockfile_ptr = NULL ;
@@ -1000,9 +972,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
1000
972
* Copy refs from cmdline to growable list, then append any
1001
973
* refs from the standard input:
1002
974
*/
1003
- ALLOC_GROW (heads , argc - i , alloc_heads );
1004
975
for (; i < argc ; i ++ )
1005
- heads [ nr_heads ++ ] = xstrdup (argv [i ]);
976
+ string_list_append ( & sought , xstrdup (argv [i ]) );
1006
977
if (args .stdin_refs ) {
1007
978
if (args .stateless_rpc ) {
1008
979
/* in stateless RPC mode we use pkt-line to read
@@ -1015,17 +986,14 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
1015
986
break ;
1016
987
if (line [n - 1 ] == '\n' )
1017
988
n -- ;
1018
- ALLOC_GROW (heads , nr_heads + 1 , alloc_heads );
1019
- heads [nr_heads ++ ] = xmemdupz (line , n );
989
+ string_list_append (& sought , xmemdupz (line , n ));
1020
990
}
1021
991
}
1022
992
else {
1023
993
/* read from stdin one ref per line, until EOF */
1024
994
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
- }
995
+ while (strbuf_getline (& line , stdin , '\n' ) != EOF )
996
+ string_list_append (& sought , strbuf_detach (& line , NULL ));
1029
997
strbuf_release (& line );
1030
998
}
1031
999
}
@@ -1042,29 +1010,26 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
1042
1010
get_remote_heads (fd [0 ], & ref , 0 , NULL );
1043
1011
1044
1012
ref = fetch_pack (& args , fd , conn , ref , dest ,
1045
- nr_heads , heads , pack_lockfile_ptr );
1013
+ & sought , pack_lockfile_ptr );
1046
1014
if (pack_lockfile ) {
1047
1015
printf ("lock %s\n" , pack_lockfile );
1048
1016
fflush (stdout );
1049
1017
}
1050
1018
close (fd [0 ]);
1051
1019
close (fd [1 ]);
1052
1020
if (finish_connect (conn ))
1053
- ref = NULL ;
1054
- ret = !ref ;
1055
-
1056
- if (!ret && nr_heads ) {
1057
- /* If the heads to pull were given, we should have
1058
- * consumed all of them by matching the remote.
1059
- * Otherwise, 'git fetch remote no-such-ref' would
1060
- * silently succeed without issuing an error.
1061
- */
1062
- for (i = 0 ; i < nr_heads ; i ++ )
1063
- if (heads [i ] && heads [i ][0 ]) {
1064
- error ("no such remote ref %s" , heads [i ]);
1065
- ret = 1 ;
1066
- }
1067
- }
1021
+ return 1 ;
1022
+
1023
+ ret = !ref || sought .nr ;
1024
+
1025
+ /*
1026
+ * If the heads to pull were given, we should have consumed
1027
+ * all of them by matching the remote. Otherwise, 'git fetch
1028
+ * remote no-such-ref' would silently succeed without issuing
1029
+ * an error.
1030
+ */
1031
+ for (i = 0 ; i < sought .nr ; i ++ )
1032
+ error ("no such remote ref %s" , sought .items [i ].string );
1068
1033
while (ref ) {
1069
1034
printf ("%s %s\n" ,
1070
1035
sha1_to_hex (ref -> old_sha1 ), ref -> name );
@@ -1074,18 +1039,12 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
1074
1039
return ret ;
1075
1040
}
1076
1041
1077
- static int compare_heads (const void * a , const void * b )
1078
- {
1079
- return strcmp (* (const char * * )a , * (const char * * )b );
1080
- }
1081
-
1082
1042
struct ref * fetch_pack (struct fetch_pack_args * my_args ,
1083
1043
int fd [], struct child_process * conn ,
1084
1044
const struct ref * ref ,
1085
- const char * dest ,
1086
- int nr_heads ,
1087
- char * * heads ,
1088
- char * * pack_lockfile )
1045
+ const char * dest ,
1046
+ struct string_list * sought ,
1047
+ char * * pack_lockfile )
1089
1048
{
1090
1049
struct stat st ;
1091
1050
struct ref * ref_cpy ;
@@ -1098,16 +1057,16 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
1098
1057
st .st_mtime = 0 ;
1099
1058
}
1100
1059
1101
- if (heads && nr_heads ) {
1102
- qsort ( heads , nr_heads , sizeof ( * heads ), compare_heads );
1103
- nr_heads = remove_duplicates ( nr_heads , heads );
1060
+ if (sought -> nr ) {
1061
+ sort_string_list ( sought );
1062
+ string_list_remove_duplicates ( sought , 0 );
1104
1063
}
1105
1064
1106
1065
if (!ref ) {
1107
1066
packet_flush (fd [1 ]);
1108
1067
die ("no matching remote head" );
1109
1068
}
1110
- ref_cpy = do_fetch_pack (fd , ref , nr_heads , heads , pack_lockfile );
1069
+ ref_cpy = do_fetch_pack (fd , ref , sought , pack_lockfile );
1111
1070
1112
1071
if (args .depth > 0 ) {
1113
1072
struct cache_time mtime ;
0 commit comments