The second batch
[git] / t / t3413-rebase-hook.sh
1 #!/bin/sh
2
3 test_description='git rebase with its hook(s)'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 test_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 &&
24         git checkout main &&
25         git log --pretty=oneline --abbrev-commit --graph --all &&
26         git branch test side
27 '
28
29 test_expect_success 'rebase' '
30         git checkout test &&
31         git reset --hard side &&
32         git rebase main &&
33         test "z$(cat git)" = zworld
34 '
35
36 test_expect_success 'rebase -i' '
37         git checkout test &&
38         git reset --hard side &&
39         EDITOR=true git rebase -i main &&
40         test "z$(cat git)" = zworld
41 '
42
43 test_expect_success 'setup pre-rebase hook' '
44         mkdir -p .git/hooks &&
45         cat >.git/hooks/pre-rebase <<EOF &&
46 #!$SHELL_PATH
47 echo "\$1,\$2" >.git/PRE-REBASE-INPUT
48 EOF
49         chmod +x .git/hooks/pre-rebase
50 '
51
52 test_expect_success 'pre-rebase hook gets correct input (1)' '
53         git checkout test &&
54         git reset --hard side &&
55         git rebase main &&
56         test "z$(cat git)" = zworld &&
57         test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,
58
59 '
60
61 test_expect_success 'pre-rebase hook gets correct input (2)' '
62         git checkout test &&
63         git reset --hard side &&
64         git rebase main test &&
65         test "z$(cat git)" = zworld &&
66         test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
67 '
68
69 test_expect_success 'pre-rebase hook gets correct input (3)' '
70         git checkout test &&
71         git reset --hard side &&
72         git checkout main &&
73         git rebase main test &&
74         test "z$(cat git)" = zworld &&
75         test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
76 '
77
78 test_expect_success 'pre-rebase hook gets correct input (4)' '
79         git checkout test &&
80         git reset --hard side &&
81         EDITOR=true git rebase -i main &&
82         test "z$(cat git)" = zworld &&
83         test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,
84
85 '
86
87 test_expect_success 'pre-rebase hook gets correct input (5)' '
88         git checkout test &&
89         git reset --hard side &&
90         EDITOR=true git rebase -i main test &&
91         test "z$(cat git)" = zworld &&
92         test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
93 '
94
95 test_expect_success 'pre-rebase hook gets correct input (6)' '
96         git checkout test &&
97         git reset --hard side &&
98         git checkout main &&
99         EDITOR=true git rebase -i main test &&
100         test "z$(cat git)" = zworld &&
101         test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
102 '
103
104 test_expect_success 'setup pre-rebase hook that fails' '
105         mkdir -p .git/hooks &&
106         cat >.git/hooks/pre-rebase <<EOF &&
107 #!$SHELL_PATH
108 false
109 EOF
110         chmod +x .git/hooks/pre-rebase
111 '
112
113 test_expect_success 'pre-rebase hook stops rebase (1)' '
114         git checkout test &&
115         git reset --hard side &&
116         test_must_fail git rebase main &&
117         test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
118         test 0 = $(git rev-list HEAD...side | wc -l)
119 '
120
121 test_expect_success 'pre-rebase hook stops rebase (2)' '
122         git checkout test &&
123         git reset --hard side &&
124         test_must_fail env EDITOR=: git rebase -i main &&
125         test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
126         test 0 = $(git rev-list HEAD...side | wc -l)
127 '
128
129 test_expect_success 'rebase --no-verify overrides pre-rebase (1)' '
130         git checkout test &&
131         git reset --hard side &&
132         git rebase --no-verify main &&
133         test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
134         test "z$(cat git)" = zworld
135 '
136
137 test_expect_success 'rebase --no-verify overrides pre-rebase (2)' '
138         git checkout test &&
139         git reset --hard side &&
140         EDITOR=true git rebase --no-verify -i main &&
141         test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
142         test "z$(cat git)" = zworld
143 '
144
145 test_done