wt-status.c: make wt_status_check_rebase() work on any worktree

This is a preparation step for find_shared_symref() to detect if any worktree is being rebased. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 22, 2016 at 20:01 UTC 81eff27b0fb652f80a43bdf18c18cd9bc25840df
2 files changed +24 -14
wt-status.c
+20 -13
@@ -15,6 +15,7 @@
15 #include "column.h"
16 #include "strbuf.h"
17 #include "utf8.h"
18 +#include "worktree.h"
19
20 static const char cut_line[] =
21 "------------------------ >8 ------------------------\n";
@@ -1262,13 +1263,13 @@ static void show_bisect_in_progress(struct wt_status *s,
1263 /*
1264 * Extract branch information from rebase/bisect
1265 */
1265 -static char *read_and_strip_branch(const char *path)
1266 +static char *get_branch(const struct worktree *wt, const char *path)
1267 {
1268 struct strbuf sb = STRBUF_INIT;
1269 unsigned char sha1[20];
1270 const char *branch_name;
1271
1271 - if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
1272 + if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1273 goto got_nothing;
1274
1275 while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1295,6 +1296,11 @@ got_nothing:
1296 return NULL;
1297 }
1298
1299 +static char *read_and_strip_branch(const char *path)
1300 +{
1301 + return get_branch(NULL, path);
1302 +}
1303 +
1304 struct grab_1st_switch_cbdata {
1305 struct strbuf buf;
1306 unsigned char nsha1[20];
@@ -1360,27 +1366,28 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
1366 strbuf_release(&cb.buf);
1367 }
1368
1363 -int wt_status_check_rebase(struct wt_status_state *state)
1369 +int wt_status_check_rebase(const struct worktree *wt,
1370 + struct wt_status_state *state)
1371 {
1372 struct stat st;
1373
1367 - if (!stat(git_path("rebase-apply"), &st)) {
1368 - if (!stat(git_path("rebase-apply/applying"), &st)) {
1374 + if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1375 + if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1376 state->am_in_progress = 1;
1370 - if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1377 + if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1378 state->am_empty_patch = 1;
1379 } else {
1380 state->rebase_in_progress = 1;
1374 - state->branch = read_and_strip_branch("rebase-apply/head-name");
1375 - state->onto = read_and_strip_branch("rebase-apply/onto");
1381 + state->branch = get_branch(wt, "rebase-apply/head-name");
1382 + state->onto = get_branch(wt, "rebase-apply/onto");
1383 }
1377 - } else if (!stat(git_path("rebase-merge"), &st)) {
1378 - if (!stat(git_path("rebase-merge/interactive"), &st))
1384 + } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1385 + if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1386 state->rebase_interactive_in_progress = 1;
1387 else
1388 state->rebase_in_progress = 1;
1382 - state->branch = read_and_strip_branch("rebase-merge/head-name");
1383 - state->onto = read_and_strip_branch("rebase-merge/onto");
1389 + state->branch = get_branch(wt, "rebase-merge/head-name");
1390 + state->onto = get_branch(wt, "rebase-merge/onto");
1391 } else
1392 return 0;
1393 return 1;
@@ -1394,7 +1401,7 @@ void wt_status_get_state(struct wt_status_state *state,
1401
1402 if (!stat(git_path_merge_head(), &st)) {
1403 state->merge_in_progress = 1;
1397 - } else if (wt_status_check_rebase(state)) {
1404 + } else if (wt_status_check_rebase(NULL, state)) {
1405 ; /* all set */
1406 } else if (!stat(git_path_cherry_pick_head(), &st) &&
1407 !get_sha1("CHERRY_PICK_HEAD", sha1)) {
wt-status.h
+4 -1
@@ -6,6 +6,8 @@
6 #include "color.h"
7 #include "pathspec.h"
8
9 +struct worktree;
10 +
11 enum color_wt_status {
12 WT_STATUS_HEADER = 0,
13 WT_STATUS_UPDATED,
@@ -100,7 +102,8 @@ void wt_status_prepare(struct wt_status *s);
102 void wt_status_print(struct wt_status *s);
103 void wt_status_collect(struct wt_status *s);
104 void wt_status_get_state(struct wt_status_state *state, int get_detached_from);
103 -int wt_status_check_rebase(struct wt_status_state *state);
105 +int wt_status_check_rebase(const struct worktree *wt,
106 + struct wt_status_state *state);
107
108 void wt_shortstatus_print(struct wt_status *s);
109 void wt_porcelain_print(struct wt_status *s);