1#!/bin/sh
2#
3# Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
4#
5
6test_description='Test reading/writing .gitmodules when not in the working tree
7
8This test verifies that, when .gitmodules is in the current branch but is not
9in the working tree reading from it still works but writing to it does not.
10
11The test setup uses a sparse checkout, however the same scenario can be set up
12also by committing .gitmodules and then just removing it from the filesystem.
13'
14
15. ./test-lib.sh
16
17test_expect_success 'sparse checkout setup which hides .gitmodules' '
18 git init upstream &&
19 git init submodule &&
20 (cd submodule &&
21 echo file >file &&
22 git add file &&
23 test_tick &&
24 git commit -m "Add file"
25 ) &&
26 (cd upstream &&
27 git submodule add ../submodule &&
28 test_tick &&
29 git commit -m "Add submodule"
30 ) &&
31 git clone upstream super &&
32 (cd super &&
33 cat >.git/info/sparse-checkout <<-\EOF &&
34 /*
35 !/.gitmodules
36 EOF
37 git config core.sparsecheckout true &&
38 git read-tree -m -u HEAD &&
39 test_path_is_missing .gitmodules
40 )
41'
42
43test_expect_success 'reading gitmodules config file when it is not checked out' '
44 echo "../submodule" >expect &&
45 git -C super submodule--helper config submodule.submodule.url >actual &&
46 test_cmp expect actual
47'
48
49test_expect_success 'not writing gitmodules config file when it is not checked out' '
50 test_must_fail git -C super submodule--helper config submodule.submodule.url newurl &&
51 test_path_is_missing super/.gitmodules
52'
53
54test_expect_success 'initialising submodule when the gitmodules config is not checked out' '
55 test_must_fail git -C super config submodule.submodule.url &&
56 git -C super submodule init &&
57 git -C super config submodule.submodule.url >actual &&
58 echo "$(pwd)/submodule" >expect &&
59 test_cmp expect actual
60'
61
62test_expect_success 'updating submodule when the gitmodules config is not checked out' '
63 test_path_is_missing super/submodule/file &&
64 git -C super submodule update &&
65 test_cmp submodule/file super/submodule/file
66'
67
68ORIG_SUBMODULE=$(git -C submodule rev-parse HEAD)
69ORIG_UPSTREAM=$(git -C upstream rev-parse HEAD)
70ORIG_SUPER=$(git -C super rev-parse HEAD)
71
72test_expect_success 're-updating submodule when the gitmodules config is not checked out' '
73 test_when_finished "git -C submodule reset --hard $ORIG_SUBMODULE;
74 git -C upstream reset --hard $ORIG_UPSTREAM;
75 git -C super reset --hard $ORIG_SUPER;
76 git -C upstream submodule update --remote;
77 git -C super pull;
78 git -C super submodule update --remote" &&
79 (cd submodule &&
80 echo file2 >file2 &&
81 git add file2 &&
82 test_tick &&
83 git commit -m "Add file2 to submodule"
84 ) &&
85 (cd upstream &&
86 git submodule update --remote &&
87 git add submodule &&
88 test_tick &&
89 git commit -m "Update submodule"
90 ) &&
91 git -C super pull &&
92 # The --for-status options reads the gitmodules config
93 git -C super submodule summary --for-status >actual &&
94 rev1=$(git -C submodule rev-parse --short HEAD) &&
95 rev2=$(git -C submodule rev-parse --short HEAD^) &&
96 cat >expect <<-EOF &&
97 * submodule ${rev1}...${rev2} (1):
98 < Add file2 to submodule
99
100 EOF
101 test_cmp expect actual &&
102 # Test that the update actually succeeds
103 test_path_is_missing super/submodule/file2 &&
104 git -C super submodule update &&
105 test_cmp submodule/file2 super/submodule/file2 &&
106 git -C super status --short >output &&
107 test_must_be_empty output
108'
109
110test_expect_success 'not adding submodules when the gitmodules config is not checked out' '
111 git clone submodule new_submodule &&
112 test_must_fail git -C super submodule add ../new_submodule &&
113 test_path_is_missing .gitmodules
114'
115
116# This test checks that the previous "git submodule add" did not leave the
117# repository in a spurious state when it failed.
118test_expect_success 'init submodule still works even after the previous add failed' '
119 git -C super submodule init
120'
121
122test_done