blob: b4acb3be5cf6f1d12521faf29a8c2afc6d3ff8e2 [file] [log] [blame]
Nanako Shiraishid70b4a82008-10-06 14:14:24 +09001#!/bin/sh
2
3test_description='git rebase with its hook(s)'
4
Johannes Schindelind1c02d92020-11-18 23:44:25 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Nanako Shiraishid70b4a82008-10-06 14:14:24 +09008. ./test-lib.sh
9
10test_expect_success setup '
11 echo hello >file &&
12 git add file &&
13 test_tick &&
14 git commit -m initial &&
15 echo goodbye >file &&
16 git add file &&
17 test_tick &&
18 git commit -m second &&
19 git checkout -b side HEAD^ &&
20 echo world >git &&
21 git add git &&
22 test_tick &&
23 git commit -m side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000024 git checkout main &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090025 git log --pretty=oneline --abbrev-commit --graph --all &&
26 git branch test side
27'
28
29test_expect_success 'rebase' '
30 git checkout test &&
31 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000032 git rebase main &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090033 test "z$(cat git)" = zworld
34'
35
36test_expect_success 'rebase -i' '
37 git checkout test &&
38 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000039 EDITOR=true git rebase -i main &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090040 test "z$(cat git)" = zworld
41'
42
43test_expect_success 'setup pre-rebase hook' '
44 mkdir -p .git/hooks &&
45 cat >.git/hooks/pre-rebase <<EOF &&
46#!$SHELL_PATH
47echo "\$1,\$2" >.git/PRE-REBASE-INPUT
48EOF
49 chmod +x .git/hooks/pre-rebase
50'
51
52test_expect_success 'pre-rebase hook gets correct input (1)' '
53 git checkout test &&
54 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000055 git rebase main &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090056 test "z$(cat git)" = zworld &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000057 test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090058
59'
60
61test_expect_success 'pre-rebase hook gets correct input (2)' '
62 git checkout test &&
63 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000064 git rebase main test &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090065 test "z$(cat git)" = zworld &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000066 test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090067'
68
69test_expect_success 'pre-rebase hook gets correct input (3)' '
70 git checkout test &&
71 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000072 git checkout main &&
73 git rebase main test &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090074 test "z$(cat git)" = zworld &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000075 test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090076'
77
78test_expect_success 'pre-rebase hook gets correct input (4)' '
79 git checkout test &&
80 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000081 EDITOR=true git rebase -i main &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090082 test "z$(cat git)" = zworld &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000083 test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090084
85'
86
87test_expect_success 'pre-rebase hook gets correct input (5)' '
88 git checkout test &&
89 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000090 EDITOR=true git rebase -i main test &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090091 test "z$(cat git)" = zworld &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000092 test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
Nanako Shiraishid70b4a82008-10-06 14:14:24 +090093'
94
95test_expect_success 'pre-rebase hook gets correct input (6)' '
96 git checkout test &&
97 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +000098 git checkout main &&
99 EDITOR=true git rebase -i main test &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900100 test "z$(cat git)" = zworld &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +0000101 test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900102'
103
104test_expect_success 'setup pre-rebase hook that fails' '
105 mkdir -p .git/hooks &&
106 cat >.git/hooks/pre-rebase <<EOF &&
107#!$SHELL_PATH
108false
109EOF
110 chmod +x .git/hooks/pre-rebase
111'
112
113test_expect_success 'pre-rebase hook stops rebase (1)' '
114 git checkout test &&
115 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +0000116 test_must_fail git rebase main &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900117 test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
118 test 0 = $(git rev-list HEAD...side | wc -l)
119'
120
121test_expect_success 'pre-rebase hook stops rebase (2)' '
122 git checkout test &&
123 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +0000124 test_must_fail env EDITOR=: git rebase -i main &&
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900125 test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
126 test 0 = $(git rev-list HEAD...side | wc -l)
127'
128
Nanako Shiraishic4427652008-10-06 14:14:29 +0900129test_expect_success 'rebase --no-verify overrides pre-rebase (1)' '
130 git checkout test &&
131 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +0000132 git rebase --no-verify main &&
Nanako Shiraishic4427652008-10-06 14:14:29 +0900133 test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
134 test "z$(cat git)" = zworld
135'
136
137test_expect_success 'rebase --no-verify overrides pre-rebase (2)' '
138 git checkout test &&
139 git reset --hard side &&
Johannes Schindelind1c02d92020-11-18 23:44:25 +0000140 EDITOR=true git rebase --no-verify -i main &&
Nanako Shiraishic4427652008-10-06 14:14:29 +0900141 test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
142 test "z$(cat git)" = zworld
143'
144
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900145test_done