@@ -18,6 +18,9 @@ static const char * const builtin_remote_usage[] = {
18
18
NULL
19
19
};
20
20
21
+ #define GET_REF_STATES (1<<0)
22
+ #define GET_HEAD_NAMES (1<<1)
23
+
21
24
static int verbose ;
22
25
23
26
static int show_all (void );
@@ -210,7 +213,7 @@ static void read_branches(void)
210
213
211
214
struct ref_states {
212
215
struct remote * remote ;
213
- struct string_list new , stale , tracked ;
216
+ struct string_list new , stale , tracked , heads ;
214
217
};
215
218
216
219
static int handle_one_branch (const char * refname ,
@@ -264,6 +267,28 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
264
267
return 0 ;
265
268
}
266
269
270
+ static int get_head_names (const struct ref * remote_refs , struct ref_states * states )
271
+ {
272
+ struct ref * ref , * matches ;
273
+ struct ref * fetch_map = NULL , * * fetch_map_tail = & fetch_map ;
274
+ struct refspec refspec ;
275
+
276
+ refspec .force = 0 ;
277
+ refspec .pattern = 1 ;
278
+ refspec .src = refspec .dst = "refs/heads/" ;
279
+ states -> heads .strdup_strings = 1 ;
280
+ get_fetch_map (remote_refs , & refspec , & fetch_map_tail , 0 );
281
+ matches = guess_remote_head (find_ref_by_name (remote_refs , "HEAD" ),
282
+ fetch_map , 1 );
283
+ for (ref = matches ; ref ; ref = ref -> next )
284
+ string_list_append (abbrev_branch (ref -> name ), & states -> heads );
285
+
286
+ free_refs (fetch_map );
287
+ free_refs (matches );
288
+
289
+ return 0 ;
290
+ }
291
+
267
292
struct known_remote {
268
293
struct known_remote * next ;
269
294
struct remote * remote ;
@@ -630,6 +655,7 @@ static void free_remote_ref_states(struct ref_states *states)
630
655
string_list_clear (& states -> new , 0 );
631
656
string_list_clear (& states -> stale , 0 );
632
657
string_list_clear (& states -> tracked , 0 );
658
+ string_list_clear (& states -> heads , 0 );
633
659
}
634
660
635
661
static int append_ref_to_tracked_list (const char * refname ,
@@ -668,7 +694,10 @@ static int get_remote_ref_states(const char *name,
668
694
remote_refs = transport_get_remote_refs (transport );
669
695
transport_disconnect (transport );
670
696
671
- get_ref_states (remote_refs , states );
697
+ if (query & GET_REF_STATES )
698
+ get_ref_states (remote_refs , states );
699
+ if (query & GET_HEAD_NAMES )
700
+ get_head_names (remote_refs , states );
672
701
} else {
673
702
for_each_ref (append_ref_to_tracked_list , states );
674
703
sort_string_list (& states -> tracked );
@@ -679,7 +708,7 @@ static int get_remote_ref_states(const char *name,
679
708
680
709
static int show (int argc , const char * * argv )
681
710
{
682
- int no_query = 0 , result = 0 ;
711
+ int no_query = 0 , result = 0 , query_flag = 0 ;
683
712
struct option options [] = {
684
713
OPT_GROUP ("show specific options" ),
685
714
OPT_BOOLEAN ('n' , NULL , & no_query , "do not query remotes" ),
@@ -692,15 +721,30 @@ static int show(int argc, const char **argv)
692
721
if (argc < 1 )
693
722
return show_all ();
694
723
724
+ if (!no_query )
725
+ query_flag = (GET_REF_STATES | GET_HEAD_NAMES );
726
+
695
727
memset (& states , 0 , sizeof (states ));
696
728
for (; argc ; argc -- , argv ++ ) {
697
729
int i ;
698
730
699
- get_remote_ref_states (* argv , & states , ! no_query );
731
+ get_remote_ref_states (* argv , & states , query_flag );
700
732
701
733
printf ("* remote %s\n URL: %s\n" , * argv ,
702
734
states .remote -> url_nr > 0 ?
703
735
states .remote -> url [0 ] : "(no URL)" );
736
+ if (no_query )
737
+ printf (" HEAD branch: (not queried)\n" );
738
+ else if (!states .heads .nr )
739
+ printf (" HEAD branch: (unknown)\n" );
740
+ else if (states .heads .nr == 1 )
741
+ printf (" HEAD branch: %s\n" , states .heads .items [0 ].string );
742
+ else {
743
+ printf (" HEAD branch (remote HEAD is ambiguous,"
744
+ " may be one of the following):\n" );
745
+ for (i = 0 ; i < states .heads .nr ; i ++ )
746
+ printf (" %s\n" , states .heads .items [i ].string );
747
+ }
704
748
705
749
for (i = 0 ; i < branch_list .nr ; i ++ ) {
706
750
struct string_list_item * branch = branch_list .items + i ;
@@ -772,7 +816,7 @@ static int prune(int argc, const char **argv)
772
816
for (; argc ; argc -- , argv ++ ) {
773
817
int i ;
774
818
775
- get_remote_ref_states (* argv , & states , 1 );
819
+ get_remote_ref_states (* argv , & states , GET_REF_STATES );
776
820
777
821
if (states .stale .nr ) {
778
822
printf ("Pruning %s\n" , * argv );
0 commit comments