2011-02-08 13:32:49 +03:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='checkout into detached HEAD state'
|
|
|
|
. ./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 19:38:34 +04:00
|
|
|
PREV_HEAD_DESC='Previous HEAD position was'
|
2011-03-20 12:04:16 +03:00
|
|
|
check_orphan_warning() {
|
2012-05-04 22:13:14 +04:00
|
|
|
test_i18ngrep "you are leaving $2 behind" "$1" &&
|
2011-10-01 19:38:34 +04:00
|
|
|
test_i18ngrep ! "$PREV_HEAD_DESC" "$1"
|
2011-03-20 12:04:16 +03:00
|
|
|
}
|
|
|
|
check_no_orphan_warning() {
|
2012-05-04 22:13:14 +04:00
|
|
|
test_i18ngrep ! "you are leaving .* commit.*behind" "$1" &&
|
2011-10-01 19:38:34 +04:00
|
|
|
test_i18ngrep "$PREV_HEAD_DESC" "$1"
|
2011-03-20 12:04:16 +03:00
|
|
|
}
|
|
|
|
|
2011-02-08 13:32:49 +03:00
|
|
|
reset () {
|
|
|
|
git checkout master &&
|
|
|
|
check_not_detached
|
|
|
|
}
|
|
|
|
|
|
|
|
test_expect_success 'setup' '
|
|
|
|
test_commit one &&
|
|
|
|
test_commit two &&
|
2011-03-20 12:04:16 +03:00
|
|
|
test_commit three && git tag -d three &&
|
|
|
|
test_commit four && git tag -d four &&
|
2011-02-08 13:32:49 +03: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 &&
|
|
|
|
git checkout master &&
|
|
|
|
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 12:04:16 +03:00
|
|
|
test_expect_success 'checkout warns on orphan commits' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach two &&
|
|
|
|
echo content >orphan &&
|
|
|
|
git add orphan &&
|
2012-05-04 22:13:14 +04:00
|
|
|
git commit -a -m orphan1 &&
|
|
|
|
echo new content >orphan &&
|
|
|
|
git commit -a -m orphan2 &&
|
|
|
|
orphan2=$(git rev-parse HEAD) &&
|
2011-04-10 23:34:08 +04:00
|
|
|
git checkout master 2>stderr
|
|
|
|
'
|
|
|
|
|
2011-04-13 03:20:32 +04:00
|
|
|
test_expect_success 'checkout warns on orphan commits: output' '
|
2012-05-04 22:13:14 +04: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 22:14:48 +04:00
|
|
|
check_orphan_warning stderr "1 commit"
|
2011-03-20 12:04:16 +03:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout does not warn leaving ref tip' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach two &&
|
2011-04-10 23:34:08 +04:00
|
|
|
git checkout master 2>stderr
|
|
|
|
'
|
|
|
|
|
2011-04-13 03:20:32 +04:00
|
|
|
test_expect_success 'checkout does not warn leaving ref tip' '
|
2011-03-20 12:04:16 +03:00
|
|
|
check_no_orphan_warning stderr
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout does not warn leaving reachable commit' '
|
|
|
|
reset &&
|
|
|
|
git checkout --detach HEAD^ &&
|
2011-04-10 23:34:08 +04:00
|
|
|
git checkout master 2>stderr
|
|
|
|
'
|
|
|
|
|
2011-04-13 03:20:32 +04:00
|
|
|
test_expect_success 'checkout does not warn leaving reachable commit' '
|
2011-03-20 12:04:16 +03:00
|
|
|
check_no_orphan_warning stderr
|
|
|
|
'
|
|
|
|
|
2011-03-20 12:09:18 +03:00
|
|
|
cat >expect <<'EOF'
|
|
|
|
Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
|
2012-11-15 14:45:00 +04:00
|
|
|
(use "git pull" to update your local branch)
|
2011-03-20 12:09:18 +03:00
|
|
|
EOF
|
|
|
|
test_expect_success 'tracking count is accurate after orphan check' '
|
|
|
|
reset &&
|
|
|
|
git branch child master^ &&
|
|
|
|
git config branch.child.remote . &&
|
|
|
|
git config branch.child.merge refs/heads/master &&
|
|
|
|
git checkout child^ &&
|
|
|
|
git checkout child >stdout &&
|
2012-04-14 08:45:31 +04:00
|
|
|
test_i18ncmp expect stdout
|
2011-03-20 12:09:18 +03:00
|
|
|
'
|
|
|
|
|
2016-08-15 21:40:21 +03: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 03:20:42 +03: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' "
|
|
|
|
|
|
|
|
# The first detach operation is more chatty than the following ones.
|
|
|
|
cat >1st_detach <<-'EOF' &&
|
|
|
|
Note: checking out 'HEAD^'.
|
|
|
|
|
|
|
|
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
|
|
|
|
state without impacting any branches by performing another checkout.
|
|
|
|
|
|
|
|
If you want to create a new branch to retain commits you create, you may
|
|
|
|
do so (now or later) by using -b with the checkout command again. Example:
|
|
|
|
|
|
|
|
git checkout -b <new-branch-name>
|
|
|
|
|
|
|
|
HEAD is now at 7c7cd714e262 three
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# The remaining ones just show info about previous and current HEADs.
|
|
|
|
cat >2nd_detach <<-'EOF' &&
|
|
|
|
Previous HEAD position was 7c7cd714e262 three
|
|
|
|
HEAD is now at 139b20d8e6c5 two
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >3rd_detach <<-'EOF' &&
|
|
|
|
Previous HEAD position was 139b20d8e6c5 two
|
|
|
|
HEAD is now at d79ce1670bdc one
|
|
|
|
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 &&
|
|
|
|
test_i18ncmp 1st_detach actual &&
|
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS="no" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
|
|
|
test_i18ncmp 2nd_detach actual &&
|
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
|
|
|
test_i18ncmp 3rd_detach actual &&
|
|
|
|
|
|
|
|
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 &&
|
|
|
|
test_i18ncmp 1st_detach actual &&
|
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS='nope' &&
|
|
|
|
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
|
|
|
test_i18ncmp 2nd_detach actual &&
|
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS=nein &&
|
|
|
|
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
|
|
|
test_i18ncmp 3rd_detach actual &&
|
|
|
|
|
|
|
|
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' "
|
|
|
|
|
|
|
|
# The first detach operation is more chatty than the following ones.
|
|
|
|
cat >1st_detach <<-'EOF' &&
|
|
|
|
Note: checking out 'HEAD^'.
|
|
|
|
|
|
|
|
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
|
|
|
|
state without impacting any branches by performing another checkout.
|
|
|
|
|
|
|
|
If you want to create a new branch to retain commits you create, you may
|
|
|
|
do so (now or later) by using -b with the checkout command again. Example:
|
|
|
|
|
|
|
|
git checkout -b <new-branch-name>
|
|
|
|
|
|
|
|
HEAD is now at 7c7cd714e262... three
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# The remaining ones just show info about previous and current HEADs.
|
|
|
|
cat >2nd_detach <<-'EOF' &&
|
|
|
|
Previous HEAD position was 7c7cd714e262... three
|
|
|
|
HEAD is now at 139b20d8e6c5... two
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat >3rd_detach <<-'EOF' &&
|
|
|
|
Previous HEAD position was 139b20d8e6c5... two
|
|
|
|
HEAD is now at d79ce1670bdc... one
|
|
|
|
EOF
|
|
|
|
|
|
|
|
reset &&
|
|
|
|
check_not_detached &&
|
|
|
|
|
|
|
|
# Various ways of asking for ellipses...
|
|
|
|
# The user can just use any kind of quoting (including none).
|
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS="yes" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
|
|
|
test_i18ncmp 1st_detach actual &&
|
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS='yes' git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
|
|
|
test_i18ncmp 2nd_detach actual &&
|
|
|
|
|
|
|
|
GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
|
|
|
|
check_detached &&
|
|
|
|
test_i18ncmp 3rd_detach actual &&
|
|
|
|
|
|
|
|
true
|
|
|
|
"
|
|
|
|
|
2011-02-08 13:32:49 +03:00
|
|
|
test_done
|