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