f0d4d398e2
Remove various redundant or obsolete code from the test_create_repo() function, and split up its use in test-lib.sh from what tests need from it. This leave us with a pass-through wrapper for "git init" in test-lib-functions.sh, in test-lib.sh we have the same, except for needing to redirect stdout/stderr, and emitting an error ourselves if it fails. We don't need to error() ourselves when test_create_repo() is invoked, as the invocation will be a part of a test's "&&"-chain. Everything below this paragraph is a detailed summary of the history of test_create_repo() explaining why it's safe to remove the various things it was doing: 1. "mkdir -p" isn't needed because "git init" itself will create leading directories if needed. 2. Since we're now a simple wrapper for "git init" we don't need to check that we have only one argument. If someone wants to run "test_create_repo --bare x" that's OK. 3. We won't ever hit that "Cannot setup test environment" error. Checking the test environment sanity when doing "git init" dates back toeea420693b
(t0000: catch trivial pilot errors., 2005-12-10) and2ccd2027b0
(trivial: check, if t/trash directory was successfully created, 2006-01-05). We can also see it in another form a bit later in my own0d314ce834
(test-lib: use subshell instead of cd $new && .. && cd $old, 2010-08-30). But since2006f0adae
(t/test-lib: make sure Git has already been built, 2012-09-17) we already check if we have a built git earlier. The one thing this was testing after that 2012 change was that we'd just built "git", but not "git-init", but since3af4c7156c
(tests: respect GIT_TEST_INSTALLED when initializing repositories, 2018-11-12) we invoke "git", not "git-init". So all of that's been checked already, and we don't need to re-check it here. 4. We don't need to move .git/hooks out of the way. That dates back toc09a69a83e
(Disable hooks during tests., 2005-10-16), since then hooks became disabled by default inf98f8cbac0
(Ship sample hooks with .sample suffix, 2008-06-24). So the hooks were already disabled by default, but as can be seen from "mkdir .git/hooks" changes various tests needed to re-setup that directory. Now they no longer do. This makes us implicitly depend on the default hooks being disabled, which is a good thing. If and when we'd have any on-by-default hooks (I see no reason we ever would) we'd want to see the subtle and not so subtle ways that would break the test suite. 5. We don't need to "cd" to the "$repo" directory at all anymore. In the code being removed here we both "cd"'d to the repository before calling "init", and did so in a subshell. It's not important to do either, so both of those can be removed. We cd'd because this code grew from test-lib.sh code where we'd have done so already, seeeedf8f97e5
(Abstract test_create_repo out for use in tests., 2006-02-17), and later "cd"'d inside a subshell since0d314ce834
to avoid having to keep track of an "old pwd" variable to cd back after the setup. Being in the repository directory made moving the hooks around easier (we wouldn't have to fully qualify the path). Since we're not moving the hooks per #4 above we don't need to "cd" for that reason either. 6. We can drop the --template argument and instead rely on the GIT_TEMPLATE_DIR set to the same path earlier in test-lib.sh. See8683a45d66
(Introduce GIT_TEMPLATE_DIR, 2006-12-19) 7. We only needed that ">&3 2>&4" redirection when invoked from test-lib.sh. We could still invoke test_create_repo() there, but as the invocation is now trivial and we don't have a good reason to use test_create_repo() elsewhere let's call "git init" there ourselves. 8. We didn't need to resolve "git" as "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" in test_create_repo(), even for the use of test-lib.sh PATH is already set up in test-lib.sh to start with GIT_TEST_INSTALLED and/or GIT_EXEC_PATH before test_create_repo() (now "git init") is called.. So we can simply run "git" and rely on the PATH lookup choosing the right executable. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
268 lines
6.5 KiB
Bash
Executable File
268 lines
6.5 KiB
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# Copyright (c) 2010 Thomas Rast
|
|
#
|
|
|
|
test_description='Test the post-rewrite hook.'
|
|
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
|
|
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
|
|
|
. ./test-lib.sh
|
|
|
|
test_expect_success 'setup' '
|
|
test_commit A foo A &&
|
|
test_commit B foo B &&
|
|
test_commit C foo C &&
|
|
test_commit D foo D &&
|
|
git checkout A^0 &&
|
|
test_commit E bar E &&
|
|
test_commit F foo F &&
|
|
git checkout main
|
|
'
|
|
|
|
cat >.git/hooks/post-rewrite <<EOF
|
|
#!/bin/sh
|
|
echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
|
|
cat > "$TRASH_DIRECTORY"/post-rewrite.data
|
|
EOF
|
|
chmod u+x .git/hooks/post-rewrite
|
|
|
|
clear_hook_input () {
|
|
rm -f post-rewrite.args post-rewrite.data
|
|
}
|
|
|
|
verify_hook_input () {
|
|
test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
|
|
test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
|
|
}
|
|
|
|
test_expect_success 'git commit --amend' '
|
|
clear_hook_input &&
|
|
echo "D new message" > newmsg &&
|
|
oldsha=$(git rev-parse HEAD^0) &&
|
|
git commit -Fnewmsg --amend &&
|
|
echo amend > expected.args &&
|
|
echo $oldsha $(git rev-parse HEAD^0) > expected.data &&
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git commit --amend --no-post-rewrite' '
|
|
clear_hook_input &&
|
|
echo "D new message again" > newmsg &&
|
|
git commit --no-post-rewrite -Fnewmsg --amend &&
|
|
test ! -f post-rewrite.args &&
|
|
test ! -f post-rewrite.data
|
|
'
|
|
|
|
test_expect_success 'git rebase --apply' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_must_fail git rebase --apply --onto A B &&
|
|
echo C > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase --apply --skip' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_must_fail git rebase --apply --onto A B &&
|
|
test_must_fail git rebase --skip &&
|
|
echo D > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase --apply --skip the last one' '
|
|
git reset --hard F &&
|
|
clear_hook_input &&
|
|
test_must_fail git rebase --apply --onto D A &&
|
|
git rebase --skip &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse E) $(git rev-parse HEAD)
|
|
$(git rev-parse F) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase -m' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_must_fail git rebase -m --onto A B &&
|
|
echo C > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase -m --skip' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_must_fail git rebase -m --onto A B &&
|
|
test_must_fail git rebase --skip &&
|
|
echo D > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase with implicit use of merge backend' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_must_fail git rebase --keep-empty --onto A B &&
|
|
echo C > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase --skip with implicit use of merge backend' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_must_fail git rebase --keep-empty --onto A B &&
|
|
test_must_fail git rebase --skip &&
|
|
echo D > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
. "$TEST_DIRECTORY"/lib-rebase.sh
|
|
|
|
set_fake_editor
|
|
|
|
# Helper to work around the lack of one-shot exporting for
|
|
# test_must_fail (as it is a shell function)
|
|
test_fail_interactive_rebase () {
|
|
(
|
|
FAKE_LINES="$1" &&
|
|
shift &&
|
|
export FAKE_LINES &&
|
|
test_must_fail git rebase -i "$@"
|
|
)
|
|
}
|
|
|
|
test_expect_success 'git rebase -i (unchanged)' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_fail_interactive_rebase "1 2" --onto A B &&
|
|
echo C > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase -i (skip)' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_fail_interactive_rebase "2" --onto A B &&
|
|
echo D > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase -i (squash)' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
test_fail_interactive_rebase "1 squash 2" --onto A B &&
|
|
echo C > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase -i (fixup without conflict)' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
FAKE_LINES="1 fixup 2" git rebase -i B &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase -i (double edit)' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
FAKE_LINES="edit 1 edit 2" git rebase -i B &&
|
|
git rebase --continue &&
|
|
echo something > foo &&
|
|
git add foo &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_expect_success 'git rebase -i (exec)' '
|
|
git reset --hard D &&
|
|
clear_hook_input &&
|
|
FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
|
|
echo something >bar &&
|
|
git add bar &&
|
|
# Fails because of exec false
|
|
test_must_fail git rebase --continue &&
|
|
git rebase --continue &&
|
|
echo rebase >expected.args &&
|
|
cat >expected.data <<-EOF &&
|
|
$(git rev-parse C) $(git rev-parse HEAD^)
|
|
$(git rev-parse D) $(git rev-parse HEAD)
|
|
EOF
|
|
verify_hook_input
|
|
'
|
|
|
|
test_done
|