9
9
#include "quote.h"
10
10
#include "parse-options.h"
11
11
#include "remote.h"
12
+ #include "color.h"
12
13
13
14
/* Quoting styles */
14
15
#define QUOTE_NONE 0
@@ -75,6 +76,8 @@ static struct {
75
76
{ "upstream" },
76
77
{ "symref" },
77
78
{ "flag" },
79
+ { "HEAD" },
80
+ { "color" },
78
81
};
79
82
80
83
/*
@@ -90,6 +93,7 @@ static struct {
90
93
static const char * * used_atom ;
91
94
static cmp_type * used_atom_type ;
92
95
static int used_atom_cnt , sort_atom_limit , need_tagged , need_symref ;
96
+ static int need_color_reset_at_eol ;
93
97
94
98
/*
95
99
* Used to parse format string and sort specifiers
@@ -176,13 +180,21 @@ static const char *find_next(const char *cp)
176
180
static int verify_format (const char * format )
177
181
{
178
182
const char * cp , * sp ;
183
+ static const char color_reset [] = "color:reset" ;
184
+
185
+ need_color_reset_at_eol = 0 ;
179
186
for (cp = format ; * cp && (sp = find_next (cp )); ) {
180
187
const char * ep = strchr (sp , ')' );
188
+ int at ;
189
+
181
190
if (!ep )
182
191
return error ("malformed format string %s" , sp );
183
192
/* sp points at "%(" and ep points at the closing ")" */
184
- parse_atom (sp + 2 , ep );
193
+ at = parse_atom (sp + 2 , ep );
185
194
cp = ep + 1 ;
195
+
196
+ if (!memcmp (used_atom [at ], "color:" , 6 ))
197
+ need_color_reset_at_eol = !!strcmp (used_atom [at ], color_reset );
186
198
}
187
199
return 0 ;
188
200
}
@@ -649,6 +661,7 @@ static void populate_value(struct refinfo *ref)
649
661
int deref = 0 ;
650
662
const char * refname ;
651
663
const char * formatp ;
664
+ struct branch * branch = NULL ;
652
665
653
666
if (* name == '*' ) {
654
667
deref = 1 ;
@@ -660,7 +673,6 @@ static void populate_value(struct refinfo *ref)
660
673
else if (!prefixcmp (name , "symref" ))
661
674
refname = ref -> symref ? ref -> symref : "" ;
662
675
else if (!prefixcmp (name , "upstream" )) {
663
- struct branch * branch ;
664
676
/* only local branches may have an upstream */
665
677
if (prefixcmp (ref -> refname , "refs/heads/" ))
666
678
continue ;
@@ -670,8 +682,13 @@ static void populate_value(struct refinfo *ref)
670
682
!branch -> merge [0 ]-> dst )
671
683
continue ;
672
684
refname = branch -> merge [0 ]-> dst ;
673
- }
674
- else if (!strcmp (name , "flag" )) {
685
+ } else if (!prefixcmp (name , "color:" )) {
686
+ char color [COLOR_MAXLEN ] = "" ;
687
+
688
+ color_parse (name + 6 , "--format" , color );
689
+ v -> s = xstrdup (color );
690
+ continue ;
691
+ } else if (!strcmp (name , "flag" )) {
675
692
char buf [256 ], * cp = buf ;
676
693
if (ref -> flag & REF_ISSYMREF )
677
694
cp = copy_advance (cp , ",symref" );
@@ -684,20 +701,62 @@ static void populate_value(struct refinfo *ref)
684
701
v -> s = xstrdup (buf + 1 );
685
702
}
686
703
continue ;
687
- }
688
- else if (!deref && grab_objectname (name , ref -> objectname , v ))
704
+ } else if (!deref && grab_objectname (name , ref -> objectname , v )) {
689
705
continue ;
690
- else
706
+ } else if (!strcmp (name , "HEAD" )) {
707
+ const char * head ;
708
+ unsigned char sha1 [20 ];
709
+
710
+ head = resolve_ref_unsafe ("HEAD" , sha1 , 1 , NULL );
711
+ if (!strcmp (ref -> refname , head ))
712
+ v -> s = "*" ;
713
+ else
714
+ v -> s = " " ;
715
+ continue ;
716
+ } else
691
717
continue ;
692
718
693
719
formatp = strchr (name , ':' );
694
- /* look for "short" refname format */
695
720
if (formatp ) {
721
+ int num_ours , num_theirs ;
722
+
696
723
formatp ++ ;
697
724
if (!strcmp (formatp , "short" ))
698
725
refname = shorten_unambiguous_ref (refname ,
699
726
warn_ambiguous_refs );
700
- else
727
+ else if (!strcmp (formatp , "track" ) &&
728
+ !prefixcmp (name , "upstream" )) {
729
+ char buf [40 ];
730
+
731
+ stat_tracking_info (branch , & num_ours , & num_theirs );
732
+ if (!num_ours && !num_theirs )
733
+ v -> s = "" ;
734
+ else if (!num_ours ) {
735
+ sprintf (buf , "[behind %d]" , num_theirs );
736
+ v -> s = xstrdup (buf );
737
+ } else if (!num_theirs ) {
738
+ sprintf (buf , "[ahead %d]" , num_ours );
739
+ v -> s = xstrdup (buf );
740
+ } else {
741
+ sprintf (buf , "[ahead %d, behind %d]" ,
742
+ num_ours , num_theirs );
743
+ v -> s = xstrdup (buf );
744
+ }
745
+ continue ;
746
+ } else if (!strcmp (formatp , "trackshort" ) &&
747
+ !prefixcmp (name , "upstream" )) {
748
+ assert (branch );
749
+ stat_tracking_info (branch , & num_ours , & num_theirs );
750
+ if (!num_ours && !num_theirs )
751
+ v -> s = "=" ;
752
+ else if (!num_ours )
753
+ v -> s = "<" ;
754
+ else if (!num_theirs )
755
+ v -> s = ">" ;
756
+ else
757
+ v -> s = "<>" ;
758
+ continue ;
759
+ } else
701
760
die ("unknown %.*s format %s" ,
702
761
(int )(formatp - name ), name , formatp );
703
762
}
@@ -875,11 +934,9 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
875
934
qsort (refs , num_refs , sizeof (struct refinfo * ), compare_refs );
876
935
}
877
936
878
- static void print_value (struct refinfo * ref , int atom , int quote_style )
937
+ static void print_value (struct atom_value * v , int quote_style )
879
938
{
880
- struct atom_value * v ;
881
939
struct strbuf sb = STRBUF_INIT ;
882
- get_value (ref , atom , & v );
883
940
switch (quote_style ) {
884
941
case QUOTE_NONE :
885
942
fputs (v -> s , stdout );
@@ -946,15 +1003,26 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
946
1003
const char * cp , * sp , * ep ;
947
1004
948
1005
for (cp = format ; * cp && (sp = find_next (cp )); cp = ep + 1 ) {
1006
+ struct atom_value * atomv ;
1007
+
949
1008
ep = strchr (sp , ')' );
950
1009
if (cp < sp )
951
1010
emit (cp , sp );
952
- print_value (info , parse_atom (sp + 2 , ep ), quote_style );
1011
+ get_value (info , parse_atom (sp + 2 , ep ), & atomv );
1012
+ print_value (atomv , quote_style );
953
1013
}
954
1014
if (* cp ) {
955
1015
sp = cp + strlen (cp );
956
1016
emit (cp , sp );
957
1017
}
1018
+ if (need_color_reset_at_eol ) {
1019
+ struct atom_value resetv ;
1020
+ char color [COLOR_MAXLEN ] = "" ;
1021
+
1022
+ color_parse ("reset" , "--format" , color );
1023
+ resetv .s = color ;
1024
+ print_value (& resetv , quote_style );
1025
+ }
958
1026
putchar ('\n' );
959
1027
}
960
1028
0 commit comments