mbox series

[0/7] teach `worktree list` verbose mode and prunable annotations

Message ID 20210104162128.95281-1-rafaeloliveira.cs@gmail.com (mailing list archive)
Headers show
Series teach `worktree list` verbose mode and prunable annotations | expand

Message

Rafael Silva Jan. 4, 2021, 4:21 p.m. UTC
In c57b3367be (worktree: teach `list` to annotate locked worktree,
2020-10-11) we taught `git worktree list` to annotate working tree that
is locked by appending "locked" text in order to signalize to the user
that a working tree is locked.  During the review, there was some
discussion about additional annotations and information that `list`
command could provide to the user that has long been envisioned and
mentioned in [2], [3] and [4].

This patch series address some of these changes by teaching
`worktree list` to show "prunable" annotation, adding verbose mode and
extending the --porcelain format with prunable and locked annotation as
follow up from [1]. Additionally, it address one shortcoming for porcelain
format to escape any newline characters (LF and CRLF) for the lock reason
to prevent breaking format mentioned in [4] and [1] during the review
cycle.

I have tried to organise in a way the series is easy to review, the
patches are organised in seven patches.

The first moves the should_prune_worktree() machinery to the top-level
worktree.c exposing the function as general API, that will be reference
by should_prune_worktree() wrapper implemented on the second patch. The
original idea was to not only move should_prune_worktree() but also
refactor to accept "struct worktree" and load the information directly,
which can simplify the `prune` command by reusing get_worktrees().
However this seems to also require refactoring get_worktrees() itself
to return "non-valid" working trees that can/should be pruned. This is
also mentioned in [5]. Having the wrapper function makes it easier to add
the prunable annotation without touching the get_worktrees() and the
other worktree sub commands.  The refactoring can be addressed in a
future patch, if this turns out to be good idea.  One possible approach
is to teach get_worktrees() to take additional flags that will tell
whether to return only valid or all worktrees in GIT_DIR/worktrees/
directory and address its own possible shortcoming, if any.

The third patch changes worktree_lock_reason() to be more gentle for the
main working tree to simply returning NULL instead of aborting the
program via assert() macro. This allow us to simplify the code that
checks if the working tree is locked for default and porcelain format.
This changes is also mentioned in [6].

The forth patch is the one that teaches `list` command to show prunable
annotation, for both default and porcelain format, and adds the verbose
option. These changes are also done for the "locked" annotation that was
not done on in [1]. Additionally, `list` learns to fail when both
--verbose and --porcelain format as used together.

The fifth patch adds worktree_escape_reason() that accepts a (char *)
text and returned the text with any LF or CRLF escaped. The caller is
responsible to freeing the escaped text. This is used by the locked
annotation in porcelain format. Currently, this is defined within
builtin/worktree.c as I was not sure whether libfying the function as
part of this series is a good idea. At this time it seems more sensible
to leave the code internally and libfying later once we are confident
about the implementation and whether it can be used in other part of the
code base but I'm open for suggestion.

The sixth and seventh patches adds tests and update the git-worktree.txt
documentation accordingly.

[1] https://lore.kernel.org/git/20200928154953.30396-1-rafaeloliveira.cs@gmail.com/
[2] https://lore.kernel.org/git/CAPig+cQF6V8HNdMX5AZbmz3_w2WhSfA4SFfNhQqxXBqPXTZL+w@mail.gmail.com/
[3] https://lore.kernel.org/git/CAPig+cSGXqJuaZPhUhOVX5X=LMrjVfv8ye_6ncMUbyKox1i7QA@mail.gmail.com/
[4] https://lore.kernel.org/git/CAPig+cTitWCs5vB=0iXuUyEY22c0gvjXvY1ZtTT90s74ydhE=A@mail.gmail.com/
[5] https://lore.kernel.org/git/CACsJy8ChM99n6skQCv-GmFiod19mnwwH4j-6R+cfZSiVFAxjgA@mail.gmail.com/
[6] https://lore.kernel.org/git/xmqq8sctlgzx.fsf@gitster.c.googlers.com/

Series is built on top of 71ca53e8125e36efbda17293c50027d31681a41f, currently
poiting to master and v2.30.0 tag.

Rafael Silva (7):
  worktree: move should_prune_worktree() to worktree.c
  worktree: implement worktree_prune_reason() wrapper
  worktree: teach worktree_lock_reason() to gently handle main worktree
  worktree: teach `list` prunable annotation and verbose
  worktree: `list` escape lock reason in --porcelain
  worktree: add tests for `list` verbose and annotations
  worktree: document `list` verbose and prunable annotations

 Documentation/git-worktree.txt |  59 ++++++++++++++-
 builtin/worktree.c             | 130 ++++++++++++++-------------------
 t/t2402-worktree-list.sh       |  94 +++++++++++++++++++++++-
 worktree.c                     |  96 +++++++++++++++++++++++-
 worktree.h                     |  17 +++++
 5 files changed, 313 insertions(+), 83 deletions(-)

Comments

Eric Sunshine Jan. 6, 2021, 5:36 a.m. UTC | #1
On Mon, Jan 4, 2021 at 11:22 AM Rafael Silva
<rafaeloliveira.cs@gmail.com> wrote:
> In c57b3367be (worktree: teach `list` to annotate locked worktree,
> 2020-10-11) we taught `git worktree list` to annotate working tree that
> is locked by appending "locked" text in order to signalize to the user
> that a working tree is locked.  During the review, there was some
> discussion about additional annotations and information that `list`
> command could provide to the user that has long been envisioned and
> mentioned in [2], [3] and [4].
>
> This patch series address some of these changes by teaching
> `worktree list` to show "prunable" annotation, adding verbose mode and
> extending the --porcelain format with prunable and locked annotation as
> follow up from [1]. Additionally, it address one shortcoming for porcelain
> format to escape any newline characters (LF and CRLF) for the lock reason
> to prevent breaking format mentioned in [4] and [1] during the review
> cycle.

Thank you for working on this. I'm happy to see these long-envisioned
enhancements finally taking shape. Before even reviewing the patches,
I decided to apply them and play with the new features, and I'm very
pleased to see that they behave exactly as I had envisioned all those
years ago.

Very nicely done.

I'll review the patches when I finish responding to this cover letter.

> The first moves the should_prune_worktree() machinery to the top-level
> worktree.c exposing the function as general API, that will be reference
> by should_prune_worktree() wrapper implemented on the second patch. The
> original idea was to not only move should_prune_worktree() but also
> refactor to accept "struct worktree" and load the information directly,
> which can simplify the `prune` command by reusing get_worktrees().
> However this seems to also require refactoring get_worktrees() itself
> to return "non-valid" working trees that can/should be pruned. This is
> also mentioned in [5]. Having the wrapper function makes it easier to add
> the prunable annotation without touching the get_worktrees() and the
> other worktree sub commands.  The refactoring can be addressed in a
> future patch, if this turns out to be good idea.  One possible approach
> is to teach get_worktrees() to take additional flags that will tell
> whether to return only valid or all worktrees in GIT_DIR/worktrees/
> directory and address its own possible shortcoming, if any.

I haven't looked at the patches yet, so I can't respond to this right now.

> The third patch changes worktree_lock_reason() to be more gentle for the
> main working tree to simply returning NULL instead of aborting the
> program via assert() macro. This allow us to simplify the code that
> checks if the working tree is locked for default and porcelain format.
> This changes is also mentioned in [6].

Sounds good.

> The forth patch is the one that teaches `list` command to show prunable
> annotation, for both default and porcelain format, and adds the verbose
> option. These changes are also done for the "locked" annotation that was
> not done on in [1]. Additionally, `list` learns to fail when both
> --verbose and --porcelain format as used together.

My knee-jerk response to disallowing --verbose and --porcelain
together was that it seemed unnecessarily harsh, but upon reflection,
I think it's the correct thing to do. After all, porcelain output,
even though extensible and flexible, should be predictable, so it
doesn't make sense for options such as --verbose to affect it.

> The fifth patch adds worktree_escape_reason() that accepts a (char *)
> text and returned the text with any LF or CRLF escaped. The caller is
> responsible to freeing the escaped text. This is used by the locked
> annotation in porcelain format. Currently, this is defined within
> builtin/worktree.c as I was not sure whether libfying the function as
> part of this series is a good idea. At this time it seems more sensible
> to leave the code internally and libfying later once we are confident
> about the implementation and whether it can be used in other part of the
> code base but I'm open for suggestion.

Perhaps I misunderstand, but I had envisioned employing one of the
codebase's existing quoting/escaping functions rather than crafting a
new one from scratch. However, I'll reserve judgment until I actually
read the patch.
Rafael Silva Jan. 8, 2021, 7:38 a.m. UTC | #2
Eric Sunshine writes:

> On Mon, Jan 4, 2021 at 11:22 AM Rafael Silva
> <rafaeloliveira.cs@gmail.com> wrote:
>> In c57b3367be (worktree: teach `list` to annotate locked worktree,
>> 2020-10-11) we taught `git worktree list` to annotate working tree that
>> is locked by appending "locked" text in order to signalize to the user
>> that a working tree is locked.  During the review, there was some
>> discussion about additional annotations and information that `list`
>> command could provide to the user that has long been envisioned and
>> mentioned in [2], [3] and [4].
>>
>> This patch series address some of these changes by teaching
>> `worktree list` to show "prunable" annotation, adding verbose mode and
>> extending the --porcelain format with prunable and locked annotation as
>> follow up from [1]. Additionally, it address one shortcoming for porcelain
>> format to escape any newline characters (LF and CRLF) for the lock reason
>> to prevent breaking format mentioned in [4] and [1] during the review
>> cycle.
>
> Thank you for working on this. I'm happy to see these long-envisioned
> enhancements finally taking shape. Before even reviewing the patches,
> I decided to apply them and play with the new features, and I'm very
> pleased to see that they behave exactly as I had envisioned all those
> years ago.
>
> Very nicely done.

Thank you. I'm glad to hear the patches are aligned with what you
envisioned.

> I'll review the patches when I finish responding to this cover letter.
>

Thank you for reviewing and applying the patches, really appreciate it.

>> The fifth patch adds worktree_escape_reason() that accepts a (char *)
>> text and returned the text with any LF or CRLF escaped. The caller is
>> responsible to freeing the escaped text. This is used by the locked
>> annotation in porcelain format. Currently, this is defined within
>> builtin/worktree.c as I was not sure whether libfying the function as
>> part of this series is a good idea. At this time it seems more sensible
>> to leave the code internally and libfying later once we are confident
>> about the implementation and whether it can be used in other part of the
>> code base but I'm open for suggestion.
>
> Perhaps I misunderstand, but I had envisioned employing one of the
> codebase's existing quoting/escaping functions rather than crafting a
> new one from scratch. However, I'll reserve judgment until I actually
> read the patch.

Agreed. It make sense to reuse one of the already implemented functions
from the code base. for some reason I was not able to find it. I believe
this was cleared out in one of the patches replies by you and Phillip Wood.

I will remove this and reuse one of the existing function on the next
revision.
Eric Sunshine Jan. 8, 2021, 8:19 a.m. UTC | #3
On Fri, Jan 8, 2021 at 2:38 AM Rafael Silva <rafaeloliveira.cs@gmail.com> wrote:
> Eric Sunshine writes:
> > On Mon, Jan 4, 2021 at 11:22 AM Rafael Silva
> > <rafaeloliveira.cs@gmail.com> wrote:
> >> The fifth patch adds worktree_escape_reason() that accepts a (char *)
> >> text and returned the text with any LF or CRLF escaped. [...]
> >
> > Perhaps I misunderstand, but I had envisioned employing one of the
> > codebase's existing quoting/escaping functions rather than crafting a
> > new one from scratch. However, I'll reserve judgment until I actually
> > read the patch.
>
> Agreed. It make sense to reuse one of the already implemented functions
> from the code base. for some reason I was not able to find it. I believe
> this was cleared out in one of the patches replies by you and Phillip Wood.

No need to apologize. It's a big project and it can be difficult to
discover existing utility functions. Fortunately, reviewers can often
point out useful alternatives.