summaryrefslogtreecommitdiff
path: root/worktree.c
diff options
context:
space:
mode:
authorRafael Silva <rafaeloliveira.cs@gmail.com>2021-01-19 21:27:35 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-01-30 17:57:20 (GMT)
commiteb36135af7b03fbaab2d3091fa7f5c62a164ff43 (patch)
treef393effd7a6e69121708c53f98fcf0c262b65c23 /worktree.c
parentfc0c7d5e9e9e396afdd669019e7635773b1d1423 (diff)
downloadgit-eb36135af7b03fbaab2d3091fa7f5c62a164ff43.zip
git-eb36135af7b03fbaab2d3091fa7f5c62a164ff43.tar.gz
git-eb36135af7b03fbaab2d3091fa7f5c62a164ff43.tar.bz2
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 <sunshine@sunshineco.com> Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
-rw-r--r--worktree.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/worktree.c b/worktree.c
index fb3e286..e008585 100644
--- a/worktree.c
+++ b/worktree.c
@@ -225,7 +225,8 @@ int is_main_worktree(const struct worktree *wt)
const char *worktree_lock_reason(struct worktree *wt)
{
- assert(!is_main_worktree(wt));
+ if (is_main_worktree(wt))
+ return NULL;
if (!wt->lock_reason_valid) {
struct strbuf path = STRBUF_INIT;