1
+#!/bin/sh
2
+
3
+test_description='git submodule--helper get-default-remote'
4
+
5
+TEST_NO_CREATE_REPO=1
6
+. ./test-lib.sh
7
+
8
+test_expect_success 'setup' '
9
+ git config --global protocol.file.allow always
10
+'
11
+
12
+test_expect_success 'setup repositories' '
13
+ # Create a repository to be used as submodule
14
+ git init sub &&
15
+ test_commit --no-tag -C sub "initial commit in sub" file.txt "sub content" &&
16
+
17
+ # Create main repository
18
+ git init super &&
19
+ (
20
+ cd super &&
21
+ mkdir subdir &&
22
+ test_commit --no-tag -C subdir "initial commit in super" main.txt "super content" &&
23
+ git submodule add ../sub subpath &&
24
+ git commit -m "add submodule 'sub' at subpath"
25
+ )
26
+'
27
+
28
+test_expect_success 'get-default-remote returns origin for initialized submodule' '
29
+ (
30
+ cd super &&
31
+ git submodule update --init &&
32
+ echo "origin" >expect &&
33
+ git submodule--helper get-default-remote subpath >actual &&
34
+ test_cmp expect actual
35
+ )
36
+'
37
+
38
+test_expect_success 'get-default-remote works from subdirectory' '
39
+ (
40
+ cd super/subdir &&
41
+ echo "origin" >expect &&
42
+ git submodule--helper get-default-remote ../subpath >actual &&
43
+ test_cmp expect actual
44
+ )
45
+'
46
+
47
+test_expect_success 'get-default-remote fails with non-existent path' '
48
+ (
49
+ cd super &&
50
+ test_must_fail git submodule--helper get-default-remote nonexistent 2>err &&
51
+ test_grep "could not get a repository handle" err
52
+ )
53
+'
54
+
55
+test_expect_success 'get-default-remote fails with non-submodule path' '
56
+ (
57
+ cd super &&
58
+ test_must_fail git submodule--helper get-default-remote subdir 2>err &&
59
+ test_grep "could not get a repository handle" err
60
+ )
61
+'
62
+
63
+test_expect_success 'get-default-remote fails without path argument' '
64
+ (
65
+ cd super &&
66
+ test_must_fail git submodule--helper get-default-remote 2>err &&
67
+ test_grep "usage:" err
68
+ )
69
+'
70
+
71
+test_expect_success 'get-default-remote fails with too many arguments' '
72
+ (
73
+ cd super &&
74
+ test_must_fail git submodule--helper get-default-remote subpath subdir 2>err &&
75
+ test_grep "usage:" err
76
+ )
77
+'
78
+
79
+test_expect_success 'setup submodule with non-origin default remote name' '
80
+ # Create another submodule path with a different remote name
81
+ (
82
+ cd super &&
83
+ git submodule add ../sub upstream-subpath &&
84
+ git commit -m "add second submodule in upstream-subpath" &&
85
+ git submodule update --init upstream-subpath &&
86
+
87
+ # Change the remote name in the submodule
88
+ cd upstream-subpath &&
89
+ git remote rename origin upstream
90
+ )
91
+'
92
+
93
+test_expect_success 'get-default-remote returns non-origin remote name' '
94
+ (
95
+ cd super &&
96
+ echo "upstream" >expect &&
97
+ git submodule--helper get-default-remote upstream-subpath >actual &&
98
+ test_cmp expect actual
99
+ )
100
+'
101
+
102
+test_expect_success 'get-default-remote handles submodule with multiple remotes' '
103
+ (
104
+ cd super/subpath &&
105
+ git remote add other-upstream ../../sub &&
106
+ git remote add myfork ../../sub
107
+ ) &&
108
+
109
+ (
110
+ cd super &&
111
+ echo "origin" >expect &&
112
+ git submodule--helper get-default-remote subpath >actual &&
113
+ test_cmp expect actual
114
+ )
115
+'
116
+
117
+test_expect_success 'get-default-remote handles submodule with multiple remotes and none are origin' '
118
+ (
119
+ cd super/upstream-subpath &&
120
+ git remote add yet-another-upstream ../../sub &&
121
+ git remote add yourfork ../../sub
122
+ ) &&
123
+
124
+ (
125
+ cd super &&
126
+ echo "upstream" >expect &&
127
+ git submodule--helper get-default-remote upstream-subpath >actual &&
128
+ test_cmp expect actual
129
+ )
130
+'
131
+
132
+test_expect_success 'setup nested submodule with non-origin remote' '
133
+ git init innersub &&
134
+ test_commit --no-tag -C innersub "initial commit in innersub" inner.txt "innersub content" &&
135
+
136
+ (
137
+ cd sub &&
138
+ git submodule add ../innersub innersubpath &&
139
+ git commit -m "add nested submodule at innersubpath"
140
+ ) &&
141
+
142
+ (
143
+ cd super/upstream-subpath &&
144
+ git pull upstream &&
145
+ git submodule update --init --recursive . &&
146
+ (
147
+ cd innersubpath &&
148
+ git remote rename origin another_upstream
149
+ )
150
+ )
151
+'
152
+
153
+test_expect_success 'get-default-remote works with nested submodule' '
154
+ (
155
+ cd super &&
156
+ echo "another_upstream" >expect &&
157
+ git submodule--helper get-default-remote upstream-subpath/innersubpath >actual &&
158
+ test_cmp expect actual
159
+ )
160
+'
161
+
162
+test_expect_success 'get-default-remote works with submodule that has no remotes' '
163
+ # Create a submodule directory manually without remotes
164
+ (
165
+ cd super &&
166
+ git init no-remote-sub &&
167
+ test_commit --no-tag -C no-remote-sub "local commit" local.txt "local content"
168
+ ) &&
169
+
170
+ # Add it as a submodule
171
+ (
172
+ cd super &&
173
+ git submodule add ./no-remote-sub &&
174
+ git commit -m "add local submodule 'no-remote-sub'"
175
+ ) &&
176
+
177
+ (
178
+ cd super &&
179
+ # Should fall back to "origin" remote name when no remotes exist
180
+ echo "origin" >expect &&
181
+ git submodule--helper get-default-remote no-remote-sub >actual &&
182
+ test_cmp expect actual
183
+ )
184
+'
185
+
186
+test_done