Skip to content

Commit eb36135

Browse files
raffsgitster
authored andcommitted
worktree: teach worktree_lock_reason() to gently handle main worktree
worktree_lock_reason() aborts with an assertion failure when called on the main worktree since locking the main worktree is nonsensical. Not only is this behavior undocumented, thus callers might not even be aware that the call could potentially crash the program, but it also forces clients to be extra careful: if (!is_main_worktree(wt) && worktree_locked_reason(...)) ... Since we know that locking makes no sense in the context of the main worktree, we can simply return false for the main worktree, thus making client code less complex by eliminating the need for the callers to have inside knowledge about the implementation: if (worktree_lock_reason(...)) ... Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Rafael Silva <[email protected]> Reviewed-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fc0c7d5 commit eb36135

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
604604
strbuf_addstr(&sb, "(error)");
605605
}
606606

607-
if (!is_main_worktree(wt) && worktree_lock_reason(wt))
607+
if (worktree_lock_reason(wt))
608608
strbuf_addstr(&sb, " locked");
609609

610610
printf("%s\n", sb.buf);

worktree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ int is_main_worktree(const struct worktree *wt)
225225

226226
const char *worktree_lock_reason(struct worktree *wt)
227227
{
228-
assert(!is_main_worktree(wt));
228+
if (is_main_worktree(wt))
229+
return NULL;
229230

230231
if (!wt->lock_reason_valid) {
231232
struct strbuf path = STRBUF_INIT;

0 commit comments

Comments
 (0)