t5411: start using the default branch name "main"
[git] / t / t5411-proc-receive-hook.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2020 Jiang Xin
4 #
5
6 test_description='Test proc-receive hook'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12
13 test_have_prereq PREPARE_FOR_MAIN_BRANCH || {
14         test_skip="In transit for the default branch name 'main'"
15         test_done
16 }
17
18 . "$TEST_DIRECTORY"/t5411/common-functions.sh
19
20 setup_upstream_and_workbench () {
21         # Refs of upstream : main(A)
22         # Refs of workbench: main(A)  tags/v123
23         test_expect_success "setup upstream and workbench" '
24                 rm -rf upstream.git &&
25                 rm -rf workbench &&
26                 git init --bare upstream.git &&
27                 git init workbench &&
28                 create_commits_in workbench A B &&
29                 (
30                         cd workbench &&
31                         # Try to make a stable fixed width for abbreviated commit ID,
32                         # this fixed-width oid will be replaced with "<OID>".
33                         git config core.abbrev 7 &&
34                         git tag -m "v123" v123 $A &&
35                         git remote add origin ../upstream.git &&
36                         git push origin main &&
37                         git update-ref refs/heads/main $A $B &&
38                         git -C ../upstream.git update-ref \
39                                 refs/heads/main $A $B
40                 ) &&
41                 TAG=$(git -C workbench rev-parse v123) &&
42
43                 # setup pre-receive hook
44                 write_script upstream.git/hooks/pre-receive <<-\EOF &&
45                 exec >&2
46                 echo "# pre-receive hook"
47                 while read old new ref
48                 do
49                         echo "pre-receive< $old $new $ref"
50                 done
51                 EOF
52
53                 # setup post-receive hook
54                 write_script upstream.git/hooks/post-receive <<-\EOF &&
55                 exec >&2
56                 echo "# post-receive hook"
57                 while read old new ref
58                 do
59                         echo "post-receive< $old $new $ref"
60                 done
61                 EOF
62
63                 upstream=upstream.git
64         '
65 }
66
67 run_proc_receive_hook_test() {
68         case $1 in
69         http)
70                 PROTOCOL="HTTP protocol"
71                 URL_PREFIX="http://.*"
72                 ;;
73         local)
74                 PROTOCOL="builtin protocol"
75                 URL_PREFIX="\.\."
76                 ;;
77         esac
78
79         # Include test cases for both file and HTTP protocol
80         for t in  "$TEST_DIRECTORY"/t5411/test-*.sh
81         do
82                 . "$t"
83         done
84 }
85
86 # Initialize the upstream repository and local workbench.
87 setup_upstream_and_workbench
88
89 # Load test cases that only need to be executed once.
90 for t in  "$TEST_DIRECTORY"/t5411/once-*.sh
91 do
92         . "$t"
93 done
94
95 # Initialize the upstream repository and local workbench.
96 setup_upstream_and_workbench
97
98 # Run test cases for 'proc-receive' hook on local file protocol.
99 run_proc_receive_hook_test local
100
101 ROOT_PATH="$PWD"
102 . "$TEST_DIRECTORY"/lib-gpg.sh
103 . "$TEST_DIRECTORY"/lib-httpd.sh
104 . "$TEST_DIRECTORY"/lib-terminal.sh
105 start_httpd
106
107 # Re-initialize the upstream repository and local workbench.
108 setup_upstream_and_workbench
109
110 # Refs of upstream : main(A)
111 # Refs of workbench: main(A)  tags/v123
112 test_expect_success "setup for HTTP protocol" '
113         git -C upstream.git config http.receivepack true &&
114         upstream="$HTTPD_DOCUMENT_ROOT_PATH/upstream.git" &&
115         mv upstream.git "$upstream" &&
116         git -C workbench remote set-url origin "$HTTPD_URL/auth-push/smart/upstream.git" &&
117         set_askpass user@host pass@host
118 '
119
120 setup_askpass_helper
121
122 # Run test cases for 'proc-receive' hook on HTTP protocol.
123 run_proc_receive_hook_test http
124
125 test_done