git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v1 4/6] Expanded branch header for Porcelain Status V2

From
Jeff Hostetler <jeffhost@microsoft.com>
Date
Jul 19, 2016, 22:10 UTC
Message-ID
<1468966258-11191-5-git-send-email-jeffhost@microsoft.com>
In-Reply-To
<1468966258-11191-1-git-send-email-jeffhost@microsoft.com>

This commit expand porcelain status V2 to print detailed information about the current branch. This includes the SHA of the current commit, the current branch name, the upstream branch name, and the ahead/behind counts.

This additional information is included when the --branch argument is given.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
---
 builtin/commit.c |  5 ++++
 wt-status.c      | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 wt-status.h      |  2 ++
 3 files changed, 96 insertions(+)
diff --git a/builtin/commit.c b/builtin/commit.c
index b5ec9b9..830f688 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -512,6 +512,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->sha_commit, sha1);
 	s->status_format = status_format;
 
 	wt_status_collect(s);
@@ -1397,6 +1399,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.sha_commit, sha1);
+
 	s.ignore_submodule_arg = ignore_submodule_arg;
 	s.status_format = status_format;
 
diff --git a/wt-status.c b/wt-status.c
index 2d4829e..9ba4da5 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1847,6 +1847,92 @@ void wt_porcelain_print(struct wt_status *s)
 	wt_shortstatus_print(s);
 }
 
+/*
+ * Print branch and tracking header for porcelain v2 output.
+ * This is printed when the '--branch' parameter is given.
+ *
+ *    "## branch: <commit> <head>[ <upstream>[ +<ahead> -<behind>]]<eol>"
+ *
+ *              <commit> ::= 40 character SHA1 of the current commit or
+ *                           "(initial)" literal to indicate initialized
+ *                           repo with no commits.
+ *
+ *                <head> ::= <branch_name> the current branch name or
+ *                           "(detached)" literal when detached head.
+ *
+ *            <upstream> ::= the upstream branch name, when set.
+ *
+ *               <ahead> ::= integer ahead value, when upstream set
+ *                           and commit is present.
+ *
+ *              <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;
+
+	memset(&state, 0, sizeof(state));
+	wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
+
+	fprintf(s->fp, "## branch:");
+	fprintf(s->fp, " %s",
+		(s->is_initial ? "(initial)" : sha1_to_hex(s->sha_commit)));
+
+	if (s->branch) {
+		if (!strcmp(s->branch, "HEAD")) {
+			fprintf(s->fp, " %s", "(detached)");
+			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, " %s", branch_name);
+		}
+
+		/* 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, " %s", base);
+			free((char *)base);
+
+			if (ab_info)
+				fprintf(s->fp, " +%d -%d", nr_ahead, nr_behind);
+		}
+	} else {
+		/*
+		 * TODO All of various print routines allow for s->branch to be null.
+		 * TODO When can this happen and what should we report here?
+		 */
+		fprintf(s->fp, " %s", "(unknown)");
+	}
+
+	fprintf(s->fp, "%c", (s->null_termination ? '\0' : '\n'));
+
+	free(state.branch);
+	free(state.onto);
+	free(state.detached_from);
+}
+
 /* Convert various submodule status values into a
  * string of characters in the buffer provided.
  */
@@ -2122,6 +2208,9 @@ void wt_porcelain_v2_print(struct wt_status *s)
 {
 	int i;
 
+	if (s->show_branch)
+		wt_porcelain_v2_print_tracking(s);
+
 	for (i = 0; i < s->change.nr; i++) {
 		struct wt_status_change_data *d;
 		struct string_list_item *it;
diff --git a/wt-status.h b/wt-status.h
index c062e30..168556f 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -88,6 +88,8 @@ struct wt_status {
 	int hints;
 
 	enum wt_status_format status_format;
+	unsigned char sha_commit[GIT_SHA1_RAWSZ]; /* Commit SHA (when not Initial) */
+
 	/* These are computed during processing of the individual sections */
 	int commitable;
 	int workdir_dirty;
-- 
2.8.0.rc4.17.gac42084.dirty
Previous: Junio C HamanoNext: Jeff King
Message 22 of 41 in “Porcelain Status V2”
  1. 0/6 Porcelain Status V2Jeff Hostetler, Jul 19, 2016
  2. 1/6 Allow --porcelain[=<n>] in status and commit commandsJeff Hostetler, Jul 19, 2016
  3. Johannes SchindelinJul 20, 2016
  4. Jeff HostetlerJul 20, 2016
  5. Johannes SchindelinJul 21, 2016
  6. Jeff KingJul 20, 2016
  7. Jeff HostetlerJul 20, 2016
  8. Junio C HamanoJul 20, 2016
  9. 2/6 Status and checkout unit tests for --porcelain[=<n>]Jeff Hostetler, Jul 19, 2016
  10. Johannes SchindelinJul 20, 2016
  11. Jeff HostetlerJul 20, 2016
  12. Jeff KingJul 20, 2016
  13. Jeff KingJul 20, 2016
  14. Jeff HostetlerJul 20, 2016
  15. Jeff HostetlerJul 20, 2016
  16. 3/6 Per-file output for Porcelain Status V2Jeff Hostetler, Jul 19, 2016
  17. Junio C HamanoJul 20, 2016
  18. Johannes SchindelinJul 21, 2016
  19. Junio C HamanoJul 20, 2016
  20. Jeff HostetlerJul 21, 2016
  21. Junio C HamanoJul 22, 2016
  22. 4/6 Expanded branch header for Porcelain Status V2Jeff Hostetler, Jul 19, 2016
  23. Jeff KingJul 20, 2016
  24. Jeff HostetlerJul 20, 2016
  25. Jeff KingJul 20, 2016
  26. Johannes SchindelinJul 21, 2016
  27. Jeff HostetlerJul 21, 2016
  28. 6/6 Unit tests for V2 porcelain statusJeff Hostetler, Jul 19, 2016
  29. Jakub NarębskiJul 20, 2016
  30. Jeff HostetlerJul 20, 2016
  31. Jakub NarębskiJul 20, 2016
  32. Johannes SchindelinJul 21, 2016
  33. 5/6 Add porcelain V2 documentation to status manpageJeff Hostetler, Jul 19, 2016
  34. Jakub NarębskiJul 20, 2016
  35. Jeff HostetlerJul 20, 2016
  36. Jakub NarębskiJul 20, 2016
  37. Junio C HamanoJul 20, 2016
  38. Jeff KingJul 20, 2016
  39. Jeff HostetlerJul 20, 2016
  40. Jeff KingJul 20, 2016
  41. Johannes SchindelinJul 21, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.