Commit a9c2d4c277afebe2aff309e7b43a32185fc37cd1

Stefan Sperling 2020-09-24T10:53:18

fix default branch name written to Git config file by 'got clone' The generated Git config file instructed Git to fetch all branches rather than the default branch as was intended. Check generated configuration files in all clone tests in order to catch such problems in the future.

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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
diff --git a/got/got.c b/got/got.c
index 16044da..3cdf1de 100644
--- a/got/got.c
+++ b/got/got.c
@@ -923,18 +923,17 @@ fetch_progress(void *arg, const char *message, off_t packfile_size,
 }
 
 static const struct got_error *
-create_symref(const char *refname, struct got_reference *target_ref,
-    int verbosity, struct got_repository *repo)
+create_symref(struct got_reference **head_symref, const char *refname,
+    struct got_reference *target_ref, int verbosity,
+    struct got_repository *repo)
 {
 	const struct got_error *err;
-	struct got_reference *head_symref;
 
-	err = got_ref_alloc_symref(&head_symref, refname, target_ref);
+	err = got_ref_alloc_symref(head_symref, refname, target_ref);
 	if (err)
 		return err;
 
-	err = got_ref_write(head_symref, repo);
-	got_ref_close(head_symref);
+	err = got_ref_write(*head_symref, repo);
 	if (err == NULL && verbosity > 0) {
 		printf("Created reference %s: %s\n", GOT_REF_HEAD,
 		    got_ref_get_name(target_ref));
@@ -1063,7 +1062,7 @@ cmd_clone(int argc, char *argv[])
 	const char *uri, *dirname;
 	char *proto, *host, *port, *repo_name, *server_path;
 	char *default_destdir = NULL, *id_str = NULL;
-	const char *repo_path, *remote_repo_path;
+	const char *repo_path;
 	struct got_repository *repo = NULL;
 	struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
 	struct got_pathlist_entry *pe;
@@ -1078,7 +1077,7 @@ cmd_clone(int argc, char *argv[])
 	ssize_t n;
 	int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
 	int list_refs_only = 0;
-	struct got_reference *head_symref = NULL;
+	struct got_reference *default_head = NULL;
 
 	TAILQ_INIT(&refs);
 	TAILQ_INIT(&symrefs);
@@ -1301,6 +1300,7 @@ cmd_clone(int argc, char *argv[])
 		const char *refname = pe->path;
 		const char *target = pe->data;
 		char *remote_refname = NULL, *remote_target = NULL;
+		struct got_reference *head_symref;
 
 		if (strcmp(refname, GOT_REF_HEAD) != 0)
 			continue;
@@ -1314,11 +1314,18 @@ cmd_clone(int argc, char *argv[])
 			goto done;
 		}
 
-		error = create_symref(refname, target_ref, verbosity, repo);
+		error = create_symref(&head_symref, refname, target_ref,
+		    verbosity, repo);
 		got_ref_close(target_ref);
 		if (error)
 			goto done;
 
+		/* First HEAD reference listed is the default branch. */
+		if (default_head == NULL)
+			default_head = head_symref;
+		else
+			got_ref_close(head_symref);
+
 		if (mirror_references)
 			continue;
 
@@ -1348,11 +1355,12 @@ cmd_clone(int argc, char *argv[])
 			}
 			goto done;
 		}
-		error = create_symref(remote_refname, target_ref,
-		    verbosity - 1, repo);
+		error = create_symref(&head_symref, remote_refname,
+		    target_ref, verbosity - 1, repo);
 		free(remote_refname);
 		free(remote_target);
 		got_ref_close(target_ref);
+		got_ref_close(head_symref);
 		if (error)
 			goto done;
 	}
@@ -1375,8 +1383,8 @@ cmd_clone(int argc, char *argv[])
 				goto done;
 			}
 
-			error = create_symref(GOT_REF_HEAD, target_ref,
-			    verbosity, repo);
+			error = create_symref(&default_head, GOT_REF_HEAD,
+			    target_ref, verbosity, repo);
 			got_ref_close(target_ref);
 			if (error)
 				goto done;
@@ -1396,9 +1404,6 @@ cmd_clone(int argc, char *argv[])
 		goto done;
 	}
 	got_path_strip_trailing_slashes(server_path);
-	remote_repo_path = server_path;
-	while (remote_repo_path[0] == '/')
-		remote_repo_path++;
 	if (asprintf(&gotconfig,
 	    "remote \"%s\" {\n"
 	    "\tserver %s\n"
@@ -1409,7 +1414,7 @@ cmd_clone(int argc, char *argv[])
 	    "}\n",
 	    GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
 	    port ? "\tport " : "", port ? port : "", port ? "\n" : "",
-	    remote_repo_path,
+	    server_path,
 	    mirror_references ? "\tmirror-references yes\n" : "") == -1) {
 		error = got_error_from_errno("asprintf");
 		goto done;
@@ -1458,8 +1463,8 @@ cmd_clone(int argc, char *argv[])
 		 * If the server specified a default branch, use just that one.
 		 * Otherwise fall back to fetching all branches on next fetch.
 		 */
-		if (head_symref) {
-			branchname = got_ref_get_symref_target(head_symref);
+		if (default_head) {
+			branchname = got_ref_get_symref_target(default_head);
 			if (strncmp(branchname, "refs/heads/", 11) == 0)
 				branchname += 11;
 		} else
@@ -1499,8 +1504,8 @@ done:
 		error = got_error_from_errno("fclose");
 	if (repo)
 		got_repo_close(repo);
-	if (head_symref)
-		got_ref_close(head_symref);
+	if (default_head)
+		got_ref_close(default_head);
 	TAILQ_FOREACH(pe, &refs, entry) {
 		free((void *)pe->path);
 		free(pe->data);
diff --git a/regress/cmdline/clone.sh b/regress/cmdline/clone.sh
index 7ad2a2f..090d275 100755
--- a/regress/cmdline/clone.sh
+++ b/regress/cmdline/clone.sh
@@ -85,6 +85,41 @@ test_clone_basic() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/master:refs/remotes/origin/master
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -149,6 +184,41 @@ test_clone_branch() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/foo:refs/remotes/origin/foo
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -189,6 +259,41 @@ test_clone_all() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/*:refs/remotes/origin/*
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -224,6 +329,43 @@ test_clone_mirror() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+	mirror-references yes
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/*:refs/*
+	mirror = true
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -259,6 +401,43 @@ test_clone_mirror_all() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+	mirror-references yes
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/*:refs/*
+	mirror = true
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -298,6 +477,41 @@ test_clone_reference() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/master:refs/remotes/origin/master
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -335,6 +549,41 @@ test_clone_branch_and_reference() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/heads/foo:refs/remotes/origin/foo
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }
@@ -369,6 +618,43 @@ test_clone_reference_mirror() {
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/got.conf.expected <<EOF
+remote "origin" {
+	server 127.0.0.1
+	protocol ssh
+	repository "$testroot/repo"
+	mirror-references yes
+}
+EOF
+	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/got.conf.expected \
+			$testroot/repo-clone/got.conf
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cat > $testroot/config.expected <<EOF
+[core]
+	repositoryformatversion = 0
+	filemode = true
+	bare = true
+
+[remote "origin"]
+	url = ssh://127.0.0.1$testroot/repo
+	fetch = +refs/*:refs/*
+	mirror = true
+EOF
+	cmp -s $testroot/repo-clone/config $testroot/config.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/config.expected \
+			$testroot/repo-clone/config
 	fi
 	test_done "$testroot" "$ret"
 }