Raw
1 #!/bin/sh
2
3 test_description='lock file PID info tests
4
5 Tests for PID info file alongside lock files.
6 The feature is opt-in via core.lockfilePid config setting (boolean).
7 '
8
9 . ./test-lib.sh
10
11 test_expect_success 'stale lock detected when PID is not running' '
12 git init repo &&
13 (
14 cd repo &&
15 touch .git/index.lock &&
16 printf "pid 99999" >.git/index~pid.lock &&
17 test_must_fail git -c core.lockfilePid=true add . 2>err &&
18 test_grep "process 99999, which is no longer running" err &&
19 test_grep "appears to be stale" err
20 )
21 '
22
23 test_expect_success 'PID info not shown by default' '
24 git init repo2 &&
25 (
26 cd repo2 &&
27 touch .git/index.lock &&
28 printf "pid 99999" >.git/index~pid.lock &&
29 test_must_fail git add . 2>err &&
30 # Should not crash, just show normal error without PID
31 test_grep "Unable to create" err &&
32 test_grep ! "is held by process" err
33 )
34 '
35
36 test_expect_success 'running process detected when PID is alive' '
37 git init repo3 &&
38 (
39 cd repo3 &&
40 echo content >file &&
41 # Get the correct PID for this platform
42 shell_pid=$$ &&
43 if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
44 then
45 # In Git for Windows, Bash uses MSYS2 PIDs but git.exe
46 # uses Windows PIDs. Use the Windows PID.
47 shell_pid=$(cat /proc/$shell_pid/winpid)
48 fi &&
49 # Create a lock and PID file with current shell PID (which is running)
50 touch .git/index.lock &&
51 printf "pid %d" "$shell_pid" >.git/index~pid.lock &&
52 # Verify our PID is shown in the error message
53 test_must_fail git -c core.lockfilePid=true add file 2>err &&
54 test_grep "held by process $shell_pid" err
55 )
56 '
57
58 test_expect_success 'PID info file cleaned up on successful operation when enabled' '
59 git init repo4 &&
60 (
61 cd repo4 &&
62 echo content >file &&
63 git -c core.lockfilePid=true add file &&
64 # After successful add, no lock or PID files should exist
65 test_path_is_missing .git/index.lock &&
66 test_path_is_missing .git/index~pid.lock
67 )
68 '
69
70 test_expect_success 'no PID file created by default' '
71 git init repo5 &&
72 (
73 cd repo5 &&
74 echo content >file &&
75 git add file &&
76 # PID file should not be created when feature is disabled
77 test_path_is_missing .git/index~pid.lock
78 )
79 '
80
81 test_expect_success 'core.lockfilePid=false does not create PID file' '
82 git init repo6 &&
83 (
84 cd repo6 &&
85 echo content >file &&
86 git -c core.lockfilePid=false add file &&
87 # PID file should not be created when feature is disabled
88 test_path_is_missing .git/index~pid.lock
89 )
90 '
91
92 test_expect_success 'existing PID files are read even when feature disabled' '
93 git init repo7 &&
94 (
95 cd repo7 &&
96 touch .git/index.lock &&
97 printf "pid 99999" >.git/index~pid.lock &&
98 # Even with lockfilePid disabled, existing PID files are read
99 # to help diagnose stale locks
100 test_must_fail git add . 2>err &&
101 test_grep "process 99999" err
102 )
103 '
104
105 test_done