refs.c: make prune_ref use a transaction to delete the ref
[git/mingw.git] / t / lib-git-p4.sh
blob5aa8adcf9c8a53b8a643a37b9474886448f6a45b
2 # Library code for git p4 tests
5 # p4 tests never use the top-level repo; always build/clone into
6 # a subdirectory called "$git"
7 TEST_NO_CREATE_REPO=NoThanks
9 . ./test-lib.sh
11 if ! test_have_prereq PYTHON
12 then
13 skip_all='skipping git p4 tests; python not available'
14 test_done
16 ( p4 -h && p4d -h ) >/dev/null 2>&1 || {
17 skip_all='skipping git p4 tests; no p4 or p4d'
18 test_done
21 # On cygwin, the NT version of Perforce can be used. When giving
22 # it paths, either on the command-line or in client specifications,
23 # be sure to use the native windows form.
25 # Older versions of perforce were available compiled natively for
26 # cygwin. Those do not accept native windows paths, so make sure
27 # not to convert for them.
28 native_path() {
29 path="$1" &&
30 if test_have_prereq CYGWIN && ! p4 -V | grep -q CYGWIN
31 then
32 path=$(cygpath --windows "$path")
33 else
34 path=$(test-path-utils real_path "$path")
35 fi &&
36 echo "$path"
39 # Try to pick a unique port: guess a large number, then hope
40 # no more than one of each test is running.
42 # This does not handle the case where somebody else is running the
43 # same tests and has chosen the same ports.
44 testid=${this_test#t}
45 git_p4_test_start=9800
46 P4DPORT=$((10669 + ($testid - $git_p4_test_start)))
48 P4PORT=localhost:$P4DPORT
49 P4CLIENT=client
50 P4USER=author
51 P4EDITOR=true
52 unset P4CHARSET
53 export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET
55 db="$TRASH_DIRECTORY/db"
56 cli="$TRASH_DIRECTORY/cli"
57 git="$TRASH_DIRECTORY/git"
58 pidfile="$TRASH_DIRECTORY/p4d.pid"
60 # git p4 submit generates a temp file, which will
61 # not get cleaned up if the submission fails. Don't
62 # clutter up /tmp on the test machine.
63 TMPDIR="$TRASH_DIRECTORY"
64 export TMPDIR
66 start_p4d() {
67 mkdir -p "$db" "$cli" "$git" &&
68 rm -f "$pidfile" &&
70 cd "$db" &&
72 p4d -q -p $P4DPORT &
73 echo $! >"$pidfile"
75 ) &&
77 # This gives p4d a long time to start up, as it can be
78 # quite slow depending on the machine. Set this environment
79 # variable to something smaller to fail faster in, say,
80 # an automated test setup. If the p4d process dies, that
81 # will be caught with the "kill -0" check below.
82 i=${P4D_START_PATIENCE:-300}
83 pid=$(cat "$pidfile")
84 ready=
85 while test $i -gt 0
87 # succeed when p4 client commands start to work
88 if p4 info >/dev/null 2>&1
89 then
90 ready=true
91 break
93 # fail if p4d died
94 kill -0 $pid 2>/dev/null || break
95 echo waiting for p4d to start
96 sleep 1
97 i=$(( $i - 1 ))
98 done
100 if test -z "$ready"
101 then
102 # p4d failed to start
103 return 1
106 # build a p4 user so author@example.com has an entry
107 p4_add_user author
109 # build a client
110 client_view "//depot/... //client/..." &&
112 return 0
115 p4_add_user() {
116 name=$1 &&
117 p4 user -f -i <<-EOF
118 User: $name
119 Email: $name@example.com
120 FullName: Dr. $name
124 kill_p4d() {
125 pid=$(cat "$pidfile")
126 # it had better exist for the first kill
127 kill $pid &&
128 for i in 1 2 3 4 5 ; do
129 kill $pid >/dev/null 2>&1 || break
130 sleep 1
131 done &&
132 # complain if it would not die
133 test_must_fail kill $pid >/dev/null 2>&1 &&
134 rm -rf "$db" "$cli" "$pidfile"
137 cleanup_git() {
138 rm -rf "$git" &&
139 mkdir "$git"
142 marshal_dump() {
143 what=$1 &&
144 line=${2:-1} &&
145 cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
146 import marshal
147 import sys
148 for i in range($line):
149 d = marshal.load(sys.stdin)
150 print d['$what']
152 "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
156 # Construct a client with this list of View lines
158 client_view() {
160 cat <<-EOF &&
161 Client: $P4CLIENT
162 Description: $P4CLIENT
163 Root: $cli
164 AltRoots: $(native_path "$cli")
165 LineEnd: unix
166 View:
168 printf "\t%s\n" "$@"
169 ) | p4 client -i
172 is_cli_file_writeable() {
173 # cygwin version of p4 does not set read-only attr,
174 # will be marked 444 but -w is true
175 file="$1" &&
176 if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
177 then
178 stat=$(stat --format=%a "$file") &&
179 test $stat = 644
180 else
181 test -w "$file"