git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/7] worktree: teach worktree_lock_reason() to gently handle main worktree

From
Rafael Silva <rafaeloliveira.cs@gmail.com>
Date
Jan 8, 2021, 07:43 UTC
Message-ID
<gohp6k8s94m15w.fsf@gmail.com>
In-Reply-To
<CAPig+cRrU-T9ULyoNVtrWxm-5yb5NVSCT3+LvLmA5xJCv4n_9Q@mail.gmail.com>
Eric Sunshine writes:
Show 8 quoted lines
> On Mon, Jan 4, 2021 at 11:22 AM Rafael Silva
> <rafaeloliveira.cs@gmail.com> wrote:
>> The main worktree should not be locked and the worktree_lock_reason() API
>> is aware of this fact and avoids running the check code for the main
>> worktree. This checks is done via assert() macro, Therefore the caller
>
> s/Therefore/therefore/
>
Nice catch. thanks.
Show 31 quoted lines
>> needs to ensure the function is never called, usually by additional code.
>>
>> We can handle that case more gently by just returning false for the main
>> worktree and not bother checking if the "locked" file exists. This will
>> allowed further simplification from the caller as they will not need to
>> ensure the main worktree is never passed to the API.
>>
>> Teach worktree_lock_reason() to be more gently and just return false for
>> the main working tree.
>
> The situation is even a bit worse since the main worktree restriction
> isn't even documented. Here's a possible rewrite of the commit message
> which addresses that point too:
>
>     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
>     callers to have inside knowledge about the implementation:
>
>         if (worktree_locked_reason(...))
>             ...
>

Yes, this is a nicer commit message to explain the current situation and having the code example in there makes even more clearer. Thanks for the suggestion will definitely make this change on the next revision.

Show 23 quoted lines
>> Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
>> ---
>> diff --git a/worktree.c b/worktree.c
>> @@ -225,9 +225,7 @@ int is_main_worktree(const struct worktree *wt)
>>  const char *worktree_lock_reason(struct worktree *wt)
>>  {
>> -       assert(!is_main_worktree(wt));
>> -
>> -       if (!wt->lock_reason_valid) {
>> +       if (!is_main_worktree(wt) && !wt->lock_reason_valid) {
>>                 struct strbuf path = STRBUF_INIT;
>
> As mentioned in my review of patch [2/7], this would be more idiomatic
> and easier to reason about if the function returns early for the main
> worktree case, thus freeing the reader from having to think about that
> case for the remainder of the code. So:
>
>     if (is_main_worktree(wt))
>         return NULL;
>     if (!wt->lock_reason_valid) {
>         ...
>
> Subjective, and not necessarily worth a re-roll, though.

I'm inclined to change here on the next revision as I will do the same for the patch [2/7] with the worktree_prune_reason() and use the same pattern for both functions.

-- 
Thanks
Rafael
Previous: Eric SunshineNext: Rafael Silva
Message 4 of 88 in “teach `worktree list` verbose mode and prunable annotations”
  1. 0/7 teach `worktree list` verbose mode and prunable annotationsRafael Silva, Jan 4, 2021
  2. 3/7 worktree: teach worktree_lock_reason() to gently handle main worktreeRafael Silva, Jan 4, 2021
  3. Eric SunshineJan 6, 2021
  4. Rafael SilvaJan 8, 2021
  5. 6/7 worktree: add tests for `list` verbose and annotationsRafael Silva, Jan 4, 2021
  6. Eric SunshineJan 6, 2021
  7. Eric SunshineJan 7, 2021
  8. Rafael SilvaJan 8, 2021
  9. 4/7 worktree: teach `list` prunable annotation and verboseRafael Silva, Jan 4, 2021
  10. Eric SunshineJan 6, 2021
  11. Rafael SilvaJan 8, 2021
  12. 7/7 worktree: document `list` verbose and prunable annotationsRafael Silva, Jan 4, 2021
  13. Eric SunshineJan 6, 2021
  14. Rafael SilvaJan 8, 2021
  15. 5/7 worktree: `list` escape lock reason in --porcelainRafael Silva, Jan 4, 2021
  16. Phillip WoodJan 5, 2021
  17. worktree: add -z option for list subcommandPhillip Wood, Jan 5, 2021
  18. Eric SunshineJan 7, 2021
  19. Phillip WoodJan 8, 2021
  20. Eric SunshineJan 10, 2021
  21. Eric SunshineJan 6, 2021
  22. Rafael SilvaJan 8, 2021
  23. Eric SunshineJan 6, 2021
  24. 1/7 worktree: move should_prune_worktree() to worktree.cRafael Silva, Jan 4, 2021
  25. Eric SunshineJan 6, 2021
  26. Rafael SilvaJan 8, 2021
  27. Eric SunshineJan 6, 2021
  28. Eric SunshineJan 7, 2021
  29. Rafael SilvaJan 8, 2021
  30. 2/7 worktree: implement worktree_prune_reason() wrapperRafael Silva, Jan 4, 2021
  31. Eric SunshineJan 6, 2021
  32. Rafael SilvaJan 8, 2021
  33. Eric SunshineJan 6, 2021
  34. Rafael SilvaJan 8, 2021
  35. Eric SunshineJan 8, 2021
  36. 0/6 teach `worktree list` verbose mode and prunable annotationsRafael Silva, Jan 17, 2021
  37. 1/6 worktree: libify should_prune_worktree()Rafael Silva, Jan 17, 2021
  38. 4/6 worktree: teach `list --porcelain` to annotate locked worktreeRafael Silva, Jan 17, 2021
  39. Eric SunshineJan 18, 2021
  40. Rafael SilvaJan 19, 2021
  41. Eric SunshineJan 19, 2021
  42. 5/6 worktree: teach `list` to annotate prunable worktreeRafael Silva, Jan 17, 2021
  43. Eric SunshineJan 18, 2021
  44. Rafael SilvaJan 19, 2021
  45. Eric SunshineJan 19, 2021
  46. 6/6 worktree: teach `list` verbose modeRafael Silva, Jan 17, 2021
  47. Eric SunshineJan 18, 2021
  48. Eric SunshineJan 18, 2021
  49. 2/6 worktree: teach worktree to lazy-load "prunable" reasonRafael Silva, Jan 17, 2021
  50. Eric SunshineJan 18, 2021
  51. Rafael SilvaJan 19, 2021
  52. 3/6 worktree: teach worktree_lock_reason() to gently handle main worktreeRafael Silva, Jan 17, 2021
  53. Eric SunshineJan 18, 2021
  54. Rafael SilvaJan 19, 2021
  55. 0/7 teach `worktree list` verbose mode and prunable annotationsRafael Silva, Jan 19, 2021
  56. 1/7 worktree: libify should_prune_worktree()Rafael Silva, Jan 19, 2021
  57. 7/7 worktree: teach `list` verbose modeRafael Silva, Jan 19, 2021
  58. Eric SunshineJan 24, 2021
  59. Rafael SilvaJan 24, 2021
  60. 2/7 worktree: teach worktree to lazy-load "prunable" reasonRafael Silva, Jan 19, 2021
  61. 6/7 worktree: teach `list` to annotate prunable worktreeRafael Silva, Jan 19, 2021
  62. Junio C HamanoJan 21, 2021
  63. Rafael SilvaJan 21, 2021
  64. Junio C HamanoJan 21, 2021
  65. 3/7 worktree: teach worktree_lock_reason() to gently handle main worktreeRafael Silva, Jan 19, 2021
  66. 4/7 t2402: ensure locked worktree is properly cleaned upRafael Silva, Jan 19, 2021
  67. Eric SunshineJan 24, 2021
  68. Rafael SilvaJan 24, 2021
  69. 5/7 worktree: teach `list --porcelain` to annotate locked worktreeRafael Silva, Jan 19, 2021
  70. Phillip WoodJan 20, 2021
  71. Junio C HamanoJan 21, 2021
  72. Rafael SilvaJan 21, 2021
  73. Eric SunshineJan 24, 2021
  74. Eric SunshineJan 24, 2021
  75. Rafael SilvaJan 24, 2021
  76. Eric SunshineJan 24, 2021
  77. Rafael SilvaJan 27, 2021
  78. 0/7 teach `worktree list` verbose mode and prunable annotationsRafael Silva, Jan 27, 2021
  79. 2/7 worktree: teach worktree to lazy-load "prunable" reasonRafael Silva, Jan 27, 2021
  80. 6/7 worktree: teach `list` to annotate prunable worktreeRafael Silva, Jan 27, 2021
  81. 5/7 worktree: teach `list --porcelain` to annotate locked worktreeRafael Silva, Jan 27, 2021
  82. 7/7 worktree: teach `list` verbose modeRafael Silva, Jan 27, 2021
  83. 3/7 worktree: teach worktree_lock_reason() to gently handle main worktreeRafael Silva, Jan 27, 2021
  84. 1/7 worktree: libify should_prune_worktree()Rafael Silva, Jan 27, 2021
  85. 4/7 t2402: ensure locked worktree is properly cleaned upRafael Silva, Jan 27, 2021
  86. Eric SunshineJan 30, 2021
  87. Rafael SilvaJan 30, 2021
  88. Junio C HamanoJan 30, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.