Skip to content

Commit 862c723

Browse files
raffsgitster
authored andcommitted
worktree: teach list --porcelain to annotate locked worktree
Commit c57b336 (worktree: teach `list` to annotate locked worktree, 2020-10-11) taught "git worktree list" to annotate locked worktrees by appending "locked" text to its output, however, this is not listed in the --porcelain format. Teach "list --porcelain" to do the same and add a "locked" attribute followed by its reason, thus making both default and porcelain format consistent. If the locked reason is not available then only "locked" is shown. The output of the "git worktree list --porcelain" becomes like so: $ git worktree list --porcelain ... worktree /path/to/locked HEAD 123abcdea123abcd123acbd123acbda123abcd12 detached locked worktree /path/to/locked-with-reason HEAD abc123abc123abc123abc123abc123abc123abc1 detached locked reason why it is locked ... In porcelain mode, if the lock reason contains special characters such as newlines, they are escaped with backslashes and the entire reason is enclosed in double quotes. For example: $ git worktree list --porcelain ... locked "worktree's path mounted in\nremovable device" ... Furthermore, let's update the documentation to state that some attributes in the porcelain format might be listed alone or together with its value depending whether the value is available or not. Thus documenting the case of the new "locked" attribute. Helped-by: Phillip Wood <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Rafael Silva <[email protected]> Reviewed-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47409e7 commit 862c723

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

Documentation/git-worktree.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,10 @@ Porcelain Format
377377
The porcelain format has a line per attribute. Attributes are listed with a
378378
label and value separated by a single space. Boolean attributes (like `bare`
379379
and `detached`) are listed as a label only, and are present only
380-
if the value is true. The first attribute of a working tree is always
381-
`worktree`, an empty line indicates the end of the record. For example:
380+
if the value is true. Some attributes (like `locked`) can be listed as a label
381+
only or with a value depending upon whether a reason is available. The first
382+
attribute of a working tree is always `worktree`, an empty line indicates the
383+
end of the record. For example:
382384

383385
------------
384386
$ git worktree list --porcelain
@@ -393,6 +395,28 @@ worktree /path/to/other-linked-worktree
393395
HEAD 1234abc1234abc1234abc1234abc1234abc1234a
394396
detached
395397

398+
worktree /path/to/linked-worktree-locked-no-reason
399+
HEAD 5678abc5678abc5678abc5678abc5678abc5678c
400+
branch refs/heads/locked-no-reason
401+
locked
402+
403+
worktree /path/to/linked-worktree-locked-with-reason
404+
HEAD 3456def3456def3456def3456def3456def3456b
405+
branch refs/heads/locked-with-reason
406+
locked reason why is locked
407+
408+
------------
409+
410+
If the lock reason contains "unusual" characters such as newline, they
411+
are escaped and the entire reason is quoted as explained for the
412+
configuration variable `core.quotePath` (see linkgit:git-config[1]).
413+
For Example:
414+
415+
------------
416+
$ git worktree list --porcelain
417+
...
418+
locked "reason\nwhy is locked"
419+
...
396420
------------
397421

398422
EXAMPLES

builtin/worktree.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "submodule.h"
1313
#include "utf8.h"
1414
#include "worktree.h"
15+
#include "quote.h"
1516

1617
static const char * const worktree_usage[] = {
1718
N_("git worktree add [<options>] <path> [<commit-ish>]"),
@@ -569,6 +570,8 @@ static int add(int ac, const char **av, const char *prefix)
569570

570571
static void show_worktree_porcelain(struct worktree *wt)
571572
{
573+
const char *reason;
574+
572575
printf("worktree %s\n", wt->path);
573576
if (wt->is_bare)
574577
printf("bare\n");
@@ -579,6 +582,16 @@ static void show_worktree_porcelain(struct worktree *wt)
579582
else if (wt->head_ref)
580583
printf("branch %s\n", wt->head_ref);
581584
}
585+
586+
reason = worktree_lock_reason(wt);
587+
if (reason && *reason) {
588+
struct strbuf sb = STRBUF_INIT;
589+
quote_c_style(reason, &sb, NULL, 0);
590+
printf("locked %s\n", sb.buf);
591+
strbuf_release(&sb);
592+
} else if (reason)
593+
printf("locked\n");
594+
582595
printf("\n");
583596
}
584597

t/t2402-worktree-list.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,38 @@ test_expect_success '"list" all worktrees with locked annotation' '
7272
! grep "/unlocked *[0-9a-f].* locked$" out
7373
'
7474

75+
test_expect_success '"list" all worktrees --porcelain with locked' '
76+
test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" &&
77+
echo "locked" >expect &&
78+
echo "locked with reason" >>expect &&
79+
git worktree add --detach locked1 &&
80+
git worktree add --detach locked2 &&
81+
# unlocked worktree should not be annotated with "locked"
82+
git worktree add --detach unlocked &&
83+
git worktree lock locked1 &&
84+
test_when_finished "git worktree unlock locked1" &&
85+
git worktree lock locked2 --reason "with reason" &&
86+
test_when_finished "git worktree unlock locked2" &&
87+
git worktree list --porcelain >out &&
88+
grep "^locked" out >actual &&
89+
test_cmp expect actual
90+
'
91+
92+
test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' '
93+
test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" &&
94+
printf "locked \"locked\\\\r\\\\nreason\"\n" >expect &&
95+
printf "locked \"locked\\\\nreason\"\n" >>expect &&
96+
git worktree add --detach locked_lf &&
97+
git worktree add --detach locked_crlf &&
98+
git worktree lock locked_lf --reason "$(printf "locked\nreason")" &&
99+
test_when_finished "git worktree unlock locked_lf" &&
100+
git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" &&
101+
test_when_finished "git worktree unlock locked_crlf" &&
102+
git worktree list --porcelain >out &&
103+
grep "^locked" out >actual &&
104+
test_cmp expect actual
105+
'
106+
75107
test_expect_success 'bare repo setup' '
76108
git init --bare bare1 &&
77109
echo "data" >file1 &&

0 commit comments

Comments
 (0)