aboutsummaryrefslogtreecommitdiff
path: root/wt-status.c
diff options
context:
space:
mode:
authorJeff Hostetler <jeffhost@microsoft.com>2016-08-11 10:45:59 -0400
committerJunio C Hamano <gitster@pobox.com>2016-08-11 11:15:40 -0700
commitd9fc746cd77910a7dec53abfec36df5c699b33c2 (patch)
tree03b5603ad466ee45e75e026c3869fa537282bea6 /wt-status.c
parent24959bad5dba353497323f10b3bf39f243c0f174 (diff)
downloadgit-d9fc746cd77910a7dec53abfec36df5c699b33c2.tar.gz
git-d9fc746cd77910a7dec53abfec36df5c699b33c2.tar.xz
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>
Diffstat (limited to 'wt-status.c')
-rw-r--r--wt-status.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/wt-status.c b/wt-status.c
index bd9075b7e..ebe4a1e24 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1813,6 +1813,92 @@ static void wt_porcelain_print(struct wt_status *s)
}
/*
+ * Print branch information for porcelain v2 output. These lines
+ * are printed when the '--branch' parameter is given.
+ *
+ * # branch.oid <commit><eol>
+ * # branch.head <head><eol>
+ * [# branch.upstream <upstream><eol>
+ * [# branch.ab +<ahead> -<behind><eol>]]
+ *
+ * <commit> ::= the current commit hash or the the literal
+ * "(initial)" to indicate an initialized repo
+ * with no commits.
+ *
+ * <head> ::= <branch_name> the current branch name or
+ * "(detached)" literal when detached head or
+ * "(unknown)" when something is wrong.
+ *
+ * <upstream> ::= the upstream branch name, when set.
+ *
+ * <ahead> ::= integer ahead value, when upstream set
+ * and the commit is present (not gone).
+ *
+ * <behind> ::= integer behind value, when upstream set
+ * and commit is present.
+ *
+ *
+ * The end-of-line is defined by the -z flag.
+ *
+ * <eol> ::= NUL when -z,
+ * LF when NOT -z.
+ *
+ */
+static void wt_porcelain_v2_print_tracking(struct wt_status *s)
+{
+ struct branch *branch;
+ const char *base;
+ const char *branch_name;
+ struct wt_status_state state;
+ int ab_info, nr_ahead, nr_behind;
+ char eol = s->null_termination ? '\0' : '\n';
+
+ memset(&state, 0, sizeof(state));
+ wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
+
+ fprintf(s->fp, "# branch.oid %s%c",
+ (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
+ eol);
+
+ if (!s->branch)
+ fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
+ else {
+ if (!strcmp(s->branch, "HEAD")) {
+ fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
+
+ if (state.rebase_in_progress || state.rebase_interactive_in_progress)
+ branch_name = state.onto;
+ else if (state.detached_from)
+ branch_name = state.detached_from;
+ else
+ branch_name = "";
+ } else {
+ branch_name = NULL;
+ skip_prefix(s->branch, "refs/heads/", &branch_name);
+
+ fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
+ }
+
+ /* Lookup stats on the upstream tracking branch, if set. */
+ branch = branch_get(branch_name);
+ base = NULL;
+ ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind, &base) == 0);
+ if (base) {
+ base = shorten_unambiguous_ref(base, 0);
+ fprintf(s->fp, "# branch.upstream %s%c", base, eol);
+ free((char *)base);
+
+ if (ab_info)
+ fprintf(s->fp, "# branch.ab +%d -%d%c", nr_ahead, nr_behind, eol);
+ }
+ }
+
+ free(state.branch);
+ free(state.onto);
+ free(state.detached_from);
+}
+
+/*
* Convert various submodule status values into a
* fixed-length string of characters in the buffer provided.
*/
@@ -2058,6 +2144,7 @@ static void wt_porcelain_v2_print_other(
/*
* Print porcelain V2 status.
*
+ * [<v2_branch>]
* [<v2_changed_items>]*
* [<v2_unmerged_items>]*
* [<v2_untracked_items>]*
@@ -2070,6 +2157,9 @@ static void wt_porcelain_v2_print(struct wt_status *s)
struct string_list_item *it;
int i;
+ if (s->show_branch)
+ wt_porcelain_v2_print_tracking(s);
+
for (i = 0; i < s->change.nr; i++) {
it = &(s->change.items[i]);
d = it->util;