Skip to contentSkip to conversation

Ensure unique worktree ids across repositories

PATCH In review Caleb White 5 participants 24 messages Nov 29, 2024 – Dec 3, 2024 Documentation

lore
Share & tools
lore
#0 lore raw

v2 cover Ensure unique worktree ids across repositories

The es/worktree-repair-copied topic added support for repairing a worktree from a copy scenario. I noted[1,2] that the topic added the ability for a repository to "take over" a worktree from another repository if the worktree_id matched a worktree inside the current repository which can happen if two repositories use the same worktree name.

This series teaches Git to create worktrees with a unique suffix so that the worktree_id is unique across all repositories even if they have the same name. For example creating a worktree develop would look like:

    foo/
    ├── .git/worktrees/develop-5445874156/
    └── develop/
    bar/
    ├── .git/worktrees/develop-1549518426/
    └── develop/

The actual worktree directory name is still develop, but the worktree_id is unique and prevents the "take over" scenario. The suffix is given by the git_rand() function which should be sufficient to ensure that the suffix is effectively unique (the likelihood of a collision with the same name and suffix is extremely low).

During a worktree move the worktree directory is moved/renamed but the repository under worktrees/<id> is not updated. For example, moving develop to master results in

    foo/
    ├── .git/worktrees/develop-5445874156/
    └── master/

This works because the linking files still point to the correct repository, but this is a little weird. This series teaches Git to also move/rename the repository / worktree id during a move so that the structure now looks like:

    foo/
    ├── .git/worktrees/master-1565465986/
    └── master/

Note that a new unique suffix is assigned to reduce the complexity of trying to parse and reuse the existing suffix.

Additionally, this series teaches worktree list to output the worktree id in the verbose and porcelain modes, which allows users and scripts to more easily obtain the id for a given worktree.

[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/r4zmcET41Skr_FMop47AKd7cms9E8bKPSvHuAUpnYavzKEY6JybJta0_7GfuYB0q-gD-XNcvh5VDTfiT3qthGKjqhS1sbT4M2lUABynOz2Q=@pm.me/

---
The base for this series is obtained by merging the cw/worktree-extension topic (2024-11-26, [email protected]) onto 090d24e9af.

Changes in v2:
- Add the worktree id to worktree list output
- Updated cover letter
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Caleb White (3):
worktree: add worktree with unique suffix
worktree: rename worktree id during worktree move
worktree: add id to worktree list output

 Documentation/git-worktree.txt     | 17 +++++--
 builtin/worktree.c                 | 35 ++++++++++++++
 t/t0035-safe-bare-repository.sh    |  4 +-
 t/t0600-reffiles-backend.sh        | 10 ++--
 t/t0601-reffiles-pack-refs.sh      |  4 +-
 t/t0610-reftable-basics.sh         | 54 +++++++++++-----------
 t/t1407-worktree-ref-store.sh      |  4 +-
 t/t1410-reflog.sh                  | 10 ++--
 t/t1415-worktree-refs.sh           | 26 +++++------
 t/t1450-fsck.sh                    | 14 +++---
 t/t1500-rev-parse.sh               |  6 +--
 t/t2400-worktree-add.sh            | 51 +++++++++++----------
 t/t2401-worktree-prune.sh          | 20 ++++----
 t/t2402-worktree-list.sh           | 16 ++++---
 t/t2403-worktree-move.sh           | 38 ++++++++--------
 t/t2405-worktree-submodule.sh      | 10 ++--
 t/t2406-worktree-repair.sh         | 93 ++++++++++++++++++++++++--------------
 t/t2407-worktree-heads.sh          | 27 +++++------
 t/t3200-branch.sh                  | 10 ++--
 t/t5304-prune.sh                   |  2 +-
 t/t7412-submodule-absorbgitdirs.sh |  4 +-
 21 files changed, 265 insertions(+), 190 deletions(-)

---
base-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559
change-id: 20241127-wt_unique_ids-1ffd7ea0bb19
prerequisite-change-id: 20241025-wt_relative_options-afa41987bc32:v6
prerequisite-patch-id: 179410e257e8eedf100f4f9faa9467cbbba4d61b
prerequisite-patch-id: 56ffe0afeadd511c9eef5f548a371659b040acab
prerequisite-patch-id: 809c1314e5dfa966f4f3d73b52f286f8aa89370f
prerequisite-patch-id: cf5f9491c8f8e58d1e0e103a5f8c64c55f2896e3
prerequisite-patch-id: 9884b33822bf4c7c3b89a9a6b49d4ab44c2670e7
prerequisite-patch-id: 62a09496d98d78a6bd1f9150ba887ee72359c7ee
prerequisite-patch-id: 5527e4b745963dd4fa08029491fcbfe3d91d5104
prerequisite-patch-id: bf433443e90939a493fa586de30938f78cb77020

Best regards,

Signature · 1 line
Caleb White <[email protected]>
#1 lore raw

PATCH v2 1/3 worktree: add worktree with unique suffix

The es/worktree-repair-copied topic added support for repairing a worktree from a copy scenario. However, the topic added the ability for a repository to "take over" a worktree from another repository if the worktree_id matched a worktree inside the current repository which can happen if two repositories use the same worktree name.

This teaches Git to create worktrees with a unique suffix so the worktree_id is unique across all repositories even if they have the same name. For example creating a worktree develop would look like:

    foo/
    ├── .git/worktrees/develop-5445874156/
    └── develop/
    bar/
    ├── .git/worktrees/develop-1549518426/
    └── develop/

The actual worktree directory name is still develop, but the worktree_id is unique and prevents the "take over" scenario. The suffix is given by the git_rand() function. Worktree ids can already differ from the actual directory name (appended with a number like develop1) if the worktree name was already taken, so this should not be a drastic change.

---
 Documentation/git-worktree.txt     |  5 +-
 builtin/worktree.c                 |  6 +++
 t/t0035-safe-bare-repository.sh    |  4 +-
 t/t0600-reffiles-backend.sh        | 10 ++--
 t/t0601-reffiles-pack-refs.sh      |  4 +-
 t/t0610-reftable-basics.sh         | 54 +++++++++++-----------
 t/t1407-worktree-ref-store.sh      |  4 +-
 t/t1410-reflog.sh                  | 10 ++--
 t/t1415-worktree-refs.sh           | 26 +++++------
 t/t1450-fsck.sh                    | 14 +++---
 t/t1500-rev-parse.sh               |  6 +--
 t/t2400-worktree-add.sh            | 51 +++++++++++----------
 t/t2401-worktree-prune.sh          | 20 ++++----
 t/t2403-worktree-move.sh           | 32 ++++++-------
 t/t2405-worktree-submodule.sh      | 10 ++--
 t/t2406-worktree-repair.sh         | 93 ++++++++++++++++++++++++--------------
 t/t2407-worktree-heads.sh          | 27 +++++------
 t/t3200-branch.sh                  | 10 ++--
 t/t5304-prune.sh                   |  2 +-
 t/t7412-submodule-absorbgitdirs.sh |  4 +-
 20 files changed, 212 insertions(+), 180 deletions(-)
Documentation/git-worktree.txt +2 -3
index 8340b7f028e6c1c3bae3de0879e9754098466d14..e0604b043361828f94b58f676a5ed4f15b116348 100644
@@ -352,12 +352,11 @@ DETAILS
 -------
 Each linked worktree has a private sub-directory in the repository's
 `$GIT_DIR/worktrees` directory.  The private sub-directory's name is usually
-the base name of the linked worktree's path, possibly appended with a
+the base name of the linked worktree's path, appended with a random
 number to make it unique.  For example, when `$GIT_DIR=/path/main/.git` the
 command `git worktree add /path/other/test-next next` creates the linked
 worktree in `/path/other/test-next` and also creates a
-`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1`
-if `test-next` is already taken).
+`$GIT_DIR/worktrees/test-next-#######` directory.
 
 Within a linked worktree, `$GIT_DIR` is set to point to this private
 directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and
builtin/worktree.c +6 -0
index fde9ff4dc9a734c655e95ccd62774282950cbba6..3ad355ca762729401fc0c8625f4fd05b154a84ec 100644
@@ -421,6 +421,7 @@ static int add_worktree(const char *path, const char *refname,
 	struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
 	struct strbuf sb = STRBUF_INIT;
 	const char *name;
+	const char *suffix;
 	struct strvec child_env = STRVEC_INIT;
 	unsigned int counter = 0;
 	int len, ret;
@@ -455,6 +456,11 @@ static int add_worktree(const char *path, const char *refname,
 	strbuf_reset(&sb);
 	name = sb_name.buf;
 	git_path_buf(&sb_repo, "worktrees/%s", name);
+	suffix = getenv("GIT_TEST_WORKTREE_SUFFIX");
+	if (suffix)
+		strbuf_addf(&sb_repo, "-%s", suffix);
+	else
+		strbuf_addf(&sb_repo, "-%u", git_rand());
 	len = sb_repo.len;
 	if (safe_create_leading_directories_const(sb_repo.buf))
 		die_errno(_("could not create leading directories of '%s'"),
t/t0035-safe-bare-repository.sh +2 -2
index d3cb2a1cb9edb8f9ad7480be6ec3e02b464046bd..30cbf7fd32e2e4afd6d680e0328ee18b791778da 100755
@@ -41,7 +41,7 @@ test_expect_success 'setup an embedded bare repo, secondary worktree and submodu
 		git -c protocol.file.allow=always \
 			submodule add --name subn -- ./bare-repo subd
 	) &&
-	test_path_is_dir outer-repo/.git/worktrees/outer-secondary &&
+	test_path_is_dir outer-repo/.git/worktrees/outer-secondary-* &&
 	test_path_is_dir outer-repo/.git/modules/subn
 '
 
@@ -97,7 +97,7 @@ test_expect_success 'no trace when "bare repository" is a subdir of .git' '
 '
 
 test_expect_success 'no trace in $GIT_DIR of secondary worktree' '
-	expect_accepted_implicit -C outer-repo/.git/worktrees/outer-secondary
+	expect_accepted_implicit -C outer-repo/.git/worktrees/outer-secondary-*
 '
 
 test_expect_success 'no trace in $GIT_DIR of a submodule' '
t/t0600-reffiles-backend.sh +5 -5
index bef2b70871364931ab8b3ead950f59d11b6fe216..8da81b1ae34f5f095ba9406e8f031fd5e43ac789 100755
@@ -256,12 +256,12 @@ test_expect_success 'delete fails cleanly if packed-refs.new write fails' '
 	test_cmp unchanged actual
 '
 
-RWT="test-tool ref-store worktree:wt"
+RWT="test-tool ref-store worktree:wt-123"
 RMAIN="test-tool ref-store worktree:main"
 
 test_expect_success 'setup worktree' '
 	test_commit first &&
-	git worktree add -b wt-main wt &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add -b wt-main wt &&
 	(
 		cd wt &&
 		test_commit second
@@ -279,9 +279,9 @@ test_expect_success 'for_each_reflog()' '
 	mkdir -p     .git/logs/refs/bisect &&
 	echo $ZERO_OID >.git/logs/refs/bisect/random &&
 
-	echo $ZERO_OID >.git/worktrees/wt/logs/PSEUDO_WT_HEAD &&
-	mkdir -p     .git/worktrees/wt/logs/refs/bisect &&
-	echo $ZERO_OID >.git/worktrees/wt/logs/refs/bisect/wt-random &&
+	echo $ZERO_OID >.git/worktrees/wt-123/logs/PSEUDO_WT_HEAD &&
+	mkdir -p     .git/worktrees/wt-123/logs/refs/bisect &&
+	echo $ZERO_OID >.git/worktrees/wt-123/logs/refs/bisect/wt-random &&
 
 	$RWT for-each-reflog >actual &&
 	cat >expected <<-\EOF &&
t/t0601-reffiles-pack-refs.sh +2 -2
index d8cbd3f202b5f00c9a94c5a2ea2dced6606b6f4d..d6597938caca5e6c3a6e86b860f3c0d309aa2140 100755
@@ -326,8 +326,8 @@ test_expect_success 'refs/worktree must not be packed' '
 	git pack-refs --all &&
 	test_path_is_missing .git/refs/tags/wt1 &&
 	test_path_is_file .git/refs/worktree/foo &&
-	test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
-	test_path_is_file .git/worktrees/wt2/refs/worktree/foo
+	test_path_is_file .git/worktrees/wt1-*/refs/worktree/foo &&
+	test_path_is_file .git/worktrees/wt2-*/refs/worktree/foo
 '
 
 # we do not want to count on running pack-refs to
t/t0610-reftable-basics.sh +27 -27

131 lines not on this page · raw message

t/t1407-worktree-ref-store.sh +2 -2
index 48b1c92a41450b25645d6cd782aa86c8e630164b..4d081627a1e6b55936cd4051ed760fa92e7cdc02 100755
@@ -8,12 +8,12 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
-RWT="test-tool ref-store worktree:wt"
+RWT="test-tool ref-store worktree:wt-456"
 RMAIN="test-tool ref-store worktree:main"
 
 test_expect_success 'setup' '
 	test_commit first &&
-	git worktree add -b wt-main wt &&
+	GIT_TEST_WORKTREE_SUFFIX=456 git worktree add -b wt-main wt &&
 	(
 		cd wt &&
 		test_commit second
t/t1410-reflog.sh +5 -5
index 246a3f46abafdf0e24528be59b33a4987ff791c1..c1b6b4d8fab8d261713a192478af12179f9bc917 100755
@@ -402,12 +402,12 @@ test_expect_success 'expire with multiple worktrees' '
 		cd main-wt &&
 		test_tick &&
 		test_commit foo &&
-		git  worktree add link-wt &&
+		GIT_TEST_WORKTREE_SUFFIX=123 git  worktree add link-wt &&
 		test_tick &&
 		test_commit -C link-wt foobar &&
 		test_tick &&
 		git reflog expire --verbose --all --expire=$test_tick &&
-		test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD >actual &&
+		test-tool ref-store worktree:link-wt-123 for-each-reflog-ent HEAD >actual &&
 		test_must_be_empty actual
 	)
 '
@@ -418,17 +418,17 @@ test_expect_success 'expire one of multiple worktrees' '
 		cd main-wt2 &&
 		test_tick &&
 		test_commit foo &&
-		git worktree add link-wt &&
+		GIT_TEST_WORKTREE_SUFFIX=456 git worktree add link-wt &&
 		test_tick &&
 		test_commit -C link-wt foobar &&
 		test_tick &&
-		test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
+		test-tool ref-store worktree:link-wt-456 for-each-reflog-ent HEAD \
 			>expect-link-wt &&
 		git reflog expire --verbose --all --expire=$test_tick \
 			--single-worktree &&
 		test-tool ref-store worktree:main for-each-reflog-ent HEAD \
 			>actual-main &&
-		test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
+		test-tool ref-store worktree:link-wt-456 for-each-reflog-ent HEAD \
 			>actual-link-wt &&
 		test_must_be_empty actual-main &&
 		test_cmp expect-link-wt actual-link-wt
t/t1415-worktree-refs.sh +13 -13
index eb4eec8becbfa64efcde4e866334363a866c01a2..c46bf29aa4d9ceac85147d685a1d23144ec23b2b 100755
@@ -9,8 +9,8 @@ test_expect_success 'setup' '
 	test_commit initial &&
 	test_commit wt1 &&
 	test_commit wt2 &&
-	git worktree add wt1 wt1 &&
-	git worktree add wt2 wt2 &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add wt1 wt1 &&
+	GIT_TEST_WORKTREE_SUFFIX=456 git worktree add wt2 wt2 &&
 	git checkout initial &&
 	git update-ref refs/worktree/foo HEAD &&
 	git -C wt1 update-ref refs/worktree/foo HEAD &&
@@ -37,16 +37,16 @@ test_expect_success 'ambiguous main-worktree/HEAD' '
 '
 
 test_expect_success 'resolve worktrees/xx/HEAD' '
-	test_cmp_rev worktrees/wt1/HEAD wt1 &&
-	( cd wt1 && test_cmp_rev worktrees/wt1/HEAD wt1 ) &&
-	( cd wt2 && test_cmp_rev worktrees/wt1/HEAD wt1 )
+	test_cmp_rev worktrees/wt1-123/HEAD wt1 &&
+	( cd wt1 && test_cmp_rev worktrees/wt1-123/HEAD wt1 ) &&
+	( cd wt2 && test_cmp_rev worktrees/wt1-123/HEAD wt1 )
 '
 
 test_expect_success 'ambiguous worktrees/xx/HEAD' '
-	git update-ref refs/heads/worktrees/wt1/HEAD $(git rev-parse HEAD) &&
-	test_when_finished git update-ref -d refs/heads/worktrees/wt1/HEAD &&
-	git rev-parse worktrees/wt1/HEAD 2>warn &&
-	grep "worktrees/wt1/HEAD.*ambiguous" warn
+	git update-ref refs/heads/worktrees/wt1-123/HEAD $(git rev-parse HEAD) &&
+	test_when_finished git update-ref -d refs/heads/worktrees/wt1-123/HEAD &&
+	git rev-parse worktrees/wt1-123/HEAD 2>warn &&
+	grep "worktrees/wt1-123/HEAD.*ambiguous" warn
 '
 
 test_expect_success 'reflog of main-worktree/HEAD' '
@@ -58,12 +58,12 @@ test_expect_success 'reflog of main-worktree/HEAD' '
 '
 
 test_expect_success 'reflog of worktrees/xx/HEAD' '
-	git -C wt2 reflog HEAD | sed "s/HEAD/worktrees\/wt2\/HEAD/" >expected &&
-	git reflog worktrees/wt2/HEAD >actual &&
+	git -C wt2 reflog HEAD | sed "s/HEAD/worktrees\/wt2-456\/HEAD/" >expected &&
+	git reflog worktrees/wt2-456/HEAD >actual &&
 	test_cmp expected actual &&
-	git -C wt1 reflog worktrees/wt2/HEAD >actual.wt1 &&
+	git -C wt1 reflog worktrees/wt2-456/HEAD >actual.wt1 &&
 	test_cmp expected actual.wt1 &&
-	git -C wt2 reflog worktrees/wt2/HEAD >actual.wt2 &&
+	git -C wt2 reflog worktrees/wt2-456/HEAD >actual.wt2 &&
 	test_cmp expected actual.wt2
 '
 
t/t1450-fsck.sh +7 -7
index 280cbf3e031e1ab67ed28aa2af4c3b105b7d254e..de25ab24fd8924e288969650a658908a5e9afd22 100755
@@ -152,10 +152,10 @@ test_expect_success REFFILES 'HEAD link pointing at a funny object (from differe
 
 test_expect_success REFFILES 'other worktree HEAD link pointing at a funny object' '
 	test_when_finished "git worktree remove -f other" &&
-	git worktree add other &&
-	echo $ZERO_OID >.git/worktrees/other/HEAD &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add other &&
+	echo $ZERO_OID >.git/worktrees/other-123/HEAD &&
 	test_must_fail git fsck 2>out &&
-	test_grep "worktrees/other/HEAD: detached HEAD points" out
+	test_grep "worktrees/other-123/HEAD: detached HEAD points" out
 '
 
 test_expect_success 'other worktree HEAD link pointing at missing object' '
@@ -164,7 +164,7 @@ test_expect_success 'other worktree HEAD link pointing at missing object' '
 	object_id=$(echo "Contents missing from repo" | git hash-object --stdin) &&
 	test-tool -C other ref-store main update-ref msg HEAD $object_id "" REF_NO_DEREF,REF_SKIP_OID_VERIFICATION &&
 	test_must_fail git fsck 2>out &&
-	test_grep "worktrees/other/HEAD: invalid sha1 pointer" out
+	test_grep "worktrees/other-.*/HEAD: invalid sha1 pointer" out
 '
 
 test_expect_success 'other worktree HEAD link pointing at a funny place' '
@@ -172,7 +172,7 @@ test_expect_success 'other worktree HEAD link pointing at a funny place' '
 	git worktree add other &&
 	git -C other symbolic-ref HEAD refs/funny/place &&
 	test_must_fail git fsck 2>out &&
-	test_grep "worktrees/other/HEAD points to something strange" out
+	test_grep "worktrees/other-.*/HEAD points to something strange" out
 '
 
 test_expect_success 'commit with multiple signatures is okay' '
@@ -1033,7 +1033,7 @@ test_expect_success 'fsck error on gitattributes with excessive size' '
 
 test_expect_success 'fsck detects problems in worktree index' '
 	test_when_finished "git worktree remove -f wt" &&
-	git worktree add wt &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add wt &&
 
 	echo "this will be removed to break the worktree index" >wt/file &&
 	git -C wt add file &&
@@ -1042,7 +1042,7 @@ test_expect_success 'fsck detects problems in worktree index' '
 
 	test_must_fail git fsck --name-objects >actual 2>&1 &&
 	cat >expect <<-EOF &&
-	missing blob $blob (.git/worktrees/wt/index:file)
+	missing blob $blob (.git/worktrees/wt-123/index:file)
 	EOF
 	test_cmp expect actual
 '
t/t1500-rev-parse.sh +3 -3
index 30c31918fde6539d52800e18dfbb3423b5b73491..49e0ac68858d922336a498caaa743f2f4011d28a 100755
@@ -80,7 +80,7 @@ test_expect_success 'setup' '
 	git checkout -b side &&
 	test_commit def &&
 	git checkout main &&
-	git worktree add worktree side
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add worktree side
 '
 
 test_rev_parse toplevel false false true '' .git "$ROOT/.git"
@@ -113,7 +113,7 @@ test_expect_success 'rev-parse --path-format=absolute' '
 	test_one "." "$ROOT/.git" --path-format=absolute --git-common-dir &&
 	test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-dir &&
 	test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-common-dir &&
-	test_one "worktree" "$ROOT/.git/worktrees/worktree" --path-format=absolute --git-dir &&
+	test_one "worktree" "$ROOT/.git/worktrees/worktree-123" --path-format=absolute --git-dir &&
 	test_one "worktree" "$ROOT/.git" --path-format=absolute --git-common-dir &&
 	test_one "." "$ROOT" --path-format=absolute --show-toplevel &&
 	test_one "." "$ROOT/.git/objects" --path-format=absolute --git-path objects &&
@@ -125,7 +125,7 @@ test_expect_success 'rev-parse --path-format=relative' '
 	test_one "." ".git" --path-format=relative --git-common-dir &&
 	test_one "sub/dir" "../../.git" --path-format=relative --git-dir &&
 	test_one "sub/dir" "../../.git" --path-format=relative --git-common-dir &&
-	test_one "worktree" "../.git/worktrees/worktree" --path-format=relative --git-dir &&
+	test_one "worktree" "../.git/worktrees/worktree-123" --path-format=relative --git-dir &&
 	test_one "worktree" "../.git" --path-format=relative --git-common-dir &&
 	test_one "." "./" --path-format=relative --show-toplevel &&
 	test_one "." ".git/objects" --path-format=relative --git-path objects &&
t/t2400-worktree-add.sh +26 -25

135 lines not on this page · raw message

t/t2401-worktree-prune.sh +10 -10
index 5eb52b9abbf29514dc082c260ebb7a5e8e63aae0..856fcdd19d376d3448ee0be46592fe68d44617c4 100755
@@ -83,29 +83,29 @@ test_expect_success 'not prune locked checkout' '
 test_expect_success 'not prune recent checkouts' '
 	test_when_finished rm -r .git/worktrees &&
 	git worktree add jlm HEAD &&
-	test -d .git/worktrees/jlm &&
+	test -d .git/worktrees/jlm-* &&
 	rm -rf jlm &&
 	git worktree prune --verbose --expire=2.days.ago &&
-	test -d .git/worktrees/jlm
+	test -d .git/worktrees/jlm-*
 '
 
 test_expect_success 'not prune proper checkouts' '
 	test_when_finished rm -r .git/worktrees &&
 	git worktree add --detach "$PWD/nop" main &&
 	git worktree prune &&
-	test -d .git/worktrees/nop
+	test -d .git/worktrees/nop-*
 '
 
 test_expect_success 'prune duplicate (linked/linked)' '
 	test_when_finished rm -fr .git/worktrees w1 w2 &&
-	git worktree add --detach w1 &&
-	git worktree add --detach w2 &&
-	sed "s/w2/w1/" .git/worktrees/w2/gitdir >.git/worktrees/w2/gitdir.new &&
-	mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir &&
+	GIT_TEST_WORKTREE_SUFFIX=1 git worktree add --detach w1 &&
+	GIT_TEST_WORKTREE_SUFFIX=2 git worktree add --detach w2 &&
+	sed "s/w2/w1/" .git/worktrees/w2-2/gitdir >.git/worktrees/w2-2/gitdir.new &&
+	mv .git/worktrees/w2-2/gitdir.new .git/worktrees/w2-2/gitdir &&
 	git worktree prune --verbose 2>actual &&
 	test_grep "duplicate entry" actual &&
-	test -d .git/worktrees/w1 &&
-	! test -d .git/worktrees/w2
+	test -d .git/worktrees/w1-1 &&
+	! test -d .git/worktrees/w2-2
 '
 
 test_expect_success 'prune duplicate (main/linked)' '
@@ -117,7 +117,7 @@ test_expect_success 'prune duplicate (main/linked)' '
 	mv repo wt &&
 	git -C wt worktree prune --verbose 2>actual &&
 	test_grep "duplicate entry" actual &&
-	! test -d .git/worktrees/wt
+	! test -d .git/worktrees/wt-*
 '
 
 test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '
t/t2403-worktree-move.sh +16 -16
index 422c1a05580057b18ab8bfdfe38da4d723749493..ba3f05c16a4969fb84d98052ae375ef162f3e73a 100755
@@ -24,27 +24,27 @@ test_expect_success 'lock main worktree' '
 test_expect_success 'lock linked worktree' '
 	git worktree lock --reason hahaha source &&
 	echo hahaha >expected &&
-	test_cmp expected .git/worktrees/source/locked
+	test_cmp expected .git/worktrees/source-*/locked
 '
 
 test_expect_success 'lock linked worktree from another worktree' '
-	rm .git/worktrees/source/locked &&
+	rm .git/worktrees/source-*/locked &&
 	git worktree add elsewhere &&
 	git -C elsewhere worktree lock --reason hahaha ../source &&
 	echo hahaha >expected &&
-	test_cmp expected .git/worktrees/source/locked
+	test_cmp expected .git/worktrees/source-*/locked
 '
 
 test_expect_success 'lock worktree twice' '
 	test_must_fail git worktree lock source &&
 	echo hahaha >expected &&
-	test_cmp expected .git/worktrees/source/locked
+	test_cmp expected .git/worktrees/source-*/locked
 '
 
 test_expect_success 'lock worktree twice (from the locked worktree)' '
 	test_must_fail git -C source worktree lock . &&
 	echo hahaha >expected &&
-	test_cmp expected .git/worktrees/source/locked
+	test_cmp expected .git/worktrees/source-*/locked
 '
 
 test_expect_success 'unlock main worktree' '
@@ -183,19 +183,19 @@ test_expect_success 'force remove worktree with untracked file' '
 
 test_expect_success 'remove missing worktree' '
 	git worktree add to-be-gone &&
-	test -d .git/worktrees/to-be-gone &&
+	test -d .git/worktrees/to-be-gone-* &&
 	mv to-be-gone gone &&
 	git worktree remove to-be-gone &&
-	test_path_is_missing .git/worktrees/to-be-gone
+	test_path_is_missing .git/worktrees/to-be-gone-*
 '
 
 test_expect_success 'NOT remove missing-but-locked worktree' '
 	git worktree add gone-but-locked &&
 	git worktree lock gone-but-locked &&
-	test -d .git/worktrees/gone-but-locked &&
+	test -d .git/worktrees/gone-but-locked-* &&
 	mv gone-but-locked really-gone-now &&
 	test_must_fail git worktree remove gone-but-locked &&
-	test_path_is_dir .git/worktrees/gone-but-locked
+	test_path_is_dir .git/worktrees/gone-but-locked-*
 '
 
 test_expect_success 'proper error when worktree not found' '
@@ -249,27 +249,27 @@ test_expect_success 'not remove a repo with initialized submodule' '
 
 test_expect_success 'move worktree with absolute path to relative path' '
 	test_config worktree.useRelativePaths false &&
-	git worktree add ./absolute &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add ./absolute &&
 	git worktree move --relative-paths absolute relative &&
-	echo "gitdir: ../.git/worktrees/absolute" >expect &&
+	echo "gitdir: ../.git/worktrees/absolute-123" >expect &&
 	test_cmp expect relative/.git &&
 	echo "../../../relative/.git" >expect &&
-	test_cmp expect .git/worktrees/absolute/gitdir &&
+	test_cmp expect .git/worktrees/absolute-123/gitdir &&
 	test_config worktree.useRelativePaths true &&
 	git worktree move relative relative2 &&
-	echo "gitdir: ../.git/worktrees/absolute" >expect &&
+	echo "gitdir: ../.git/worktrees/absolute-123" >expect &&
 	test_cmp expect relative2/.git &&
 	echo "../../../relative2/.git" >expect &&
-	test_cmp expect .git/worktrees/absolute/gitdir
+	test_cmp expect .git/worktrees/absolute-123/gitdir
 '
 
 test_expect_success 'move worktree with relative path to absolute path' '
 	test_config worktree.useRelativePaths true &&
 	git worktree move --no-relative-paths relative2 absolute &&
-	echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect &&
+	echo "gitdir: $(pwd)/.git/worktrees/absolute-123" >expect &&
 	test_cmp expect absolute/.git &&
 	echo "$(pwd)/absolute/.git" >expect &&
-	test_cmp expect .git/worktrees/absolute/gitdir
+	test_cmp expect .git/worktrees/absolute-123/gitdir
 '
 
 test_done
t/t2405-worktree-submodule.sh +6 -4
index 1d7f60563387f9c2f53dfc3a79ac0289afe57611..5479b2a74aa0d9b1e8880ed7c038307ffa1d0c54 100755
@@ -9,6 +9,7 @@ TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 base_path=$(pwd -P)
+suffix=4567
 
 test_expect_success 'setup: create origin repos'  '
 	git config --global protocol.file.allow always &&
@@ -61,9 +62,10 @@ test_expect_success 'submodule is checked out after manually adding submodule wo
 '
 
 test_expect_success 'checkout --recurse-submodules uses $GIT_DIR for submodules in a linked worktree' '
-	git -C main worktree add "$base_path/checkout-recurse" --detach  &&
+	GIT_TEST_WORKTREE_SUFFIX=$suffix \
+		git -C main worktree add "$base_path/checkout-recurse" --detach  &&
 	git -C checkout-recurse submodule update --init &&
-	echo "gitdir: ../../main/.git/worktrees/checkout-recurse/modules/sub" >expect-gitfile &&
+	echo "gitdir: ../../main/.git/worktrees/checkout-recurse-$suffix/modules/sub" >expect-gitfile &&
 	cat checkout-recurse/sub/.git >actual-gitfile &&
 	test_cmp expect-gitfile actual-gitfile &&
 	git -C main/sub rev-parse HEAD >expect-head-main &&
@@ -82,14 +84,14 @@ test_expect_success 'core.worktree is removed in $GIT_DIR/modules/<name>/config,
 	git -C checkout-recurse/sub config --get core.worktree >actual-linked &&
 	test_cmp expect-linked actual-linked &&
 	git -C checkout-recurse checkout --recurse-submodules first &&
-	test_expect_code 1 git -C main/.git/worktrees/checkout-recurse/modules/sub config --get core.worktree >linked-config &&
+	test_expect_code 1 git -C main/.git/worktrees/checkout-recurse-$suffix/modules/sub config --get core.worktree >linked-config &&
 	test_must_be_empty linked-config &&
 	git -C main/sub config --get core.worktree >actual-main &&
 	test_cmp expect-main actual-main
 '
 
 test_expect_success 'unsetting core.worktree does not prevent running commands directly against the submodule repository' '
-	git -C main/.git/worktrees/checkout-recurse/modules/sub log
+	git -C main/.git/worktrees/checkout-recurse-$suffix/modules/sub log
 '
 
 test_done
t/t2406-worktree-repair.sh +58 -35

188 lines not on this page · raw message

t/t2407-worktree-heads.sh +14 -13
index f6835c91dcc49cfeb23881fe0ef7a96629bfb2e6..1587dadfd1e1fa122edccc62ffd9aa4c20f0ec80 100755
@@ -19,7 +19,8 @@ test_expect_success 'setup' '
 		test_commit $i &&
 		git branch wt-$i &&
 		git branch fake-$i &&
-		git worktree add wt-$i wt-$i || return 1
+		GIT_TEST_WORKTREE_SUFFIX=$i \
+			git worktree add wt-$i wt-$i || return 1
 	done &&
 
 	# Create a server that updates each branch by one commit
@@ -132,20 +133,20 @@ test_expect_success 'refuse to overwrite when in error states' '
 	test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
 
 	# Both branches are currently under rebase.
-	mkdir -p .git/worktrees/wt-3/rebase-merge &&
-	touch .git/worktrees/wt-3/rebase-merge/interactive &&
-	echo refs/heads/fake-1 >.git/worktrees/wt-3/rebase-merge/head-name &&
-	echo refs/heads/fake-2 >.git/worktrees/wt-3/rebase-merge/onto &&
-	mkdir -p .git/worktrees/wt-4/rebase-merge &&
-	touch .git/worktrees/wt-4/rebase-merge/interactive &&
-	echo refs/heads/fake-2 >.git/worktrees/wt-4/rebase-merge/head-name &&
-	echo refs/heads/fake-1 >.git/worktrees/wt-4/rebase-merge/onto &&
+	mkdir -p .git/worktrees/wt-3-3/rebase-merge &&
+	touch .git/worktrees/wt-3-3/rebase-merge/interactive &&
+	echo refs/heads/fake-1 >.git/worktrees/wt-3-3/rebase-merge/head-name &&
+	echo refs/heads/fake-2 >.git/worktrees/wt-3-3/rebase-merge/onto &&
+	mkdir -p .git/worktrees/wt-4-4/rebase-merge &&
+	touch .git/worktrees/wt-4-4/rebase-merge/interactive &&
+	echo refs/heads/fake-2 >.git/worktrees/wt-4-4/rebase-merge/head-name &&
+	echo refs/heads/fake-1 >.git/worktrees/wt-4-4/rebase-merge/onto &&
 
 	# Both branches are currently under bisect.
-	touch .git/worktrees/wt-4/BISECT_LOG &&
-	echo refs/heads/fake-2 >.git/worktrees/wt-4/BISECT_START &&
-	touch .git/worktrees/wt-1/BISECT_LOG &&
-	echo refs/heads/fake-1 >.git/worktrees/wt-1/BISECT_START &&
+	touch .git/worktrees/wt-4-4/BISECT_LOG &&
+	echo refs/heads/fake-2 >.git/worktrees/wt-4-4/BISECT_START &&
+	touch .git/worktrees/wt-1-1/BISECT_LOG &&
+	echo refs/heads/fake-1 >.git/worktrees/wt-1-1/BISECT_START &&
 
 	for i in 1 2
 	do
t/t3200-branch.sh +5 -5
index ccfa6a720d090c2f7f2a085f60065bdcfaf8d1d9..e44497ac94394119662115b1f6aa035c7f0565d2 100755
@@ -246,14 +246,14 @@ test_expect_success 'git branch -M baz bam should succeed when baz is checked ou
 '
 
 test_expect_success REFFILES 'git branch -M fails if updating any linked working tree fails' '
-	git worktree add -b baz bazdir1 &&
-	git worktree add -f bazdir2 baz &&
-	touch .git/worktrees/bazdir1/HEAD.lock &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add -b baz bazdir1 &&
+	GIT_TEST_WORKTREE_SUFFIX=456 git worktree add -f bazdir2 baz &&
+	touch .git/worktrees/bazdir1-123/HEAD.lock &&
 	test_must_fail git branch -M baz bam &&
 	test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
 	git branch -M bam baz &&
-	rm .git/worktrees/bazdir1/HEAD.lock &&
-	touch .git/worktrees/bazdir2/HEAD.lock &&
+	rm .git/worktrees/bazdir1-123/HEAD.lock &&
+	touch .git/worktrees/bazdir2-456/HEAD.lock &&
 	test_must_fail git branch -M baz bam &&
 	test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam &&
 	rm -rf bazdir1 bazdir2 &&
t/t5304-prune.sh +1 -1
index e641df0116c24404e4892a0e30af4ef4bf8db493..0e98c6627a98ed197d7ab1ff41e8dd41eeaff3ac 100755
@@ -293,7 +293,7 @@ test_expect_success 'prune: handle HEAD in multiple worktrees' '
 	echo "new blob for third-worktree" >third-worktree/blob &&
 	git -C third-worktree add blob &&
 	git -C third-worktree commit -m "third" &&
-	rm .git/worktrees/third-worktree/index &&
+	rm .git/worktrees/third-worktree-*/index &&
 	test_must_fail git -C third-worktree show :blob &&
 	git prune --expire=now &&
 	git -C third-worktree show HEAD:blob >actual &&
t/t7412-submodule-absorbgitdirs.sh +2 -2
index f77832185765585e2bda1677f8cbbe13841127f7..acf9544e35966f054cbc90c37e84377c9f461f2b 100755
@@ -123,7 +123,7 @@ test_expect_success 'absorb the git dir outside of primary worktree' '
 	test_when_finished "rm -rf repo-bare.git" &&
 	git clone --bare . repo-bare.git &&
 	test_when_finished "rm -rf repo-wt" &&
-	git -C repo-bare.git worktree add ../repo-wt &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git -C repo-bare.git worktree add ../repo-wt &&
 
 	test_when_finished "rm -f .gitconfig" &&
 	test_config_global protocol.file.allow always &&
@@ -134,7 +134,7 @@ test_expect_success 'absorb the git dir outside of primary worktree' '
 	cat >expect <<-EOF &&
 	Migrating git directory of '\''sub2'\'' from
 	'\''$cwd/repo-wt/sub2/.git'\'' to
-	'\''$cwd/repo-bare.git/worktrees/repo-wt/modules/sub2'\''
+	'\''$cwd/repo-bare.git/worktrees/repo-wt-123/modules/sub2'\''
 	EOF
 	git -C repo-wt submodule absorbgitdirs 2>actual &&
 	test_cmp expect actual
Signature · 1 line
2.47.0
#2 lore raw

PATCH v2 2/3 worktree: rename worktree id during worktree move

In reply to Caleb White

During a worktree move the worktree directory is moved/renamed but the repository under worktrees/<id> is not updated. For example, given the following structure:

    foo/
    ├── .git/worktrees/develop-5445874156/
    └── develop/

moving develop to master results in

    foo/
    ├── .git/worktrees/develop-5445874156/
    └── master/

This works because the linking files still point to the correct repository, but this is a little weird. This teaches Git to also move/rename the repository / worktree id during a move so that the structure now looks like:

    foo/
    ├── .git/worktrees/master-1565465986/
    └── master/

Note that a new unique suffix is assigned to reduce the complexity of trying to parse and reuse the existing suffix.

---
 builtin/worktree.c       | 24 ++++++++++++++++++++++++
 t/t2403-worktree-move.sh | 18 +++++++++---------
 2 files changed, 33 insertions(+), 9 deletions(-)
builtin/worktree.c +24 -0
index 3ad355ca762729401fc0c8625f4fd05b154a84ec..36235546b492803707707ff208b13fe777bff1b4 100644
@@ -1202,9 +1202,14 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 	};
 	struct worktree **worktrees, *wt;
 	struct strbuf dst = STRBUF_INIT;
+	struct strbuf repo = STRBUF_INIT;
+	struct strbuf repo_dst = STRBUF_INIT;
 	struct strbuf errmsg = STRBUF_INIT;
 	const char *reason = NULL;
+	const char *new_id;
+	const char *suffix;
 	char *path;
+	int len;
 
 	ac = parse_options(ac, av, prefix, options, git_worktree_move_usage,
 			   0);
@@ -1250,9 +1255,28 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 	if (rename(wt->path, dst.buf) == -1)
 		die_errno(_("failed to move '%s' to '%s'"), wt->path, dst.buf);
 
+	strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
+	new_id = worktree_basename(dst.buf, &len);
+	strbuf_add(&repo_dst, new_id, dst.buf + len - new_id);
+	strbuf_realpath(&repo_dst, git_common_path("worktrees/%s", repo_dst.buf), 1);
+	suffix = getenv("GIT_TEST_WORKTREE_SUFFIX");
+	if (suffix)
+		strbuf_addf(&repo_dst, "-%s", suffix);
+	else
+		strbuf_addf(&repo_dst, "-%u", git_rand());
+	new_id = strrchr(repo_dst.buf, '/') + 1;
+	if (rename(repo.buf, repo_dst.buf) == -1)
+		die_errno(_("failed to move '%s' to '%s'"), repo.buf, repo_dst.buf);
+	else {
+		free(wt->id);
+		wt->id = xstrdup(new_id);
+	}
+
 	update_worktree_location(wt, dst.buf, use_relative_paths);
 
 	strbuf_release(&dst);
+	strbuf_release(&repo);
+	strbuf_release(&repo_dst);
 	free_worktrees(worktrees);
 	return 0;
 }
t/t2403-worktree-move.sh +9 -9
index ba3f05c16a4969fb84d98052ae375ef162f3e73a..703aa58d10643e99ffaf803aa38dabfd4af68a10 100755
@@ -250,26 +250,26 @@ test_expect_success 'not remove a repo with initialized submodule' '
 test_expect_success 'move worktree with absolute path to relative path' '
 	test_config worktree.useRelativePaths false &&
 	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add ./absolute &&
-	git worktree move --relative-paths absolute relative &&
-	echo "gitdir: ../.git/worktrees/absolute-123" >expect &&
+	GIT_TEST_WORKTREE_SUFFIX=456 git worktree move --relative-paths absolute relative &&
+	echo "gitdir: ../.git/worktrees/relative-456" >expect &&
 	test_cmp expect relative/.git &&
 	echo "../../../relative/.git" >expect &&
-	test_cmp expect .git/worktrees/absolute-123/gitdir &&
+	test_cmp expect .git/worktrees/relative-456/gitdir &&
 	test_config worktree.useRelativePaths true &&
-	git worktree move relative relative2 &&
-	echo "gitdir: ../.git/worktrees/absolute-123" >expect &&
+	GIT_TEST_WORKTREE_SUFFIX=789 git worktree move relative relative2 &&
+	echo "gitdir: ../.git/worktrees/relative2-789" >expect &&
 	test_cmp expect relative2/.git &&
 	echo "../../../relative2/.git" >expect &&
-	test_cmp expect .git/worktrees/absolute-123/gitdir
+	test_cmp expect .git/worktrees/relative2-789/gitdir
 '
 
 test_expect_success 'move worktree with relative path to absolute path' '
 	test_config worktree.useRelativePaths true &&
-	git worktree move --no-relative-paths relative2 absolute &&
-	echo "gitdir: $(pwd)/.git/worktrees/absolute-123" >expect &&
+	GIT_TEST_WORKTREE_SUFFIX=851 git worktree move --no-relative-paths relative2 absolute &&
+	echo "gitdir: $(pwd)/.git/worktrees/absolute-851" >expect &&
 	test_cmp expect absolute/.git &&
 	echo "$(pwd)/absolute/.git" >expect &&
-	test_cmp expect .git/worktrees/absolute-123/gitdir
+	test_cmp expect .git/worktrees/absolute-851/gitdir
 '
 
 test_done
Signature · 1 line
2.47.0
#3 lore raw

PATCH v2 3/3 worktree: add id to `worktree list` output

In reply to Caleb White

The worktree id is relatively hidden from the user, however, there may be times where a user or script needs to determine the worktree id for a linked worktree (e.g., to manually operate on some refs). While the id is stored in the worktree .git file, it would be nice if there was an easier method of obtaining it. This teaches Git to output the worktree id (for linked worktrees) in the worktree list verbose and porcelain modes.

---
 Documentation/git-worktree.txt | 12 +++++++++++-
 builtin/worktree.c             |  5 +++++
 t/t2402-worktree-list.sh       | 16 ++++++++++------
 3 files changed, 26 insertions(+), 7 deletions(-)
Documentation/git-worktree.txt +11 -1
index e0604b043361828f94b58f676a5ed4f15b116348..2bd8118852a97251fc1081d19ea9756428d190f4 100644
@@ -428,16 +428,21 @@ $ git worktree list
 
 For these annotations, a reason might also be available and this can be
 seen using the verbose mode. The annotation is then moved to the next line
-indented followed by the additional information.
+indented followed by the additional information. In verbose mode, the worktree
+id is also shown on the next line if available.
 
 ------------
 $ git worktree list --verbose
 /path/to/linked-worktree              abcd1234 [master]
+	id: linked-worktree-12345678
 /path/to/locked-worktree-no-reason    abcd5678 (detached HEAD) locked
+	id: locked-worktree-no-reason-89765464
 /path/to/locked-worktree-with-reason  1234abcd (brancha)
 	locked: worktree path is mounted on a portable device
+	id: locked-worktree-with-reason-41564654
 /path/to/prunable-worktree            5678abc1 (detached HEAD)
 	prunable: gitdir file points to non-existent location
+	id: prunable-worktree-98454651
 ------------
 
 Note that the annotation is moved to the next line if the additional
@@ -461,24 +466,29 @@ worktree /path/to/bare-source
 bare
 
 worktree /path/to/linked-worktree
+id linked-worktree-12345678
 HEAD abcd1234abcd1234abcd1234abcd1234abcd1234
 branch refs/heads/master
 
 worktree /path/to/other-linked-worktree
+id other-linked-worktree-879456466
 HEAD 1234abc1234abc1234abc1234abc1234abc1234a
 detached
 
 worktree /path/to/linked-worktree-locked-no-reason
+id locked-worktree-no-reason-89765464
 HEAD 5678abc5678abc5678abc5678abc5678abc5678c
 branch refs/heads/locked-no-reason
 locked
 
 worktree /path/to/linked-worktree-locked-with-reason
+id locked-worktree-with-reason-41564654
 HEAD 3456def3456def3456def3456def3456def3456b
 branch refs/heads/locked-with-reason
 locked reason why is locked
 
 worktree /path/to/linked-worktree-prunable
+id prunable-worktree-98454651
 HEAD 1233def1234def1234def1234def1234def1234b
 detached
 prunable gitdir file points to non-existent location
builtin/worktree.c +5 -0
index 36235546b492803707707ff208b13fe777bff1b4..fca8a9cda51643e434b5f8905e32e537c6b4418b 100644
@@ -944,6 +944,8 @@ static void show_worktree_porcelain(struct worktree *wt, int line_terminator)
 	const char *reason;
 
 	printf("worktree %s%c", wt->path, line_terminator);
+	if (!is_main_worktree(wt))
+		printf("id %s%c", wt->id, line_terminator);
 	if (wt->is_bare)
 		printf("bare%c", line_terminator);
 	else {
@@ -1009,6 +1011,9 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
 	else if (reason)
 		strbuf_addstr(&sb, " prunable");
 
+	if (verbose && !is_main_worktree(wt))
+		strbuf_addf(&sb, "\n\tid: %s", wt->id);
+
 	printf("%s\n", sb.buf);
 	strbuf_release(&sb);
 }
t/t2402-worktree-list.sh +10 -6
index 780daa6cd6351f8fa9434619cc212aade8f01420..502d35cabb837121d178937673b580cd767d17aa 100755
@@ -56,8 +56,9 @@ test_expect_success '"list" all worktrees --porcelain' '
 	echo "branch $(git symbolic-ref HEAD)" >>expect &&
 	echo >>expect &&
 	test_when_finished "rm -rf here actual expect && git worktree prune" &&
-	git worktree add --detach here main &&
+	GIT_TEST_WORKTREE_SUFFIX=123 git worktree add --detach here main &&
 	echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
+	echo "id here-123" >>expect &&
 	echo "HEAD $(git rev-parse HEAD)" >>expect &&
 	echo "detached" >>expect &&
 	echo >>expect &&
@@ -71,9 +72,10 @@ test_expect_success '"list" all worktrees --porcelain -z' '
 	printf "worktree %sQHEAD %sQbranch %sQQ" \
 		"$(git rev-parse --show-toplevel)" \
 		$(git rev-parse HEAD --symbolic-full-name HEAD) >expect &&
-	git worktree add --detach here main &&
-	printf "worktree %sQHEAD %sQdetachedQQ" \
+	GIT_TEST_WORKTREE_SUFFIX=456 git worktree add --detach here main &&
+	printf "worktree %sQid %sQHEAD %sQdetachedQQ" \
 		"$(git -C here rev-parse --show-toplevel)" \
+		"here-456" \
 		"$(git rev-parse HEAD)" >>expect &&
 	git worktree list --porcelain -z >_actual &&
 	nul_to_q <_actual >actual &&
@@ -166,16 +168,17 @@ test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
 test_expect_success '"list" all worktrees --verbose with locked' '
 	test_when_finished "rm -rf locked1 locked2 out actual expect && git worktree prune" &&
 	git worktree add locked1 --detach &&
-	git worktree add locked2 --detach &&
+	GIT_TEST_WORKTREE_SUFFIX=456 git worktree add locked2 --detach &&
 	git worktree lock locked1 &&
 	test_when_finished "git worktree unlock locked1" &&
 	git worktree lock locked2 --reason "with reason" &&
 	test_when_finished "git worktree unlock locked2" &&
 	echo "$(git -C locked2 rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
 	printf "\tlocked: with reason\n" >>expect &&
+	printf "\tid: locked2-456\n" >>expect &&
 	git worktree list --verbose >out &&
 	grep "/locked1  *[0-9a-f].* locked$" out &&
-	sed -n "s/  */ /g;/\/locked2  *[0-9a-f].*$/,/locked: .*$/p" <out >actual &&
+	sed -n "s/  */ /g;/\/locked2  *[0-9a-f].*$/,/id: .*$/p" <out >actual &&
 	test_cmp actual expect
 '
 
@@ -211,11 +214,12 @@ test_expect_success '"list" all worktrees from bare main' '
 
 test_expect_success '"list" all worktrees --porcelain from bare main' '
 	test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
-	git -C bare1 worktree add --detach ../there main &&
+	GIT_TEST_WORKTREE_SUFFIX=456 git -C bare1 worktree add --detach ../there main &&
 	echo "worktree $(pwd)/bare1" >expect &&
 	echo "bare" >>expect &&
 	echo >>expect &&
 	echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
+	echo "id there-456" >>expect &&
 	echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
 	echo "detached" >>expect &&
 	echo >>expect &&
Signature · 1 line
2.47.0
#4 lore raw
In reply to Caleb White
Quoted 91 lines · Caleb White

The es/worktree-repair-copied topic added support for repairing a worktree from a copy scenario. I noted[1,2] that the topic added the ability for a repository to "take over" a worktree from another repository if the worktree_id matched a worktree inside the current repository which can happen if two repositories use the same worktree name.

This series teaches Git to create worktrees with a unique suffix so that the worktree_id is unique across all repositories even if they have the same name. For example creating a worktree develop would look like:

   foo/
   ├── .git/worktrees/develop-5445874156/
   └── develop/
   bar/
   ├── .git/worktrees/develop-1549518426/
   └── develop/

The actual worktree directory name is still develop, but the worktree_id is unique and prevents the "take over" scenario. The suffix is given by the git_rand() function which should be sufficient to ensure that the suffix is effectively unique (the likelihood of a collision with the same name and suffix is extremely low).

During a worktree move the worktree directory is moved/renamed but the repository under worktrees/<id> is not updated. For example, moving develop to master results in

   foo/
   ├── .git/worktrees/develop-5445874156/
   └── master/

This works because the linking files still point to the correct repository, but this is a little weird. This series teaches Git to also move/rename the repository / worktree id during a move so that the structure now looks like:

   foo/
   ├── .git/worktrees/master-1565465986/
   └── master/

Note that a new unique suffix is assigned to reduce the complexity of trying to parse and reuse the existing suffix.

Additionally, this series teaches worktree list to output the worktree id in the verbose and porcelain modes, which allows users and scripts to more easily obtain the id for a given worktree.

[1]: https://lore.kernel.org/git/[email protected]/
[2]:
https://lore.kernel.org/git/r4zmcET41Skr_FMop47AKd7cms9E8bKPSvHuAUpnYav
zKEY6JybJta0_7GfuYB0q-gD- [email protected]/

---
The base for this series is obtained by merging the cw/worktree-extension topic (2024-11-26, [email protected]) onto 090d24e9af.

Changes in v2:
- Add the worktree id to worktree list output
- Updated cover letter
- Link to v1: https://lore.kernel.org/r/20241128-wt_unique_ids-v1-0-
[email protected]

---
Caleb White (3):
worktree: add worktree with unique suffix
worktree: rename worktree id during worktree move
worktree: add id to worktree list output

Documentation/git-worktree.txt     | 17 +++++--
builtin/worktree.c                 | 35 ++++++++++++++
t/t0035-safe-bare-repository.sh    |  4 +-
t/t0600-reffiles-backend.sh        | 10 ++--
t/t0601-reffiles-pack-refs.sh      |  4 +-
t/t0610-reftable-basics.sh         | 54 +++++++++++-----------
t/t1407-worktree-ref-store.sh      |  4 +-
t/t1410-reflog.sh                  | 10 ++--
t/t1415-worktree-refs.sh           | 26 +++++------
t/t1450-fsck.sh                    | 14 +++---
t/t1500-rev-parse.sh               |  6 +--
t/t2400-worktree-add.sh            | 51 +++++++++++----------
t/t2401-worktree-prune.sh          | 20 ++++----
t/t2402-worktree-list.sh           | 16 ++++---
t/t2403-worktree-move.sh           | 38 ++++++++--------
t/t2405-worktree-submodule.sh      | 10 ++--
t/t2406-worktree-repair.sh         | 93 ++++++++++++++++++++++++--------------
t/t2407-worktree-heads.sh          | 27 +++++------
t/t3200-branch.sh                  | 10 ++--
t/t5304-prune.sh                   |  2 +-
t/t7412-submodule-absorbgitdirs.sh |  4 +-
21 files changed, 265 insertions(+), 190 deletions(-)
Quoted 12 lines

---
base-commit: 090d24e9af6e9f59c3f7bee97c42bb1ae3c7f559
change-id: 20241127-wt_unique_ids-1ffd7ea0bb19
prerequisite-change-id: 20241025-wt_relative_options-afa41987bc32:v6
prerequisite-patch-id: 179410e257e8eedf100f4f9faa9467cbbba4d61b
prerequisite-patch-id: 56ffe0afeadd511c9eef5f548a371659b040acab
prerequisite-patch-id: 809c1314e5dfa966f4f3d73b52f286f8aa89370f
prerequisite-patch-id: cf5f9491c8f8e58d1e0e103a5f8c64c55f2896e3
prerequisite-patch-id: 9884b33822bf4c7c3b89a9a6b49d4ab44c2670e7
prerequisite-patch-id: 62a09496d98d78a6bd1f9150ba887ee72359c7ee
prerequisite-patch-id: 5527e4b745963dd4fa08029491fcbfe3d91d5104
prerequisite-patch-id: bf433443e90939a493fa586de30938f78cb77020

General comment on this series: Is there a mechanism of preserving existing functionality for those of us who have existing scripts that depend on the existing branch and worktree naming?

#5 lore raw
Quoted 3 lines · rsbecker

General comment on this series: Is there a mechanism of preserving existing functionality for those of us who have existing scripts that depend on the existing branch and worktree naming?

Existing worktrees will continue to work as they do now. The only change is the worktree id for new worktrees. However, there's not an option to preserve the existing behavior for new worktrees (nor do I think there should be).

As stated in the v1 threads, the worktree id is already not guaranteed to be equal to the worktree/branch name (there's several ways that this can occur), so it's buggy behavior for scripts to make this assumption. Any script that needs the worktree id should be parsing it from the .git file, git rev-parse --git-dir, or (with the changes in this series) git worktree list.

Best,

Caleb

#6 lore raw
Quoted 8 lines · Caleb White
Quoted 3 lines · rsbecker

General comment on this series: Is there a mechanism of preserving existing functionality for those of us who have existing scripts that depend on the existing branch and worktree naming?

Existing worktrees will continue to work as they do now. The only change is the worktree id for new worktrees. However, there's not an option to preserve the existing behavior for new worktrees (nor do I think there should be).

I do not agree. Companies that have existing scripts should have some way to preserve their investment. Just saying "No more worktrees for you" is not really considerate.

Quoted 6 lines

As stated in the v1 threads, the worktree id is already not guaranteed to be equal to the worktree/branch name (there's several ways that this can occur), so it's buggy behavior for scripts to make this assumption.
Any script that needs the worktree id should be parsing it from the .git file, git rev- parse --git-dir, or (with the changes in this
series) git worktree list.

I agree, but I think having some kind of notice beyond one release is important, rather than pulling the rug out from under people.

Just my suggestion that there should be a migration period of this critical function. --Randall

#7 lore raw
Quoted 6 lines · rsbecker
Quoted 5 lines · Caleb White
Quoted 3 lines · rsbecker

General comment on this series: Is there a mechanism of preserving existing functionality for those of us who have existing scripts that depend on the existing branch and worktree naming?

Quoted 7 lines
Quoted 3 lines

Existing worktrees will continue to work as they do now. The only change is the worktree id for new worktrees. However, there's not an option to preserve the existing behavior for new worktrees (nor do I think there should be).

I do not agree. Companies that have existing scripts should have some way to preserve their investment. Just saying "No more worktrees for you" is not really considerate.

How exactly are your scripts depending on the worktree id? There are very few reasons a script might need to know the worktree id, and I suspect that there's some confusion here. The worktree name is still used with the git worktree commands, so there no change on that front.

Best,

Caleb

#8 lore raw
Quoted 12 lines · Caleb White
Quoted 9 lines · rsbecker
Quoted 8 lines · Caleb White
Quoted 3 lines · rsbecker

General comment on this series: Is there a mechanism of preserving existing functionality for those of us who have existing scripts that depend on the existing branch and worktree naming?

Existing worktrees will continue to work as they do now. The only change is the worktree id for new worktrees. However, there's not an option to preserve the existing behavior for new worktrees (nor do I think there

should be).

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

I do not agree. Companies that have existing scripts should have some way to preserve their investment. Just saying "No more worktrees for you" is not really considerate.

How exactly are your scripts depending on the worktree id? There are very few reasons a script might need to know the worktree id, and I suspect that there's some confusion here. The worktree name is still used with the git worktree commands, so there no change on that front.

The graphic describing this showed the id in addition to the worktree name. During cleanup detection, the directory of the worktree is significant. If that Observation is wrong, I retract all this.

#9 lore raw
Quoted 17 lines · rsbecker
Quoted 16 lines · Caleb White
Quoted 9 lines · rsbecker
Quoted 8 lines · Caleb White
Quoted 3 lines · rsbecker

General comment on this series: Is there a mechanism of preserving existing functionality for those of us who have existing scripts that depend on the existing branch and worktree naming?

Existing worktrees will continue to work as they do now. The only change is the worktree id for new worktrees. However, there's not an option to preserve the existing behavior for new worktrees (nor do I think there

should be).

Quoted 4 lines

I do not agree. Companies that have existing scripts should have some way to preserve their investment. Just saying "No more worktrees for you" is not really considerate.

Quoted 8 lines
Quoted 4 lines

How exactly are your scripts depending on the worktree id? There are very few reasons a script might need to know the worktree id, and I suspect that there's some confusion here. The worktree name is still used with the git worktree commands, so there no change on that front.

The graphic describing this showed the id in addition to the worktree name. During cleanup detection, the directory of the worktree is significant. If that Observation is wrong, I retract all this.

So here's the graphic again:

    foo/
    ├── .git/worktrees/develop-5445874156/
    └── develop/

Here, the develop directory is the worktree directory (this can be located anywhere), and the develop-5445874156 is the worktree id. However, the worktree id can already be something like develop1 or something else entirely if the develop directory was renamed in the past. Again, there are very few things a script should need to know the worktree id for.

If the develop directory is deleted, cleanup detection is handled by the git worktree prune command, which will remove worktrees under .git/worktrees/* that are no longer valid. This happens automatically after the expiry time or it can be executed manually. Of course, executing git worktree remove develop will also remove the worktree and its associated worktree id.

Best,

Caleb

#10 lore raw
Quoted 44 lines · Caleb White

To: [email protected]; [email protected]
Cc: 'shejialuo' <[email protected]>; 'Junio C Hamano' <[email protected]>
Subject: Re: [PATCH v2 0/3] Ensure unique worktree ids across repositories

Quoted 18 lines · rsbecker
Quoted 17 lines · Caleb White
Quoted 10 lines · rsbecker
Quoted 9 lines · Caleb White
Quoted 3 lines · rsbecker

General comment on this series: Is there a mechanism of preserving existing functionality for those of us who have existing scripts that depend on the existing branch and worktree naming?

Existing worktrees will continue to work as they do now. The only change is the worktree id for new worktrees. However, there's not an option to preserve the existing behavior for new worktrees (nor do I think there

should be).

Quoted 4 lines

I do not agree. Companies that have existing scripts should have some way to preserve their investment. Just saying "No more worktrees for you" is not really considerate.

Quoted 8 lines
Quoted 4 lines

How exactly are your scripts depending on the worktree id? There are very few reasons a script might need to know the worktree id, and I suspect that there's some confusion here. The worktree name is still used with the git worktree commands, so there no change on that front.

The graphic describing this showed the id in addition to the worktree name. During cleanup detection, the directory of the worktree is significant. If that Observation is wrong, I retract all this.

So here's the graphic again:

   foo/
   ├── .git/worktrees/develop-5445874156/
   └── develop/

Here, the develop directory is the worktree directory (this can be located anywhere), and the develop-5445874156 is the worktree id.
However, the worktree id can already be something like develop1 or something else entirely if the develop directory was renamed in the past. Again, there are very few things a script should need to know the worktree id for.

Quoted 5 lines

If the develop directory is deleted, cleanup detection is handled by the git worktree prune command, which will remove worktrees under .git/worktrees/* that are no longer valid. This happens automatically after the expiry time or it can be executed manually. Of course, executing git worktree remove develop will also remove the worktree and its associated worktree id.

This last bit is an assumption, and not necessarily valid. Scripts that use worktrees may maintain lists or their own pointers. It is important to be able to emulate cleanup functions - something I discovered early in the worktree functions when released. I need to make sure that cleanup will continue to have enough information - prior to git worktree cleanup - to function correctly. This will need coordination with people who have such scripts in my community. It probably will not impact you, but I would have appreciated more than one release notice on this capability.

#11 lore raw
Quoted 7 lines · rsbecker
Quoted 5 lines · Caleb White

If the develop directory is deleted, cleanup detection is handled by the git worktree prune command, which will remove worktrees under .git/worktrees/* that are no longer valid. This happens automatically after the expiry time or it can be executed manually. Of course, executing git worktree remove develop will also remove the worktree and its associated worktree id.

Quoted 8 lines

This last bit is an assumption, and not necessarily valid. Scripts that use worktrees may maintain lists or their own pointers. It is important to be able to emulate cleanup functions - something I discovered early in the worktree functions when released. I need to make sure that cleanup will continue to have enough information - prior to git worktree cleanup - to function correctly. This will need coordination with people who have such scripts in my community. It probably will not impact you, but I would have appreciated more than one release notice on this capability.

I'm not sure I understand the specific use-case you're talking about. Could you provide an example?

However, I suppose I can add a config / env variable to be able to disable this new functionality.

Best,

Caleb

#12 lore raw
Quoted 17 lines · Caleb White
Quoted 6 lines · rsbecker
Quoted 5 lines · Caleb White

If the develop directory is deleted, cleanup detection is handled by the git worktree prune command, which will remove worktrees under .git/worktrees/* that are no longer valid. This happens automatically after the expiry time or it can be executed manually. Of course, executing git worktree remove develop will also remove the worktree

and its associated worktree id.

Quoted 9 lines

This last bit is an assumption, and not necessarily valid. Scripts that use worktrees may maintain lists or their own pointers. It is important to be able to emulate cleanup functions - something I discovered early in the worktree functions when released. I need to make sure that cleanup will continue to have enough information - prior to git worktree cleanup - to function correctly. This will need coordination with people who have such scripts in my community. It probably will not impact you, but I would have appreciated more than one release

Quoted 4 lines

notice on this capability.

I'm not sure I understand the specific use-case you're talking about. Could you provide an example?

Speaking as a professional product manager...

I'm not expressing "maintaining compatibility for 2 releases" or something like that is a reasonable use case. There are customers who depend on things working in a particular way. It is fine if you want to change it and improve it, and I am supportive. However, when making a change that causes git to behave differently without allowing people to plan for such a change is impolite. People outside this list do not read each patch looking for compatibility breaking changes - they only get told in release notes. A statement like "this is going to change with 2.49" for a breaking enhancement is what I would expect - unless it is a defect correction.

Quoted 2 lines

However, I suppose I can add a config / env variable to be able to disable this new functionality.

That would be very helpful although an opt-in is generally better than an opt-out.

I think we should have this as a general policy, not just for this series.

Thanks,
Randall

#13 lore raw
In reply to Caleb White
Quoted 1 line · Caleb White

- Add the worktree id to worktree list output

I have always thought that we deliberately hid the "ID" from the end user's view because it is a mere implementation detail, and used the filesystem path of the worktree directory instead to identify each instance of the worktree in the end-user interaction. It is unclear why this change is a good idea from that point of view.

Thanks.

#14 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 1 line · Caleb White

- Add the worktree id to worktree list output

I have always thought that we deliberately hid the "ID" from the end user's view because it is a mere implementation detail, and used the filesystem path of the worktree directory instead to identify each instance of the worktree in the end-user interaction. It is unclear why this change is a good idea from that point of view.

I have a discussion with Caleb in the first version. Because appending a hash / random number will cause the worktree id has more digits, the user cannot easily use the following commands to make a ref in the main worktree point to linked worktree ref:

    ```sh
    git symbolic-ref refs/heads/foo \
        worktrees/<worktree id>/refs/worktree/foo
    ```

And I expressed my concern about above situation, the user types the extra hash to do above. So, Caleb decides to list the worktree id.

Actually, this usage should not be common. But when implementing the consistency check for files backend, you have told me that the above situation could happen. And you have said we _deliberately_ hide the "ID" from the end user's view.

But cross-ref operations between worktrees must explicitly specify the worktree id, so I am wondering whether we should allow the user do cross-ref operations in the first place:

1. main worktree symref points to linked worktree ref.
2. A linked worktree ref points to another linked worktree ref.

Thanks,
Jialuo

#15 lore raw
Quoted 6 lines · shejialuo

But cross-ref operations between worktrees must explicitly specify the worktree id, so I am wondering whether we should allow the user do cross-ref operations in the first place:

1. main worktree symref points to linked worktree ref.
2. A linked worktree ref points to another linked worktree ref.

What is a cross-ref operation? A worktree is either the primary working tree for a (non-bare) repository, or something added with "git add worktree" (i.e. whose .git is not the repository but a link file into the real repository). Are you adding another mode where a worktree points at another worktree and not the repository?

#16 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 4 lines · shejialuo

But cross-ref operations between worktrees must explicitly specify the worktree id, so I am wondering whether we should allow the user do cross-ref operations in the first place:

Quoted 8 lines
Quoted 2 lines

1. main worktree symref points to linked worktree ref.
2. A linked worktree ref points to another linked worktree ref.

What is a cross-ref operation? A worktree is either the primary working tree for a (non-bare) repository, or something added with "git add worktree" (i.e. whose .git is not the repository but a link file into the real repository). Are you adding another mode where a worktree points at another worktree and not the repository?

Unfortunately, I haven't had time lately to follow all these worktree-related changes or discussions, but perhaps shejialuo is referring to the ability, from within one worktree, to mention a ref from another worktree (which is a feature Duy added some time after the initial worktree implementation). Specifically, from the git-worktree man page:

Refs that are per-worktree can still be accessed from another worktree via two special paths, main-worktree and worktrees. The former gives access to per-worktree refs of the main worktree, while the latter to all linked worktrees.

For example, main-worktree/HEAD or main-worktree/refs/bisect/good resolve to the same value as the main worktree’s HEAD and refs/bisect/good respectively. Similarly, worktrees/foo/HEAD or worktrees/bar/refs/bisect/bad are the same as $GIT_COMMON_DIR/worktrees/foo/HEAD and $GIT_COMMON_DIR/worktrees/bar/refs/bisect/bad.

#17 lore raw
Quoted 5 lines · Eric Sunshine

Unfortunately, I haven't had time lately to follow all these worktree-related changes or discussions, but perhaps shejialuo is referring to the ability, from within one worktree, to mention a ref from another worktree (which is a feature Duy added some time after the initial worktree implementation).

Ah, yes, that exposes (and has to expose) the worktree ID. It still does not have to be unique across repositories (only has to unique among the worktrees that share the same single repository).

Thanks.

#18 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 5 lines · Eric Sunshine

Unfortunately, I haven't had time lately to follow all these worktree-related changes or discussions, but perhaps shejialuo is referring to the ability, from within one worktree, to mention a ref from another worktree (which is a feature Duy added some time after the initial worktree implementation).

Ah, yes, that exposes (and has to expose) the worktree ID. It still does

not have to

Quoted 1 line

be unique across repositories (only has to unique among the worktrees that

share

Quoted 1 line

the same single repository).

I might be mistaken, but I think the intent of the worktree series being discussed
deliberately wanted the worktree ID to be globally unique on a specific machine.
I might be wrong (hoping I am). The original author should comment on this.

#19 lore raw
Quoted 10 lines · <[email protected]> writes:
Quoted 1 line

Ah, yes, that exposes (and has to expose) the worktree ID. It still does

not have to

Quoted 1 line

be unique across repositories (only has to unique among the worktrees that

share

Quoted 1 line

the same single repository).

I might be mistaken, but I think the intent of the worktree series being discussed
deliberately wanted the worktree ID to be globally unique on a specific machine.

That is my understanding, but I do not understand why such a uniqueness is needed. Repositories are not even aware of other repositories, in any sense to make it matter to know worktree IDs other repositories are using. Until there is an attempt to link a worktree that used to belong to a repository to a different repository, that is. At that time, names must be made unique among worktrees that belong to the adopting repository, of course, but the names used in the original repository for its worktrees would not matter at that point, I would think.

Quoted 1 line

I might be wrong (hoping I am). The original author should comment on this.

Sure. Thanks for commenting.

#20 lore raw
Quoted 13 lines · Junio C Hamano
Quoted 10 lines · <[email protected]> writes:
Quoted 1 line

Ah, yes, that exposes (and has to expose) the worktree ID. It still does

not have to

Quoted 1 line

be unique across repositories (only has to unique among the worktrees that

share

Quoted 1 line

the same single repository).

I might be mistaken, but I think the intent of the worktree series being discussed
deliberately wanted the worktree ID to be globally unique on a specific machine.

Quoted 9 lines

That is my understanding, but I do not understand why such a uniqueness is needed. Repositories are not even aware of other repositories, in any sense to make it matter to know worktree IDs other repositories are using. Until there is an attempt to link a worktree that used to belong to a repository to a different repository, that is. At that time, names must be made unique among worktrees that belong to the adopting repository, of course, but the names used in the original repository for its worktrees would not matter at that point, I would think.

Perhaps I should've have come up with a better series name, I think there's been a lot of hang-up with the term "unique". When I refer to uniqueness in this context, I'm not advocating for strict, absolute uniqueness in the sense of ensuring no collisions under any conceivable circumstance, or requiring that repositories are now aware of other repositories. Instead, I'm discussing uniqueness from a practical
perspective: the combination of a random 32-bit integer from a CSPRNG with a worktree name should be "unique" for all intents and purposes. The theoretical risk of a collision does exist, of course, but the probability is astronomically lower than the current approach, rendering it effectively "unique" in practice.

You're correct in that the worktree ids are only relevant within the context of a single repository. However, I've already demonstrated that it's possible for a repository to "repair" (i.e., take over) a worktree belonging to another repository if the ids match (inferred backlink). In my experience, there's some pretty common names for worktrees (e.g., "main", "master", "develop", "hotfix", etc.), and it's not uncommon for multiple repositories to have worktrees with the same name. This can be avoided entirely by introducing some randomness into the worktree id and significantly reducing the probability of a collision (e.g., one repository would have a develop-54678976 id while another would have a develop-987465246 id), which is the primary motivation behind this series.

As I've mentioned earlier, the concept of a suffix is not new and should not be a breaking change. It's already possible to have worktrees with a different id from the public worktree directory name, so users and scripts should not just assume them to be the same (this is buggy behavior), but instead should be querying the worktree id from the .git file or git rev-parse --git-dir if they really need it (very rare). As part of this series I did add the worktree id to the worktree list output to make it easier for scripts to query this information if they do need it.

Perhaps this "take-over" scenario is not that big of a concern in practice, I just noted that this behavior was made possible in the es/worktree-repair-copied topic and I thought it was worth addressing. If it's decided that this is not that big of a concern, then I suppose this series can be dropped (although I've made some other QoL improvements that may be useful).

Best,

Caleb

#21 lore raw
Quoted 4 lines · Caleb White

You're correct in that the worktree ids are only relevant within the context of a single repository. However, I've already demonstrated that it's possible for a repository to "repair" (i.e., take over) a worktree belonging to another repository if the ids match (inferred backlink).

I know. But isn't that a BUG in the code that "repair"s? If a worktree had a name 'develop' that was OK in the context of repository X, and when you "repair" things so that it becomes one of the worktrees of a different repository Y, the "repair" operation is what MUST make sure that the worktree that used to be known as 'develop' to repository X does not interfere any existing worktrees that is attached to the repository Y. If the repository Y already had a worktree called 'develop', the "repair" operation must make sure that the newly adopted worktree would get a different name.

But then, the concern is exactly the same when you try to create a new worktree (no "repair" involved) in repository Y and try to give it a name 'develop', isn't it? You have to make sure that there is no worktree that is called 'develop' in the repository Y before giving it the name. Is it broken? If not, what are we doing to make sure we won't give the name 'develop' to the new worktree? Certainly we do not use any hash or random number for that, so why does this new series need to use a random number?

#22 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Caleb White

You're correct in that the worktree ids are only relevant within the context of a single repository. However, I've already demonstrated that it's possible for a repository to "repair" (i.e., take over) a worktree belonging to another repository if the ids match (inferred backlink).

Quoted 9 lines

I know. But isn't that a BUG in the code that "repair"s? If a worktree had a name 'develop' that was OK in the context of repository X, and when you "repair" things so that it becomes one of the worktrees of a different repository Y, the "repair" operation is what MUST make sure that the worktree that used to be known as 'develop' to repository X does not interfere any existing worktrees that is attached to the repository Y. If the repository Y already had a worktree called 'develop', the "repair" operation must make sure that the newly adopted worktree would get a different name.

No, this is incorrect---there should be no reason to "repair" a worktree from another repository in the first place. That would be undefined behavior and is indicative of a user mistake or unintentional repair (e.g., it would make no sense for the php-src repository to repair a worktree from this git repository). The repair operation is only intended to be used with worktrees of the same repository.

Quoted 8 lines

But then, the concern is exactly the same when you try to create a new worktree (no "repair" involved) in repository Y and try to give it a name 'develop', isn't it? You have to make sure that there is no worktree that is called 'develop' in the repository Y before giving it the name. Is it broken? If not, what are we doing to make sure we won't give the name 'develop' to the new worktree? Certainly we do not use any hash or random number for that, so why does this new series need to use a random number?

We currently suffix an auto incrementing number in this use case, so you can have two develop worktrees (located in different directories of course), and one will have an id of develop and the other will have an id of develop1.

Best,

#23 lore raw
In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · shejialuo

But cross-ref operations between worktrees must explicitly specify the worktree id, so I am wondering whether we should allow the user do cross-ref operations in the first place:

Quoted 8 lines
Quoted 2 lines

1. main worktree symref points to linked worktree ref.
2. A linked worktree ref points to another linked worktree ref.

What is a cross-ref operation? A worktree is either the primary working tree for a (non-bare) repository, or something added with "git add worktree" (i.e. whose .git is not the repository but a link file into the real repository). Are you adding another mode where a worktree points at another worktree and not the repository?

I am sorry that my words may confuse you here. And the Eric has already explained what I mean here. At current, we have the ability to mention a ref another worktree within the current worktree. You also have tole me that in [1], there is a possibility that the user could create a symbolic link to some worktree-specific ref in another worktree.

So, if our intention is to deliberately hide the worktree id. Why we allow such ability? Let me give an example.

If we are in the main-worktree, and we want to access the worktree specified refs, we must specify the worktree id like the following (also if we are in the linked-worktree, we want to access another linked-worktree refs):

worktrees/<worktree id>/refs/worktree/foo

We do not want to the user know the worktree id. However, we allow above. This is something I feel really strange during the review process. To the front-end user, the worktree path is the interface. However, for above ability, we need the user to explicitly specify the worktree id.

From my perspective, this is not a good design which is against our design.

[1] https://lore.kernel.org/git/[email protected]/