2011-02-08 11:32:49 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='checkout into detached HEAD state'
|
2020-11-19 00:44:22 +01:00
|
|
|
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
|
tests: mark tests relying on the current default for `init.defaultBranch`
In addition to the manual adjustment to let the `linux-gcc` CI job run
the test suite with `master` and then with `main`, this patch makes sure
that GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME is set in all test scripts
that currently rely on the initial branch name being `master by default.
To determine which test scripts to mark up, the first step was to
force-set the default branch name to `master` in
- all test scripts that contain the keyword `master`,
- t4211, which expects `t/t4211/history.export` with a hard-coded ref to
initialize the default branch,
- t5560 because it sources `t/t556x_common` which uses `master`,
- t8002 and t8012 because both source `t/annotate-tests.sh` which also
uses `master`)
This trick was performed by this command:
$ sed -i '/^ *\. \.\/\(test-lib\|lib-\(bash\|cvs\|git-svn\)\|gitweb-lib\)\.sh$/i\
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master\
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\
' $(git grep -l master t/t[0-9]*.sh) \
t/t4211*.sh t/t5560*.sh t/t8002*.sh t/t8012*.sh
After that, careful, manual inspection revealed that some of the test
scripts containing the needle `master` do not actually rely on a
specific default branch name: either they mention `master` only in a
comment, or they initialize that branch specificially, or they do not
actually refer to the current default branch. Therefore, the
aforementioned modification was undone in those test scripts thusly:
$ git checkout HEAD -- \
t/t0027-auto-crlf.sh t/t0060-path-utils.sh \
t/t1011-read-tree-sparse-checkout.sh \
t/t1305-config-include.sh t/t1309-early-config.sh \
t/t1402-check-ref-format.sh t/t1450-fsck.sh \
t/t2024-checkout-dwim.sh \
t/t2106-update-index-assume-unchanged.sh \
t/t3040-subprojects-basic.sh t/t3301-notes.sh \
t/t3308-notes-merge.sh t/t3423-rebase-reword.sh \
t/t3436-rebase-more-options.sh \
t/t4015-diff-whitespace.sh t/t4257-am-interactive.sh \
t/t5323-pack-redundant.sh t/t5401-update-hooks.sh \
t/t5511-refspec.sh t/t5526-fetch-submodules.sh \
t/t5529-push-errors.sh t/t5530-upload-pack-error.sh \
t/t5548-push-porcelain.sh \
t/t5552-skipping-fetch-negotiator.sh \
t/t5572-pull-submodule.sh t/t5608-clone-2gb.sh \
t/t5614-clone-submodules-shallow.sh \
t/t7508-status.sh t/t7606-merge-custom.sh \
t/t9302-fast-import-unpack-limit.sh
We excluded one set of test scripts in these commands, though: the range
of `git p4` tests. The reason? `git p4` stores the (foreign) remote
branch in the branch called `p4/master`, which is obviously not the
default branch. Manual analysis revealed that only five of these tests
actually require a specific default branch name to pass; They were
modified thusly:
$ sed -i '/^ *\. \.\/lib-git-p4\.sh$/i\
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master\
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\
' t/t980[0167]*.sh t/t9811*.sh
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-11-19 00:44:19 +01:00
|
|
|
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
|
|
|
|
2022-08-02 17:33:16 +02:00
|
|
|
TEST_PASSES_SANITIZE_LEAK=true
|
2011-02-08 11:32:49 +01:00
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
check_detached () {
|
|
|
|
test_must_fail git symbolic-ref -q HEAD >/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
check_not_detached () {
|
|
|
|
git symbolic-ref -q HEAD >/dev/null
|
|
|
|
}
|
|
|
|
|
2011-10-01 17:38:34 +02:00
|
|
|
PREV_HEAD_DESC='Previous HEAD position was'
|
2011-03-20 10:04:16 +01:00
|
|
|
check_orphan_warning() {
|
2012-05-04 20:13:14 +02:00
|
|
|
test_i18ngrep "you are leaving $2 behind" "$1" &&
|
2011-10-01 17:38:34 +02:00
|
|
|
test_i18ngrep ! "$PREV_HEAD_DESC" "$1"
|
2011-03-20 10:04:16 +01:00
|
|
|
}
|
|
|
|
check_no_orphan_warning() {
|
2012-05-04 20:13:14 +02:00
|
|
|
test_i18ngrep ! "you are leaving .* commit.*behind" "$1" &&
|
2011-10-01 17:38:34 +02:00
|
|
|
test_i18ngrep "$PREV_HEAD_DESC" "$1"
|
2011-03-20 10:04:16 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 11:32:49 +01:00
|
|
|
reset () {
|
2020-11-19 00:44:22 +01:00
|
|
|
git checkout main &&
|
2011-02-08 11:32:49 +01:00
|
|
|
check_not_detached
|
|
|
|
}
|
|
|
|
|
|
|
|
test_expect_success 'setup' '
|
|
|
|
test_commit one &&
|
|
|
|
test_commit two &&
|
2011-03-20 10:04:16 +01:00
|
|
|
test_commit three && git tag -d three &&
|
|
|
|
test_commit four && git tag -d four &&
|
2011-02-08 11:32:49 +01:00
|
|
|
git branch branch &&
|
|
|
|
git tag tag
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout branch does not detach' '
|
|
|
|
reset &&
|
|
|
|
git checkout branch &&
|
|
|
|
check_not_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout tag detaches' '
|
|
|
|
reset &&
|
|
|
|
git checkout tag &&
|
|
|
|
check_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout branch by full name detaches' '
|
|
|
|
reset &&
|
|
|
|
git checkout refs/heads/branch &&
|
|
|
|
check_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout non-ref detaches' '
|
|
|
|
reset &&
|
|
|
|
git checkout branch^ &&
|
|
|
|
check_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout ref^0 detaches' '
|
|
|
|
reset &&
|
|
|
|
git checkout branch^0 &&
|
|
|
|
check_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout --detach detaches' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach branch &&
|
|
|
|
check_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout --detach without branch name' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach &&
|
|
|
|
check_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout --detach errors out for non-commit' '
|
|
|
|
reset &&
|
|
|
|
test_must_fail git checkout --detach one^{tree} &&
|
|
|
|
check_not_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout --detach errors out for extra argument' '
|
|
|
|
reset &&
|
2020-11-19 00:44:22 +01:00
|
|
|
git checkout main &&
|
2011-02-08 11:32:49 +01:00
|
|
|
test_must_fail git checkout --detach tag one.t &&
|
|
|
|
check_not_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout --detached and -b are incompatible' '
|
|
|
|
reset &&
|
|
|
|
test_must_fail git checkout --detach -b newbranch tag &&
|
|
|
|
check_not_detached
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout --detach moves HEAD' '
|
|
|
|
reset &&
|
|
|
|
git checkout one &&
|
|
|
|
git checkout --detach two &&
|
|
|
|
git diff --exit-code HEAD &&
|
|
|
|
git diff --exit-code two
|
|
|
|
'
|
|
|
|
|
2011-03-20 10:04:16 +01:00
|
|
|
test_expect_success 'checkout warns on orphan commits' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach two &&
|
|
|
|
echo content >orphan &&
|
|
|
|
git add orphan &&
|
2012-05-04 20:13:14 +02:00
|
|
|
git commit -a -m orphan1 &&
|
|
|
|
echo new content >orphan &&
|
|
|
|
git commit -a -m orphan2 &&
|
|
|
|
orphan2=$(git rev-parse HEAD) &&
|
2020-11-19 00:44:22 +01:00
|
|
|
git checkout main 2>stderr
|
2011-04-10 21:34:08 +02:00
|
|
|
'
|
|
|
|
|
2011-04-13 01:20:32 +02:00
|
|
|
test_expect_success 'checkout warns on orphan commits: output' '
|
2012-05-04 20:13:14 +02:00
|
|
|
check_orphan_warning stderr "2 commits"
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout warns orphaning 1 of 2 commits' '
|
|
|
|
git checkout "$orphan2" &&
|
|
|
|
git checkout HEAD^ 2>stderr
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout warns orphaning 1 of 2 commits: output' '
|
2012-05-04 20:14:48 +02:00
|
|
|
check_orphan_warning stderr "1 commit"
|
2011-03-20 10:04:16 +01:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout does not warn leaving ref tip' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach two &&
|
2020-11-19 00:44:22 +01:00
|
|
|
git checkout main 2>stderr
|
2011-04-10 21:34:08 +02:00
|
|
|
'
|
|
|
|
|
2011-04-13 01:20:32 +02:00
|
|
|
test_expect_success 'checkout does not warn leaving ref tip' '
|
2011-03-20 10:04:16 +01:00
|
|
|
check_no_orphan_warning stderr
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout does not warn leaving reachable commit' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach HEAD^ &&
|
2020-11-19 00:44:22 +01:00
|
|
|
git checkout main 2>stderr
|
2011-04-10 21:34:08 +02:00
|
|
|
'
|
|
|
|
|
2011-04-13 01:20:32 +02:00
|
|
|
test_expect_success 'checkout does not warn leaving reachable commit' '
|
2011-03-20 10:04:16 +01:00
|
|
|
check_no_orphan_warning stderr
|
|
|
|
'
|
|
|
|
|
2011-03-20 10:09:18 +01:00
|
|
|
cat >expect <<'EOF'
|
2020-11-19 00:44:22 +01:00
|
|
|
Your branch is behind 'main' by 1 commit, and can be fast-forwarded.
|
2012-11-15 11:45:00 +01:00
|
|
|
(use "git pull" to update your local branch)
|
2011-03-20 10:09:18 +01:00
|
|
|
EOF
|
|
|
|
test_expect_success 'tracking count is accurate after orphan check' '
|
|
|
|
reset &&
|
2020-11-19 00:44:22 +01:00
|
|
|
git branch child main^ &&
|
2011-03-20 10:09:18 +01:00
|
|
|
git config branch.child.remote . &&
|
2020-11-19 00:44:22 +01:00
|
|
|
git config branch.child.merge refs/heads/main &&
|
2011-03-20 10:09:18 +01:00
|
|
|
git checkout child^ &&
|
|
|
|
git checkout child >stdout &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp expect stdout
|
2011-03-20 10:09:18 +01:00
|
|
|
'
|
|
|
|
|
2016-08-15 20:40:21 +02:00
|
|
|
test_expect_success 'no advice given for explicit detached head state' '
|
|
|
|
# baseline
|
|
|
|
test_config advice.detachedHead true &&
|
|
|
|
git checkout child && git checkout HEAD^0 >expect.advice 2>&1 &&
|
|
|
|
test_config advice.detachedHead false &&
|
|
|
|
git checkout child && git checkout HEAD^0 >expect.no-advice 2>&1 &&
|
|
|
|
test_unconfig advice.detachedHead &&
|
|
|
|
# without configuration, the advice.* variables default to true
|
|
|
|
git checkout child && git checkout HEAD^0 >actual 2>&1 &&
|
|
|
|
test_cmp expect.advice actual &&
|
|
|
|
|
|
|
|
# with explicit --detach
|
|
|
|
# no configuration
|
|
|
|
test_unconfig advice.detachedHead &&
|
|
|
|
git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
|
|
|
|
test_cmp expect.no-advice actual &&
|
|
|
|
|
|
|
|
# explicitly decline advice
|
|
|
|
test_config advice.detachedHead false &&
|
|
|
|
git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
|
|
|
|
test_cmp expect.no-advice actual
|
|
|
|
'
|
|
|
|
|
2017-12-06 01:20:42 +01:00
|
|
|
# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
|
|
|
|
test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
|
|
|
|
|
2020-11-19 00:44:22 +01:00
|
|
|
commit=$(git rev-parse --short=12 main^) &&
|
|
|
|
commit2=$(git rev-parse --short=12 main~2) &&
|
|
|
|
commit3=$(git rev-parse --short=12 main~3) &&
|
2018-03-25 21:20:52 +02:00
|
|
|
|
2017-12-06 01:20:42 +01:00
|
|
|
# The first detach operation is more chatty than the following ones.
|
2018-03-25 21:20:52 +02:00
|
|
|
cat >1st_detach <<-EOF &&
|
2019-03-29 11:39:19 +01:00
|
|
|
Note: switching to 'HEAD^'.
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
You are in 'detached HEAD' state. You can look around, make experimental
|
|
|
|
changes and commit them, and you can discard any commits you make in this
|
2019-03-29 11:39:19 +01:00
|
|
|
state without impacting any branches by switching back to a branch.
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
If you want to create a new branch to retain commits you create, you may
|
2019-03-29 11:39:19 +01:00
|
|
|
do so (now or later) by using -c with the switch command. Example:
|
2017-12-06 01:20:42 +01:00
|
|
|
|
2019-03-29 11:39:19 +01:00
|
|
|
git switch -c <new-branch-name>
|
2017-12-06 01:20:42 +01:00
|
|
|
|
2019-03-29 11:39:19 +01:00
|
|
|
Or undo this operation with:
|
2019-03-29 11:38:58 +01:00
|
|
|
|
2019-03-29 11:39:19 +01:00
|
|
|
git switch -
|
2019-03-29 11:38:58 +01:00
|
|
|
|
|
|
|
Turn off this advice by setting config variable advice.detachedHead to false
|
|
|
|
|
2018-03-25 21:20:52 +02:00
|
|
|
HEAD is now at \$commit three
|
2017-12-06 01:20:42 +01:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# The remaining ones just show info about previous and current HEADs.
|
2018-03-25 21:20:52 +02:00
|
|
|
cat >2nd_detach <<-EOF &&
|
|
|
|
Previous HEAD position was \$commit three
|
|
|
|
HEAD is now at \$commit2 two
|
2017-12-06 01:20:42 +01:00
|
|
|
EOF
|
|
|
|
|
2018-03-25 21:20:52 +02:00
|
|
|
cat >3rd_detach <<-EOF &&
|
|
|
|
Previous HEAD position was \$commit2 two
|
|
|
|
HEAD is now at \$commit3 one
|
2017-12-06 01:20:42 +01:00
|
|
|
EOF
|
|
|
|
|
|
|
|
reset &&
|
|
|
|
check_not_detached &&
|
|
|
|
|
|
|
|
# Various ways of *not* asking for ellipses
|
|
|
|
|
|
|
|
sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
|
|
|
|
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 1st_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS="no" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 2nd_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 3rd_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
|
|
|
|
|
|
|
|
# We only have four commits, but we can re-use them
|
|
|
|
reset &&
|
|
|
|
check_not_detached &&
|
|
|
|
|
|
|
|
# Make no mention of the env var at all
|
|
|
|
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 1st_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS='nope' &&
|
|
|
|
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 2nd_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS=nein &&
|
|
|
|
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 3rd_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
true
|
|
|
|
"
|
|
|
|
|
|
|
|
# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
|
|
|
|
test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked to' "
|
|
|
|
|
2020-11-19 00:44:22 +01:00
|
|
|
commit=$(git rev-parse --short=12 main^) &&
|
|
|
|
commit2=$(git rev-parse --short=12 main~2) &&
|
|
|
|
commit3=$(git rev-parse --short=12 main~3) &&
|
2018-03-25 21:20:52 +02:00
|
|
|
|
2017-12-06 01:20:42 +01:00
|
|
|
# The first detach operation is more chatty than the following ones.
|
2018-03-25 21:20:52 +02:00
|
|
|
cat >1st_detach <<-EOF &&
|
2019-03-29 11:39:19 +01:00
|
|
|
Note: switching to 'HEAD^'.
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
You are in 'detached HEAD' state. You can look around, make experimental
|
|
|
|
changes and commit them, and you can discard any commits you make in this
|
2019-03-29 11:39:19 +01:00
|
|
|
state without impacting any branches by switching back to a branch.
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
If you want to create a new branch to retain commits you create, you may
|
2019-03-29 11:39:19 +01:00
|
|
|
do so (now or later) by using -c with the switch command. Example:
|
2017-12-06 01:20:42 +01:00
|
|
|
|
2019-03-29 11:39:19 +01:00
|
|
|
git switch -c <new-branch-name>
|
2017-12-06 01:20:42 +01:00
|
|
|
|
2019-03-29 11:39:19 +01:00
|
|
|
Or undo this operation with:
|
2019-03-29 11:38:58 +01:00
|
|
|
|
2019-03-29 11:39:19 +01:00
|
|
|
git switch -
|
2019-03-29 11:38:58 +01:00
|
|
|
|
|
|
|
Turn off this advice by setting config variable advice.detachedHead to false
|
|
|
|
|
2018-03-25 21:20:52 +02:00
|
|
|
HEAD is now at \$commit... three
|
2017-12-06 01:20:42 +01:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# The remaining ones just show info about previous and current HEADs.
|
2018-03-25 21:20:52 +02:00
|
|
|
cat >2nd_detach <<-EOF &&
|
|
|
|
Previous HEAD position was \$commit... three
|
|
|
|
HEAD is now at \$commit2... two
|
2017-12-06 01:20:42 +01:00
|
|
|
EOF
|
|
|
|
|
2018-03-25 21:20:52 +02:00
|
|
|
cat >3rd_detach <<-EOF &&
|
|
|
|
Previous HEAD position was \$commit2... two
|
|
|
|
HEAD is now at \$commit3... one
|
2017-12-06 01:20:42 +01:00
|
|
|
EOF
|
|
|
|
|
|
|
|
reset &&
|
|
|
|
check_not_detached &&
|
|
|
|
|
|
|
|
# Various ways of asking for ellipses...
|
|
|
|
# The user can just use any kind of quoting (including none).
|
|
|
|
|
2017-12-06 17:10:07 +01:00
|
|
|
GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
2017-12-06 01:20:42 +01:00
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 1st_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
2017-12-06 17:10:07 +01:00
|
|
|
GIT_PRINT_SHA1_ELLIPSIS=Yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
2017-12-06 01:20:42 +01:00
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 2nd_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
2017-12-06 17:10:07 +01:00
|
|
|
GIT_PRINT_SHA1_ELLIPSIS=YES git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
2017-12-06 01:20:42 +01:00
|
|
|
check_detached &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp 3rd_detach actual &&
|
2017-12-06 01:20:42 +01:00
|
|
|
|
|
|
|
true
|
|
|
|
"
|
|
|
|
|
2011-02-08 11:32:49 +01:00
|
|
|
test_done
|