Commit 99495ddb79841fe89b0746d35a49bb81e9220096

Stefan Sperling 2021-01-10T23:48:51

add a 'reference' directive to remote repositories in got.conf(5) Make use of this in 'got clone' to persist -R option arguments given on the command line in the cloned repository's got.conf(5) file.

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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
diff --git a/got/got.1 b/got/got.1
index 17b5f07..4a92790 100644
--- a/got/got.1
+++ b/got/got.1
@@ -193,8 +193,10 @@ and
 .Pa config
 files of the cloned repository to store the
 .Ar repository-url
-and
+and any
 .Ar branch
+or
+.Ar reference
 arguments for future use by
 .Cm got fetch
 or
diff --git a/got/got.c b/got/got.c
index 53829fb..588f144 100644
--- a/got/got.c
+++ b/got/got.c
@@ -927,6 +927,7 @@ struct got_fetch_progress_arg {
 	struct {
 		struct got_pathlist_head *symrefs;
 		struct got_pathlist_head *wanted_branches;
+		struct got_pathlist_head *wanted_refs;
 		const char *proto;
 		const char *host;
 		const char *port;
@@ -942,7 +943,8 @@ static const struct got_error *
 create_config_files(const char *proto, const char *host, const char *port,
     const char *remote_repo_path, const char *git_url, int fetch_all_branches,
     int mirror_references, struct got_pathlist_head *symrefs,
-    struct got_pathlist_head *wanted_branches, struct got_repository *repo);
+    struct got_pathlist_head *wanted_branches,
+    struct got_pathlist_head *wanted_refs, struct got_repository *repo);
 
 static const struct got_error *
 fetch_progress(void *arg, const char *message, off_t packfile_size,
@@ -969,7 +971,8 @@ fetch_progress(void *arg, const char *message, off_t packfile_size,
 		    a->config_info.fetch_all_branches,
 		    a->config_info.mirror_references,
 		    a->config_info.symrefs,
-		    a->config_info.wanted_branches, a->repo);
+		    a->config_info.wanted_branches,
+		    a->config_info.wanted_refs, a->repo);
 		if (err)
 			return err;
 		a->configs_created = 1;
@@ -1165,14 +1168,15 @@ static const struct got_error *
 create_gotconfig(const char *proto, const char *host, const char *port,
     const char *remote_repo_path, const char *default_branch,
     int fetch_all_branches, struct got_pathlist_head *wanted_branches,
-    int mirror_references, struct got_repository *repo)
+    struct got_pathlist_head *wanted_refs, int mirror_references,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *gotconfig_path = NULL;
 	char *gotconfig = NULL;
 	FILE *gotconfig_file = NULL;
 	const char *branchname = NULL;
-	char *branches = NULL;
+	char *branches = NULL, *refs = NULL;
 	ssize_t n;
 
 	if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
@@ -1199,6 +1203,22 @@ create_gotconfig(const char *proto, const char *host, const char *port,
 			goto done;
 		}
 	}
+	if (!TAILQ_EMPTY(wanted_refs)) {
+		struct got_pathlist_entry *pe;
+		TAILQ_FOREACH(pe, wanted_refs, entry) {
+			char *s;
+			const char *refname = pe->path;
+			if (strncmp(refname, "refs/", 5) == 0)
+				branchname += 5;
+			if (asprintf(&s, "%s\"%s\" ",
+			    refs ? refs : "", refname) == -1) {
+				err = got_error_from_errno("asprintf");
+				goto done;
+			}
+			free(refs);
+			refs = s;
+		}
+	}
 
 	/* Create got.conf(5). */
 	gotconfig_path = got_repo_get_path_gotconfig(repo);
@@ -1218,6 +1238,7 @@ create_gotconfig(const char *proto, const char *host, const char *port,
 	    "%s%s%s"
 	    "\trepository \"%s\"\n"
 	    "%s%s%s"
+	    "%s%s%s"
 	    "%s"
 	    "%s"
 	    "}\n",
@@ -1225,6 +1246,7 @@ create_gotconfig(const char *proto, const char *host, const char *port,
 	    port ? "\tport " : "", port ? port : "", port ? "\n" : "",
 	    remote_repo_path, branches ? "\tbranch { " : "",
 	    branches ? branches : "", branches ? "}\n" : "", 
+	    refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "", 
 	    mirror_references ? "\tmirror-references yes\n" : "",
 	    fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
 		err = got_error_from_errno("asprintf");
@@ -1247,13 +1269,14 @@ done:
 static const struct got_error *
 create_gitconfig(const char *git_url, const char *default_branch,
     int fetch_all_branches, struct got_pathlist_head *wanted_branches,
-    int mirror_references, struct got_repository *repo)
+    struct got_pathlist_head *wanted_refs, int mirror_references,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *gitconfig_path = NULL;
 	char *gitconfig = NULL;
 	FILE *gitconfig_file = NULL;
-	char *branches = NULL;
+	char *branches = NULL, *refs = NULL;
 	const char *branchname, *mirror = NULL;
 	ssize_t n;
 
@@ -1319,13 +1342,34 @@ create_gitconfig(const char *git_url, const char *default_branch,
 			goto done;
 		}
 	}
+	if (!mirror_references && !TAILQ_EMPTY(wanted_refs)) {
+		struct got_pathlist_entry *pe;
+		TAILQ_FOREACH(pe, wanted_refs, entry) {
+			char *s;
+			const char *refname = pe->path;
+			if (strncmp(refname, "refs/", 5) == 0)
+				refname += 5;
+			if (asprintf(&s,
+			    "%s\tfetch = +refs/%s:refs/remotes/%s/%s\n",
+			    refs ? refs : "",
+			    refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
+			    refname) == -1) {
+				err = got_error_from_errno("asprintf");
+				goto done;
+			}
+			free(refs);
+			refs = s;
+		}
+	}
+
 	if (asprintf(&gitconfig,
 	    "[remote \"%s\"]\n"
 	    "\turl = %s\n"
 	    "%s"
+	    "%s"
 	    "%s",
 	    GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
-	    mirror ? mirror : "") == -1) {
+	    refs ? refs : "", mirror ? mirror : "") == -1) {
 		err = got_error_from_errno("asprintf");
 		goto done;
 	}
@@ -1346,7 +1390,8 @@ static const struct got_error *
 create_config_files(const char *proto, const char *host, const char *port,
     const char *remote_repo_path, const char *git_url, int fetch_all_branches,
     int mirror_references, struct got_pathlist_head *symrefs,
-    struct got_pathlist_head *wanted_branches, struct got_repository *repo)
+    struct got_pathlist_head *wanted_branches,
+    struct got_pathlist_head *wanted_refs, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	const char *default_branch = NULL;
@@ -1376,13 +1421,13 @@ create_config_files(const char *proto, const char *host, const char *port,
 	/* Create got.conf(5). */
 	err = create_gotconfig(proto, host, port, remote_repo_path,
 	    default_branch, fetch_all_branches, wanted_branches,
-	    mirror_references, repo);
+	    wanted_refs, mirror_references, repo);
 	if (err)
 		return err;
 
 	/* Create a config file Git can understand. */
 	return create_gitconfig(git_url, default_branch, fetch_all_branches,
-	    wanted_branches, mirror_references, repo);
+	    wanted_branches, wanted_refs, mirror_references, repo);
 }
 
 static const struct got_error *
@@ -1566,6 +1611,7 @@ cmd_clone(int argc, char *argv[])
 	fpa.repo = repo;
 	fpa.config_info.symrefs = &symrefs;
 	fpa.config_info.wanted_branches = &wanted_branches;
+	fpa.config_info.wanted_refs = &wanted_refs;
 	fpa.config_info.proto = proto;
 	fpa.config_info.host = host;
 	fpa.config_info.port = port;
@@ -2220,6 +2266,12 @@ cmd_fetch(int argc, char *argv[])
 			    remote->branches[i], NULL);
 		}
 	}
+	if (TAILQ_EMPTY(&wanted_refs)) {
+		for (i = 0; i < remote->nrefs; i++) {
+			got_pathlist_append(&wanted_refs,
+			    remote->refs[i], NULL);
+		}
+	}
 
 	error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
 	    &repo_name, remote->url);
diff --git a/got/got.conf.5 b/got/got.conf.5
index b4fe08c..846205a 100644
--- a/got/got.conf.5
+++ b/got/got.conf.5
@@ -125,6 +125,26 @@ command line with the
 option, and any
 .Cm branch
 configuration settings for this remote repository will be ignored.
+.It Ic reference Brq Ar reference ...
+Specify one or more arbitrary references which
+.Cm got fetch
+should fetch by default, in addition to the branches and tags that will
+be fetched.
+The list of references specified here can be overridden at the
+.Cm got fetch
+command line with the
+.Fl R
+option.
+.Cm got fetch
+will refuse to fetch references from the remote repository's
+.Dq refs/remotes/
+or
+.Dq refs/got/
+namespace.
+In any case, references in the
+.Dq refs/tags/
+namespace will always be fetched and mapped directly to local references
+in the same namespace.
 .It Ic mirror-references Ar yes | no
 This option controls the behaviour of
 .Cm got fetch
diff --git a/include/got_repository.h b/include/got_repository.h
index 8cc337b..56d5bf0 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -70,6 +70,10 @@ struct got_remote_repo {
 	/* Branches to fetch by default. */
 	int nbranches;
 	char **branches;
+
+	/* Other arbitrary references to fetch by default. */
+	int nrefs;
+	char **refs;
 };
 
 /*
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index fba84c4..8dbb8d0 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -380,9 +380,11 @@ struct got_imsg_remote {
 	int mirror_references;
 	int fetch_all_branches;
 	int nbranches;
+	int nrefs;
 
 	/* Followed by name_len + url_len data bytes. */
 	/* Followed by nbranches GOT_IMSG_GITCONFIG_STR_VAL messages. */
+	/* Followed by nrefs GOT_IMSG_GITCONFIG_STR_VAL messages. */
 } __attribute__((__packed__));
 
 /*
diff --git a/lib/privsep.c b/lib/privsep.c
index a4c526b..4fb6f24 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -1808,6 +1808,9 @@ free_remote_data(struct got_remote_repo *remote)
 	for (i = 0; i < remote->nbranches; i++)
 		free(remote->branches[i]);
 	free(remote->branches);
+	for (i = 0; i < remote->nrefs; i++)
+		free(remote->refs[i]);
+	free(remote->refs);
 }
 
 const struct got_error *
@@ -1892,6 +1895,8 @@ got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
 			remote->fetch_all_branches = iremote.fetch_all_branches;
 			remote->nbranches = 0;
 			remote->branches = NULL;
+			remote->nrefs = 0;
+			remote->refs = NULL;
 			(*nremotes)++;
 			break;
 		default:
@@ -2118,6 +2123,27 @@ got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
 				remote->branches[i] = branch;
 				remote->nbranches++;
 			}
+			if (iremote.nrefs > 0) {
+				remote->refs = recallocarray(NULL, 0,
+				    iremote.nrefs, sizeof(char *));
+				if (remote->refs == NULL) {
+					err = got_error_from_errno("calloc");
+					free_remote_data(remote);
+					break;
+				}
+			}
+			remote->nrefs = 0;
+			for (i = 0; i < iremote.nrefs; i++) {
+				char *ref;
+				err = got_privsep_recv_gotconfig_str(&ref,
+				    ibuf);
+				if (err) {
+					free_remote_data(remote);
+					goto done;
+				}
+				remote->refs[i] = ref;
+				remote->nrefs++;
+			}
 			(*nremotes)++;
 			break;
 		default:
diff --git a/libexec/got-read-gotconfig/got-read-gotconfig.c b/libexec/got-read-gotconfig/got-read-gotconfig.c
index 70e9932..2fe537d 100644
--- a/libexec/got-read-gotconfig/got-read-gotconfig.c
+++ b/libexec/got-read-gotconfig/got-read-gotconfig.c
@@ -143,7 +143,8 @@ send_gotconfig_remotes(struct imsgbuf *ibuf,
 		size_t len = sizeof(iremote);
 		struct ibuf *wbuf;
 		struct node_branch *branch;
-		int nbranches = 0;
+		struct node_ref *ref;
+		int nbranches = 0, nrefs = 0;
 
 		branch = repo->branch;
 		while (branch) {
@@ -151,7 +152,14 @@ send_gotconfig_remotes(struct imsgbuf *ibuf,
 			nbranches++;
 		}
 
+		ref = repo->ref;
+		while (ref) {
+			ref = ref->next;
+			nrefs++;
+		}
+
 		iremote.nbranches = nbranches;
+		iremote.nrefs = nrefs;
 		iremote.mirror_references = repo->mirror_references;
 		iremote.fetch_all_branches = repo->fetch_all_branches;
 
@@ -207,6 +215,14 @@ send_gotconfig_remotes(struct imsgbuf *ibuf,
 				break;
 			branch = branch->next;
 		}
+
+		ref = repo->ref;
+		while (ref) {
+			err = send_gotconfig_str(ibuf, ref->ref_name);
+			if (err)
+				break;
+			ref = ref->next;
+		}
 	}
 
 	free(url);
diff --git a/libexec/got-read-gotconfig/gotconfig.h b/libexec/got-read-gotconfig/gotconfig.h
index 212b87b..5f1429f 100644
--- a/libexec/got-read-gotconfig/gotconfig.h
+++ b/libexec/got-read-gotconfig/gotconfig.h
@@ -21,6 +21,12 @@ struct node_branch {
 	struct node_branch *tail;
 };
 
+struct node_ref {
+	char *ref_name;
+	struct node_ref *next;
+	struct node_ref *tail;
+};
+
 struct gotconfig_remote_repo {
 	TAILQ_ENTRY(gotconfig_remote_repo) entry;
 	char	*name;
@@ -31,6 +37,7 @@ struct gotconfig_remote_repo {
 	int	mirror_references;
 	int	fetch_all_branches;
 	struct	node_branch *branch;
+	struct	node_ref *ref;
 };
 TAILQ_HEAD(gotconfig_remote_repo_list, gotconfig_remote_repo);
 
diff --git a/libexec/got-read-gotconfig/parse.y b/libexec/got-read-gotconfig/parse.y
index 07d4556..821eff3 100644
--- a/libexec/got-read-gotconfig/parse.y
+++ b/libexec/got-read-gotconfig/parse.y
@@ -87,6 +87,7 @@ typedef struct {
 		long long	 number;
 		char		*string;
 		struct node_branch *branch;
+		struct node_ref *ref;
 	} v;
 	int lineno;
 } YYSTYPE;
@@ -95,12 +96,13 @@ typedef struct {
 
 %token	ERROR
 %token	REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
-%token	AUTHOR FETCH_ALL_BRANCHES
+%token	AUTHOR FETCH_ALL_BRANCHES REFERENCE
 %token	<v.string>	STRING
 %token	<v.number>	NUMBER
 %type	<v.number>	boolean portplain
 %type	<v.string>	numberstring
 %type	<v.branch>	branch xbranch branch_list
+%type	<v.ref>		ref xref ref_list
 
 %%
 
@@ -163,7 +165,27 @@ branch_list	: xbranch optnl			{ $$ = $1; }
 			$$ = $1;
 		}
 		;
-
+ref		: /* empty */		{ $$ = NULL; }
+		| xref			{ $$ = $1; }
+		| '{' optnl ref_list '}'	{ $$ = $3; }
+		;
+xref		: STRING {
+			$$ = calloc(1, sizeof(struct node_ref));
+			if ($$ == NULL) {
+				yyerror("calloc");
+				YYERROR;
+			}
+			$$->ref_name = $1;
+			$$->tail = $$;
+		}
+		;
+ref_list	: xref optnl			{ $$ = $1; }
+		| ref_list comma xref optnl {
+			$1->tail->next = $3;
+			$1->tail = $3;
+			$$ = $1;
+		}
+		;
 remoteopts2	: remoteopts2 remoteopts1 nl
 	   	| remoteopts1 optnl
 		;
@@ -206,6 +228,9 @@ remoteopts1	: REPOSITORY STRING {
 		| BRANCH branch {
 			remote->branch = $2;
 		}
+		| REFERENCE ref {
+			remote->ref = $2;
+		}
 	   	;
 remote		: REMOTE STRING {
 			static const struct got_error* error;
@@ -293,6 +318,7 @@ lookup(char *s)
 		{"mirror-references",	MIRROR_REFERENCES},
 		{"port",		PORT},
 		{"protocol",		PROTOCOL},
+		{"reference",		REFERENCE},
 		{"remote",		REMOTE},
 		{"repository",		REPOSITORY},
 		{"server",		SERVER},
diff --git a/regress/cmdline/clone.sh b/regress/cmdline/clone.sh
index b36a5f4..44c5e03 100755
--- a/regress/cmdline/clone.sh
+++ b/regress/cmdline/clone.sh
@@ -492,6 +492,7 @@ remote "origin" {
 	protocol ssh
 	repository "$testroot/repo"
 	branch { "master" }
+	reference { "hoo" }
 }
 EOF
 	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
@@ -512,6 +513,7 @@ EOF
 [remote "origin"]
 	url = ssh://127.0.0.1$testroot/repo
 	fetch = +refs/heads/master:refs/remotes/origin/master
+	fetch = +refs/hoo:refs/remotes/origin/hoo
 EOF
 	cmp -s $testroot/repo-clone/config $testroot/config.expected
 	ret="$?"
@@ -565,6 +567,7 @@ remote "origin" {
 	protocol ssh
 	repository "$testroot/repo"
 	branch { "foo" }
+	reference { "hoo/boo/zoo" }
 }
 EOF
 	cmp -s $testroot/repo-clone/got.conf $testroot/got.conf.expected
@@ -585,6 +588,7 @@ EOF
 [remote "origin"]
 	url = ssh://127.0.0.1$testroot/repo
 	fetch = +refs/heads/foo:refs/remotes/origin/foo
+	fetch = +refs/hoo/boo/zoo:refs/remotes/origin/hoo/boo/zoo
 EOF
 	cmp -s $testroot/repo-clone/config $testroot/config.expected
 	ret="$?"
@@ -635,6 +639,7 @@ remote "origin" {
 	protocol ssh
 	repository "$testroot/repo"
 	branch { "master" }
+	reference { "hoo" }
 	mirror-references yes
 }
 EOF
diff --git a/regress/cmdline/fetch.sh b/regress/cmdline/fetch.sh
index 0af6ce5..fa4ff05 100755
--- a/regress/cmdline/fetch.sh
+++ b/regress/cmdline/fetch.sh
@@ -1050,9 +1050,44 @@ EOF
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
 	fi
-	test_done "$testroot" "$ret"
 
+cat > $testroot/repo-clone/got.conf <<EOF
+remote "origin" {
+	protocol ssh
+	server 127.0.0.1
+	repository "$testroot/repo"
+	branch { "foo" }
+	reference { "hoo/boo/zoo" }
+}
+EOF
+	(cd $testroot/repo-clone && got fetch -q > $testroot/stdout)
+
+	local tag_id=`got ref -r $testroot/repo -l \
+		| grep "^refs/tags/1.0" | tr -d ' ' | cut -d: -f2`
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/heads/foo: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/foo: $commit_id" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/hoo/boo/zoo: $commit_id" \
+		>> $testroot/stdout.expected
+	echo "refs/remotes/origin/master: $commit_id" \
+		>> $testroot/stdout.expected
+	echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+
+	got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+	fi
+	test_done "$testroot" "$ret"
 }
 
 test_parseargs "$@"