@@ -39,6 +39,20 @@ struct ref_array_item {
39
39
char * refname ;
40
40
};
41
41
42
+ struct ref_array {
43
+ int nr , alloc ;
44
+ struct ref_array_item * * items ;
45
+ };
46
+
47
+ struct ref_filter {
48
+ const char * * name_patterns ;
49
+ };
50
+
51
+ struct ref_filter_cbdata {
52
+ struct ref_array array ;
53
+ struct ref_filter filter ;
54
+ };
55
+
42
56
static struct {
43
57
const char * name ;
44
58
cmp_type cmp_type ;
@@ -844,12 +858,6 @@ static void get_value(struct ref_array_item *ref, int atom, struct atom_value **
844
858
* v = & ref -> value [atom ];
845
859
}
846
860
847
- struct grab_ref_cbdata {
848
- struct ref_array_item * * grab_array ;
849
- const char * * grab_pattern ;
850
- int grab_cnt ;
851
- };
852
-
853
861
/*
854
862
* Return 1 if the refname matches one of the patterns, otherwise 0.
855
863
* A pattern can be path prefix (e.g. a refname "refs/heads/master"
@@ -895,15 +903,16 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
895
903
static int grab_single_ref (const char * refname , const struct object_id * oid ,
896
904
int flag , void * cb_data )
897
905
{
898
- struct grab_ref_cbdata * cb = cb_data ;
906
+ struct ref_filter_cbdata * ref_cbdata = cb_data ;
907
+ struct ref_filter * filter = & ref_cbdata -> filter ;
899
908
struct ref_array_item * ref ;
900
909
901
910
if (flag & REF_BAD_NAME ) {
902
911
warning ("ignoring ref with broken name %s" , refname );
903
912
return 0 ;
904
913
}
905
914
906
- if (* cb -> grab_pattern && !match_name_as_path (cb -> grab_pattern , refname ))
915
+ if (* filter -> name_patterns && !match_name_as_path (filter -> name_patterns , refname ))
907
916
return 0 ;
908
917
909
918
/*
@@ -913,8 +922,8 @@ static int grab_single_ref(const char *refname, const struct object_id *oid,
913
922
*/
914
923
ref = new_ref_array_item (refname , oid -> hash , flag );
915
924
916
- REALLOC_ARRAY (cb -> grab_array , cb -> grab_cnt + 1 );
917
- cb -> grab_array [ cb -> grab_cnt ++ ] = ref ;
925
+ REALLOC_ARRAY (ref_cbdata -> array . items , ref_cbdata -> array . nr + 1 );
926
+ ref_cbdata -> array . items [ ref_cbdata -> array . nr ++ ] = ref ;
918
927
return 0 ;
919
928
}
920
929
@@ -957,10 +966,10 @@ static int compare_refs(const void *a_, const void *b_)
957
966
return 0 ;
958
967
}
959
968
960
- static void sort_refs (struct ref_sort * sort , struct ref_array_item * * refs , int num_refs )
969
+ static void sort_refs (struct ref_sort * sort , struct ref_array * array )
961
970
{
962
971
ref_sort = sort ;
963
- qsort (refs , num_refs , sizeof (struct ref_array_item * ), compare_refs );
972
+ qsort (array -> items , array -> nr , sizeof (struct ref_array_item * ), compare_refs );
964
973
}
965
974
966
975
static void print_value (struct atom_value * v , int quote_style )
@@ -1096,12 +1105,11 @@ static char const * const for_each_ref_usage[] = {
1096
1105
1097
1106
int cmd_for_each_ref (int argc , const char * * argv , const char * prefix )
1098
1107
{
1099
- int i , num_refs ;
1108
+ int i ;
1100
1109
const char * format = "%(objectname) %(objecttype)\t%(refname)" ;
1101
1110
struct ref_sort * sort = NULL , * * sort_tail = & sort ;
1102
1111
int maxcount = 0 , quote_style = 0 ;
1103
- struct ref_array_item * * refs ;
1104
- struct grab_ref_cbdata cbdata ;
1112
+ struct ref_filter_cbdata ref_cbdata ;
1105
1113
1106
1114
struct option opts [] = {
1107
1115
OPT_BIT ('s' , "shell" , & quote_style ,
@@ -1139,17 +1147,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1139
1147
/* for warn_ambiguous_refs */
1140
1148
git_config (git_default_config , NULL );
1141
1149
1142
- memset (& cbdata , 0 , sizeof (cbdata ));
1143
- cbdata .grab_pattern = argv ;
1144
- for_each_rawref (grab_single_ref , & cbdata );
1145
- refs = cbdata .grab_array ;
1146
- num_refs = cbdata .grab_cnt ;
1150
+ memset (& ref_cbdata , 0 , sizeof (ref_cbdata ));
1151
+ ref_cbdata .filter .name_patterns = argv ;
1152
+ for_each_rawref (grab_single_ref , & ref_cbdata );
1147
1153
1148
- sort_refs (sort , refs , num_refs );
1154
+ sort_refs (sort , & ref_cbdata . array );
1149
1155
1150
- if (!maxcount || num_refs < maxcount )
1151
- maxcount = num_refs ;
1156
+ if (!maxcount || ref_cbdata . array . nr < maxcount )
1157
+ maxcount = ref_cbdata . array . nr ;
1152
1158
for (i = 0 ; i < maxcount ; i ++ )
1153
- show_ref (refs [i ], format , quote_style );
1159
+ show_ref (ref_cbdata . array . items [i ], format , quote_style );
1154
1160
return 0 ;
1155
1161
}
0 commit comments