forked from microsoft/git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht0401-post-command-hook.sh
More file actions
executable file
·127 lines (106 loc) · 3.63 KB
/
t0401-post-command-hook.sh
File metadata and controls
executable file
·127 lines (106 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh
test_description='post-command hook'
. ./test-lib.sh
test_expect_success 'hook does not block git help' '
git config help.autocorrect immediate &&
git commit --allow-empty -m "a single log entry" &&
mkdir -p .git/hooks &&
write_script .git/hooks/post-command <<-EOF &&
echo "\$*" | sed "s/ --git-pid=[0-9]*//" \
>\$(git rev-parse --git-dir)/post-command.out
EOF
# intentional typo "logg" gets autocorrected to "log"
git logg --format=%s --first-parent > actual &&
test "log --format=%s --first-parent --exit_code=0" = "$(cat .git/post-command.out)" &&
echo "a single log entry" >expect &&
test_cmp expect actual
'
test_expect_success 'with no hook' '
echo "first" > file &&
git add file &&
git commit -m "first"
'
test_expect_success 'with succeeding hook' '
mkdir -p .git/hooks &&
write_script .git/hooks/post-command <<-EOF &&
echo "\$*" | sed "s/ --git-pid=[0-9]*//" \
>\$(git rev-parse --git-dir)/post-command.out
EOF
echo "second" >> file &&
git add file &&
test "add file --exit_code=0" = "$(cat .git/post-command.out)"
'
test_expect_success 'with failing pre-command hook' '
test_when_finished rm -f .git/hooks/pre-command &&
write_script .git/hooks/pre-command <<-EOF &&
exit 1
EOF
echo "third" >> file &&
test_must_fail git add file &&
test_path_is_missing "$(cat .git/post-command.out)"
'
test_expect_success 'with post-index-change config' '
mkdir -p internal-hooks &&
write_script internal-hooks/post-command <<-EOF &&
echo ran >post-command.out
EOF
write_script internal-hooks/post-index-change <<-EOF &&
echo ran >post-index-change.out
EOF
# prevent writing of sentinel files to this directory.
test_when_finished chmod 775 internal-hooks &&
chmod a-w internal-hooks &&
git config core.hooksPath internal-hooks &&
# First, show expected behavior.
echo ran >expect &&
rm -f post-command.out post-index-change.out &&
# rev-parse leaves index intact, but runs post-command.
git rev-parse HEAD &&
test_path_is_missing post-index-change.out &&
test_cmp expect post-command.out &&
rm -f post-command.out &&
echo stuff >>file &&
# add updates the index and runs post-command.
git add file &&
test_cmp expect post-index-change.out &&
test_cmp expect post-command.out &&
# Now, show configured behavior
git config postCommand.strategy worktree-change &&
# rev-parse leaves index intact and thus skips post-command.
rm -f post-command.out post-index-change.out &&
git rev-parse HEAD &&
test_path_is_missing post-index-change.out &&
test_path_is_missing post-command.out &&
echo stuff >>file &&
# add keeps the worktree the same, so does not run post-command.
rm -f post-command.out post-index-change.out &&
git add file &&
test_cmp expect post-index-change.out &&
test_path_is_missing post-command.out &&
# add keeps the worktree the same, so does not run post-command.
# and this should work through an alias.
git config alias.addalias add &&
rm -f post-command.out post-index-change.out &&
echo more stuff >>file &&
git addalias file &&
test_cmp expect post-index-change.out &&
test_path_is_missing post-command.out &&
echo stuff >>file &&
# reset --hard updates the worktree.
# even through an alias
git config alias.resetalias "reset --hard" &&
rm -f post-command.out post-index-change.out &&
git resetalias &&
test_cmp expect post-index-change.out &&
test_cmp expect post-command.out &&
rm -f post-command.out &&
test_must_fail git && # get help text
test_path_is_missing post-command.out &&
rm -f post-command.out &&
git version &&
test_path_is_missing post-command.out &&
rm -f post-command.out &&
test_must_fail git typo &&
test_path_is_missing post-command.out
'
test_done