Skip to content

Commit 8c0cff9

Browse files
jeffhostetlerdscho
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8dcba40 commit 8c0cff9

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

builtin/commit.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
510510
s->fp = fp;
511511
s->nowarn = nowarn;
512512
s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
513+
if (!s->is_initial)
514+
hashcpy(s->sha1_commit, sha1);
513515
s->status_format = status_format;
514516
s->ignore_submodule_arg = ignore_submodule_arg;
515517

@@ -1378,6 +1380,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
13781380
fd = hold_locked_index(&index_lock, 0);
13791381

13801382
s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1383+
if (!s.is_initial)
1384+
hashcpy(s.sha1_commit, sha1);
1385+
13811386
s.ignore_submodule_arg = ignore_submodule_arg;
13821387
s.status_format = status_format;
13831388
s.verbose = verbose;

wt-status.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,92 @@ static void wt_porcelain_print(struct wt_status *s)
18111811
wt_shortstatus_print(s);
18121812
}
18131813

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+
18141900
/*
18151901
* Convert various submodule status values into a
18161902
* fixed-length string of characters in the buffer provided.
@@ -2057,6 +2143,7 @@ static void wt_porcelain_v2_print_other(
20572143
/*
20582144
* Print porcelain V2 status.
20592145
*
2146+
* [<v2_branch>]
20602147
* [<v2_changed_items>]*
20612148
* [<v2_unmerged_items>]*
20622149
* [<v2_untracked_items>]*
@@ -2069,6 +2156,9 @@ static void wt_porcelain_v2_print(struct wt_status *s)
20692156
struct string_list_item *it;
20702157
int i;
20712158

2159+
if (s->show_branch)
2160+
wt_porcelain_v2_print_tracking(s);
2161+
20722162
for (i = 0; i < s->change.nr; i++) {
20732163
it = &(s->change.items[i]);
20742164
d = it->util;

wt-status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct wt_status {
8080
int hints;
8181

8282
enum wt_status_format status_format;
83+
unsigned char sha1_commit[GIT_SHA1_RAWSZ]; /* when not Initial */
8384

8485
/* These are computed during processing of the individual sections */
8586
int commitable;

0 commit comments

Comments
 (0)