From: Jeff Hostetler <git@jeffhostetler.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, Johannes.Schindelin@gmx.de,
Jeff Hostetler <jeffhost@microsoft.com>,
Jeff Hostetler <git@jeffhostetler.com>
Subject: [PATCH v4 6/8] status: print branch info with --porcelain=v2 --branch
Date: Tue, 2 Aug 2016 10:12:15 -0400 [thread overview]
Message-ID: <1470147137-17498-7-git-send-email-git@jeffhostetler.com> (raw)
In-Reply-To: <1470147137-17498-1-git-send-email-git@jeffhostetler.com>
From: Jeff Hostetler <jeffhost@microsoft.com>
Expand porcelain v2 output to include branch and tracking
branch information. This includes the commit SHA, the branch,
the upstream branch, and the ahead and behind counts.
Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
Signed-off-by: Jeff Hostetler <git@jeffhostetler.com>
---
builtin/commit.c | 5 ++++
wt-status.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
wt-status.h | 1 +
3 files changed, 96 insertions(+)
diff --git a/builtin/commit.c b/builtin/commit.c
index 93ce28c..b1fd2d1 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -510,6 +510,8 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
s->fp = fp;
s->nowarn = nowarn;
s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
+ if (!s->is_initial)
+ hashcpy(s->sha1_commit, sha1);
s->status_format = status_format;
s->ignore_submodule_arg = ignore_submodule_arg;
@@ -1378,6 +1380,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
fd = hold_locked_index(&index_lock, 0);
s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
+ if (!s.is_initial)
+ hashcpy(s.sha1_commit, sha1);
+
s.ignore_submodule_arg = ignore_submodule_arg;
s.status_format = status_format;
s.verbose = verbose;
diff --git a/wt-status.c b/wt-status.c
index 46061d4..592fbd2 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1814,6 +1814,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.
*/
@@ -2057,6 +2143,7 @@ static void wt_porcelain_v2_print_other(
/*
* Print porcelain V2 status.
*
+ * [<v2_branch>]
* [<v2_changed_items>]*
* [<v2_unmerged_items>]*
* [<v2_untracked_items>]*
@@ -2069,6 +2156,9 @@ 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;
diff --git a/wt-status.h b/wt-status.h
index 89a6d43..baedfe3 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -80,6 +80,7 @@ struct wt_status {
int hints;
enum wt_status_format status_format;
+ unsigned char sha1_commit[GIT_SHA1_RAWSZ]; /* when not Initial */
/* These are computed during processing of the individual sections */
int commitable;
--
2.8.0.rc4.17.gac42084.dirty
next prev parent reply other threads:[~2016-08-02 14:16 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-02 14:12 [PATCH v4 0/8] status: V2 porcelain status Jeff Hostetler
2016-08-02 14:12 ` [PATCH v4 1/8] status: rename long-format print routines Jeff Hostetler
2016-08-03 21:28 ` Junio C Hamano
2016-08-04 12:30 ` Jeff Hostetler
2016-08-02 14:12 ` [PATCH v4 2/8] status: cleanup API to wt_status_print Jeff Hostetler
2016-08-03 21:36 ` Junio C Hamano
2016-08-04 12:35 ` Jeff Hostetler
2016-08-02 14:12 ` [PATCH v4 3/8] status: support --porcelain[=<version>] Jeff Hostetler
2016-08-03 21:37 ` Junio C Hamano
2016-08-02 14:12 ` [PATCH v4 4/8] status: per-file data collection for --porcelain=v2 Jeff Hostetler
2016-08-02 14:12 ` [PATCH v4 5/8] status: print per-file porcelain v2 status data Jeff Hostetler
2016-08-05 21:02 ` Jeff King
2016-08-05 21:09 ` Junio C Hamano
2016-08-05 21:14 ` Jeff King
2016-08-05 21:21 ` Junio C Hamano
2016-08-05 21:43 ` Stefan Beller
2016-08-05 21:47 ` Stefan Beller
2016-08-05 21:27 ` Jeff Hostetler
2016-08-02 14:12 ` Jeff Hostetler [this message]
2016-08-05 17:01 ` [PATCH v4 6/8] status: print branch info with --porcelain=v2 --branch Junio C Hamano
2016-08-05 18:11 ` Jeff Hostetler
2016-08-02 14:12 ` [PATCH v4 7/8] git-status.txt: describe --porcelain=v2 format Jeff Hostetler
2016-08-05 17:50 ` Junio C Hamano
2016-08-05 18:27 ` Jeff Hostetler
2016-08-02 14:12 ` [PATCH v4 8/8] status: tests for --porcelain=v2 Jeff Hostetler
2016-08-05 18:12 ` Junio C Hamano
2016-08-05 18:31 ` Jeff Hostetler
2016-08-03 15:09 ` [PATCH v4 0/8] status: V2 porcelain status Johannes Schindelin
2016-08-03 15:23 ` Junio C Hamano
2016-08-03 16:10 ` Johannes Schindelin
2016-08-03 16:59 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1470147137-17498-7-git-send-email-git@jeffhostetler.com \
--to=git@jeffhostetler.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jeffhost@microsoft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.