git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees
@ 2020-09-28 15:49 Rafael Silva
  2020-09-28 15:49 ` [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree Rafael Silva
                   ` (4 more replies)
  0 siblings, 5 replies; 23+ messages in thread
From: Rafael Silva @ 2020-09-28 15:49 UTC (permalink / raw)
  To: git; +Cc: Rafael Silva

This patch series introduces a new information on the git `worktree list`
command output, to mark when a worktree is locked with a (locked) text mark.

The intent is to improve the user experience to earlier sinalize that a linked
worktree is locked, instead of realising later when attempting to remove it
with `remove` command as it happened to me twice :)

The patches are divided into two parts. First part introduces
the new marker to the worktree list command and small documentation
change. And the second adds one test case into t2402 to test
if the (locked) text will be properly set for a locked worktree, and
not mistankely set to a unlocked or master worktree.

This is the output of the worktree list with locked marker:

  $ git worktree list
  /repo/to/main                abc123 [master]
  /path/to/unlocked-worktree1  456def [brancha]
  /path/to/locked-worktree     123abc (detached HEAD) (locked)

This patches are marked with RFC mainly due to:

  - This will change the default behaviour of the worktree list, I am
    not sure whether will be better to make this tuned via a config
    and/or a git parameter. (assuming this change is a good idea ;) )

  - Perhaps the `(locked)` marker is not the best suitable way to output
    this information and we might need to come with a better way.

  - I am a new contributor to the code base, still learning a lot of git
    internals data structure and commands. Likely this patch will require
    updates.

Rafael Silva (2):
  teach `list` to mark locked worktree
  t2402: add test to locked linked worktree marker

 Documentation/git-worktree.txt |  5 +++--
 builtin/worktree.c             |  6 +++++-
 t/t2402-worktree-list.sh       | 13 +++++++++++++
 3 files changed, 21 insertions(+), 3 deletions(-)

-- 
2.28.0.763.ge7086f1eef


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree
  2020-09-28 15:49 [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Rafael Silva
@ 2020-09-28 15:49 ` Rafael Silva
  2020-09-28 21:37   ` Junio C Hamano
  2020-09-28 15:49 ` [RFC PATCH 2/2] t2402: add test to locked linked worktree marker Rafael Silva
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 23+ messages in thread
From: Rafael Silva @ 2020-09-28 15:49 UTC (permalink / raw)
  To: git; +Cc: Rafael Silva

The output of `worktree list` command is extended to mark a locked
worktree with `(locked)` text. This is used to communicate to the
user that a linked worktree is locked instead of learning only when
attempting to remove it.

This is the output of the worktree list with locked marker:

  $ git worktree list
  /repo/to/main                abc123 [master]
  /path/to/unlocked-worktree1  456def [brancha]
  /path/to/locked-worktree     123abc (detached HEAD) (locked)

Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
---
 Documentation/git-worktree.txt | 5 +++--
 builtin/worktree.c             | 6 +++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 32e8440cde..a3781dd664 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -96,8 +96,9 @@ list::
 
 List details of each working tree.  The main working tree is listed first,
 followed by each of the linked working trees.  The output details include
-whether the working tree is bare, the revision currently checked out, and the
-branch currently checked out (or "detached HEAD" if none).
+whether the working tree is bare, the revision currently checked out, the
+branch currently checked out (or "detached HEAD" if none), and whether
+the worktree is locked.
 
 lock::
 
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 99abaeec6c..8ad2cdd2f9 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -676,8 +676,12 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
 		} else
 			strbuf_addstr(&sb, "(error)");
 	}
-	printf("%s\n", sb.buf);
 
+	if (!is_main_worktree(wt) &&
+	    worktree_lock_reason(wt))
+		strbuf_addstr(&sb, " (locked)");
+
+	printf("%s\n", sb.buf);
 	strbuf_release(&sb);
 }
 
-- 
2.28.0.763.ge7086f1eef


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [RFC PATCH 2/2] t2402: add test to locked linked worktree marker
  2020-09-28 15:49 [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Rafael Silva
  2020-09-28 15:49 ` [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree Rafael Silva
@ 2020-09-28 15:49 ` Rafael Silva
  2020-09-28 21:54   ` Junio C Hamano
  2020-09-30  8:06   ` Eric Sunshine
  2020-09-28 21:19 ` [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Junio C Hamano
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 23+ messages in thread
From: Rafael Silva @ 2020-09-28 15:49 UTC (permalink / raw)
  To: git; +Cc: Rafael Silva

Test the output of the `worktree list` command to show when
a linked worktree is locked and test to not mistakenly
mark main or unlocked worktrees.

Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
---
 t/t2402-worktree-list.sh | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
index 52585ec2aa..07bd9a3350 100755
--- a/t/t2402-worktree-list.sh
+++ b/t/t2402-worktree-list.sh
@@ -61,6 +61,19 @@ test_expect_success '"list" all worktrees --porcelain' '
 	test_cmp expect actual
 '
 
+test_expect_success 'show locked worktree with (locked)' '
+	echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
+	test_when_finished "rm -rf locked unlocked out actual expect && git worktree prune" &&
+	git worktree add --detach locked master &&
+	git worktree add --detach unlocked master &&
+	git worktree lock locked &&
+	echo "$(git -C locked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD) (locked)" >>expect &&
+	echo "$(git -C unlocked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
+	git worktree list >out &&
+	sed "s/  */ /g" <out >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'bare repo setup' '
 	git init --bare bare1 &&
 	echo "data" >file1 &&
-- 
2.28.0.763.ge7086f1eef


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees
  2020-09-28 15:49 [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Rafael Silva
  2020-09-28 15:49 ` [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree Rafael Silva
  2020-09-28 15:49 ` [RFC PATCH 2/2] t2402: add test to locked linked worktree marker Rafael Silva
@ 2020-09-28 21:19 ` Junio C Hamano
  2020-09-29 21:35   ` Rafael Silva
  2020-09-30  7:19 ` Eric Sunshine
  2020-10-10 18:55 ` [PATCH v2 0/1] Teach "worktree list" to annotate " Rafael Silva
  4 siblings, 1 reply; 23+ messages in thread
From: Junio C Hamano @ 2020-09-28 21:19 UTC (permalink / raw)
  To: Rafael Silva; +Cc: git, Eric Sunshine

Rafael Silva <rafaeloliveira.cs@gmail.com> writes:

> This patch series introduces a new information on the git `worktree list`
> command output, to mark when a worktree is locked with a (locked) text mark.
>
> The intent is to improve the user experience to earlier sinalize that a linked
> worktree is locked, instead of realising later when attempting to remove it
> with `remove` command as it happened to me twice :)

Change with a good intention, it seems.

> The patches are divided into two parts. First part introduces
> the new marker to the worktree list command and small documentation
> change. And the second adds one test case into t2402 to test
> if the (locked) text will be properly set for a locked worktree, and
> not mistankely set to a unlocked or master worktree.

Probably they belong together in a single patch (I am saying this
after only seeing the above five lines, without reading either of
these two patches, so there may be some things in them that makes it
make sense to have them separate).

> This is the output of the worktree list with locked marker:
>
>   $ git worktree list
>   /repo/to/main                abc123 [master]
>   /path/to/unlocked-worktree1  456def [brancha]
>   /path/to/locked-worktree     123abc (detached HEAD) (locked)

Looks OK to me

> This patches are marked with RFC mainly due to:
>
>   - This will change the default behaviour of the worktree list, I am
>     not sure whether will be better to make this tuned via a config
>     and/or a git parameter. (assuming this change is a good idea ;) )

The default output is meant for human consumption (scripts that want
to read from the command and expect stable output would be using the
"--porcelain" option).

The ideal case is a new output is universally useful for everybody,
in which case we can just change it without any new configuration or
command line option.

>   - Perhaps the `(locked)` marker is not the best suitable way to output
>     this information and we might need to come with a better way.

It looks good enough to me.  I am not qualified to have a strong
opinion on this part, as I do not use the command all that often.

    $ git shortlog --no-merges --since=18.months builtin/worktree.c

tells me that Eric Sunshine (CC'ed) may be a good source of wisdom
on this command.

>   - I am a new contributor to the code base, still learning a lot of git
>     internals data structure and commands. Likely this patch will require
>     updates.

Welcome.

> Rafael Silva (2):
>   teach `list` to mark locked worktree
>   t2402: add test to locked linked worktree marker
>
>  Documentation/git-worktree.txt |  5 +++--
>  builtin/worktree.c             |  6 +++++-
>  t/t2402-worktree-list.sh       | 13 +++++++++++++
>  3 files changed, 21 insertions(+), 3 deletions(-)

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree
  2020-09-28 15:49 ` [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree Rafael Silva
@ 2020-09-28 21:37   ` Junio C Hamano
  2020-09-29 21:36     ` Rafael Silva
  2020-09-30  7:35     ` Eric Sunshine
  0 siblings, 2 replies; 23+ messages in thread
From: Junio C Hamano @ 2020-09-28 21:37 UTC (permalink / raw)
  To: Rafael Silva; +Cc: git

Rafael Silva <rafaeloliveira.cs@gmail.com> writes:

> The output of `worktree list` command is extended to mark a locked
> worktree with `(locked)` text. This is used to communicate to the
> user that a linked worktree is locked instead of learning only when
> attempting to remove it.
>
> This is the output of the worktree list with locked marker:
>
>   $ git worktree list
>   /repo/to/main                abc123 [master]
>   /path/to/unlocked-worktree1  456def [brancha]
>   /path/to/locked-worktree     123abc (detached HEAD) (locked)


In our log message, we tend NOT to say "This commit does X" or "X is
done", because such a statement is often insufficient to illustrate
if the commit indeed does X, and explain why it is a good thing to
do X in the first place.

Instead, we 

 - first explain that the current system does not do X (in present
   tense, so we do NOT say "previously we did not do X"), then

 - explain why doing X would be a good thing, and finally

 - give an order to the codebase to start doing X.


For this change, it might look like this:

    The "git worktree list" shows the absolute path to the working
    tree, the commit that is checked out and the name of the branch.
    It is not immediately obvious which of the worktrees, if any,
    are locked.

    "git worktree remove" refuses to remove a locked worktree with
    an error message.  If "git worktree list" told which worktrees
    are locked in its output, the user would not even attempt to
    remove such a worktree.

    Teach "git worktree list" to append "(locked)" to its output.
    The output from the command becomes like so:

          $ git worktree list
          /repo/to/main                abc123 [master]
          /path/to/unlocked-worktree1  456def [brancha]
          /path/to/locked-worktree     123abc (detached HEAD) (locked)



> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> index 32e8440cde..a3781dd664 100644
> --- a/Documentation/git-worktree.txt
> +++ b/Documentation/git-worktree.txt
> @@ -96,8 +96,9 @@ list::
>  
>  List details of each working tree.  The main working tree is listed first,
>  followed by each of the linked working trees.  The output details include
> -whether the working tree is bare, the revision currently checked out, and the
> -branch currently checked out (or "detached HEAD" if none).
> +whether the working tree is bare, the revision currently checked out, the
> +branch currently checked out (or "detached HEAD" if none), and whether
> +the worktree is locked.

At the first glance, the above gave me an impression that you'd be
adding "(unlocked)" or "(locked)" for each working tree, but that is
not the case.  How about keeping the original sentence intact, and
adding something like "For a locked worktree, the marker (locked) is
also shown at the end"?

> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index 99abaeec6c..8ad2cdd2f9 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -676,8 +676,12 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
>  		} else
>  			strbuf_addstr(&sb, "(error)");
>  	}
> -	printf("%s\n", sb.buf);
>  
> +	if (!is_main_worktree(wt) &&
> +	    worktree_lock_reason(wt))
> +		strbuf_addstr(&sb, " (locked)");

Is this because for the primary worktree, worktree_lock_reason()
will always yield true?

    ... goes and looks ...

Ah, OK, the callers are not even allowed to ask the question on the
primary one.  That's a bit strange API but OK.

Writing that on a single line would perfectly be readable, by the
way.

	if (!is_main_worktree(wt) && worktree_lock_reason(wt))
		strbuf_addstr(&sb, " (locked)");

> +	printf("%s\n", sb.buf);
>  	strbuf_release(&sb);
>  }

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 2/2] t2402: add test to locked linked worktree marker
  2020-09-28 15:49 ` [RFC PATCH 2/2] t2402: add test to locked linked worktree marker Rafael Silva
@ 2020-09-28 21:54   ` Junio C Hamano
  2020-09-29 21:37     ` Rafael Silva
  2020-09-30  8:06   ` Eric Sunshine
  1 sibling, 1 reply; 23+ messages in thread
From: Junio C Hamano @ 2020-09-28 21:54 UTC (permalink / raw)
  To: Rafael Silva; +Cc: git

Rafael Silva <rafaeloliveira.cs@gmail.com> writes:

> Test the output of the `worktree list` command to show when
> a linked worktree is locked and test to not mistakenly
> mark main or unlocked worktrees.
>
> Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
> ---
>  t/t2402-worktree-list.sh | 13 +++++++++++++
>  1 file changed, 13 insertions(+)

I think this should be part of [1/2], as the change necessary to
implement the new feature is small enough that there is no reason
to split the test part out.

> diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
> index 52585ec2aa..07bd9a3350 100755
> --- a/t/t2402-worktree-list.sh
> +++ b/t/t2402-worktree-list.sh
> @@ -61,6 +61,19 @@ test_expect_success '"list" all worktrees --porcelain' '
>  	test_cmp expect actual
>  '
>  
> +test_expect_success 'show locked worktree with (locked)' '
> +	echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
> +	test_when_finished "rm -rf locked unlocked out actual expect && git worktree prune" &&
> +	git worktree add --detach locked master &&
> +	git worktree add --detach unlocked master &&
> +	git worktree lock locked &&
> +	echo "$(git -C locked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD) (locked)" >>expect &&
> +	echo "$(git -C unlocked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
> +	git worktree list >out &&
> +	sed "s/  */ /g" <out >actual &&
> +	test_cmp expect actual
> +'

This seems to prescribe the output from the command too strictly
(you do avoid being overly too strict by removing the indentation
with 's/ */ /g' though).  

If the leading path to the $TRASH_DIRECTORY has two or more
consecutive SPs (and that is not something under our control), the
'expect' file would keep such a double-SP, but such a double-SP in
'out' would have been squashed out in the 'actual' file.

I wonder if

	grep '/locked  *[0-9a-f].* (locked)' out &&
	! grep '/unlocked  *[0-9a-f].* (locked)' out

might be a better way to test?  That is

 - we do not care what the leading directories are called
 - we do not care what branch is checked out or how they are presented
 - we care the one that ends with /locked is (locked)
 - we care the one that ends with /unlocked is not (locked)

After all, this new test piece is not about verifying that the
object name or branch name is correct.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees
  2020-09-28 21:19 ` [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Junio C Hamano
@ 2020-09-29 21:35   ` Rafael Silva
  0 siblings, 0 replies; 23+ messages in thread
From: Rafael Silva @ 2020-09-29 21:35 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Eric Sunshine, git

Thank you for all the feedback, I will work on a another
patch that will address all the feedback provided on this series,
including having the changes into a single patch.

On Mon, Sep 28, 2020 at 02:19:53PM -0700, Junio C Hamano wrote:
> Rafael Silva <rafaeloliveira.cs@gmail.com> writes:
> 
> > This patch series introduces a new information on the git `worktree list`
> > command output, to mark when a worktree is locked with a (locked) text mark.
> >
> > The intent is to improve the user experience to earlier sinalize that a linked
> > worktree is locked, instead of realising later when attempting to remove it
> > with `remove` command as it happened to me twice :)
> 
> Change with a good intention, it seems.
> 
> > The patches are divided into two parts. First part introduces
> > the new marker to the worktree list command and small documentation
> > change. And the second adds one test case into t2402 to test
> > if the (locked) text will be properly set for a locked worktree, and
> > not mistankely set to a unlocked or master worktree.
> 
> Probably they belong together in a single patch (I am saying this
> after only seeing the above five lines, without reading either of
> these two patches, so there may be some things in them that makes it
> make sense to have them separate).
> 

Yes, I believe it make sense to have them in a single patch.

> > This is the output of the worktree list with locked marker:
> >
> >   $ git worktree list
> >   /repo/to/main                abc123 [master]
> >   /path/to/unlocked-worktree1  456def [brancha]
> >   /path/to/locked-worktree     123abc (detached HEAD) (locked)
> 
> Looks OK to me
> 
> > This patches are marked with RFC mainly due to:
> >
> >   - This will change the default behaviour of the worktree list, I am
> >     not sure whether will be better to make this tuned via a config
> >     and/or a git parameter. (assuming this change is a good idea ;) )
> 
> The default output is meant for human consumption (scripts that want
> to read from the command and expect stable output would be using the
> "--porcelain" option).
> 
> The ideal case is a new output is universally useful for everybody,
> in which case we can just change it without any new configuration or
> command line option.

Thanks for the explanation, will keep that in mind.

> 
> >   - Perhaps the `(locked)` marker is not the best suitable way to output
> >     this information and we might need to come with a better way.
> 
> It looks good enough to me.  I am not qualified to have a strong
> opinion on this part, as I do not use the command all that often.
> 
>     $ git shortlog --no-merges --since=18.months builtin/worktree.c
> 
> tells me that Eric Sunshine (CC'ed) may be a good source of wisdom
> on this command.
> 

Thank you, I will keep Eric Sunshine in the loop as well.

> >   - I am a new contributor to the code base, still learning a lot of git
> >     internals data structure and commands. Likely this patch will require
> >     updates.
> 
> Welcome.

Thank you.

> 
> > Rafael Silva (2):
> >   teach `list` to mark locked worktree
> >   t2402: add test to locked linked worktree marker
> >
> >  Documentation/git-worktree.txt |  5 +++--
> >  builtin/worktree.c             |  6 +++++-
> >  t/t2402-worktree-list.sh       | 13 +++++++++++++
> >  3 files changed, 21 insertions(+), 3 deletions(-)

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree
  2020-09-28 21:37   ` Junio C Hamano
@ 2020-09-29 21:36     ` Rafael Silva
  2020-09-30  7:35     ` Eric Sunshine
  1 sibling, 0 replies; 23+ messages in thread
From: Rafael Silva @ 2020-09-29 21:36 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Eric Sunshine

On Mon, Sep 28, 2020 at 02:37:54PM -0700, Junio C Hamano wrote:
> Rafael Silva <rafaeloliveira.cs@gmail.com> writes:
> 
> > The output of `worktree list` command is extended to mark a locked
> > worktree with `(locked)` text. This is used to communicate to the
> > user that a linked worktree is locked instead of learning only when
> > attempting to remove it.
> >
> > This is the output of the worktree list with locked marker:
> >
> >   $ git worktree list
> >   /repo/to/main                abc123 [master]
> >   /path/to/unlocked-worktree1  456def [brancha]
> >   /path/to/locked-worktree     123abc (detached HEAD) (locked)
> 
> 
> In our log message, we tend NOT to say "This commit does X" or "X is
> done", because such a statement is often insufficient to illustrate
> if the commit indeed does X, and explain why it is a good thing to
> do X in the first place.
> 
> Instead, we 
> 
>  - first explain that the current system does not do X (in present
>    tense, so we do NOT say "previously we did not do X"), then
> 
>  - explain why doing X would be a good thing, and finally
> 
>  - give an order to the codebase to start doing X.
> 
> 
> For this change, it might look like this:
> 
>     The "git worktree list" shows the absolute path to the working
>     tree, the commit that is checked out and the name of the branch.
>     It is not immediately obvious which of the worktrees, if any,
>     are locked.
> 
>     "git worktree remove" refuses to remove a locked worktree with
>     an error message.  If "git worktree list" told which worktrees
>     are locked in its output, the user would not even attempt to
>     remove such a worktree.
> 
>     Teach "git worktree list" to append "(locked)" to its output.
>     The output from the command becomes like so:
> 
>           $ git worktree list
>           /repo/to/main                abc123 [master]
>           /path/to/unlocked-worktree1  456def [brancha]
>           /path/to/locked-worktree     123abc (detached HEAD) (locked)
>

Thank you for such detailed explanation. I totally agree that it seems
much better to organise the commit message this way, I will definitely
include this message (or something very similar) when resending these patches.

> 
> > diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> > index 32e8440cde..a3781dd664 100644
> > --- a/Documentation/git-worktree.txt
> > +++ b/Documentation/git-worktree.txt
> > @@ -96,8 +96,9 @@ list::
> >  
> >  List details of each working tree.  The main working tree is listed first,
> >  followed by each of the linked working trees.  The output details include
> > -whether the working tree is bare, the revision currently checked out, and the
> > -branch currently checked out (or "detached HEAD" if none).
> > +whether the working tree is bare, the revision currently checked out, the
> > +branch currently checked out (or "detached HEAD" if none), and whether
> > +the worktree is locked.
> 
> At the first glance, the above gave me an impression that you'd be
> adding "(unlocked)" or "(locked)" for each working tree, but that is
> not the case.  How about keeping the original sentence intact, and
> adding something like "For a locked worktree, the marker (locked) is
> also shown at the end"?

Yes, it sounds good.

> 
> > diff --git a/builtin/worktree.c b/builtin/worktree.c
> > index 99abaeec6c..8ad2cdd2f9 100644
> > --- a/builtin/worktree.c
> > +++ b/builtin/worktree.c
> > @@ -676,8 +676,12 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
> >  		} else
> >  			strbuf_addstr(&sb, "(error)");
> >  	}
> > -	printf("%s\n", sb.buf);
> >  
> > +	if (!is_main_worktree(wt) &&
> > +	    worktree_lock_reason(wt))
> > +		strbuf_addstr(&sb, " (locked)");
> 
> Is this because for the primary worktree, worktree_lock_reason()
> will always yield true?
> 
>     ... goes and looks ...
> 
> Ah, OK, the callers are not even allowed to ask the question on the
> primary one.  That's a bit strange API but OK.
> 
> Writing that on a single line would perfectly be readable, by the
> way.
> 
> 	if (!is_main_worktree(wt) && worktree_lock_reason(wt))
> 		strbuf_addstr(&sb, " (locked)");

Agreed. will be much better to have it in a single line.

> 
> > +	printf("%s\n", sb.buf);
> >  	strbuf_release(&sb);
> >  }

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 2/2] t2402: add test to locked linked worktree marker
  2020-09-28 21:54   ` Junio C Hamano
@ 2020-09-29 21:37     ` Rafael Silva
  0 siblings, 0 replies; 23+ messages in thread
From: Rafael Silva @ 2020-09-29 21:37 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Sep 28, 2020 at 02:54:24PM -0700, Junio C Hamano wrote:
> Rafael Silva <rafaeloliveira.cs@gmail.com> writes:
> 
> > Test the output of the `worktree list` command to show when
> > a linked worktree is locked and test to not mistakenly
> > mark main or unlocked worktrees.
> >
> > Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
> > ---
> >  t/t2402-worktree-list.sh | 13 +++++++++++++
> >  1 file changed, 13 insertions(+)
> 
> I think this should be part of [1/2], as the change necessary to
> implement the new feature is small enough that there is no reason
> to split the test part out.

Sounds good

> 
> > diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
> > index 52585ec2aa..07bd9a3350 100755
> > --- a/t/t2402-worktree-list.sh
> > +++ b/t/t2402-worktree-list.sh
> > @@ -61,6 +61,19 @@ test_expect_success '"list" all worktrees --porcelain' '
> >  	test_cmp expect actual
> >  '
> >  
> > +test_expect_success 'show locked worktree with (locked)' '
> > +	echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
> > +	test_when_finished "rm -rf locked unlocked out actual expect && git worktree prune" &&
> > +	git worktree add --detach locked master &&
> > +	git worktree add --detach unlocked master &&
> > +	git worktree lock locked &&
> > +	echo "$(git -C locked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD) (locked)" >>expect &&
> > +	echo "$(git -C unlocked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
> > +	git worktree list >out &&
> > +	sed "s/  */ /g" <out >actual &&
> > +	test_cmp expect actual
> > +'
> 
> This seems to prescribe the output from the command too strictly
> (you do avoid being overly too strict by removing the indentation
> with 's/ */ /g' though).  
> 
> If the leading path to the $TRASH_DIRECTORY has two or more
> consecutive SPs (and that is not something under our control), the
> 'expect' file would keep such a double-SP, but such a double-SP in
> 'out' would have been squashed out in the 'actual' file.
> 
> I wonder if
> 
> 	grep '/locked  *[0-9a-f].* (locked)' out &&
> 	! grep '/unlocked  *[0-9a-f].* (locked)' out
> 
> might be a better way to test?  That is
> 
>  - we do not care what the leading directories are called
>  - we do not care what branch is checked out or how they are presented
>  - we care the one that ends with /locked is (locked)
>  - we care the one that ends with /unlocked is not (locked)
> 
> After all, this new test piece is not about verifying that the
> object name or branch name is correct.

Sounds good and I agree with the all, and seems even easier
to understand the test code this way. Will address this change on the next patch,
although I'm not sure whether somebody could run into any issues when
running the `grep` command in other platforms. I'll look into it.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees
  2020-09-28 15:49 [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Rafael Silva
                   ` (2 preceding siblings ...)
  2020-09-28 21:19 ` [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Junio C Hamano
@ 2020-09-30  7:19 ` Eric Sunshine
  2020-10-02 16:28   ` Rafael Silva
  2020-10-10 18:55 ` [PATCH v2 0/1] Teach "worktree list" to annotate " Rafael Silva
  4 siblings, 1 reply; 23+ messages in thread
From: Eric Sunshine @ 2020-09-30  7:19 UTC (permalink / raw)
  To: Rafael Silva; +Cc: Git List

On Mon, Sep 28, 2020 at 11:50 AM Rafael Silva
<rafaeloliveira.cs@gmail.com> wrote:
> This patch series introduces a new information on the git `worktree list`
> command output, to mark when a worktree is locked with a (locked) text mark.

Thanks for working on this. "locked" is one of several additional
annotations to the output of "git worktree list" which have long been
envisioned. For reference, here are some earlier messages related to
this topic:

[1]: https://lore.kernel.org/git/CAPig+cTTrv2C7JLu1dr4+N8xo+7YQ+deiwLDA835wBGD6fhS1g@mail.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/

I'll leave a few review comments to supplement those already by Junio.

> This is the output of the worktree list with locked marker:
>
>  $ git worktree list
>  /repo/to/main        abc123 [master]
>  /path/to/unlocked-worktree1 456def [brancha]
>  /path/to/locked-worktree   123abc (detached HEAD) (locked)

In [2], I gave an example of output similar to this but without
encapsulating "locked" within parentheses:

    % git worktree list
    giggle     89ea799ffc [master]
    ../bobble  f172cb543d [feature1] locked
    ../fumple  6453c84b7d (detached HEAD) prunable

I omitted the parentheses partly due to the extra noise they
introduce, but mostly because I foresaw that a single entry might
eventually have multiple annotations, for instance:

    % git worktree list
    giggle     89ea799ffc [master]
    ../bobble  f172cb543d [feature1] locked prunable

in which case, the eyes glide over "locked prunable" a bit more easily
than over "(locked) (prunable)" or "(locked, prunable)" or some such.
Generally speaking, "the less noise, the better".

> This patches are marked with RFC mainly due to:
>
>  - Perhaps the `(locked)` marker is not the best suitable way to output
>   this information and we might need to come with a better way.

Taking [2] into consideration, I think it's fine to annotate the line
with "locked" (sans the parentheses), and it's compatible with the the
verbose mode, also proposed by [2]:

    % git worktree list -v
    giggle     89ea799ffc [master]
    ../bobble  f172cb543d [feature1]
        locked: worktree on removable media
    ../fumple  6453c84b7d (detached HEAD)
        prunable: directory does not exist

in which case the short "locked" annotation gets moved to the next
line, indented, and expanded to include the reason, if available
(otherwise would probably not be moved to the next line).

I'm not suggesting that this patch series implement verbose mode, but
bring it to attention to make sure we don't paint ourselves into a
corner when deciding how the "locked" annotation should be presented.

>  - I am a new contributor to the code base, still learning a lot of git
>   internals data structure and commands. Likely this patch will require
>   updates.

Under normal circumstances, I would be hesitant to accept a
contribution which makes an addition to the human-consumable "git
worktree list" output without also making the corresponding addition
to the --porcelain format. Thus, for instance, I would expect the
porcelain format to be updated, as well, to produce output such as
this (taken from [4]):

    worktree /blah
    branch refs/heads/blah
    locked Sneaker-net removable storage\nNot always mounted

That's a bit complicated because the lock reason may need escaping if
it contains special characters (such as the newline in the example).
Thus, I'm a bit hesitant to expect such a change from a newcomer.

More problematic, though, with regard to the porcelain format is that
the documentation is so woefully under-specified, as explained in [4],
that some people may interpret it as meaning that no additional
information can be added. I'm not particularly sympathetic to that
view since the intention from the start was that the porcelain format
should be extensible[4], thus adding new attributes should be allowed.
But I'm hesitant to ask a newcomer to undertake the task of addressing
these shortcomings. As such, I think it may be okay merely to change
the human-consumable output as this series does, and leave porcelain
output for a later date if someone wants to tackle it.

A reason that it would be nice to address the shortcomings of
porcelain format is because there are several additional pieces of
information it could be providing. Summarizing from [1], in addition
to the worktree path, its head, checked out branch, whether its bare
or detached, for each worktree, porcelain could also show:

    * whether it is locked
      - the lock reason (if available)
      - and whether the worktree is currently accessible (mounted)
    * whether it can be pruned
      - and the prune reason if so
    * worktree ID (the <id> of .git/worktrees/<id>/)

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree
  2020-09-28 21:37   ` Junio C Hamano
  2020-09-29 21:36     ` Rafael Silva
@ 2020-09-30  7:35     ` Eric Sunshine
  1 sibling, 0 replies; 23+ messages in thread
From: Eric Sunshine @ 2020-09-30  7:35 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Rafael Silva, Git List

On Mon, Sep 28, 2020 at 5:38 PM Junio C Hamano <gitster@pobox.com> wrote:
> Rafael Silva <rafaeloliveira.cs@gmail.com> writes:
> > The output of `worktree list` command is extended to mark a locked
> > worktree with `(locked)` text. This is used to communicate to the
> > user that a linked worktree is locked instead of learning only when
> > attempting to remove it.
>
> For this change, it might look like this:
>
>     The "git worktree list" shows the absolute path to the working
>     tree, the commit that is checked out and the name of the branch.
>     It is not immediately obvious which of the worktrees, if any,
>     are locked.
>
>     "git worktree remove" refuses to remove a locked worktree with
>     an error message.  If "git worktree list" told which worktrees
>     are locked in its output, the user would not even attempt to
>     remove such a worktree.

Nicely written. I might end the final sentence like this:

    ... the user would not even attempt to remove such a worktree or
    would know to use `git worktree remove -f -f <path>`.

> > diff --git a/builtin/worktree.c b/builtin/worktree.c
> > @@ -676,8 +676,12 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
> > +     if (!is_main_worktree(wt) &&
> > +         worktree_lock_reason(wt))
> > +             strbuf_addstr(&sb, " (locked)");
>
> Is this because for the primary worktree, worktree_lock_reason()
> will always yield true?
>
>     ... goes and looks ...
>
> Ah, OK, the callers are not even allowed to ask the question on the
> primary one.  That's a bit strange API but OK.

That is indeed a slightly hostile API, and it wouldn't hurt to change
it simply to return 'false' for the main worktree, but that's not
something this patch series need tackle.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 2/2] t2402: add test to locked linked worktree marker
  2020-09-28 15:49 ` [RFC PATCH 2/2] t2402: add test to locked linked worktree marker Rafael Silva
  2020-09-28 21:54   ` Junio C Hamano
@ 2020-09-30  8:06   ` Eric Sunshine
  1 sibling, 0 replies; 23+ messages in thread
From: Eric Sunshine @ 2020-09-30  8:06 UTC (permalink / raw)
  To: Rafael Silva; +Cc: Git List

On Mon, Sep 28, 2020 at 11:50 AM Rafael Silva
<rafaeloliveira.cs@gmail.com> wrote:
> Test the output of the `worktree list` command to show when
> a linked worktree is locked and test to not mistakenly
> mark main or unlocked worktrees.

In addition to Junio's review comments...

> Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
> ---
> diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
> @@ -61,6 +61,19 @@ test_expect_success '"list" all worktrees --porcelain' '
> +test_expect_success 'show locked worktree with (locked)' '

Existing test titles in this script seem to follow a particular
pattern (for the most part). Perhaps this could say instead:

    test_expect_success '"list" all worktrees with "locked"' '

> +       echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&

I see that this is following existing practice in this script of
piling up $(git ...) invocations which can lose the individual exit
codes, so I can't complain about it, but I'll note that these days
we'd more likely than not try to preserve the exit codes, perhaps like
this:

    path=$(git rev-parse --show-toplevel) &&
    rev=$(git rev-parse --short HEAD) &&
    sym=$(git symbolic-ref --short HEAD) &&
    echo "$path $rev [$sym]" >expect &&

However, that gets verbose since you're doing it for multiple lines (below).

> +       test_when_finished "rm -rf locked unlocked out actual expect && git worktree prune" &&
> +       git worktree add --detach locked master &&
> +       git worktree add --detach unlocked master &&
> +       git worktree lock locked &&
> +       echo "$(git -C locked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD) (locked)" >>expect &&
> +       echo "$(git -C unlocked rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
> +       git worktree list >out &&
> +       sed "s/  */ /g" <out >actual &&
> +       test_cmp expect actual
> +'

I realize that Junio proposed an alternate simpler approach which
checks specifically the attribute in which you are interested, but
here's yet another (typed-in-email) approach you could use:

    test_when_finished "..." &&
    git worktree add --detach locked master &&
    git worktree add --detach unlocked master &&
    git worktree list >out &&
    sed '/\/locked/s/$/ (locked)/' out >expect &&
    git worktree lock locked &&
    git worktree list >actual &&
    test_cmp expect actual

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees
  2020-09-30  7:19 ` Eric Sunshine
@ 2020-10-02 16:28   ` Rafael Silva
  2020-10-09 22:50     ` Eric Sunshine
  0 siblings, 1 reply; 23+ messages in thread
From: Rafael Silva @ 2020-10-02 16:28 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Junio C Hamano

On Wed, Sep 30, 2020 at 03:19:27AM -0400, Eric Sunshine wrote:

> On Mon, Sep 28, 2020 at 11:50 AM Rafael Silva
> <rafaeloliveira.cs@gmail.com> wrote:
> > This patch series introduces a new information on the git `worktree list`
> > command output, to mark when a worktree is locked with a (locked) text mark.
> 
> Thanks for working on this. "locked" is one of several additional
> annotations to the output of "git worktree list" which have long been
> envisioned. For reference, here are some earlier messages related to
> this topic:
> 
> [1]: https://lore.kernel.org/git/CAPig+cTTrv2C7JLu1dr4+N8xo+7YQ+deiwLDA835wBGD6fhS1g@mail.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/

Thank you for all this reference, it's really helpful. It is nice to see
that we already have few discussion on this topic that we can use and
work on top of that.

Sorry for the bit late response.

> 
> I'll leave a few review comments to supplement those already by Junio.
> 
> > This is the output of the worktree list with locked marker:
> >
> >  $ git worktree list
> >  /repo/to/main        abc123 [master]
> >  /path/to/unlocked-worktree1 456def [brancha]
> >  /path/to/locked-worktree   123abc (detached HEAD) (locked)
> 
> In [2], I gave an example of output similar to this but without
> encapsulating "locked" within parentheses:
> 
>     % git worktree list
>     giggle     89ea799ffc [master]
>     ../bobble  f172cb543d [feature1] locked
>     ../fumple  6453c84b7d (detached HEAD) prunable
> 
> I omitted the parentheses partly due to the extra noise they
> introduce, but mostly because I foresaw that a single entry might
> eventually have multiple annotations, for instance:
> 
>     % git worktree list
>     giggle     89ea799ffc [master]
>     ../bobble  f172cb543d [feature1] locked prunable
> 
> in which case, the eyes glide over "locked prunable" a bit more easily
> than over "(locked) (prunable)" or "(locked, prunable)" or some such.
> Generally speaking, "the less noise, the better".

Agreed. Without the parentheses is cleaner and it make easier to
extend to future annotations. I will drop the paretheses for the 
next patch version.

> > This patches are marked with RFC mainly due to:
> >
> >  - Perhaps the `(locked)` marker is not the best suitable way to output
> >   this information and we might need to come with a better way.
> 
> Taking [2] into consideration, I think it's fine to annotate the line
> with "locked" (sans the parentheses), and it's compatible with the the
> verbose mode, also proposed by [2]:
> 
>     % git worktree list -v
>     giggle     89ea799ffc [master]
>     ../bobble  f172cb543d [feature1]
>         locked: worktree on removable media
>     ../fumple  6453c84b7d (detached HEAD)
>         prunable: directory does not exist
> 
> in which case the short "locked" annotation gets moved to the next
> line, indented, and expanded to include the reason, if available
> (otherwise would probably not be moved to the next line).

The verbose mode seems like a very good extension for the worktree list
with the extended reason for `locked/prunable` on the next line.

> 
> I'm not suggesting that this patch series implement verbose mode, but
> bring it to attention to make sure we don't paint ourselves into a
> corner when deciding how the "locked" annotation should be presented.
> 

Doing a little investigation on the code, it seems the machinery for checking
whether a worktree is prunable it seems is already there implemented
on the `should_prune_worktree()`.

In such case, I would love to get started working on a bigger patch that
will implemented not only the annotation, but the verbose mode as well.
Specially because I was also thinking about how to make the "locked reason"
message available to the command output and the design proposed by [2]
sounds like a good way to manage that.

Additionally, having the ability to see the annotation and the reason in
case you see the annotation seems like more complete work for the intention
of the patch.

Unless you think that is better to start with the annotation, and some time
later addressing the other changes specified by [2].

> [2]: https://lore.kernel.org/git/CAPig+cQF6V8HNdMX5AZbmz3_w2WhSfA4SFfNhQqxXBqPXTZL+w@mail.gmail.com/

> >  - I am a new contributor to the code base, still learning a lot of git
> >   internals data structure and commands. Likely this patch will require
> >   updates.
> 
> Under normal circumstances, I would be hesitant to accept a
> contribution which makes an addition to the human-consumable "git
> worktree list" output without also making the corresponding addition
> to the --porcelain format. Thus, for instance, I would expect the
> porcelain format to be updated, as well, to produce output such as
> this (taken from [4]):
> 
>     worktree /blah
>     branch refs/heads/blah
>     locked Sneaker-net removable storage\nNot always mounted
> 
> That's a bit complicated because the lock reason may need escaping if
> it contains special characters (such as the newline in the example).
> Thus, I'm a bit hesitant to expect such a change from a newcomer.
> 
> More problematic, though, with regard to the porcelain format is that
> the documentation is so woefully under-specified, as explained in [4],
> that some people may interpret it as meaning that no additional
> information can be added. I'm not particularly sympathetic to that
> view since the intention from the start was that the porcelain format
> should be extensible[4], thus adding new attributes should be allowed.
> But I'm hesitant to ask a newcomer to undertake the task of addressing
> these shortcomings. As such, I think it may be okay merely to change
> the human-consumable output as this series does, and leave porcelain
> output for a later date if someone wants to tackle it.
> 
> A reason that it would be nice to address the shortcomings of
> porcelain format is because there are several additional pieces of
> information it could be providing. Summarizing from [1], in addition
> to the worktree path, its head, checked out branch, whether its bare
> or detached, for each worktree, porcelain could also show:
> 
>     * whether it is locked
>       - the lock reason (if available)
>       - and whether the worktree is currently accessible (mounted)
>     * whether it can be pruned
>       - and the prune reason if so
>     * worktree ID (the <id> of .git/worktrees/<id>/)

Thank you for the detailed explanation. much appreciated.

That something that can also work on. But I agreed that it could be bit
more work for a newcomer. I was thinking that I can split the work in 
three series of patches. 

  1. Implementing the annotation for the standards "list" command, implementing
     not only the locked but the prunable as on aforementioned in [2].

  2. A second series of patch that will introduce the verbose as defined in [2]

  3. Third and final series that extend the porcelain format.

I would like to kindly ask your opnion on this. Whether you think it will
be a good idea to implement all these changes this way and I can start
working on that.

I will change this series to become the first part of annotations, specially
because after reading your response and references, it seems this will be
much complete functionality that I would like to have on Git.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees
  2020-10-02 16:28   ` Rafael Silva
@ 2020-10-09 22:50     ` Eric Sunshine
  2020-10-10 19:06       ` Rafael Silva
  0 siblings, 1 reply; 23+ messages in thread
From: Eric Sunshine @ 2020-10-09 22:50 UTC (permalink / raw)
  To: Rafael Silva; +Cc: Git List, Junio C Hamano

On Fri, Oct 2, 2020 at 12:28 PM Rafael Silva
<rafaeloliveira.cs@gmail.com> wrote:
> On Wed, Sep 30, 2020 at 03:19:27AM -0400, Eric Sunshine wrote:
> > [...] For reference, here are some earlier messages related to
> > this topic:
>
> Thank you for all this reference, it's really helpful. It is nice to see
> that we already have few discussion on this topic that we can use and
> work on top of that.
>
> Sorry for the bit late response.

Likewise.

> > I'm not suggesting that this patch series implement verbose mode, but
> > bring it to attention to make sure we don't paint ourselves into a
> > corner when deciding how the "locked" annotation should be presented.
>
> Doing a little investigation on the code, it seems the machinery for checking
> whether a worktree is prunable it seems is already there implemented
> on the `should_prune_worktree()`.

Yes, when I mentioned that in [1], I envisioned
should_prune_worktree() being moved from builtin/worktree.c to
top-level worktree.c and possibly generalized a bit if necessary.

One thing to note is that should_prune_worktree() is somewhat
expensive, so we'd probably want to make determination of "prunable
reason" lazy, much like the lock reason is retrieved lazily rather
than doing it when get_worktrees() is called. Thus, like the lock
reason, the prunable reason would be accessed indirectly via a
function, say worktree_prunable_reason(), rather than directly from
'struct worktree'.

[1]: https://lore.kernel.org/git/CAPig+cTTrv2C7JLu1dr4+N8xo+7YQ+deiwLDA835wBGD6fhS1g@mail.gmail.com/

> In such case, I would love to get started working on a bigger patch that
> will implemented not only the annotation, but the verbose mode as well.
> Specially because I was also thinking about how to make the "locked reason"
> message available to the command output and the design proposed by [2]
> sounds like a good way to manage that.

I'd be happy to see that implemented.

> Additionally, having the ability to see the annotation and the reason in
> case you see the annotation seems like more complete work for the intention
> of the patch.
>
> Unless you think that is better to start with the annotation, and some time
> later addressing the other changes specified by [2].

Whatever you feel comfortable tackling is fine. The simple "locked"
annotation is nicely standalone, so it could be resubmitted with the
changes suggested by reviewers, and graduate without waiting for the
more complex tasks which could be done as follow-up series. Or, expand
the current series to tackle verbose mode and/or prunable status or
both or any combination.

> > A reason that it would be nice to address the shortcomings of
> > porcelain format is because there are several additional pieces of
> > information it could be providing. Summarizing from [1], in addition
> > to the worktree path, its head, checked out branch, whether its bare
> > or detached, for each worktree, porcelain could also show:
> >
> >   * whether it is locked
> >    - the lock reason (if available)
> >    - and whether the worktree is currently accessible (mounted)
> >   * whether it can be pruned
> >    - and the prune reason if so
> >   * worktree ID (the <id> of .git/worktrees/<id>/)
>
> That something that can also work on. But I agreed that it could be bit
> more work for a newcomer. I was thinking that I can split the work in
> three series of patches.
>
>  1. Implementing the annotation for the standards "list" command, implementing
>   not only the locked but the prunable as on aforementioned in [2].
>
>  2. A second series of patch that will introduce the verbose as defined in [2]
>
>  3. Third and final series that extend the porcelain format.
>
> I would like to kindly ask your opnion on this. Whether you think it will
> be a good idea to implement all these changes this way and I can start
> working on that.

Such an organization would be fine. Tackle what you feel is
appropriate and what "scratches your itch". Breaking the changes down
into smaller chunks, as you propose, also helps reviewers since it's
easier to review a shorter series than a long one.

> I will change this series to become the first part of annotations, specially
> because after reading your response and references, it seems this will be
> much complete functionality that I would like to have on Git.

Makes sense.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH v2 0/1] Teach "worktree list" to annotate locked worktrees
  2020-09-28 15:49 [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Rafael Silva
                   ` (3 preceding siblings ...)
  2020-09-30  7:19 ` Eric Sunshine
@ 2020-10-10 18:55 ` Rafael Silva
  2020-10-10 18:55   ` [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree Rafael Silva
  2020-10-11 10:11   ` [PATCH v3 0/1] Teach "worktree list" to annotate locked worktrees Rafael Silva
  4 siblings, 2 replies; 23+ messages in thread
From: Rafael Silva @ 2020-10-10 18:55 UTC (permalink / raw)
  To: git; +Cc: sunshine, gitster, Rafael Silva

This patch introduces a new information on the "git worktree list"
command output to annotate when a worktree is locked with a "locked" text.

The intent is to improve the user experience as the "git worktree remove"
refuses to remove a locked worktree. By adding the "locked" annotation
to the "git worktree list" command, the user would not attempt to remove
a worktree or would know how use "git worktree remove -f -f <path>"
to force a worktree removal.

The output of the "worktree list" command becomes:

  $ git worktree list
  /path/to/main             abc123 [master]
  /path/to/worktree         456def [brancha]
  /path/to/locked-worktree  123abc (detached HEAD) locked

Changes since v1:

  * Drooped the parenthesis in "(locked)" to reduce the noise and allow
    easier extensions of more annotations design proposed in [2].
  * Rewrite of the commit message with a much better one
  * Simplification of the test added to `t2402` with only caring about
    the "locked" annotation at the end of the "worktree list" output.

[2]: https://lore.kernel.org/git/CAPig+cQF6V8HNdMX5AZbmz3_w2WhSfA4SFfNhQqxXBqPXTZL+w@mail.gmail.com/

Thank you Junio C Hamano and Eric Sunshine for the detailed review and
helping with this patch.

Rafael Silva (1):
  worktree: teach `list` to annotate locked worktree

 Documentation/git-worktree.txt |  3 ++-
 builtin/worktree.c             |  5 ++++-
 t/t2402-worktree-list.sh       | 10 ++++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

-- 
2.29.0.rc0.253.gc238dab514


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree
  2020-10-10 18:55 ` [PATCH v2 0/1] Teach "worktree list" to annotate " Rafael Silva
@ 2020-10-10 18:55   ` Rafael Silva
  2020-10-11  6:26     ` Eric Sunshine
  2020-10-11 10:11   ` [PATCH v3 0/1] Teach "worktree list" to annotate locked worktrees Rafael Silva
  1 sibling, 1 reply; 23+ messages in thread
From: Rafael Silva @ 2020-10-10 18:55 UTC (permalink / raw)
  To: git; +Cc: sunshine, gitster, Rafael Silva

The "git worktree list" shows the absolute path to the working tree,
the commit that is checked out and the name of the branch. It is not
immediately obvious which of the worktrees, if any, are locked.

"git worktree remove" refuses to remove a locked worktree with
an error message. If "git worktree list" told which worktrees
are locked in its output, the user would not even attempt to
remove such a worktree or would know how to use
`git worktree remove -f -f <path>`

Teach "git worktree list" to append "locked" to its output.
The output from the command becomes like so:

    $ git worktree list
    /path/to/main             abc123 [master]
    /path/to/worktree         456def (detached HEAD)
    /path/to/locked-worktree  123abc (detached HEAD) locked

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
---
 Documentation/git-worktree.txt |  3 ++-
 builtin/worktree.c             |  5 ++++-
 t/t2402-worktree-list.sh       | 10 ++++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 32e8440cde..40ef4c18c5 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -97,7 +97,8 @@ list::
 List details of each working tree.  The main working tree is listed first,
 followed by each of the linked working trees.  The output details include
 whether the working tree is bare, the revision currently checked out, and the
-branch currently checked out (or "detached HEAD" if none).
+branch currently checked out (or "detached HEAD" if none). For a locked
+worktree the `locked` annotation is also shown.
 
 lock::
 
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 99abaeec6c..ce56fdaaa9 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -676,8 +676,11 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
 		} else
 			strbuf_addstr(&sb, "(error)");
 	}
-	printf("%s\n", sb.buf);
 
+	if (!is_main_worktree(wt) && worktree_lock_reason(wt))
+		strbuf_addstr(&sb, " locked");
+
+	printf("%s\n", sb.buf);
 	strbuf_release(&sb);
 }
 
diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
index 52585ec2aa..74275407ee 100755
--- a/t/t2402-worktree-list.sh
+++ b/t/t2402-worktree-list.sh
@@ -61,6 +61,16 @@ test_expect_success '"list" all worktrees --porcelain' '
 	test_cmp expect actual
 '
 
+test_expect_success '"list" all worktress with locked annotation' '
+	test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
+	git worktree add --detach locked master &&
+	git worktree add --detach unlocked master &&
+	git worktree lock locked &&
+	git worktree list >out &&
+	grep "/locked *[0-9a-f].* locked" out &&
+	! grep "/unlocked *[0-9a-f].* locked" out
+'
+
 test_expect_success 'bare repo setup' '
 	git init --bare bare1 &&
 	echo "data" >file1 &&
-- 
2.29.0.rc0.253.gc238dab514


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees
  2020-10-09 22:50     ` Eric Sunshine
@ 2020-10-10 19:06       ` Rafael Silva
  0 siblings, 0 replies; 23+ messages in thread
From: Rafael Silva @ 2020-10-10 19:06 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Junio C Hamano

On Fri, Oct 09, 2020 at 06:50:02PM -0400, Eric Sunshine wrote:
> >
> > Sorry for the bit late response.
> 
> Likewise.
> 
> >
> > Doing a little investigation on the code, it seems the machinery for checking
> > whether a worktree is prunable it seems is already there implemented
> > on the `should_prune_worktree()`.
> 
> Yes, when I mentioned that in [1], I envisioned
> should_prune_worktree() being moved from builtin/worktree.c to
> top-level worktree.c and possibly generalized a bit if necessary.
> 
> One thing to note is that should_prune_worktree() is somewhat
> expensive, so we'd probably want to make determination of "prunable
> reason" lazy, much like the lock reason is retrieved lazily rather
> than doing it when get_worktrees() is called. Thus, like the lock
> reason, the prunable reason would be accessed indirectly via a
> function, say worktree_prunable_reason(), rather than directly from
> 'struct worktree'.
> 
> [1]: https://lore.kernel.org/git/CAPig+cTTrv2C7JLu1dr4+N8xo+7YQ+deiwLDA835wBGD6fhS1g@mail.gmail.com/

Appreciate the tip, I will be working on the prunable annotations, verbose
and other information that was proposed previously for the "worktree list"
command.

> > Additionally, having the ability to see the annotation and the reason in
> > case you see the annotation seems like more complete work for the intention
> > of the patch.
> >
> > Unless you think that is better to start with the annotation, and some time
> > later addressing the other changes specified by [2].
> 
> Whatever you feel comfortable tackling is fine. The simple "locked"
> annotation is nicely standalone, so it could be resubmitted with the
> changes suggested by reviewers, and graduate without waiting for the
> more complex tasks which could be done as follow-up series. Or, expand
> the current series to tackle verbose mode and/or prunable status or
> both or any combination.
> 

Thanks. I've just resubmitted the "locked" annotation patch, as you said,
it's nice standalone and can be integrated and hopefully will be already
useful for other git users and soon (hopefully :) ) will submit new patches
for the other changes as proposed by [1].

[1]: https://lore.kernel.org/git/CAPig+cTTrv2C7JLu1dr4+N8xo+7YQ+deiwLDA835wBGD6fhS1g@mail.gmail.com/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree
  2020-10-10 18:55   ` [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree Rafael Silva
@ 2020-10-11  6:26     ` Eric Sunshine
  2020-10-11  6:34       ` Eric Sunshine
  2020-10-11 10:04       ` Rafael Silva
  0 siblings, 2 replies; 23+ messages in thread
From: Eric Sunshine @ 2020-10-11  6:26 UTC (permalink / raw)
  To: Rafael Silva; +Cc: Git List, Junio C Hamano

On Sat, Oct 10, 2020 at 2:56 PM Rafael Silva
<rafaeloliveira.cs@gmail.com> wrote:
> The "git worktree list" shows the absolute path to the working tree,
> the commit that is checked out and the name of the branch. It is not
> immediately obvious which of the worktrees, if any, are locked.
>
> "git worktree remove" refuses to remove a locked worktree with
> an error message. If "git worktree list" told which worktrees
> are locked in its output, the user would not even attempt to
> remove such a worktree or would know how to use
> `git worktree remove -f -f <path>`

I would drop "how" from "would know how to" so it instead reads "would
know to" since seeing the `locked` annotation only lets the user know
that removal must be forced; the `locked` annotation doesn't teach the
user _how_ to remove the worktree using force. But, perhaps, my
original suggestion[1], which did not use "how", was confusing. Maybe
it could be worded instead:

    ... not even attempt to remove such a worktree, or would
    realize that `git worktree remove -f -f <path>` is required.

Anyhow, this is a very minor nit about the commit message; not
necessarily worth a re-roll. More comments below...

[1]: https://lore.kernel.org/git/CAPig+cQHDuWy1vc_ngXbMQZQ=a9fd6S5_cCU-2sb_+Te5aEOhw@mail.gmail.com/

> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> @@ -97,7 +97,8 @@ list::
>  List details of each working tree.  The main working tree is listed first,
>  followed by each of the linked working trees.  The output details include
>  whether the working tree is bare, the revision currently checked out, and the
> -branch currently checked out (or "detached HEAD" if none).
> +branch currently checked out (or "detached HEAD" if none). For a locked
> +worktree the `locked` annotation is also shown.

I might have dropped the "and" in the final context line and instead
written this as:

    ... branch currently checked out (or "detached HEAD" if none),
    and "locked" if the worktree is locked.

But not worth a re-roll.

> diff --git a/builtin/worktree.c b/builtin/worktree.c
> @@ -676,8 +676,11 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
>                 } else
>                         strbuf_addstr(&sb, "(error)");
>
> +       if (!is_main_worktree(wt) && worktree_lock_reason(wt))
> +               strbuf_addstr(&sb, " locked");

I was going to ask if "locked" should be localizable like this:

    strbuf_addf(&sb, " %s", _("locked"));

but I see that none of the other words ("bare", "detached", "error")
in this function are localizable, so this is fine as-is.

However, all of the other human-consumable text emitted by "git
worktree" is localizable, so making these strings localizable, as
well, is something that can be added to a To-Do list. Note that I'm
talking only about human-consumable "git worktree list" output, not
porcelain format. Also, I'm not suggesting you tackle it, and it's
certainly not something that this patch or patch series needs to do;
just something which someone can tackle in the future.

> diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
> @@ -61,6 +61,16 @@ test_expect_success '"list" all worktrees --porcelain' '
> +test_expect_success '"list" all worktress with locked annotation' '
> +       test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
> +       git worktree add --detach locked master &&
> +       git worktree add --detach unlocked master &&
> +       git worktree lock locked &&
> +       git worktree list >out &&
> +       grep "/locked *[0-9a-f].* locked" out &&
> +       ! grep "/unlocked *[0-9a-f].* locked" out
> +'

These grep invocations are a bit loose, thus concern me a little bit.

First, in Junio's original example of using grep[2], he had two spaces
after the path component, not one as you have here. The two spaces in
the regex ensure that there is at least one space separating `/locked`
and `/unlocked` from the OID hex string, whereas with just one space
in the regex, as is done here, the space following the path component
is entirely optional (thus is a less desirable regex).

Second, because these regexes are not anchored, they could match with
a false-positive if the person's TRASH_DIRECTORY path is something
like `/home/proj/unlocked dead locked/git/t/...`. If you anchor the
pattern with `$`, then this problem goes away:

    grep "/locked  *[0-9a-f].* locked$" out &&
    ! grep "/unlocked  *[0-9a-f].* locked$" out

Third, this is checking only that the first character following the
path component is a hex digit but then accepts _anything_ before
"locked". The regex can be tightened to allow only hex digits:

    grep "/locked  *[0-9a-f][0-9a-f]* locked$" out &&
    ! grep "/unlocked  *[0-9a-f][0-9a-f]* locked$" out

[2]: https://lore.kernel.org/git/xmqq3631lg8f.fsf@gitster.c.googlers.com/

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree
  2020-10-11  6:26     ` Eric Sunshine
@ 2020-10-11  6:34       ` Eric Sunshine
  2020-10-11 10:04       ` Rafael Silva
  1 sibling, 0 replies; 23+ messages in thread
From: Eric Sunshine @ 2020-10-11  6:34 UTC (permalink / raw)
  To: Rafael Silva; +Cc: Git List, Junio C Hamano

On Sun, Oct 11, 2020 at 2:26 AM Eric Sunshine <sunshine@sunshineco.com> wrote:
> Third, this is checking only that the first character following the
> path component is a hex digit but then accepts _anything_ before
> "locked". The regex can be tightened to allow only hex digits:
>
>     grep "/locked  *[0-9a-f][0-9a-f]* locked$" out &&
>     ! grep "/unlocked  *[0-9a-f][0-9a-f]* locked$" out

Nevermind this last point. I see that there is other gunk after the
hex string but before the `locked` annotation, so this suggestion
breaks the test. The other two points -- (1) mandatory whitespace
following the path component, and (2) anchoring the pattern -- would
be welcome.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree
  2020-10-11  6:26     ` Eric Sunshine
  2020-10-11  6:34       ` Eric Sunshine
@ 2020-10-11 10:04       ` Rafael Silva
  1 sibling, 0 replies; 23+ messages in thread
From: Rafael Silva @ 2020-10-11 10:04 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Junio C Hamano

On Sun, Oct 11, 2020 at 02:26:31AM -0400, Eric Sunshine wrote:
> On Sat, Oct 10, 2020 at 2:56 PM Rafael Silva
> <rafaeloliveira.cs@gmail.com> wrote:
> > The "git worktree list" shows the absolute path to the working tree,
> > the commit that is checked out and the name of the branch. It is not
> > immediately obvious which of the worktrees, if any, are locked.
> >
> > "git worktree remove" refuses to remove a locked worktree with
> > an error message. If "git worktree list" told which worktrees
> > are locked in its output, the user would not even attempt to
> > remove such a worktree or would know how to use
> > `git worktree remove -f -f <path>`
> 
> I would drop "how" from "would know how to" so it instead reads "would
> know to" since seeing the `locked` annotation only lets the user know
> that removal must be forced; the `locked` annotation doesn't teach the
> user _how_ to remove the worktree using force. But, perhaps, my
> original suggestion[1], which did not use "how", was confusing. Maybe
> it could be worded instead:
> 
>     ... not even attempt to remove such a worktree, or would
>     realize that `git worktree remove -f -f <path>` is required.
> 
> Anyhow, this is a very minor nit about the commit message; not
> necessarily worth a re-roll. More comments below...
> 
> [1]: https://lore.kernel.org/git/CAPig+cQHDuWy1vc_ngXbMQZQ=a9fd6S5_cCU-2sb_+Te5aEOhw@mail.gmail.com/

The "would realise ..." seems clear to me, will change on the
patch as I will address the test changes aforementioned.

> 
> > diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> > @@ -97,7 +97,8 @@ list::
> >  List details of each working tree.  The main working tree is listed first,
> >  followed by each of the linked working trees.  The output details include
> >  whether the working tree is bare, the revision currently checked out, and the
> > -branch currently checked out (or "detached HEAD" if none).
> > +branch currently checked out (or "detached HEAD" if none). For a locked
> > +worktree the `locked` annotation is also shown.
> 
> I might have dropped the "and" in the final context line and instead
> written this as:
> 
>     ... branch currently checked out (or "detached HEAD" if none),
>     and "locked" if the worktree is locked.
> 
> But not worth a re-roll.

Agreed. I believe it makes the documentation more concise.

> > diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
> > @@ -61,6 +61,16 @@ test_expect_success '"list" all worktrees --porcelain' '
> > +test_expect_success '"list" all worktress with locked annotation' '
> > +       test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
> > +       git worktree add --detach locked master &&
> > +       git worktree add --detach unlocked master &&
> > +       git worktree lock locked &&
> > +       git worktree list >out &&
> > +       grep "/locked *[0-9a-f].* locked" out &&
> > +       ! grep "/unlocked *[0-9a-f].* locked" out
> > +'
> 
> These grep invocations are a bit loose, thus concern me a little bit.
> 
> First, in Junio's original example of using grep[2], he had two spaces
> after the path component, not one as you have here. The two spaces in
> the regex ensure that there is at least one space separating `/locked`
> and `/unlocked` from the OID hex string, whereas with just one space
> in the regex, as is done here, the space following the path component
> is entirely optional (thus is a less desirable regex).

That is interesting, I didn't know that will be the case - Nice to know :).
Thank you for the explanation.

> 
> Second, because these regexes are not anchored, they could match with
> a false-positive if the person's TRASH_DIRECTORY path is something
> like `/home/proj/unlocked dead locked/git/t/...`. If you anchor the
> pattern with `$`, then this problem goes away:
> 
>     grep "/locked  *[0-9a-f].* locked$" out &&
>     ! grep "/unlocked  *[0-9a-f].* locked$" out
> 

That is very good point and I will re-roll the patch with these two points
addressing the test cases. Thanks. 


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH v3 0/1] Teach "worktree list" to annotate locked worktrees
  2020-10-10 18:55 ` [PATCH v2 0/1] Teach "worktree list" to annotate " Rafael Silva
  2020-10-10 18:55   ` [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree Rafael Silva
@ 2020-10-11 10:11   ` Rafael Silva
  2020-10-11 10:11     ` [PATCH v3] worktree: teach `list` to annotate locked worktree Rafael Silva
  1 sibling, 1 reply; 23+ messages in thread
From: Rafael Silva @ 2020-10-11 10:11 UTC (permalink / raw)
  To: git; +Cc: sunshine, gitster, Rafael Silva

This patch introduces a new information on the "git worktree list"
command output to annotate when a worktree is locked with a "locked" text.

The intent is to improve the user experience as the "git worktree remove"
refuses to remove a locked worktree. By adding the "locked" annotation
to the "git worktree list" command, the user would not attempt to remove
a worktree or would know to use "git worktree remove -f -f <path>"
to force a worktree removal.

The output of the "worktree list" command becomes:

  $ git worktree list
  /path/to/main             abc123 [master]
  /path/to/worktree         456def [brancha]
  /path/to/locked-worktree  123abc (detached HEAD) locked

Changes since v2:

  * Regexes used to test the "locked" anotation are anchored and space is added
    between the worktree path and OID hex string to avoid false-positives and
    to ensure there is space between the worktree path and the OID.  

Changes since v1:

  * Drooped the parenthesis in "(locked)" to reduce the noise and allow
    easier extensions of more annotations design proposed in [2].
  * Rewrite of the commit message with a much better one.
  * Simplification of the test added to `t2402` with only caring about
    the "locked" annotation at the end of the "worktree list" output.

[2]: https://lore.kernel.org/git/CAPig+cQF6V8HNdMX5AZbmz3_w2WhSfA4SFfNhQqxXBqPXTZL+w@mail.gmail.com/

Thank you Junio C Hamano and Eric Sunshine for the detailed review
and helping with this patch.

Rafael Silva (1):
  worktree: teach `list` to annotate locked worktree

 Documentation/git-worktree.txt |  3 ++-
 builtin/worktree.c             |  5 ++++-
 t/t2402-worktree-list.sh       | 10 ++++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

-- 
2.29.0.rc0.253.gc238dab514


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH v3] worktree: teach `list` to annotate locked worktree
  2020-10-11 10:11   ` [PATCH v3 0/1] Teach "worktree list" to annotate locked worktrees Rafael Silva
@ 2020-10-11 10:11     ` Rafael Silva
  2020-10-12  2:24       ` Eric Sunshine
  0 siblings, 1 reply; 23+ messages in thread
From: Rafael Silva @ 2020-10-11 10:11 UTC (permalink / raw)
  To: git; +Cc: sunshine, gitster, Rafael Silva

The "git worktree list" shows the absolute path to the working tree,
the commit that is checked out and the name of the branch. It is not
immediately obvious which of the worktrees, if any, are locked.

"git worktree remove" refuses to remove a locked worktree with
an error message. If "git worktree list" told which worktrees
are locked in its output, the user would not even attempt to
remove such a worktree, or would realize that
"git worktree remove -f -f <path>" is required.

Teach "git worktree list" to append "locked" to its output.
The output from the command becomes like so:

    $ git worktree list
    /path/to/main             abc123 [master]
    /path/to/worktree         456def (detached HEAD)
    /path/to/locked-worktree  123abc (detached HEAD) locked

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
---
 Documentation/git-worktree.txt |  3 ++-
 builtin/worktree.c             |  5 ++++-
 t/t2402-worktree-list.sh       | 10 ++++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 32e8440cde..6a6b7c4ad1 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -97,7 +97,8 @@ list::
 List details of each working tree.  The main working tree is listed first,
 followed by each of the linked working trees.  The output details include
 whether the working tree is bare, the revision currently checked out, and the
-branch currently checked out (or "detached HEAD" if none).
+branch currently checked out (or "detached HEAD" if none), and "locked" if
+the worktree is locked.
 
 lock::
 
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 99abaeec6c..ce56fdaaa9 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -676,8 +676,11 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
 		} else
 			strbuf_addstr(&sb, "(error)");
 	}
-	printf("%s\n", sb.buf);
 
+	if (!is_main_worktree(wt) && worktree_lock_reason(wt))
+		strbuf_addstr(&sb, " locked");
+
+	printf("%s\n", sb.buf);
 	strbuf_release(&sb);
 }
 
diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
index 52585ec2aa..b85bd2655d 100755
--- a/t/t2402-worktree-list.sh
+++ b/t/t2402-worktree-list.sh
@@ -61,6 +61,16 @@ test_expect_success '"list" all worktrees --porcelain' '
 	test_cmp expect actual
 '
 
+test_expect_success '"list" all worktress with locked annotation' '
+	test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
+	git worktree add --detach locked master &&
+	git worktree add --detach unlocked master &&
+	git worktree lock locked &&
+	git worktree list >out &&
+	grep "/locked  *[0-9a-f].* locked$" out &&
+	! grep "/unlocked  *[0-9a-f].* locked$" out
+'
+
 test_expect_success 'bare repo setup' '
 	git init --bare bare1 &&
 	echo "data" >file1 &&
-- 
2.29.0.rc0.253.gc238dab514


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [PATCH v3] worktree: teach `list` to annotate locked worktree
  2020-10-11 10:11     ` [PATCH v3] worktree: teach `list` to annotate locked worktree Rafael Silva
@ 2020-10-12  2:24       ` Eric Sunshine
  0 siblings, 0 replies; 23+ messages in thread
From: Eric Sunshine @ 2020-10-12  2:24 UTC (permalink / raw)
  To: Rafael Silva; +Cc: Git List, Junio C Hamano

On Sun, Oct 11, 2020 at 6:12 AM Rafael Silva
<rafaeloliveira.cs@gmail.com> wrote:
> Teach "git worktree list" to append "locked" to its output.
>
> Signed-off-by: Rafael Silva <rafaeloliveira.cs@gmail.com>
> ---
> diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
> @@ -97,7 +97,8 @@ list::
>  followed by each of the linked working trees.  The output details include
>  whether the working tree is bare, the revision currently checked out, and the

Nit: I'd probably drop the "and" just before the end of this line...

> -branch currently checked out (or "detached HEAD" if none).
> +branch currently checked out (or "detached HEAD" if none), and "locked" if
> +the worktree is locked.

... which would leave just the one "and" here. However, this is minor
and almost certainly not worth a re-roll.

> +test_expect_success '"list" all worktress with locked annotation' '
> +       test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
> +       git worktree add --detach locked master &&
> +       git worktree add --detach unlocked master &&
> +       git worktree lock locked &&
> +       git worktree list >out &&
> +       grep "/locked  *[0-9a-f].* locked$" out &&
> +       ! grep "/unlocked  *[0-9a-f].* locked$" out
> +'

Looks good. With or without the minor suggested documentation tweak,
consider this patch:

    Reviewed-by: Eric Sunshine <sunshine@sunshineco.com>

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2020-10-12  2:24 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-28 15:49 [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Rafael Silva
2020-09-28 15:49 ` [RFC PATCH 1/2] worktree: teach `list` to mark locked worktree Rafael Silva
2020-09-28 21:37   ` Junio C Hamano
2020-09-29 21:36     ` Rafael Silva
2020-09-30  7:35     ` Eric Sunshine
2020-09-28 15:49 ` [RFC PATCH 2/2] t2402: add test to locked linked worktree marker Rafael Silva
2020-09-28 21:54   ` Junio C Hamano
2020-09-29 21:37     ` Rafael Silva
2020-09-30  8:06   ` Eric Sunshine
2020-09-28 21:19 ` [RFC PATCH 0/2] teach `worktree list` to mark locked worktrees Junio C Hamano
2020-09-29 21:35   ` Rafael Silva
2020-09-30  7:19 ` Eric Sunshine
2020-10-02 16:28   ` Rafael Silva
2020-10-09 22:50     ` Eric Sunshine
2020-10-10 19:06       ` Rafael Silva
2020-10-10 18:55 ` [PATCH v2 0/1] Teach "worktree list" to annotate " Rafael Silva
2020-10-10 18:55   ` [PATCH v2 1/1] worktree: teach `list` to annotate locked worktree Rafael Silva
2020-10-11  6:26     ` Eric Sunshine
2020-10-11  6:34       ` Eric Sunshine
2020-10-11 10:04       ` Rafael Silva
2020-10-11 10:11   ` [PATCH v3 0/1] Teach "worktree list" to annotate locked worktrees Rafael Silva
2020-10-11 10:11     ` [PATCH v3] worktree: teach `list` to annotate locked worktree Rafael Silva
2020-10-12  2:24       ` Eric Sunshine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).