432356863621fa82fb52bd7e6cf00b3edc3a852b
[scm/test.git] / test / test-unlock.sh
1 #!/usr/bin/env bash
2
3 . "test/testlib.sh"
4
5 begin_test "unlocking a lock by path"
6 (
7   set -e
8
9   reponame="unlock_by_path"
10   setup_remote_repo_with_file "unlock_by_path" "c.dat"
11
12   git lfs lock --json "c.dat" | tee lock.log
13
14   id=$(assert_lock lock.log c.dat)
15   assert_server_lock "$reponame" "$id"
16
17   git lfs unlock "c.dat" 2>&1 | tee unlock.log
18   refute_server_lock "$reponame" "$id"
19 )
20 end_test
21
22 begin_test "unlocking a file makes it readonly"
23 (
24   set -e
25
26   reponame="unlock_set_readonly"
27   setup_remote_repo_with_file "$reponame" "c.dat"
28
29   git lfs lock --json "c.dat"
30   assert_file_writeable c.dat
31
32   git lfs unlock "c.dat"
33   refute_file_writeable c.dat
34 )
35 end_test
36
37 begin_test "unlocking a file makes ignores readonly"
38 (
39   set -e
40
41   reponame="unlock_set_readonly_ignore"
42   setup_remote_repo_with_file "$reponame" "c.dat"
43
44   git lfs lock --json "c.dat"
45   assert_file_writeable c.dat
46
47   git -c lfs.setlockablereadonly=false lfs unlock "c.dat"
48   assert_file_writeable c.dat
49 )
50 end_test
51
52 begin_test "force unlocking lock with missing file"
53 (
54   set -e
55
56   reponame="force-unlock-missing-file"
57   setup_remote_repo_with_file "$reponame" "a.dat"
58
59   git lfs lock --json "a.dat" | tee lock.log
60   id=$(assert_lock lock.log a.dat)
61   assert_server_lock "$reponame" "$id"
62
63   git rm a.dat
64   git commit -m "a.dat"
65   rm *.log *.json # ensure clean git status
66   git status
67
68   git lfs unlock "a.dat" 2>&1 | tee unlock.log
69   grep "Unable to determine path" unlock.log
70   assert_server_lock "$reponame" "$id"
71
72   rm unlock.log
73   git lfs unlock --force "a.dat" 2>&1 | tee unlock.log
74   refute_server_lock "$reponame" "$id"
75 )
76 end_test
77
78 begin_test "unlocking a lock (--json)"
79 (
80   set -e
81
82   reponame="unlock_by_path_json"
83   setup_remote_repo_with_file "$reponame" "c_json.dat"
84
85   git lfs lock --json "c_json.dat" | tee lock.log
86
87   id=$(assert_lock lock.log c_json.dat)
88   assert_server_lock "$reponame" "$id"
89
90   git lfs unlock --json "c_json.dat" 2>&1 | tee unlock.log
91   grep "\"unlocked\":true" unlock.log
92
93   refute_server_lock "$reponame" "$id"
94 )
95 end_test
96
97 begin_test "unlocking a lock by id"
98 (
99   set -e
100
101   reponame="unlock_by_id"
102   setup_remote_repo_with_file "unlock_by_id" "d.dat"
103
104   git lfs lock --json "d.dat" | tee lock.log
105
106   id=$(assert_lock lock.log d.dat)
107   assert_server_lock "$reponame" "$id"
108
109   git lfs unlock --id="$id"
110   refute_server_lock "$reponame" "$id"
111 )
112 end_test
113
114 begin_test "unlocking a lock without sufficient info"
115 (
116   set -e
117
118   reponame="unlock_ambiguous"
119   setup_remote_repo_with_file "$reponame" "e.dat"
120
121   git lfs lock --json "e.dat" | tee lock.log
122
123   id=$(assert_lock lock.log e.dat)
124   assert_server_lock "$reponame" "$id"
125
126   git lfs unlock 2>&1 | tee unlock.log
127   grep "Usage: git lfs unlock" unlock.log
128   assert_server_lock "$reponame" "$id"
129 )
130 end_test
131
132 begin_test "unlocking a lock while uncommitted"
133 (
134   set -e
135
136   reponame="unlock_modified"
137   setup_remote_repo_with_file "$reponame" "mod.dat"
138
139   git lfs lock --json "mod.dat" | tee lock.log
140
141   id=$(assert_lock lock.log mod.dat)
142   assert_server_lock "$reponame" "$id"
143
144   echo "\nSomething" >> mod.dat
145
146   git lfs unlock "mod.dat" 2>&1 | tee unlock.log
147   [ ${PIPESTATUS[0]} -ne "0" ]
148
149   grep "Cannot unlock file with uncommitted changes" unlock.log
150
151   assert_server_lock "$reponame" "$id"
152
153   # should allow after discard
154   git checkout mod.dat
155   git lfs unlock "mod.dat" 2>&1 | tee unlock.log
156   refute_server_lock "$reponame" "$id"
157 )
158 end_test
159
160 begin_test "unlocking a lock with ambiguious arguments"
161 (
162   set -e
163
164   reponame="unlock_ambiguious_args"
165   setup_remote_repo_with_file "$reponame" "a.dat"
166
167   git lfs lock --json "a.dat" | tee lock.log
168
169   id=$(assert_lock lock.log a.dat)
170   assert_server_lock "$reponame" "$id"
171
172   git lfs unlock --id "$id" a.dat 2>&1 | tee unlock.log
173   if [ "0" -eq "${PIPESTATUS[0]}" ]; then
174     echo >&2 "expected ambiguous \`git lfs unlock\` command to exit, didn't"
175     exit 1
176   fi
177
178   grep "Usage:" unlock.log
179   assert_server_lock "$reponame" "$id"
180 )
181 end_test
182
183 begin_test "unlocking a lock while uncommitted with --force"
184 (
185   set -e
186
187   reponame="unlock_modified_force"
188   setup_remote_repo_with_file "$reponame" "modforce.dat"
189
190   git lfs lock --json "modforce.dat" | tee lock.log
191
192   id=$(assert_lock lock.log modforce.dat)
193   assert_server_lock "$reponame" "$id"
194
195   echo "\nSomething" >> modforce.dat
196
197   # should allow with --force
198   git lfs unlock --force "modforce.dat" 2>&1 | tee unlock.log
199   grep "Warning: unlocking with uncommitted changes" unlock.log
200   refute_server_lock "$reponame" "$id"
201 )
202 end_test
203
204 begin_test "unlocking a lock while untracked"
205 (
206   set -e
207
208   reponame="unlock_untracked"
209   setup_remote_repo_with_file "$reponame" "notrelevant.dat"
210
211   git lfs track "*.dat"
212   # Create file but don't add it to git
213   # Shouldn't be able to unlock it
214   echo "something" > untracked.dat
215   git lfs lock --json "untracked.dat" | tee lock.log
216
217   id=$(assert_lock lock.log untracked.dat)
218   assert_server_lock "$reponame" "$id"
219
220   git lfs unlock "untracked.dat" 2>&1 | tee unlock.log
221   [ ${PIPESTATUS[0]} -ne "0" ]
222
223   grep "Cannot unlock file with uncommitted changes" unlock.log
224
225   assert_server_lock "$reponame" "$id"
226
227   # should allow after add/commit
228   git add untracked.dat
229   git commit -m "Added untracked"
230   git lfs unlock "untracked.dat" 2>&1 | tee unlock.log
231   refute_server_lock "$reponame" "$id"
232 )
233 end_test