@@ -37,6 +37,8 @@ static struct used_atom {
37
37
union {
38
38
char color [COLOR_MAXLEN ];
39
39
struct align align ;
40
+ enum { RR_NORMAL , RR_SHORTEN , RR_TRACK , RR_TRACKSHORT }
41
+ remote_ref ;
40
42
} u ;
41
43
} * used_atom ;
42
44
static int used_atom_cnt , need_tagged , need_symref ;
@@ -50,6 +52,20 @@ static void color_atom_parser(struct used_atom *atom, const char *color_value)
50
52
die (_ ("unrecognized color: %%(color:%s)" ), color_value );
51
53
}
52
54
55
+ static void remote_ref_atom_parser (struct used_atom * atom , const char * arg )
56
+ {
57
+ if (!arg )
58
+ atom -> u .remote_ref = RR_NORMAL ;
59
+ else if (!strcmp (arg , "short" ))
60
+ atom -> u .remote_ref = RR_SHORTEN ;
61
+ else if (!strcmp (arg , "track" ))
62
+ atom -> u .remote_ref = RR_TRACK ;
63
+ else if (!strcmp (arg , "trackshort" ))
64
+ atom -> u .remote_ref = RR_TRACKSHORT ;
65
+ else
66
+ die (_ ("unrecognized format: %%(%s)" ), atom -> name );
67
+ }
68
+
53
69
static align_type parse_align_position (const char * s )
54
70
{
55
71
if (!strcmp (s , "right" ))
@@ -132,8 +148,8 @@ static struct {
132
148
{ "subject" },
133
149
{ "body" },
134
150
{ "contents" },
135
- { "upstream" },
136
- { "push" },
151
+ { "upstream" , FIELD_STR , remote_ref_atom_parser },
152
+ { "push" , FIELD_STR , remote_ref_atom_parser },
137
153
{ "symref" },
138
154
{ "flag" },
139
155
{ "HEAD" },
@@ -840,6 +856,43 @@ static const char *strip_ref_components(const char *refname, const char *nr_arg)
840
856
return start ;
841
857
}
842
858
859
+ static void fill_remote_ref_details (struct used_atom * atom , const char * refname ,
860
+ struct branch * branch , const char * * s )
861
+ {
862
+ int num_ours , num_theirs ;
863
+ if (atom -> u .remote_ref == RR_SHORTEN )
864
+ * s = shorten_unambiguous_ref (refname , warn_ambiguous_refs );
865
+ else if (atom -> u .remote_ref == RR_TRACK ) {
866
+ if (stat_tracking_info (branch , & num_ours ,
867
+ & num_theirs , NULL ))
868
+ return ;
869
+
870
+ if (!num_ours && !num_theirs )
871
+ * s = "" ;
872
+ else if (!num_ours )
873
+ * s = xstrfmt ("[behind %d]" , num_theirs );
874
+ else if (!num_theirs )
875
+ * s = xstrfmt ("[ahead %d]" , num_ours );
876
+ else
877
+ * s = xstrfmt ("[ahead %d, behind %d]" ,
878
+ num_ours , num_theirs );
879
+ } else if (atom -> u .remote_ref == RR_TRACKSHORT ) {
880
+ if (stat_tracking_info (branch , & num_ours ,
881
+ & num_theirs , NULL ))
882
+ return ;
883
+
884
+ if (!num_ours && !num_theirs )
885
+ * s = "=" ;
886
+ else if (!num_ours )
887
+ * s = "<" ;
888
+ else if (!num_theirs )
889
+ * s = ">" ;
890
+ else
891
+ * s = "<>" ;
892
+ } else /* RR_NORMAL */
893
+ * s = refname ;
894
+ }
895
+
843
896
/*
844
897
* Parse the object referred by ref, and grab needed value.
845
898
*/
@@ -891,8 +944,9 @@ static void populate_value(struct ref_array_item *ref)
891
944
branch = branch_get (branch_name );
892
945
893
946
refname = branch_get_upstream (branch , NULL );
894
- if (!refname )
895
- continue ;
947
+ if (refname )
948
+ fill_remote_ref_details (atom , refname , branch , & v -> s );
949
+ continue ;
896
950
} else if (starts_with (name , "push" )) {
897
951
const char * branch_name ;
898
952
if (!skip_prefix (ref -> refname , "refs/heads/" ,
@@ -903,6 +957,8 @@ static void populate_value(struct ref_array_item *ref)
903
957
refname = branch_get_push (branch , NULL );
904
958
if (!refname )
905
959
continue ;
960
+ fill_remote_ref_details (atom , refname , branch , & v -> s );
961
+ continue ;
906
962
} else if (starts_with (name , "color:" )) {
907
963
v -> s = atom -> u .color ;
908
964
continue ;
@@ -944,7 +1000,6 @@ static void populate_value(struct ref_array_item *ref)
944
1000
945
1001
formatp = strchr (name , ':' );
946
1002
if (formatp ) {
947
- int num_ours , num_theirs ;
948
1003
const char * arg ;
949
1004
950
1005
formatp ++ ;
@@ -953,43 +1008,7 @@ static void populate_value(struct ref_array_item *ref)
953
1008
warn_ambiguous_refs );
954
1009
else if (skip_prefix (formatp , "strip=" , & arg ))
955
1010
refname = strip_ref_components (refname , arg );
956
- else if (!strcmp (formatp , "track" ) &&
957
- (starts_with (name , "upstream" ) ||
958
- starts_with (name , "push" ))) {
959
-
960
- if (stat_tracking_info (branch , & num_ours ,
961
- & num_theirs , NULL ))
962
- continue ;
963
-
964
- if (!num_ours && !num_theirs )
965
- v -> s = "" ;
966
- else if (!num_ours )
967
- v -> s = xstrfmt ("[behind %d]" , num_theirs );
968
- else if (!num_theirs )
969
- v -> s = xstrfmt ("[ahead %d]" , num_ours );
970
- else
971
- v -> s = xstrfmt ("[ahead %d, behind %d]" ,
972
- num_ours , num_theirs );
973
- continue ;
974
- } else if (!strcmp (formatp , "trackshort" ) &&
975
- (starts_with (name , "upstream" ) ||
976
- starts_with (name , "push" ))) {
977
- assert (branch );
978
-
979
- if (stat_tracking_info (branch , & num_ours ,
980
- & num_theirs , NULL ))
981
- continue ;
982
-
983
- if (!num_ours && !num_theirs )
984
- v -> s = "=" ;
985
- else if (!num_ours )
986
- v -> s = "<" ;
987
- else if (!num_theirs )
988
- v -> s = ">" ;
989
- else
990
- v -> s = "<>" ;
991
- continue ;
992
- } else
1011
+ else
993
1012
die ("unknown %.*s format %s" ,
994
1013
(int )(formatp - name ), name , formatp );
995
1014
}
0 commit comments