2014-11-30 08:24:47 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_description='test git worktree add'
|
2014-11-30 08:24:47 +00:00
|
|
|
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
2016-04-22 13:01:33 +00:00
|
|
|
. "$TEST_DIRECTORY"/lib-rebase.sh
|
|
|
|
|
2014-11-30 08:24:47 +00:00
|
|
|
test_expect_success 'setup' '
|
|
|
|
test_commit init
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" an existing worktree' '
|
2014-11-30 08:24:58 +00:00
|
|
|
mkdir -p existing/subtree &&
|
2015-07-06 17:30:54 +00:00
|
|
|
test_must_fail git worktree add --detach existing master
|
2014-11-30 08:24:47 +00:00
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" an existing empty worktree' '
|
2014-11-30 08:24:58 +00:00
|
|
|
mkdir existing_empty &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add --detach existing_empty master
|
2014-11-30 08:24:58 +00:00
|
|
|
'
|
|
|
|
|
2016-05-27 13:17:08 +00:00
|
|
|
test_expect_success '"add" using shorthand - fails when no previous branch' '
|
|
|
|
test_must_fail git worktree add existing_short -
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" using - shorthand' '
|
|
|
|
git checkout -b newbranch &&
|
|
|
|
echo hello >myworld &&
|
|
|
|
git add myworld &&
|
|
|
|
git commit -m myworld &&
|
|
|
|
git checkout master &&
|
|
|
|
git worktree add short-hand - &&
|
|
|
|
echo refs/heads/newbranch >expect &&
|
|
|
|
git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" refuses to checkout locked branch' '
|
|
|
|
test_must_fail git worktree add zere master &&
|
2014-11-30 08:24:50 +00:00
|
|
|
! test -d zere &&
|
|
|
|
! test -d .git/worktrees/zere
|
|
|
|
'
|
|
|
|
|
2015-06-12 10:49:24 +00:00
|
|
|
test_expect_success 'checking out paths not complaining about linked checkouts' '
|
|
|
|
(
|
|
|
|
cd existing_empty &&
|
|
|
|
echo dirty >>init.t &&
|
|
|
|
git checkout master -- init.t
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" worktree' '
|
2014-11-30 08:24:49 +00:00
|
|
|
git rev-parse HEAD >expect &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add --detach here master &&
|
2014-11-30 08:24:47 +00:00
|
|
|
(
|
|
|
|
cd here &&
|
|
|
|
test_cmp ../init.t init.t &&
|
2014-11-30 08:24:49 +00:00
|
|
|
test_must_fail git symbolic-ref HEAD &&
|
|
|
|
git rev-parse HEAD >actual &&
|
|
|
|
test_cmp ../expect actual &&
|
2014-11-30 08:24:47 +00:00
|
|
|
git fsck
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2017-04-12 13:58:05 +00:00
|
|
|
test_expect_success '"add" worktree with lock' '
|
|
|
|
git rev-parse HEAD >expect &&
|
|
|
|
git worktree add --detach --lock here-with-lock master &&
|
|
|
|
test -f .git/worktrees/here-with-lock/locked
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" worktree from a subdir' '
|
2014-11-30 08:24:47 +00:00
|
|
|
(
|
|
|
|
mkdir sub &&
|
|
|
|
cd sub &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add --detach here master &&
|
2014-11-30 08:24:47 +00:00
|
|
|
cd here &&
|
|
|
|
test_cmp ../../init.t init.t
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" from a linked checkout' '
|
2014-11-30 08:24:47 +00:00
|
|
|
(
|
|
|
|
cd here &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add --detach nested-here master &&
|
2014-11-30 08:24:47 +00:00
|
|
|
cd nested-here &&
|
|
|
|
git fsck
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" worktree creating new branch' '
|
|
|
|
git worktree add -b newmaster there master &&
|
2014-11-30 08:24:47 +00:00
|
|
|
(
|
|
|
|
cd there &&
|
|
|
|
test_cmp ../init.t init.t &&
|
|
|
|
git symbolic-ref HEAD >actual &&
|
|
|
|
echo refs/heads/newmaster >expect &&
|
|
|
|
test_cmp expect actual &&
|
|
|
|
git fsck
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2014-11-30 08:24:49 +00:00
|
|
|
test_expect_success 'die the same branch is already checked out' '
|
|
|
|
(
|
|
|
|
cd here &&
|
|
|
|
test_must_fail git checkout newmaster
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-07-17 23:00:03 +00:00
|
|
|
test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
|
|
|
|
head=$(git -C there rev-parse --git-path HEAD) &&
|
|
|
|
ref=$(git -C there symbolic-ref HEAD) &&
|
|
|
|
rm "$head" &&
|
|
|
|
ln -s "$ref" "$head" &&
|
|
|
|
test_must_fail git -C here checkout newmaster
|
|
|
|
'
|
|
|
|
|
2015-01-03 09:41:26 +00:00
|
|
|
test_expect_success 'not die the same branch is already checked out' '
|
|
|
|
(
|
|
|
|
cd here &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add --force anothernewmaster newmaster
|
2015-01-03 09:41:26 +00:00
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2014-11-30 08:24:49 +00:00
|
|
|
test_expect_success 'not die on re-checking out current branch' '
|
|
|
|
(
|
|
|
|
cd there &&
|
|
|
|
git checkout newmaster
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" from a bare repo' '
|
2014-11-30 08:24:56 +00:00
|
|
|
(
|
|
|
|
git clone --bare . bare &&
|
|
|
|
cd bare &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add -b bare-master ../there2 master
|
2014-11-30 08:24:56 +00:00
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success 'checkout from a bare repo without "add"' '
|
2014-11-30 08:24:56 +00:00
|
|
|
(
|
|
|
|
cd bare &&
|
|
|
|
test_must_fail git checkout master
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2016-10-12 16:41:07 +00:00
|
|
|
test_expect_success '"add" default branch of a bare repo' '
|
|
|
|
(
|
|
|
|
git clone --bare . bare2 &&
|
|
|
|
cd bare2 &&
|
|
|
|
git worktree add ../there3 master
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2014-11-30 08:24:57 +00:00
|
|
|
test_expect_success 'checkout with grafts' '
|
|
|
|
test_when_finished rm .git/info/grafts &&
|
|
|
|
test_commit abc &&
|
2015-12-22 15:05:52 +00:00
|
|
|
SHA1=$(git rev-parse HEAD) &&
|
2014-11-30 08:24:57 +00:00
|
|
|
test_commit def &&
|
|
|
|
test_commit xyz &&
|
2015-12-22 15:05:52 +00:00
|
|
|
echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
|
2014-11-30 08:24:57 +00:00
|
|
|
cat >expected <<-\EOF &&
|
|
|
|
xyz
|
|
|
|
abc
|
|
|
|
EOF
|
|
|
|
git log --format=%s -2 >actual &&
|
|
|
|
test_cmp expected actual &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add --detach grafted master &&
|
2014-11-30 08:24:57 +00:00
|
|
|
git --git-dir=grafted/.git log --format=%s -2 >actual &&
|
|
|
|
test_cmp expected actual
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:54 +00:00
|
|
|
test_expect_success '"add" from relative HEAD' '
|
2015-07-06 17:30:45 +00:00
|
|
|
test_commit a &&
|
|
|
|
test_commit b &&
|
|
|
|
test_commit c &&
|
|
|
|
git rev-parse HEAD~1 >expected &&
|
2015-07-06 17:30:54 +00:00
|
|
|
git worktree add relhead HEAD~1 &&
|
2015-07-06 17:30:45 +00:00
|
|
|
git -C relhead rev-parse HEAD >actual &&
|
|
|
|
test_cmp expected actual
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:58 +00:00
|
|
|
test_expect_success '"add -b" with <branch> omitted' '
|
|
|
|
git worktree add -b burble flornk &&
|
|
|
|
test_cmp_rev HEAD burble
|
|
|
|
'
|
|
|
|
|
2015-07-17 23:00:09 +00:00
|
|
|
test_expect_success '"add --detach" with <branch> omitted' '
|
|
|
|
git worktree add --detach fishhook &&
|
|
|
|
git rev-parse HEAD >expected &&
|
|
|
|
git -C fishhook rev-parse HEAD >actual &&
|
|
|
|
test_cmp expected actual &&
|
|
|
|
test_must_fail git -C fishhook symbolic-ref HEAD
|
|
|
|
'
|
|
|
|
|
2015-07-06 17:30:59 +00:00
|
|
|
test_expect_success '"add" with <branch> omitted' '
|
|
|
|
git worktree add wiffle/bat &&
|
|
|
|
test_cmp_rev HEAD bat
|
|
|
|
'
|
|
|
|
|
2018-04-24 21:56:35 +00:00
|
|
|
test_expect_success '"add" checks out existing branch of dwimd name' '
|
|
|
|
git branch dwim HEAD~1 &&
|
|
|
|
git worktree add dwim &&
|
|
|
|
test_cmp_rev HEAD~1 dwim &&
|
|
|
|
(
|
|
|
|
cd dwim &&
|
|
|
|
test_cmp_rev HEAD dwim
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add <path>" dwim fails with checked out branch' '
|
|
|
|
git checkout -b test-branch &&
|
|
|
|
test_must_fail git worktree add test-branch &&
|
|
|
|
test_path_is_missing test-branch
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add --force" with existing dwimd name doesnt die' '
|
|
|
|
git checkout test-branch &&
|
|
|
|
git worktree add --force test-branch
|
2015-07-06 17:30:59 +00:00
|
|
|
'
|
|
|
|
|
2015-07-17 23:00:09 +00:00
|
|
|
test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
|
|
|
|
git worktree add --detach mish/mash &&
|
|
|
|
test_must_fail git rev-parse mash -- &&
|
|
|
|
test_must_fail git -C mish/mash symbolic-ref HEAD
|
|
|
|
'
|
|
|
|
|
2015-07-17 23:00:08 +00:00
|
|
|
test_expect_success '"add" -b/-B mutually exclusive' '
|
|
|
|
test_must_fail git worktree add -b poodle -B poodle bamboo master
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" -b/--detach mutually exclusive' '
|
|
|
|
test_must_fail git worktree add -b poodle --detach bamboo master
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" -B/--detach mutually exclusive' '
|
|
|
|
test_must_fail git worktree add -B poodle --detach bamboo master
|
|
|
|
'
|
|
|
|
|
2016-02-15 13:35:33 +00:00
|
|
|
test_expect_success '"add -B" fails if the branch is checked out' '
|
|
|
|
git rev-parse newmaster >before &&
|
|
|
|
test_must_fail git worktree add -B newmaster bamboo master &&
|
|
|
|
git rev-parse newmaster >after &&
|
|
|
|
test_cmp before after
|
|
|
|
'
|
|
|
|
|
2016-02-15 13:35:32 +00:00
|
|
|
test_expect_success 'add -B' '
|
|
|
|
git worktree add -B poodle bamboo2 master^ &&
|
|
|
|
git -C bamboo2 symbolic-ref HEAD >actual &&
|
|
|
|
echo refs/heads/poodle >expected &&
|
|
|
|
test_cmp expected actual &&
|
|
|
|
test_cmp_rev master^ poodle
|
|
|
|
'
|
|
|
|
|
2018-08-15 20:56:30 +00:00
|
|
|
test_expect_success 'add --quiet' '
|
|
|
|
git worktree add --quiet another-worktree master 2>actual &&
|
|
|
|
test_must_be_empty actual
|
|
|
|
'
|
|
|
|
|
2015-09-28 13:06:15 +00:00
|
|
|
test_expect_success 'local clone from linked checkout' '
|
|
|
|
git clone --local here here-clone &&
|
|
|
|
( cd here-clone && git fsck )
|
|
|
|
'
|
|
|
|
|
2017-12-11 23:16:12 +00:00
|
|
|
test_expect_success 'local clone --shared from linked checkout' '
|
|
|
|
git -C bare worktree add --detach ../baretree &&
|
|
|
|
git clone --local --shared baretree bare-clone &&
|
|
|
|
grep /bare/ bare-clone/.git/objects/info/alternates
|
|
|
|
'
|
|
|
|
|
2016-03-29 10:11:01 +00:00
|
|
|
test_expect_success '"add" worktree with --no-checkout' '
|
|
|
|
git worktree add --no-checkout -b swamp swamp &&
|
|
|
|
! test -e swamp/init.t &&
|
|
|
|
git -C swamp reset --hard &&
|
|
|
|
test_cmp init.t swamp/init.t
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" worktree with --checkout' '
|
|
|
|
git worktree add --checkout -b swmap2 swamp2 &&
|
|
|
|
test_cmp init.t swamp2/init.t
|
|
|
|
'
|
|
|
|
|
2016-04-22 13:01:33 +00:00
|
|
|
test_expect_success 'put a worktree under rebase' '
|
|
|
|
git worktree add under-rebase &&
|
|
|
|
(
|
|
|
|
cd under-rebase &&
|
|
|
|
set_fake_editor &&
|
|
|
|
FAKE_LINES="edit 1" git rebase -i HEAD^ &&
|
|
|
|
git worktree list | grep "under-rebase.*detached HEAD"
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'add a worktree, checking out a rebased branch' '
|
|
|
|
test_must_fail git worktree add new-rebase under-rebase &&
|
|
|
|
! test -d new-rebase
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checking out a rebased branch from another worktree' '
|
|
|
|
git worktree add new-place &&
|
|
|
|
test_must_fail git -C new-place checkout under-rebase
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'not allow to delete a branch under rebase' '
|
|
|
|
(
|
|
|
|
cd under-rebase &&
|
|
|
|
test_must_fail git branch -D under-rebase
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2016-04-22 13:01:36 +00:00
|
|
|
test_expect_success 'rename a branch under rebase not allowed' '
|
|
|
|
test_must_fail git branch -M under-rebase rebase-with-new-name
|
|
|
|
'
|
|
|
|
|
2016-04-22 13:01:33 +00:00
|
|
|
test_expect_success 'check out from current worktree branch ok' '
|
|
|
|
(
|
|
|
|
cd under-rebase &&
|
|
|
|
git checkout under-rebase &&
|
|
|
|
git checkout - &&
|
|
|
|
git rebase --abort
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2016-04-22 13:01:35 +00:00
|
|
|
test_expect_success 'checkout a branch under bisect' '
|
|
|
|
git worktree add under-bisect &&
|
|
|
|
(
|
|
|
|
cd under-bisect &&
|
|
|
|
git bisect start &&
|
|
|
|
git bisect bad &&
|
|
|
|
git bisect good HEAD~2 &&
|
|
|
|
git worktree list | grep "under-bisect.*detached HEAD" &&
|
|
|
|
test_must_fail git worktree add new-bisect under-bisect &&
|
|
|
|
! test -d new-bisect
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2016-04-22 13:01:36 +00:00
|
|
|
test_expect_success 'rename a branch under bisect not allowed' '
|
|
|
|
test_must_fail git branch -M under-bisect bisect-with-new-name
|
|
|
|
'
|
2017-11-26 19:43:53 +00:00
|
|
|
# Is branch "refs/heads/$1" set to pull from "$2/$3"?
|
|
|
|
test_branch_upstream () {
|
|
|
|
printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
|
|
|
|
{
|
|
|
|
git config "branch.$1.remote" &&
|
|
|
|
git config "branch.$1.merge"
|
|
|
|
} >actual.upstream &&
|
|
|
|
test_cmp expect.upstream actual.upstream
|
|
|
|
}
|
|
|
|
|
|
|
|
test_expect_success '--track sets up tracking' '
|
|
|
|
test_when_finished rm -rf track &&
|
|
|
|
git worktree add --track -b track track master &&
|
|
|
|
test_branch_upstream track . master
|
|
|
|
'
|
|
|
|
|
|
|
|
# setup remote repository $1 and repository $2 with $1 set up as
|
|
|
|
# remote. The remote has two branches, master and foo.
|
|
|
|
setup_remote_repo () {
|
|
|
|
git init $1 &&
|
|
|
|
(
|
|
|
|
cd $1 &&
|
|
|
|
test_commit $1_master &&
|
|
|
|
git checkout -b foo &&
|
|
|
|
test_commit upstream_foo
|
|
|
|
) &&
|
|
|
|
git init $2 &&
|
|
|
|
(
|
|
|
|
cd $2 &&
|
|
|
|
test_commit $2_master &&
|
|
|
|
git remote add $1 ../$1 &&
|
|
|
|
git config remote.$1.fetch \
|
|
|
|
"refs/heads/*:refs/remotes/$1/*" &&
|
|
|
|
git fetch --all
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
test_expect_success '--no-track avoids setting up tracking' '
|
|
|
|
test_when_finished rm -rf repo_upstream repo_local foo &&
|
|
|
|
setup_remote_repo repo_upstream repo_local &&
|
|
|
|
(
|
|
|
|
cd repo_local &&
|
|
|
|
git worktree add --no-track -b foo ../foo repo_upstream/foo
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
cd foo &&
|
|
|
|
test_must_fail git config "branch.foo.remote" &&
|
|
|
|
test_must_fail git config "branch.foo.merge" &&
|
|
|
|
test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
|
|
|
|
)
|
|
|
|
'
|
2016-04-22 13:01:36 +00:00
|
|
|
|
2017-11-26 19:43:54 +00:00
|
|
|
test_expect_success '"add" <path> <non-existent-branch> fails' '
|
|
|
|
test_must_fail git worktree add foo non-existent
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" <path> <branch> dwims' '
|
|
|
|
test_when_finished rm -rf repo_upstream repo_dwim foo &&
|
|
|
|
setup_remote_repo repo_upstream repo_dwim &&
|
|
|
|
git init repo_dwim &&
|
|
|
|
(
|
|
|
|
cd repo_dwim &&
|
|
|
|
git worktree add ../foo foo
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
cd foo &&
|
|
|
|
test_branch_upstream foo repo_upstream foo &&
|
|
|
|
test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
checkout & worktree: introduce checkout.defaultRemote
Introduce a checkout.defaultRemote setting which can be used to
designate a remote to prefer (via checkout.defaultRemote=origin) when
running e.g. "git checkout master" to mean origin/master, even though
there's other remotes that have the "master" branch.
I want this because it's very handy to use this workflow to checkout a
repository and create a topic branch, then get back to a "master" as
retrieved from upstream:
(
cd /tmp &&
rm -rf tbdiff &&
git clone git@github.com:trast/tbdiff.git &&
cd tbdiff &&
git branch -m topic &&
git checkout master
)
That will output:
Branch 'master' set up to track remote branch 'master' from 'origin'.
Switched to a new branch 'master'
But as soon as a new remote is added (e.g. just to inspect something
from someone else) the DWIMery goes away:
(
cd /tmp &&
rm -rf tbdiff &&
git clone git@github.com:trast/tbdiff.git &&
cd tbdiff &&
git branch -m topic &&
git remote add avar git@github.com:avar/tbdiff.git &&
git fetch avar &&
git checkout master
)
Will output (without the advice output added earlier in this series):
error: pathspec 'master' did not match any file(s) known to git.
The new checkout.defaultRemote config allows me to say that whenever
that ambiguity comes up I'd like to prefer "origin", and it'll still
work as though the only remote I had was "origin".
Also adjust the advice.checkoutAmbiguousRemoteBranchName message to
mention this new config setting to the user, the full output on my
git.git is now (the last paragraph is new):
$ ./git --exec-path=$PWD checkout master
error: pathspec 'master' did not match any file(s) known to git.
hint: 'master' matched more than one remote tracking branch.
hint: We found 26 remotes with a reference that matched. So we fell back
hint: on trying to resolve the argument as a path, but failed there too!
hint:
hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
hint: you can do so by fully qualifying the name with the --track option:
hint:
hint: git checkout --track origin/<name>
hint:
hint: If you'd like to always have checkouts of an ambiguous <name> prefer
hint: one remote, e.g. the 'origin' remote, consider setting
hint: checkout.defaultRemote=origin in your config.
I considered splitting this into checkout.defaultRemote and
worktree.defaultRemote, but it's probably less confusing to break our
own rules that anything shared between config should live in core.*
than have two config settings, and I couldn't come up with a short
name under core.* that made sense (core.defaultRemoteForCheckout?).
See also 70c9ac2f19 ("DWIM "git checkout frotz" to "git checkout -b
frotz origin/frotz"", 2009-10-18) which introduced this DWIM feature
to begin with, and 4e85333197 ("worktree: make add <path> <branch>
dwim", 2017-11-26) which added it to git-worktree.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-06-05 14:40:49 +00:00
|
|
|
test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
|
|
|
|
test_when_finished rm -rf repo_upstream repo_dwim foo &&
|
|
|
|
setup_remote_repo repo_upstream repo_dwim &&
|
|
|
|
git init repo_dwim &&
|
|
|
|
(
|
|
|
|
cd repo_dwim &&
|
|
|
|
git remote add repo_upstream2 ../repo_upstream &&
|
|
|
|
git fetch repo_upstream2 &&
|
|
|
|
test_must_fail git worktree add ../foo foo &&
|
|
|
|
git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
|
|
|
|
git status -uno --porcelain >status.actual &&
|
2018-07-27 17:48:11 +00:00
|
|
|
test_must_be_empty status.actual
|
checkout & worktree: introduce checkout.defaultRemote
Introduce a checkout.defaultRemote setting which can be used to
designate a remote to prefer (via checkout.defaultRemote=origin) when
running e.g. "git checkout master" to mean origin/master, even though
there's other remotes that have the "master" branch.
I want this because it's very handy to use this workflow to checkout a
repository and create a topic branch, then get back to a "master" as
retrieved from upstream:
(
cd /tmp &&
rm -rf tbdiff &&
git clone git@github.com:trast/tbdiff.git &&
cd tbdiff &&
git branch -m topic &&
git checkout master
)
That will output:
Branch 'master' set up to track remote branch 'master' from 'origin'.
Switched to a new branch 'master'
But as soon as a new remote is added (e.g. just to inspect something
from someone else) the DWIMery goes away:
(
cd /tmp &&
rm -rf tbdiff &&
git clone git@github.com:trast/tbdiff.git &&
cd tbdiff &&
git branch -m topic &&
git remote add avar git@github.com:avar/tbdiff.git &&
git fetch avar &&
git checkout master
)
Will output (without the advice output added earlier in this series):
error: pathspec 'master' did not match any file(s) known to git.
The new checkout.defaultRemote config allows me to say that whenever
that ambiguity comes up I'd like to prefer "origin", and it'll still
work as though the only remote I had was "origin".
Also adjust the advice.checkoutAmbiguousRemoteBranchName message to
mention this new config setting to the user, the full output on my
git.git is now (the last paragraph is new):
$ ./git --exec-path=$PWD checkout master
error: pathspec 'master' did not match any file(s) known to git.
hint: 'master' matched more than one remote tracking branch.
hint: We found 26 remotes with a reference that matched. So we fell back
hint: on trying to resolve the argument as a path, but failed there too!
hint:
hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
hint: you can do so by fully qualifying the name with the --track option:
hint:
hint: git checkout --track origin/<name>
hint:
hint: If you'd like to always have checkouts of an ambiguous <name> prefer
hint: one remote, e.g. the 'origin' remote, consider setting
hint: checkout.defaultRemote=origin in your config.
I considered splitting this into checkout.defaultRemote and
worktree.defaultRemote, but it's probably less confusing to break our
own rules that anything shared between config should live in core.*
than have two config settings, and I couldn't come up with a short
name under core.* that made sense (core.defaultRemoteForCheckout?).
See also 70c9ac2f19 ("DWIM "git checkout frotz" to "git checkout -b
frotz origin/frotz"", 2009-10-18) which introduced this DWIM feature
to begin with, and 4e85333197 ("worktree: make add <path> <branch>
dwim", 2017-11-26) which added it to git-worktree.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-06-05 14:40:49 +00:00
|
|
|
) &&
|
|
|
|
(
|
|
|
|
cd foo &&
|
|
|
|
test_branch_upstream foo repo_upstream foo &&
|
|
|
|
test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2017-11-29 20:04:50 +00:00
|
|
|
test_expect_success 'git worktree add does not match remote' '
|
|
|
|
test_when_finished rm -rf repo_a repo_b foo &&
|
|
|
|
setup_remote_repo repo_a repo_b &&
|
|
|
|
(
|
|
|
|
cd repo_b &&
|
|
|
|
git worktree add ../foo
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
cd foo &&
|
|
|
|
test_must_fail git config "branch.foo.remote" &&
|
|
|
|
test_must_fail git config "branch.foo.merge" &&
|
|
|
|
! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'git worktree add --guess-remote sets up tracking' '
|
|
|
|
test_when_finished rm -rf repo_a repo_b foo &&
|
|
|
|
setup_remote_repo repo_a repo_b &&
|
|
|
|
(
|
|
|
|
cd repo_b &&
|
|
|
|
git worktree add --guess-remote ../foo
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
cd foo &&
|
|
|
|
test_branch_upstream foo repo_a foo &&
|
|
|
|
test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2017-11-29 20:04:51 +00:00
|
|
|
test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
|
|
|
|
test_when_finished rm -rf repo_a repo_b foo &&
|
|
|
|
setup_remote_repo repo_a repo_b &&
|
|
|
|
(
|
|
|
|
cd repo_b &&
|
|
|
|
git config worktree.guessRemote true &&
|
|
|
|
git worktree add ../foo
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
cd foo &&
|
|
|
|
test_branch_upstream foo repo_a foo &&
|
|
|
|
test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'git worktree --no-guess-remote option overrides config' '
|
|
|
|
test_when_finished rm -rf repo_a repo_b foo &&
|
|
|
|
setup_remote_repo repo_a repo_b &&
|
|
|
|
(
|
|
|
|
cd repo_b &&
|
|
|
|
git config worktree.guessRemote true &&
|
|
|
|
git worktree add --no-guess-remote ../foo
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
cd foo &&
|
|
|
|
test_must_fail git config "branch.foo.remote" &&
|
|
|
|
test_must_fail git config "branch.foo.merge" &&
|
|
|
|
! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2017-12-07 21:20:17 +00:00
|
|
|
post_checkout_hook () {
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
gitdir=${1:-.git}
|
|
|
|
test_when_finished "rm -f $gitdir/hooks/post-checkout" &&
|
|
|
|
mkdir -p $gitdir/hooks &&
|
|
|
|
write_script $gitdir/hooks/post-checkout <<-\EOF
|
|
|
|
{
|
|
|
|
echo $*
|
|
|
|
git rev-parse --git-dir --show-toplevel
|
|
|
|
} >hook.actual
|
2017-12-07 21:20:17 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
test_expect_success '"add" invokes post-checkout hook (branch)' '
|
|
|
|
post_checkout_hook &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
{
|
2018-05-13 02:24:13 +00:00
|
|
|
echo $ZERO_OID $(git rev-parse HEAD) 1 &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
echo $(pwd)/.git/worktrees/gumby &&
|
|
|
|
echo $(pwd)/gumby
|
|
|
|
} >hook.expect &&
|
2017-12-07 21:20:17 +00:00
|
|
|
git worktree add gumby &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
test_cmp hook.expect gumby/hook.actual
|
2017-12-07 21:20:17 +00:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" invokes post-checkout hook (detached)' '
|
|
|
|
post_checkout_hook &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
{
|
2018-05-13 02:24:13 +00:00
|
|
|
echo $ZERO_OID $(git rev-parse HEAD) 1 &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
echo $(pwd)/.git/worktrees/grumpy &&
|
|
|
|
echo $(pwd)/grumpy
|
|
|
|
} >hook.expect &&
|
2017-12-07 21:20:17 +00:00
|
|
|
git worktree add --detach grumpy &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
test_cmp hook.expect grumpy/hook.actual
|
2017-12-07 21:20:17 +00:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
|
|
|
|
post_checkout_hook &&
|
|
|
|
rm -f hook.actual &&
|
|
|
|
git worktree add --no-checkout gloopy &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
test_path_is_missing gloopy/hook.actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" in other worktree invokes post-checkout hook' '
|
|
|
|
post_checkout_hook &&
|
|
|
|
{
|
2018-05-13 02:24:13 +00:00
|
|
|
echo $ZERO_OID $(git rev-parse HEAD) 1 &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
echo $(pwd)/.git/worktrees/guppy &&
|
|
|
|
echo $(pwd)/guppy
|
|
|
|
} >hook.expect &&
|
|
|
|
git -C gloopy worktree add --detach ../guppy &&
|
|
|
|
test_cmp hook.expect guppy/hook.actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" in bare repo invokes post-checkout hook' '
|
|
|
|
rm -rf bare &&
|
|
|
|
git clone --bare . bare &&
|
|
|
|
{
|
2018-05-13 02:24:13 +00:00
|
|
|
echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
|
worktree: add: fix 'post-checkout' not knowing new worktree location
Although "git worktree add" learned to run the 'post-checkout' hook in
ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
neglected to change to the directory of the newly-created worktree
before running the hook. Instead, the hook runs within the directory
from which the "git worktree add" command itself was invoked, which
effectively neuters the hook since it knows nothing about the new
worktree directory.
Further, ade546be47 failed to sanitize the environment before running
the hook, which means that user-assigned values of GIT_DIR and
GIT_WORK_TREE could mislead the hook about the location of the new
worktree. In the case of "git worktree add" being run from a bare
repository, the GIT_DIR="." assigned by Git itself leaks into the hook's
environment and breaks Git commands; this is so even when the working
directory is correctly changed to the new worktree before the hook runs
since ".", relative to the new worktree directory, does not point at the
bare repository.
Fix these problems by (1) changing to the new worktree's directory
before running the hook, and (2) sanitizing the environment of GIT_DIR
and GIT_WORK_TREE so hooks can't be confused by misleading values.
Enhance the t2025 'post-checkout' tests to verify that the hook is
indeed run within the correct directory and that Git commands invoked by
the hook compute Git-dir and top-level worktree locations correctly.
While at it, also add two new tests: (1) verify that the hook is run
within the correct directory even when the new worktree is created from
a sibling worktree (as opposed to the main worktree); (2) verify that
the hook is provided with correct context when the new worktree is
created from a bare repository (test provided by Lars Schneider).
Implementation Notes:
Rather than sanitizing the environment of GIT_DIR and GIT_WORK_TREE, an
alternative would be to set them explicitly, as is already done for
other Git commands run internally by "git worktree add". This patch opts
instead to sanitize the environment in order to clearly document that
the worktree is fully functional by the time the hook is run, thus does
not require special environmental overrides.
The hook is run manually, rather than via run_hook_le(), since it needs
to change the working directory to that of the worktree, and
run_hook_le() does not provide such functionality. As this is a one-off
case, adding 'run_hook' overloads which allow the directory to be set
does not seem warranted at this time.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-15 19:18:41 +00:00
|
|
|
echo $(pwd)/bare/worktrees/goozy &&
|
|
|
|
echo $(pwd)/goozy
|
|
|
|
} >hook.expect &&
|
|
|
|
post_checkout_hook bare &&
|
|
|
|
git -C bare worktree add --detach ../goozy &&
|
|
|
|
test_cmp hook.expect goozy/hook.actual
|
2017-12-07 21:20:17 +00:00
|
|
|
'
|
|
|
|
|
2018-08-28 21:20:22 +00:00
|
|
|
test_expect_success '"add" an existing but missing worktree' '
|
|
|
|
git worktree add --detach pneu &&
|
|
|
|
test_must_fail git worktree add --detach pneu &&
|
|
|
|
rm -fr pneu &&
|
2018-08-28 21:20:23 +00:00
|
|
|
test_must_fail git worktree add --detach pneu &&
|
|
|
|
git worktree add --force --detach pneu
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success '"add" an existing locked but missing worktree' '
|
|
|
|
git worktree add --detach gnoo &&
|
|
|
|
git worktree lock gnoo &&
|
|
|
|
test_when_finished "git worktree unlock gnoo || :" &&
|
|
|
|
rm -fr gnoo &&
|
|
|
|
test_must_fail git worktree add --detach gnoo &&
|
|
|
|
test_must_fail git worktree add --force --detach gnoo &&
|
|
|
|
git worktree add --force --force --detach gnoo
|
2018-08-28 21:20:22 +00:00
|
|
|
'
|
|
|
|
|
2019-03-08 09:28:34 +00:00
|
|
|
test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
|
|
|
|
git worktree add --detach ". weird*..?.lock.lock" &&
|
|
|
|
test -d .git/worktrees/---weird-.-
|
|
|
|
'
|
|
|
|
|
2019-05-13 10:49:44 +00:00
|
|
|
test_expect_success '"add" should not fail because of another bad worktree' '
|
|
|
|
git init add-fail &&
|
|
|
|
(
|
|
|
|
cd add-fail &&
|
|
|
|
test_commit first &&
|
|
|
|
mkdir sub &&
|
|
|
|
git worktree add sub/to-be-deleted &&
|
|
|
|
rm -rf sub &&
|
|
|
|
git worktree add second
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2014-11-30 08:24:47 +00:00
|
|
|
test_done
|