checkout: do not imply "-f" on unborn branches
When checkout sees that HEAD points to a non-existent ref,
it currently acts as if "-f" was given; this behavior dates
back to 5a03e7f, which enabled checkout from unborn branches
in the shell version of "git-checkout". The reasoning given
is to avoid the code path which tries to merge the tree
contents. When checkout was converted to C, this code
remained intact.
The unfortunate side effect of this strategy is that the
"force" code path will overwrite working tree and index
state that may be precious to the user. Instead of enabling
"force", this patch uses the normal "merge" codepath for an
unborn branch, but substitutes the empty tree for the "old"
commit.
This means that in the absence of an index, any files in the
working tree will be treated as untracked files, and a
checkout which would overwrite them is aborted. Similarly,
any paths in the index will be merged with an empty entry
as the base, meaning that unless the new branch's content is
identical to what's in the index, there will be a conflict
and the checkout will be aborted.
The user is then free to correct the situation or proceed
with "-f" as appropriate.
This patch also removes the "warning: you are on a branch
yet to be born" message. Its function was to warn the user
that we were enabling the "-f" option. Since we are no
longer doing that, there is no reason for the user to care
whether we are switching away from an unborn branch.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-08-25 07:03:16 +04:00
|
|
|
#!/bin/sh
|
|
|
|
|
2012-01-31 00:10:08 +04:00
|
|
|
test_description='checkout from unborn branch'
|
checkout: do not imply "-f" on unborn branches
When checkout sees that HEAD points to a non-existent ref,
it currently acts as if "-f" was given; this behavior dates
back to 5a03e7f, which enabled checkout from unborn branches
in the shell version of "git-checkout". The reasoning given
is to avoid the code path which tries to merge the tree
contents. When checkout was converted to C, this code
remained intact.
The unfortunate side effect of this strategy is that the
"force" code path will overwrite working tree and index
state that may be precious to the user. Instead of enabling
"force", this patch uses the normal "merge" codepath for an
unborn branch, but substitutes the empty tree for the "old"
commit.
This means that in the absence of an index, any files in the
working tree will be treated as untracked files, and a
checkout which would overwrite them is aborted. Similarly,
any paths in the index will be merged with an empty entry
as the base, meaning that unless the new branch's content is
identical to what's in the index, there will be a conflict
and the checkout will be aborted.
The user is then free to correct the situation or proceed
with "-f" as appropriate.
This patch also removes the "warning: you are on a branch
yet to be born" message. Its function was to warn the user
that we were enabling the "-f" option. Since we are no
longer doing that, there is no reason for the user to care
whether we are switching away from an unborn branch.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-08-25 07:03:16 +04:00
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
test_expect_success 'setup' '
|
|
|
|
mkdir parent &&
|
|
|
|
(cd parent &&
|
|
|
|
git init &&
|
|
|
|
echo content >file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -m base
|
|
|
|
) &&
|
|
|
|
git fetch parent master:origin
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout from unborn preserves untracked files' '
|
|
|
|
echo precious >expect &&
|
|
|
|
echo precious >file &&
|
|
|
|
test_must_fail git checkout -b new origin &&
|
|
|
|
test_cmp expect file
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout from unborn preserves index contents' '
|
|
|
|
echo precious >expect &&
|
|
|
|
echo precious >file &&
|
|
|
|
git add file &&
|
|
|
|
test_must_fail git checkout -b new origin &&
|
|
|
|
test_cmp expect file &&
|
|
|
|
git show :file >file &&
|
|
|
|
test_cmp expect file
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout from unborn merges identical index contents' '
|
|
|
|
echo content >file &&
|
|
|
|
git add file &&
|
|
|
|
git checkout -b new origin
|
|
|
|
'
|
|
|
|
|
2012-01-31 00:10:08 +04:00
|
|
|
test_expect_success 'checking out another branch from unborn state' '
|
|
|
|
git checkout --orphan newroot &&
|
|
|
|
git checkout -b anothername &&
|
|
|
|
test_must_fail git show-ref --verify refs/heads/newroot &&
|
|
|
|
git symbolic-ref HEAD >actual &&
|
|
|
|
echo refs/heads/anothername >expect &&
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
2012-05-08 21:22:33 +04:00
|
|
|
test_expect_success 'checking out in a newly created repo' '
|
|
|
|
test_create_repo empty &&
|
|
|
|
(
|
|
|
|
cd empty &&
|
|
|
|
git symbolic-ref HEAD >expect &&
|
|
|
|
test_must_fail git checkout &&
|
|
|
|
git symbolic-ref HEAD >actual &&
|
|
|
|
test_cmp expect actual
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
checkout: do not imply "-f" on unborn branches
When checkout sees that HEAD points to a non-existent ref,
it currently acts as if "-f" was given; this behavior dates
back to 5a03e7f, which enabled checkout from unborn branches
in the shell version of "git-checkout". The reasoning given
is to avoid the code path which tries to merge the tree
contents. When checkout was converted to C, this code
remained intact.
The unfortunate side effect of this strategy is that the
"force" code path will overwrite working tree and index
state that may be precious to the user. Instead of enabling
"force", this patch uses the normal "merge" codepath for an
unborn branch, but substitutes the empty tree for the "old"
commit.
This means that in the absence of an index, any files in the
working tree will be treated as untracked files, and a
checkout which would overwrite them is aborted. Similarly,
any paths in the index will be merged with an empty entry
as the base, meaning that unless the new branch's content is
identical to what's in the index, there will be a conflict
and the checkout will be aborted.
The user is then free to correct the situation or proceed
with "-f" as appropriate.
This patch also removes the "warning: you are on a branch
yet to be born" message. Its function was to warn the user
that we were enabling the "-f" option. Since we are no
longer doing that, there is no reason for the user to care
whether we are switching away from an unborn branch.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-08-25 07:03:16 +04:00
|
|
|
test_done
|