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