status: print branch info with --porcelain=v2 --branch

Expand porcelain v2 output to include branch and tracking branch information. This includes the commit id, the branch, the upstream branch, and the ahead and behind counts. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Aug 11, 2016 at 10:45 UTC d9fc746cd77910a7dec53abfec36df5c699b33c2
3 files changed +96
builtin/commit.c
+5
@@ -510,6 +510,8 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
510 s->fp = fp;
511 s->nowarn = nowarn;
512 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
513 + if (!s->is_initial)
514 + hashcpy(s->sha1_commit, sha1);
515 s->status_format = status_format;
516 s->ignore_submodule_arg = ignore_submodule_arg;
517
@@ -1378,6 +1380,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1380 fd = hold_locked_index(&index_lock, 0);
1381
1382 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1383 + if (!s.is_initial)
1384 + hashcpy(s.sha1_commit, sha1);
1385 +
1386 s.ignore_submodule_arg = ignore_submodule_arg;
1387 s.status_format = status_format;
1388 s.verbose = verbose;
wt-status.c
+90
@@ -1812,6 +1812,92 @@ static void wt_porcelain_print(struct wt_status *s)
1812 wt_shortstatus_print(s);
1813 }
1814
1815 +/*
1816 + * Print branch information for porcelain v2 output. These lines
1817 + * are printed when the '--branch' parameter is given.
1818 + *
1819 + * # branch.oid <commit><eol>
1820 + * # branch.head <head><eol>
1821 + * [# branch.upstream <upstream><eol>
1822 + * [# branch.ab +<ahead> -<behind><eol>]]
1823 + *
1824 + * <commit> ::= the current commit hash or the the literal
1825 + * "(initial)" to indicate an initialized repo
1826 + * with no commits.
1827 + *
1828 + * <head> ::= <branch_name> the current branch name or
1829 + * "(detached)" literal when detached head or
1830 + * "(unknown)" when something is wrong.
1831 + *
1832 + * <upstream> ::= the upstream branch name, when set.
1833 + *
1834 + * <ahead> ::= integer ahead value, when upstream set
1835 + * and the commit is present (not gone).
1836 + *
1837 + * <behind> ::= integer behind value, when upstream set
1838 + * and commit is present.
1839 + *
1840 + *
1841 + * The end-of-line is defined by the -z flag.
1842 + *
1843 + * <eol> ::= NUL when -z,
1844 + * LF when NOT -z.
1845 + *
1846 + */
1847 +static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1848 +{
1849 + struct branch *branch;
1850 + const char *base;
1851 + const char *branch_name;
1852 + struct wt_status_state state;
1853 + int ab_info, nr_ahead, nr_behind;
1854 + char eol = s->null_termination ? '\0' : '\n';
1855 +
1856 + memset(&state, 0, sizeof(state));
1857 + wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1858 +
1859 + fprintf(s->fp, "# branch.oid %s%c",
1860 + (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1861 + eol);
1862 +
1863 + if (!s->branch)
1864 + fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1865 + else {
1866 + if (!strcmp(s->branch, "HEAD")) {
1867 + fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1868 +
1869 + if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1870 + branch_name = state.onto;
1871 + else if (state.detached_from)
1872 + branch_name = state.detached_from;
1873 + else
1874 + branch_name = "";
1875 + } else {
1876 + branch_name = NULL;
1877 + skip_prefix(s->branch, "refs/heads/", &branch_name);
1878 +
1879 + fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1880 + }
1881 +
1882 + /* Lookup stats on the upstream tracking branch, if set. */
1883 + branch = branch_get(branch_name);
1884 + base = NULL;
1885 + ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind, &base) == 0);
1886 + if (base) {
1887 + base = shorten_unambiguous_ref(base, 0);
1888 + fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1889 + free((char *)base);
1890 +
1891 + if (ab_info)
1892 + fprintf(s->fp, "# branch.ab +%d -%d%c", nr_ahead, nr_behind, eol);
1893 + }
1894 + }
1895 +
1896 + free(state.branch);
1897 + free(state.onto);
1898 + free(state.detached_from);
1899 +}
1900 +
1901 /*
1902 * Convert various submodule status values into a
1903 * fixed-length string of characters in the buffer provided.
@@ -2058,6 +2144,7 @@ static void wt_porcelain_v2_print_other(
2144 /*
2145 * Print porcelain V2 status.
2146 *
2147 + * [<v2_branch>]
2148 * [<v2_changed_items>]*
2149 * [<v2_unmerged_items>]*
2150 * [<v2_untracked_items>]*
@@ -2070,6 +2157,9 @@ static void wt_porcelain_v2_print(struct wt_status *s)
2157 struct string_list_item *it;
2158 int i;
2159
2160 + if (s->show_branch)
2161 + wt_porcelain_v2_print_tracking(s);
2162 +
2163 for (i = 0; i < s->change.nr; i++) {
2164 it = &(s->change.items[i]);
2165 d = it->util;
wt-status.h
+1
@@ -80,6 +80,7 @@ struct wt_status {
80 int hints;
81
82 enum wt_status_format status_format;
83 + unsigned char sha1_commit[GIT_SHA1_RAWSZ]; /* when not Initial */
84
85 /* These are computed during processing of the individual sections */
86 int commitable;