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

[PATCH v2 1/4] wt-status: split wt_status_state parsing function out

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 6, 2013, 12:21 UTC
Message-ID
<1362572482-20570-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1362572482-20570-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 wt-status.c | 52 +++++++++++++++++++++++++++++++---------------------
 wt-status.h |  5 +++--
 2 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/wt-status.c b/wt-status.c
index ef405d0..183aafe 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -970,7 +970,7 @@ static void show_bisect_in_progress(struct wt_status *s,
  * Extract branch information from rebase/bisect
  */
 static void read_and_strip_branch(struct strbuf *sb,
-				  const char **branch,
+				  char **branch,
 				  const char *path)
 {
 	unsigned char sha1[20];
@@ -994,52 +994,62 @@ static void read_and_strip_branch(struct strbuf *sb,
 		strbuf_addstr(sb, abbrev);
 		*branch = sb->buf;
 	} else if (!strcmp(sb->buf, "detached HEAD")) /* rebase */
-		;
+		*branch = NULL;
 	else			/* bisect */
 		*branch = sb->buf;
+	if (*branch)
+		*branch = xstrdup(*branch);
 }
 
-static void wt_status_print_state(struct wt_status *s)
+void wt_status_get_state(struct wt_status_state *state)
 {
-	const char *state_color = color(WT_STATUS_HEADER, s);
 	struct strbuf branch = STRBUF_INIT;
 	struct strbuf onto = STRBUF_INIT;
-	struct wt_status_state state;
 	struct stat st;
 
-	memset(&state, 0, sizeof(state));
+	memset(state, 0, sizeof(*state));
 
 	if (!stat(git_path("MERGE_HEAD"), &st)) {
-		state.merge_in_progress = 1;
+		state->merge_in_progress = 1;
 	} else if (!stat(git_path("rebase-apply"), &st)) {
 		if (!stat(git_path("rebase-apply/applying"), &st)) {
-			state.am_in_progress = 1;
+			state->am_in_progress = 1;
 			if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
-				state.am_empty_patch = 1;
+				state->am_empty_patch = 1;
 		} else {
-			state.rebase_in_progress = 1;
-			read_and_strip_branch(&branch, &state.branch,
+			state->rebase_in_progress = 1;
+			read_and_strip_branch(&branch, &state->branch,
 					      "rebase-apply/head-name");
-			read_and_strip_branch(&onto, &state.onto,
+			read_and_strip_branch(&onto, &state->onto,
 					      "rebase-apply/onto");
 		}
 	} else if (!stat(git_path("rebase-merge"), &st)) {
 		if (!stat(git_path("rebase-merge/interactive"), &st))
-			state.rebase_interactive_in_progress = 1;
+			state->rebase_interactive_in_progress = 1;
 		else
-			state.rebase_in_progress = 1;
-		read_and_strip_branch(&branch, &state.branch,
+			state->rebase_in_progress = 1;
+		read_and_strip_branch(&branch, &state->branch,
 				      "rebase-merge/head-name");
-		read_and_strip_branch(&onto, &state.onto,
+		read_and_strip_branch(&onto, &state->onto,
 				      "rebase-merge/onto");
 	} else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
-		state.cherry_pick_in_progress = 1;
+		state->cherry_pick_in_progress = 1;
 	}
 	if (!stat(git_path("BISECT_LOG"), &st)) {
-		state.bisect_in_progress = 1;
-		read_and_strip_branch(&branch, &state.branch,
+		state->bisect_in_progress = 1;
+		read_and_strip_branch(&branch, &state->branch,
 				      "BISECT_START");
 	}
+	strbuf_release(&branch);
+	strbuf_release(&onto);
+}
+
+static void wt_status_print_state(struct wt_status *s)
+{
+	const char *state_color = color(WT_STATUS_HEADER, s);
+	struct wt_status_state state;
+
+	wt_status_get_state(&state);
 
 	if (state.merge_in_progress)
 		show_merge_in_progress(s, &state, state_color);
@@ -1051,8 +1061,8 @@ static void wt_status_print_state(struct wt_status *s)
 		show_cherry_pick_in_progress(s, &state, state_color);
 	if (state.bisect_in_progress)
 		show_bisect_in_progress(s, &state, state_color);
-	strbuf_release(&branch);
-	strbuf_release(&onto);
+	free(state.branch);
+	free(state.onto);
 }
 
 void wt_status_print(struct wt_status *s)
diff --git a/wt-status.h b/wt-status.h
index 81e1dcf..5ddcbf6 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -79,13 +79,14 @@ struct wt_status_state {
 	int rebase_interactive_in_progress;
 	int cherry_pick_in_progress;
 	int bisect_in_progress;
-	const char *branch;
-	const char *onto;
+	char *branch;
+	char *onto;
 };
 
 void wt_status_prepare(struct wt_status *s);
 void wt_status_print(struct wt_status *s);
 void wt_status_collect(struct wt_status *s);
+void wt_status_get_state(struct wt_status_state *state);
 
 void wt_shortstatus_print(struct wt_status *s);
 void wt_porcelain_print(struct wt_status *s);
-- 
1.8.1.2.536.gf441e6d
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 14 of 38 in “nd/branch-show-rebase-bisect-state updates”
  1. 0/5 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 3, 2013
  2. 1/5 checkout: record full target ref in reflogNguyễn Thái Ngọc Duy, Mar 3, 2013
  3. 2/5 wt-status: split wt_status_state parsing function outNguyễn Thái Ngọc Duy, Mar 3, 2013
  4. 3/5 wt-status: move wt_status_get_state() out to wt_status_print()Nguyễn Thái Ngọc Duy, Mar 3, 2013
  5. 4/5 status: show the ref that is checked out, even if it's detachedNguyễn Thái Ngọc Duy, Mar 3, 2013
  6. Junio C HamanoMar 3, 2013
  7. Duy NguyenMar 4, 2013
  8. Junio C HamanoMar 4, 2013
  9. Duy NguyenMar 5, 2013
  10. Matthieu MoyMar 5, 2013
  11. 5/5 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 3, 2013
  12. Junio C HamanoMar 3, 2013
  13. 0/4 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 6, 2013
  14. 1/4 wt-status: split wt_status_state parsing function outNguyễn Thái Ngọc Duy, Mar 6, 2013
  15. Junio C HamanoMar 6, 2013
  16. Junio C HamanoMar 6, 2013
  17. 2/4 wt-status: move wt_status_get_state() out to wt_status_print()Nguyễn Thái Ngọc Duy, Mar 6, 2013
  18. 3/4 status: show more info than "currently not on any branch"Nguyễn Thái Ngọc Duy, Mar 6, 2013
  19. Junio C HamanoMar 6, 2013
  20. Duy NguyenMar 8, 2013
  21. Junio C HamanoMar 8, 2013
  22. 4/4 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 6, 2013
  23. 0/4 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 6, 2013
  24. 4/4 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 6, 2013
  25. 0/5 nd/branch-show-rebase-bisect-state updatesNguyễn Thái Ngọc Duy, Mar 13, 2013
  26. 1/5 wt-status: move strbuf into read_and_strip_branch()Nguyễn Thái Ngọc Duy, Mar 13, 2013
  27. Junio C HamanoMar 13, 2013
  28. 1/5 wt-status: move strbuf into read_and_strip_branch()Nguyễn Thái Ngọc Duy, Mar 16, 2013
  29. 2/5 wt-status: split wt_status_state parsing function outNguyễn Thái Ngọc Duy, Mar 13, 2013
  30. 3/5 wt-status: move wt_status_get_state() out to wt_status_print()Nguyễn Thái Ngọc Duy, Mar 13, 2013
  31. 4/5 status: show more info than "currently not on any branch"Nguyễn Thái Ngọc Duy, Mar 13, 2013
  32. Junio C HamanoMar 13, 2013
  33. 5/5 branch: show more information when HEAD is detachedNguyễn Thái Ngọc Duy, Mar 13, 2013
  34. Junio C HamanoMar 19, 2013
  35. Duy NguyenMar 20, 2013
  36. Junio C HamanoMar 20, 2013
  37. status, branch: fix the misleading "bisecting" messageNguyễn Thái Ngọc Duy, Mar 23, 2013
  38. Junio C HamanoMar 24, 2013

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.