[PATCH V2 2/3] wt-status: pass struct repository and wt_status through function parameters
- From
Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com>
- Date
- Feb 5, 2026, 10:13 UTC
- Message-ID
- <20260205101524.125452-3-shreyanshpaliwalcmsmn@gmail.com>
- In-Reply-To
- <20260205101524.125452-1-shreyanshpaliwalcmsmn@gmail.com>
Some functions in wt-status.c relied on the_repository because no repository instance was available in their local scope. There is also a specific case in wt_status_check_rebase() where the worktree can be NULL, so accessing wt->repo may lead to a segfault.
Update these functions to accept a struct repository or struct wt_status parameter, and adjust callers accordingly. Replace the remaining uses of the_repository in these functions with the passed-in repository instance.
This removes the use of the_repository global variable from wt-status.c completely.
Signed-off-by: Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> --- branch.c | 2 +- worktree.c | 2 +- wt-status.c | 51 ++++++++++++++++++++++++++------------------------- wt-status.h | 5 +++-- 4 files changed, 31 insertions(+), 29 deletions(-)
diff --git a/branch.c b/branch.c index 243db7d0fc..9fe2df8cef 100644 --- a/branch.c +++ b/branch.c @@ -412,7 +412,7 @@ static void prepare_checked_out_branches(void) free(old); } - if (wt_status_check_rebase(wt, &state) && + if (wt_status_check_rebase(wt->repo, wt, &state) && (state.rebase_in_progress || state.rebase_interactive_in_progress) && state.branch) { struct strbuf ref = STRBUF_INIT; diff --git a/worktree.c b/worktree.c index 9308389cb6..d05531813c 100644 --- a/worktree.c +++ b/worktree.c @@ -443,7 +443,7 @@ int is_worktree_being_rebased(const struct worktree *wt, int found_rebase; memset(&state, 0, sizeof(state)); - found_rebase = wt_status_check_rebase(wt, &state) && + found_rebase = wt_status_check_rebase(wt->repo, wt, &state) && (state.rebase_in_progress || state.rebase_interactive_in_progress) && state.branch && diff --git a/wt-status.c b/wt-status.c index f71addc35f..b008682043 100644 --- a/wt-status.c +++ b/wt-status.c @@ -984,17 +984,17 @@ static int stash_count_refs(const char *refname UNUSED, return 0; } -static int count_stash_entries(void) +static int count_stash_entries(struct repository *r) { int n = 0; - refs_for_each_reflog_ent(get_main_ref_store(the_repository), + refs_for_each_reflog_ent(get_main_ref_store(r), "refs/stash", stash_count_refs, &n); return n; } static void wt_longstatus_print_stash_summary(struct wt_status *s) { - int stash_count = count_stash_entries(); + int stash_count = count_stash_entries(s->repo); if (stash_count > 0) status_printf_ln(s, GIT_COLOR_NORMAL, @@ -1287,10 +1287,10 @@ static void show_am_in_progress(struct wt_status *s, wt_longstatus_print_trailer(s); } -static char *read_line_from_git_path(const char *filename) +static char *read_line_from_git_path(struct repository *r, const char *filename) { struct strbuf buf = STRBUF_INIT; - FILE *fp = fopen_or_warn(repo_git_path_append(the_repository, &buf, + FILE *fp = fopen_or_warn(repo_git_path_append(r, &buf, "%s", filename), "r"); if (!fp) { @@ -1325,8 +1325,8 @@ static int split_commit_in_progress(struct wt_status *s) if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF) return 0; - rebase_amend = read_line_from_git_path("rebase-merge/amend"); - rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head"); + rebase_amend = read_line_from_git_path(s->repo, "rebase-merge/amend"); + rebase_orig_head = read_line_from_git_path(s->repo, "rebase-merge/orig-head"); if (!rebase_amend || !rebase_orig_head) ; /* fall through, no split in progress */ @@ -1350,7 +1350,7 @@ static int split_commit_in_progress(struct wt_status *s) * The function assumes that the line does not contain useless spaces * before or after the command. */ -static void abbrev_oid_in_line(struct strbuf *line) +static void abbrev_oid_in_line(struct repository *r, struct strbuf *line) { struct string_list split = STRING_LIST_INIT_DUP; struct object_id oid; @@ -1362,7 +1362,7 @@ static void abbrev_oid_in_line(struct strbuf *line) return; if ((2 <= string_list_split(&split, line->buf, " ", 2)) && - !repo_get_oid(the_repository, split.items[1].string, &oid)) { + !repo_get_oid(r, split.items[1].string, &oid)) { strbuf_reset(line); strbuf_addf(line, "%s ", split.items[0].string); strbuf_add_unique_abbrev(line, &oid, DEFAULT_ABBREV); @@ -1372,10 +1372,10 @@ static void abbrev_oid_in_line(struct strbuf *line) string_list_clear(&split, 0); } -static int read_rebase_todolist(const char *fname, struct string_list *lines) +static int read_rebase_todolist(struct repository *r, const char *fname, struct string_list *lines) { struct strbuf buf = STRBUF_INIT; - FILE *f = fopen(repo_git_path_append(the_repository, &buf, "%s", fname), "r"); + FILE *f = fopen(repo_git_path_append(r, &buf, "%s", fname), "r"); int ret; if (!f) { @@ -1384,7 +1384,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines) goto out; } die_errno("Could not open file %s for reading", - repo_git_path_replace(the_repository, &buf, "%s", fname)); + repo_git_path_replace(r, &buf, "%s", fname)); } while (!strbuf_getline_lf(&buf, f)) { if (starts_with(buf.buf, comment_line_str)) @@ -1392,7 +1392,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines) strbuf_trim(&buf); if (!buf.len) continue; - abbrev_oid_in_line(&buf); + abbrev_oid_in_line(r, &buf); string_list_append(lines, buf.buf); } fclose(f); @@ -1413,8 +1413,8 @@ static void show_rebase_information(struct wt_status *s, struct string_list have_done = STRING_LIST_INIT_DUP; struct string_list yet_to_do = STRING_LIST_INIT_DUP; - read_rebase_todolist("rebase-merge/done", &have_done); - if (read_rebase_todolist("rebase-merge/git-rebase-todo", + read_rebase_todolist(s->repo, "rebase-merge/done", &have_done); + if (read_rebase_todolist(s->repo, "rebase-merge/git-rebase-todo", &yet_to_do)) status_printf_ln(s, color, _("git-rebase-todo is missing.")); @@ -1718,23 +1718,24 @@ static void wt_status_get_detached_from(struct repository *r, strbuf_release(&cb.buf); } -int wt_status_check_rebase(const struct worktree *wt, - struct wt_status_state *state) +int wt_status_check_rebase(struct repository *r, + const struct worktree *wt, + struct wt_status_state *state) { struct stat st; - if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) { - if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) { + if (!stat(worktree_git_path(r, wt, "rebase-apply"), &st)) { + if (!stat(worktree_git_path(r, wt, "rebase-apply/applying"), &st)) { state->am_in_progress = 1; - if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size) + if (!stat(worktree_git_path(r, wt, "rebase-apply/patch"), &st) && !st.st_size) state->am_empty_patch = 1; } else { state->rebase_in_progress = 1; state->branch = get_branch(wt, "rebase-apply/head-name"); state->onto = get_branch(wt, "rebase-apply/onto"); } - } else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) { - if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st)) + } else if (!stat(worktree_git_path(r, wt, "rebase-merge"), &st)) { + if (!stat(worktree_git_path(r, wt, "rebase-merge/interactive"), &st)) state->rebase_interactive_in_progress = 1; else state->rebase_in_progress = 1; @@ -1797,9 +1798,9 @@ void wt_status_get_state(struct repository *r, enum replay_action action; if (!stat(git_path_merge_head(r), &st)) { - wt_status_check_rebase(NULL, state); + wt_status_check_rebase(r, NULL, state); state->merge_in_progress = 1; - } else if (wt_status_check_rebase(NULL, state)) { + } else if (wt_status_check_rebase(r, NULL, state)) { ; /* all set */ } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) { @@ -2259,7 +2260,7 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s) */ static void wt_porcelain_v2_print_stash(struct wt_status *s) { - int stash_count = count_stash_entries(); + int stash_count = count_stash_entries(s->repo); char eol = s->null_termination ? '\0' : '\n'; if (stash_count > 0) diff --git a/wt-status.h b/wt-status.h index e40a27214a..110e3907f8 100644 --- a/wt-status.h +++ b/wt-status.h @@ -164,8 +164,9 @@ void wt_status_state_free_buffers(struct wt_status_state *s); void wt_status_get_state(struct repository *repo, struct wt_status_state *state, int get_detached_from); -int wt_status_check_rebase(const struct worktree *wt, - struct wt_status_state *state); +int wt_status_check_rebase(struct repository *r, + const struct worktree *wt, + struct wt_status_state *state); int wt_status_check_bisect(const struct worktree *wt, struct wt_status_state *state);
-- 2.52.0