[PATCH 5/7] wt-status.c: make wt_status_get_state() support worktree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The function is to be used to detect if a ref is checked out in another
worktree. In some cases, parsing "HEAD" alone is not enough because the
worktree may be under rebase or bisect.

Note that because get_sha1() cannot take a worktree (and of course will
not, we need something like worktree_get_sha1), cherry-pick and revert
detection will not work when the examined worktree is not the current
one. The same for get_detached_from.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 wt-status.c | 42 +++++++++++++++++++++++++-----------------
 wt-status.h |  3 +++
 2 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/wt-status.c b/wt-status.c
index 1ea2ebe..6cca3a6 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -15,6 +15,7 @@
 #include "column.h"
 #include "strbuf.h"
 #include "utf8.h"
+#include "worktree.h"
 
 static const char cut_line[] =
 "------------------------ >8 ------------------------\n";
@@ -1262,13 +1263,13 @@ static void show_bisect_in_progress(struct wt_status *s,
 /*
  * Extract branch information from rebase/bisect
  */
-static char *read_and_strip_branch(const char *path)
+static char *read_and_strip_branch(const struct worktree *wt, const char *path)
 {
 	struct strbuf sb = STRBUF_INIT;
 	unsigned char sha1[20];
 	const char *branch_name;
 
-	if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
+	if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
 		goto got_nothing;
 
 	while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1363,45 +1364,52 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
 void wt_status_get_state(struct wt_status_state *state,
 			 int get_detached_from)
 {
+	const struct worktree *wt = state->wt;
 	struct stat st;
 	unsigned char sha1[20];
 
-	if (!stat(git_path_merge_head(), &st)) {
+	if (!stat(worktree_git_path_merge_head(wt), &st)) {
 		state->merge_in_progress = 1;
-	} else if (!stat(git_path("rebase-apply"), &st)) {
-		if (!stat(git_path("rebase-apply/applying"), &st)) {
+	} else if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
+		if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
 			state->am_in_progress = 1;
-			if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
+			if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
 				state->am_empty_patch = 1;
 		} else {
 			state->rebase_in_progress = 1;
-			state->branch = read_and_strip_branch("rebase-apply/head-name");
-			state->onto = read_and_strip_branch("rebase-apply/onto");
+			state->branch = read_and_strip_branch(wt, "rebase-apply/head-name");
+			state->onto = read_and_strip_branch(wt, "rebase-apply/onto");
 		}
-	} else if (!stat(git_path("rebase-merge"), &st)) {
-		if (!stat(git_path("rebase-merge/interactive"), &st))
+	} else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
+		if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
 			state->rebase_interactive_in_progress = 1;
 		else
 			state->rebase_in_progress = 1;
-		state->branch = read_and_strip_branch("rebase-merge/head-name");
-		state->onto = read_and_strip_branch("rebase-merge/onto");
-	} else if (!stat(git_path_cherry_pick_head(), &st) &&
+		state->branch = read_and_strip_branch(wt, "rebase-merge/head-name");
+		state->onto = read_and_strip_branch(wt, "rebase-merge/onto");
+	} else if (!wt && /* TODO: get_sha1() cannot select worktree yet */
+		   !stat(worktree_git_path_cherry_pick_head(wt), &st) &&
 			!get_sha1("CHERRY_PICK_HEAD", sha1)) {
 		state->cherry_pick_in_progress = 1;
 		hashcpy(state->cherry_pick_head_sha1, sha1);
 	}
-	if (!stat(git_path("BISECT_LOG"), &st)) {
+	if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
 		state->bisect_in_progress = 1;
-		state->branch = read_and_strip_branch("BISECT_START");
+		state->branch = read_and_strip_branch(wt, "BISECT_START");
 	}
-	if (!stat(git_path_revert_head(), &st) &&
+	if (!wt &&  /* TODO: get_sha1() cannot select worktree yet */
+	    !stat(worktree_git_path_revert_head(wt), &st) &&
 	    !get_sha1("REVERT_HEAD", sha1)) {
 		state->revert_in_progress = 1;
 		hashcpy(state->revert_head_sha1, sha1);
 	}
 
-	if (get_detached_from)
+	if (get_detached_from) {
+		if (wt)
+			die("BUG: %s:%d: non-zero get_detached_from is not supported",
+			    __FILE__, __LINE__);
 		wt_status_get_detached_from(state);
+	}
 }
 
 static void wt_status_print_state(struct wt_status *s,
diff --git a/wt-status.h b/wt-status.h
index c9b3b74..d31db38 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -6,6 +6,8 @@
 #include "color.h"
 #include "pathspec.h"
 
+struct worktree;
+
 enum color_wt_status {
 	WT_STATUS_HEADER = 0,
 	WT_STATUS_UPDATED,
@@ -77,6 +79,7 @@ struct wt_status {
 };
 
 struct wt_status_state {
+	struct worktree *wt;
 	int merge_in_progress;
 	int am_in_progress;
 	int am_empty_patch;
-- 
2.8.0.rc0.210.gd302cd2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]