git/t/t1415-worktree-refs.sh

117 lines
3.7 KiB
Bash
Raw Normal View History

#!/bin/sh
test_description='per-worktree refs'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success 'setup' '
test_commit initial &&
test_commit wt1 &&
test_commit wt2 &&
git worktree add wt1 wt1 &&
git worktree add wt2 wt2 &&
git checkout initial &&
git update-ref refs/worktree/foo HEAD &&
git -C wt1 update-ref refs/worktree/foo HEAD &&
git -C wt2 update-ref refs/worktree/foo HEAD
'
# The 'packed-refs' file is stored directly in .git/. This means it is global
# to the repository, and can only contain refs that are shared across all
# worktrees.
test_expect_success REFFILES '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_expect_success 'refs/worktree are per-worktree' '
test_cmp_rev worktree/foo initial &&
( cd wt1 && test_cmp_rev worktree/foo wt1 ) &&
( cd wt2 && test_cmp_rev worktree/foo wt2 )
'
refs: new ref types to make per-worktree refs visible to all worktrees One of the problems with multiple worktree is accessing per-worktree refs of one worktree from another worktree. This was sort of solved by multiple ref store, where the code can open the ref store of another worktree and has access to the ref space of that worktree. The problem with this is reporting. "HEAD" in another ref space is also called "HEAD" like in the current ref space. In order to differentiate them, all the code must somehow carry the ref store around and print something like "HEAD from this ref store". But that is not feasible (or possible with a _lot_ of work). With the current design, we pass a reference around as a string (so called "refname"). Extending this design to pass a string _and_ a ref store is a nightmare, especially when handling extended SHA-1 syntax. So we do it another way. Instead of entering a separate ref space, we make refs from other worktrees available in the current ref space. So "HEAD" is always HEAD of the current worktree, but then we can have "worktrees/blah/HEAD" to denote HEAD from a worktree named "blah". This syntax coincidentally matches the underlying directory structure which makes implementation a bit easier. The main worktree has to be treated specially because well... it's special from the beginning. So HEAD from the main worktree is acccessible via the name "main-worktree/HEAD" instead of "worktrees/main/HEAD" because "main" could be just another secondary worktree. This patch also makes it possible to specify refs from one worktree in another one, e.g. git log worktrees/foo/HEAD Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-10-21 08:08:54 +00:00
test_expect_success 'resolve main-worktree/HEAD' '
test_cmp_rev main-worktree/HEAD initial &&
( cd wt1 && test_cmp_rev main-worktree/HEAD initial ) &&
( cd wt2 && test_cmp_rev main-worktree/HEAD initial )
'
test_expect_success 'ambiguous main-worktree/HEAD' '
test_when_finished git update-ref -d refs/heads/main-worktree/HEAD &&
git update-ref refs/heads/main-worktree/HEAD $(git rev-parse HEAD) &&
refs: new ref types to make per-worktree refs visible to all worktrees One of the problems with multiple worktree is accessing per-worktree refs of one worktree from another worktree. This was sort of solved by multiple ref store, where the code can open the ref store of another worktree and has access to the ref space of that worktree. The problem with this is reporting. "HEAD" in another ref space is also called "HEAD" like in the current ref space. In order to differentiate them, all the code must somehow carry the ref store around and print something like "HEAD from this ref store". But that is not feasible (or possible with a _lot_ of work). With the current design, we pass a reference around as a string (so called "refname"). Extending this design to pass a string _and_ a ref store is a nightmare, especially when handling extended SHA-1 syntax. So we do it another way. Instead of entering a separate ref space, we make refs from other worktrees available in the current ref space. So "HEAD" is always HEAD of the current worktree, but then we can have "worktrees/blah/HEAD" to denote HEAD from a worktree named "blah". This syntax coincidentally matches the underlying directory structure which makes implementation a bit easier. The main worktree has to be treated specially because well... it's special from the beginning. So HEAD from the main worktree is acccessible via the name "main-worktree/HEAD" instead of "worktrees/main/HEAD" because "main" could be just another secondary worktree. This patch also makes it possible to specify refs from one worktree in another one, e.g. git log worktrees/foo/HEAD Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-10-21 08:08:54 +00:00
git rev-parse main-worktree/HEAD 2>warn &&
grep "main-worktree/HEAD.*ambiguous" warn
'
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_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 &&
refs: new ref types to make per-worktree refs visible to all worktrees One of the problems with multiple worktree is accessing per-worktree refs of one worktree from another worktree. This was sort of solved by multiple ref store, where the code can open the ref store of another worktree and has access to the ref space of that worktree. The problem with this is reporting. "HEAD" in another ref space is also called "HEAD" like in the current ref space. In order to differentiate them, all the code must somehow carry the ref store around and print something like "HEAD from this ref store". But that is not feasible (or possible with a _lot_ of work). With the current design, we pass a reference around as a string (so called "refname"). Extending this design to pass a string _and_ a ref store is a nightmare, especially when handling extended SHA-1 syntax. So we do it another way. Instead of entering a separate ref space, we make refs from other worktrees available in the current ref space. So "HEAD" is always HEAD of the current worktree, but then we can have "worktrees/blah/HEAD" to denote HEAD from a worktree named "blah". This syntax coincidentally matches the underlying directory structure which makes implementation a bit easier. The main worktree has to be treated specially because well... it's special from the beginning. So HEAD from the main worktree is acccessible via the name "main-worktree/HEAD" instead of "worktrees/main/HEAD" because "main" could be just another secondary worktree. This patch also makes it possible to specify refs from one worktree in another one, e.g. git log worktrees/foo/HEAD Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-10-21 08:08:54 +00:00
git rev-parse worktrees/wt1/HEAD 2>warn &&
grep "worktrees/wt1/HEAD.*ambiguous" warn
'
test_expect_success 'reflog of main-worktree/HEAD' '
git reflog HEAD | sed "s/HEAD/main-worktree\/HEAD/" >expected &&
git reflog main-worktree/HEAD >actual &&
test_cmp expected actual &&
git -C wt1 reflog main-worktree/HEAD >actual.wt1 &&
test_cmp expected actual.wt1
'
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 &&
test_cmp expected actual &&
git -C wt1 reflog worktrees/wt2/HEAD >actual.wt1 &&
test_cmp expected actual.wt1 &&
git -C wt2 reflog worktrees/wt2/HEAD >actual.wt2 &&
test_cmp expected actual.wt2
'
test_expect_success 'for-each-ref from main worktree' '
mkdir fer1 &&
git -C fer1 init repo &&
test_commit -C fer1/repo initial &&
git -C fer1/repo worktree add ../second &&
git -C fer1/repo update-ref refs/bisect/first HEAD &&
git -C fer1/repo update-ref refs/rewritten/first HEAD &&
git -C fer1/repo update-ref refs/worktree/first HEAD &&
git -C fer1/repo for-each-ref --format="%(refname)" | grep first >actual &&
cat >expected <<-\EOF &&
refs/bisect/first
refs/rewritten/first
refs/worktree/first
EOF
test_cmp expected actual
'
test_expect_success 'for-each-ref from linked worktree' '
mkdir fer2 &&
git -C fer2 init repo &&
test_commit -C fer2/repo initial &&
git -C fer2/repo worktree add ../second &&
git -C fer2/second update-ref refs/bisect/second HEAD &&
git -C fer2/second update-ref refs/rewritten/second HEAD &&
git -C fer2/second update-ref refs/worktree/second HEAD &&
git -C fer2/second for-each-ref --format="%(refname)" | grep second >actual &&
cat >expected <<-\EOF &&
refs/bisect/second
refs/heads/second
refs/rewritten/second
refs/worktree/second
EOF
test_cmp expected actual
'
test_done