-
Notifications
You must be signed in to change notification settings - Fork 18
/
safe_git_test.sh
executable file
·437 lines (372 loc) · 13.6 KB
/
safe_git_test.sh
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/bin/sh
# If you only want to run a subset of tests, run this with the
# tests you want to run as arguments. You can get a list by
# running this script with `-l` or `--list`.
# All failures are fatal!
set -e
ROOT=/tmp/safe_git_test_repos
SAFE_GIT="$PWD/safe_git.sh"
# $1: filename to check
# $2: expected contents. Can use "\n" for newline. Be sure to put it in
# single-quotes!
_assert_file() {
/bin/echo -ne "$2" | cmp - "$1" || {
echo "$1: unexpected contents."
echo "Expected:"
echo "---"
/bin/echo -ne "$2"
echo "---"
echo "Actual:"
echo "---"
cat "$1"
echo "---"
exit 1
}
}
# $1: the dir of the repo you want to verify is at master.
# $2: the dir of the same repo in "origin"
_verify_at_master() {
diff -r -u -x .git "$1" "$2"
}
# $1: filename (relative to the repo-root)
# $2 (optional): text to append to the filename each commit, defaults to $1
# $3 (optional): number of commits to do (defaults to 1)
create_git_history() {
for i in `seq ${3-1}`; do
echo "${2-$1}" >> "$1"
git add -f "$1"
git commit -m "$1: commit #$i"
done
}
create_test_repos() {
(
mkdir -p origin
cd origin
git init subrepo1
git init subrepo2
git init subrepo3
git init "subrepo3 sub"
git init subrepo_extra
cd subrepo1
create_git_history ".gitignore" "README"
create_git_history "foo" "foo subrepo1" 3
create_git_history "bar" "bar subrepo1" 3
cd ../subrepo2
create_git_history "foo" "foo subrepo2" 3
cd "../subrepo3 sub"
create_git_history "foo" "foo subrepo3 sub" 3
cd ../subrepo3
create_git_history "foo" "foo subrepo3" 3
git submodule add "../subrepo3 sub"
git commit -a -m "Added sub-subrepo"
# This one isn't in the repo by default, but is used for tests
# that add a new submodule.
cd ../subrepo_extra
create_git_history "foo" "foo subrepo_extra" 3
cd ..
git init repo
cd repo
git submodule add ../subrepo1
git submodule add ../subrepo2
git submodule add ../subrepo3
git commit -a -m "Added subrepos"
create_git_history "foo" "foo" 3
create_git_history "bar" "bar" 3
git submodule update --init --recursive
)
}
# --- Tests that we overwrite local changes (that are not reflected
# --- in the remote)
test_make_sure_sync_to_origin_actually_takes_us_to_master() {
_verify_at_master repo ../origin/repo
}
test_sync_to_a_previous_commit_and_make_sure_we_go_back() {
( cd repo; git reset --hard HEAD^ )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_check_in_a_change_local_only_and_make_sure_sync_to_origin_ignores_it() {
sha1=`cd repo && git rev-parse HEAD`
( cd repo; create_git_history "foo" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "$sha1"
_verify_at_master repo ../origin/repo
}
test_check_in_some_submodule_changes() {
sha1=`cd repo && git rev-parse HEAD`
( cd repo/subrepo1; create_git_history "foo" )
( cd repo/subrepo2; create_git_history "foo" )
( cd repo/subrepo3/subrepo3\ sub; create_git_history "foo" )
( cd repo/subrepo3; git commit -am "Sub-submodule" )
( cd repo; git commit -am "Submodules" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "$sha1"
_verify_at_master repo ../origin/repo
}
test_change_some_files_without_checking_them_in() {
# Likewise, check in a submodule change but don't update substate.
sha1=`cd repo && git rev-parse HEAD`
echo "foo" >> repo/foo
echo "foo" >> repo/subrepo1/foo
echo "foo" >> repo/subrepo3/subrepo3\ sub/foo
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "$sha1"
_verify_at_master repo ../origin/repo
}
test_add_some_files_without_checking_them_in() {
sha1=`cd repo && git rev-parse HEAD`
echo "foo" >> repo/newfoo
echo "foo" >> repo/subrepo1/newfoo
echo "foo" >> repo/subrepo3/subrepo3\ sub/newfoo
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "$sha1"
_verify_at_master repo ../origin/repo
}
test_delete_some_files_without_checking_them_in() {
sha1=`cd repo && git rev-parse HEAD`
rm repo/foo
rm repo/subrepo1/foo
rm repo/subrepo3/subrepo3\ sub/foo
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "$sha1"
_verify_at_master repo ../origin/repo
}
test_delete_a_submodule_directory_and_make_sure_we_get_it_back() {
rm -rf repo/subrepo2
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_add_a_directory_and_make_sure_it_goes_away() {
mkdir -p repo/empty_dir
echo "new" repo/empty_dir/new
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
# TODO(csilvers): delete empty dirs
#_verify_at_master repo ../origin/repo
}
test_add_a_submodule_locally_and_make_sure_it_goes_away() {
( cd repo
git submodule add ../../origin/subrepo_extra
git commit -a -m "Added new subrepo"
)
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_delete_a_submodule_locally_and_make_sure_it_comes_back() {
( cd repo
git rm subrepo3
git commit -a -m "Deleted subrepo"
)
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_replace_a_submodule_with_a_file_locally() {
( cd repo
git rm subrepo3
git commit -a -m "Deleted subrepo"
echo "now a file" > subrepo3
git add subrepo3
git commit -a -m "Repo is now a file"
)
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_replace_a_file_with_a_submodule_locally() {
( cd repo
git rm foo
git commit -a -m "Deleted file"
git submodule add ../../origin/subrepo_extra
git commit -a -m "File is now a submodule"
)
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
# --- Tests that changes to the remote are reflected faithfully locally
test_update_a_file() {
( cd ../origin/repo; create_git_history "foo" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_add_a_file() {
( cd ../origin/repo; create_git_history "baz" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_delete_a_file() {
( cd ../origin/repo; git rm bar; git commit -am "deleted bar" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_update_substate() {
( cd ../origin/subrepo1; create_git_history "foo" )
( cd ../origin/repo/subrepo1; git pull;
cd ..; git commit -am "Submodules" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_update_substate_and_modify_locally() {
( cd ../origin/subrepo1; create_git_history "foo" )
( cd ../origin/repo/subrepo1; git pull;
cd ..; git commit -am "Submodules" )
echo "moar foo" >> repo/subrepo1/foo
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_update_subsubstate() {
( cd ../origin/subrepo3\ sub; create_git_history "foo" )
( cd ../origin/subrepo3/subrepo3\ sub; git checkout master; git pull;
cd ..; git commit -am "Sub-submodule" )
( cd ../origin/repo/subrepo3; git checkout master; git pull; git submodule update --recursive;
cd ..; git commit -am "Submodules" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_rollback_some_substate() {
( cd ../origin/subrepo1; git reset --hard HEAD^ )
( cd ../origin/repo/subrepo1; git pull;
cd ..; git commit -am "Submodules" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_rollback_subsubstate() {
( cd ../origin/subrepo3\ sub; git reset --hard HEAD^ )
( cd ../origin/repo/subrepo3/subrepo3\ sub; git pull;
cd ..; git commit -am "Sub-submodule";
cd ..; git commit -am "Submodules" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_add_a_new_submodule() {
( cd ../origin/repo; git submodule add ../subrepo2 subrepo2_again;
git commit -am "New submodule" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_delete_a_submodule() {
( cd ../origin/repo; git rm subrepo3; git commit -am "Nix subrepo" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_delete_a_subsubmodule() {
( cd ../origin/subrepo3; git rm subrepo3\ sub; git commit -am "Nix subsubrepo" )
( cd ../origin/repo/subrepo3; git checkout master; git pull; git clean -ffd;
cd ..; git commit -am "Update subrepo";
git submodule update --recursive )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_delete_a_submodule_with_gitignored_files() {
( cd ../origin/subrepo1; create_git_history "README" )
( cd ../origin/repo/subrepo1; git pull;
cd ..; git commit -am "Submodules" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
( cd ../origin/repo; git rm subrepo1; git commit -am "Nix subrepo" )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_change_what_a_submodule_points_to() {
( cd ../origin/repo;
perl -pli -e 's,url = ../subrepo2,url = ../subrepo1,' .gitmodules;
git submodule sync; git submodule update;
cd subrepo2; git checkout master;
git reset --hard origin/master; cd -;
git commit -am "Repointed submodule";
git submodule update --init --recursive )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_replace_a_submodule_with_a_file() {
( cd ../origin/repo
git rm subrepo3
git commit -a -m "Deleted subrepo"
echo "now a file" > subrepo3
git add subrepo3
git commit -a -m "Repo is now a file"
)
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_replace_a_file_with_a_submodule() {
( cd ../origin/repo
git rm foo
git commit -a -m "Deleted file"
git submodule add ../../origin/subrepo_extra
git commit -a -m "File is now a submodule"
)
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
test_rollback_some_substate_when_rolling_back_the_repo() {
( cd ../origin/subrepo1;
create_git_history "Subrepo history (to be rolled back)" )
( cd ../origin/repo/subrepo1; git fetch origin;
git reset --hard origin/master;
cd ..; git commit -am "Substate repo1 (to be rolled back)")
( cd ../origin/subrepo3\ sub;
create_git_history "Sub-subrepo history (to be rolled back)" )
( cd ../origin/repo/subrepo3/subrepo3\ sub; git fetch origin;
git reset --hard origin/master;
cd ..; git commit -am "Sub-substate repo3_sub (to be rolled back)";
cd ..; git commit -am "Substate repo3 (to be rolled back)")
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
( cd ../origin/repo; git reset --hard HEAD^;
git submodule update --recursive )
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
_verify_at_master repo ../origin/repo
}
# --- Tests of pull_in_branch
test_pulling_does_not_reset_submodule_branch_refs() {
# Commit something to master from within repo's copy of subrepo1
( cd repo/subrepo1; git checkout master; git pull; create_git_history "new" )
# Pulling in repo will change the pointer to HEAD in repo's copy
# of subrepo1, but it should *not* change the pointer to master.
"$SAFE_GIT" pull_in_branch repo master
subrepo_master_commitmsg=`cd repo/subrepo1; git log master --pretty=format:'%s' -n 1`
[ "$subrepo_master_commitmsg" = "new: commit #1" ]
}
# Introspection, shell-script style!
ALL_TESTS=`grep -o '^test_[^(]*()' "$0" | tr -d '()'`
# Let's make sure we didn't define two tests with the same name.
duplicate_tests=`echo "$ALL_TESTS" | sort | uniq -d`
if [ -n "$duplicate_tests" ]; then
echo "Defined multiple tests with the same name:"
echo "$duplicate_tests"
exit 1
fi
if [ "$1" = "-l" -o "$1" = "--list" ]; then
echo "Tests you can run:"
echo "$ALL_TESTS"
exit 0
elif [ -n "$1" ]; then # they specified which tests to run
tests_to_run="$@"
else
tests_to_run="$ALL_TESTS"
fi
rm -rf "$ROOT"
mkdir -p "$ROOT"
cd "$ROOT"
# Set the envvars that safe_git.sh looks at
export WORKSPACE_ROOT="$ROOT"
export REPOS_ROOT="$ROOT/repositories"
export SLACK_CHANNEL=.
mkdir -p "$WORKSPACE_ROOT" "$REPOS_ROOT"
create_test_repos
cp -a origin origin.clean
failed_tests=""
for test in $tests_to_run; do
(
echo "--- $test"
# Reset origin/ back to the start state.
rm -rf origin
cp -a origin.clean origin
# Set up the workspace to match master.
mkdir -p "$test"
cd "$test"
export WORKSPACE_ROOT=.
"$SAFE_GIT" sync_to_origin "$ROOT/origin/repo" "master"
# Run the test!
$test
) && echo "PASS: $test" || failed_tests="$failed_tests $test"
done
if [ -n "$failed_tests" ]; then
echo "FAILED"
echo "To re-run failed tests, run:"
echo " $0$failed_tests"
else
echo "All done! PASS"
fi