@@ -1815,6 +1815,92 @@ static void wt_porcelain_print(struct wt_status *s)
1815
1815
wt_shortstatus_print (s );
1816
1816
}
1817
1817
1818
+ /*
1819
+ * Print branch information for porcelain v2 output. These lines
1820
+ * are printed when the '--branch' parameter is given.
1821
+ *
1822
+ * # branch.oid <commit><eol>
1823
+ * # branch.head <head><eol>
1824
+ * [# branch.upstream <upstream><eol>
1825
+ * [# branch.ab +<ahead> -<behind><eol>]]
1826
+ *
1827
+ * <commit> ::= the current commit hash or the the literal
1828
+ * "(initial)" to indicate an initialized repo
1829
+ * with no commits.
1830
+ *
1831
+ * <head> ::= <branch_name> the current branch name or
1832
+ * "(detached)" literal when detached head or
1833
+ * "(unknown)" when something is wrong.
1834
+ *
1835
+ * <upstream> ::= the upstream branch name, when set.
1836
+ *
1837
+ * <ahead> ::= integer ahead value, when upstream set
1838
+ * and the commit is present (not gone).
1839
+ *
1840
+ * <behind> ::= integer behind value, when upstream set
1841
+ * and commit is present.
1842
+ *
1843
+ *
1844
+ * The end-of-line is defined by the -z flag.
1845
+ *
1846
+ * <eol> ::= NUL when -z,
1847
+ * LF when NOT -z.
1848
+ *
1849
+ */
1850
+ static void wt_porcelain_v2_print_tracking (struct wt_status * s )
1851
+ {
1852
+ struct branch * branch ;
1853
+ const char * base ;
1854
+ const char * branch_name ;
1855
+ struct wt_status_state state ;
1856
+ int ab_info , nr_ahead , nr_behind ;
1857
+ char eol = s -> null_termination ? '\0' : '\n' ;
1858
+
1859
+ memset (& state , 0 , sizeof (state ));
1860
+ wt_status_get_state (& state , s -> branch && !strcmp (s -> branch , "HEAD" ));
1861
+
1862
+ fprintf (s -> fp , "# branch.oid %s%c" ,
1863
+ (s -> is_initial ? "(initial)" : sha1_to_hex (s -> sha1_commit )),
1864
+ eol );
1865
+
1866
+ if (!s -> branch )
1867
+ fprintf (s -> fp , "# branch.head %s%c" , "(unknown)" , eol );
1868
+ else {
1869
+ if (!strcmp (s -> branch , "HEAD" )) {
1870
+ fprintf (s -> fp , "# branch.head %s%c" , "(detached)" , eol );
1871
+
1872
+ if (state .rebase_in_progress || state .rebase_interactive_in_progress )
1873
+ branch_name = state .onto ;
1874
+ else if (state .detached_from )
1875
+ branch_name = state .detached_from ;
1876
+ else
1877
+ branch_name = "" ;
1878
+ } else {
1879
+ branch_name = NULL ;
1880
+ skip_prefix (s -> branch , "refs/heads/" , & branch_name );
1881
+
1882
+ fprintf (s -> fp , "# branch.head %s%c" , branch_name , eol );
1883
+ }
1884
+
1885
+ /* Lookup stats on the upstream tracking branch, if set. */
1886
+ branch = branch_get (branch_name );
1887
+ base = NULL ;
1888
+ ab_info = (stat_tracking_info (branch , & nr_ahead , & nr_behind , & base ) == 0 );
1889
+ if (base ) {
1890
+ base = shorten_unambiguous_ref (base , 0 );
1891
+ fprintf (s -> fp , "# branch.upstream %s%c" , base , eol );
1892
+ free ((char * )base );
1893
+
1894
+ if (ab_info )
1895
+ fprintf (s -> fp , "# branch.ab +%d -%d%c" , nr_ahead , nr_behind , eol );
1896
+ }
1897
+ }
1898
+
1899
+ free (state .branch );
1900
+ free (state .onto );
1901
+ free (state .detached_from );
1902
+ }
1903
+
1818
1904
/*
1819
1905
* Convert various submodule status values into a
1820
1906
* fixed-length string of characters in the buffer provided.
@@ -2061,6 +2147,7 @@ static void wt_porcelain_v2_print_other(
2061
2147
/*
2062
2148
* Print porcelain V2 status.
2063
2149
*
2150
+ * [<v2_branch>]
2064
2151
* [<v2_changed_items>]*
2065
2152
* [<v2_unmerged_items>]*
2066
2153
* [<v2_untracked_items>]*
@@ -2073,6 +2160,9 @@ static void wt_porcelain_v2_print(struct wt_status *s)
2073
2160
struct string_list_item * it ;
2074
2161
int i ;
2075
2162
2163
+ if (s -> show_branch )
2164
+ wt_porcelain_v2_print_tracking (s );
2165
+
2076
2166
for (i = 0 ; i < s -> change .nr ; i ++ ) {
2077
2167
it = & (s -> change .items [i ]);
2078
2168
d = it -> util ;
0 commit comments