Commit 469dd7264e07bc434adc24b250cd154db9e18d02

Stefan Sperling 2020-03-20T11:44:14

add support for repository mirrors to 'got clone' and 'got fetch'

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
diff --git a/got/got.1 b/got/got.1
index ec8f587..8100849 100644
--- a/got/got.1
+++ b/got/got.1
@@ -190,6 +190,35 @@ The options for
 .Cm got clone
 are as follows:
 .Bl -tag -width Ds
+.It Fl m
+Create the cloned repository as a mirror of the original repository.
+This is useful if the cloned repository will not be used to store
+local changes as created by
+.Cm got commit .
+.Pp
+The repository's
+.Pa config
+file will be set up with the
+.Dq mirror
+option enabled, such that
+.Cm got fetch
+or
+.Xr git-fetch 1
+will write incoming changes directly to branches in the
+.Dq refs/heads/
+reference namespace, rather than to branches in the
+.Dq refs/remotes/
+namespace.
+This avoids the usual requirement of having to run
+.Cm got rebase
+after
+.Cm got fetch
+in order to make incoming changes appear on branches in the
+.Dq refs/heads/
+namespace.
+But maintaining custom branches with local changes in the cloned
+repository becomes difficult since local changes are at risk of
+being discarded whenever incoming changes are fetched.
 .It Fl q
 Suppress progress reporting output.
 The same option will be passed to
@@ -220,16 +249,26 @@ file of the repository, as created by
 .Pp
 Branch references in the
 .Dq refs/remotes/
-reference namespace will be updated to point at the newly fetched commits.
-The
+reference namespace will be updated to point at the newly fetched commits,
+and the
 .Cm got rebase
 command can then be used to make new changes visible on branches in the
 .Dq refs/heads/
+reference namespace, merging incoming changes with local changes as necessary.
+.Pp
+However, if the repository is configured as a mirror then all references will
+be updated as needed to match the corresponding references in the remote
+repository, including branches in the
+.Dq refs/heads/
 reference namespace.
+If those branches contained local commits, these will no longer be reachable
+via a reference and will therefore be at risk of being discarded by Git's
+garbage collector.
 .Pp
-Existing references in the
+In any case, existing references in the
 .Dq refs/tags/
-namespace will be changed to match tags contained in the remote repository.
+namespace will always be changed to match tags contained in the remote
+repository.
 .Pp
 The options for
 .Cm got fetch
@@ -1715,12 +1754,31 @@ to be amended and perhaps committed again:
 .Dl $ # now back out the previous backout :-)
 .Dl $ got backout unified-buffer-cache
 .Pp
-Fetch new upstream commits into the local repository's master branch.
-This step currently requires
-.Xr git 1 :
+Fetch new upstream commits into the local repository's
+.Dq origin/master
+branch:
+.Pp
+.Dl $ cd /usr/src
+.Dl $ got fetch
+.Pp
+In a repository created with a HTTP URL and
+.Cm git clone --bare
+the
+.Xr git-fetch 1
+command must be used instead:
 .Pp
 .Dl $ cd /var/git/src.git
-.Dl $ git fetch origin master:master
+.Dl $ git fetch origin master:refs/remotes/origin/master
+.Pp
+Rebase the local
+.Dq master
+branch to merge the new changes that are now visible on the
+.Dq origin/master
+branch:
+.Pp
+.Dl $ cd /usr/src
+.Dl $ got update -b origin/master
+.Dl $ got rebase master
 .Pp
 Rebase the
 .Dq unified-buffer-cache
@@ -1746,11 +1804,12 @@ branch:
 .Dl $ got update -c master
 .Dl $ got histedit
 .Pp
-Additional steps are necessary if local changes need to be pushed back
+.Pp
+Additional steps may be necessary if local changes need to be pushed back
 to the remote repository, which currently requires
 .Cm git push .
 Before working against existing branches in a repository cloned with
-.Dq git clone --bare
+.Cm git clone --bare
 instead of
 .Cm got clone ,
 a Git
@@ -1769,9 +1828,15 @@ command:
 .Pp
 .Dl $ cd /var/git/repo
 .Dl $ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
+Additionally, the
+.Dq mirror
+option must be disabled:
+.Pp
+.Dl $ cd /var/git/repo
+.Dl $ git config remote.origin.mirror false
 .Pp
 Alternatively, the following
-.Pa fetch
+.Xr git-fetch 1
 configuration item can be added manually to the Git repository's
 .Pa config
 file:
@@ -1779,6 +1844,7 @@ file:
 .Dl [remote "origin"]
 .Dl url = ...
 .Dl fetch = +refs/heads/*:refs/remotes/origin/*
+.Dl mirror = false
 .Pp
 This configuration leaves the local repository's
 .Dq refs/heads
@@ -1789,12 +1855,15 @@ and, if needed, created with
 .Pp
 Branches in the
 .Dq remotes/origin
-namespace can be updated with incoming changes from the remote
+namespace can now be updated with incoming changes from the remote
 repository with
-.Cm got fetch :
+.Cm got fetch
+or
+.Xr git-fetch 1
+without extra command line arguments:
 .Pp
 .Dl $ cd /var/git/repo
-.Dl $ got fetch
+.Dl $ git fetch
 .Pp
 To make changes fetched from the remote repository appear on the
 .Dq master
diff --git a/got/got.c b/got/got.c
index 5ab89fa..740999c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -811,7 +811,7 @@ done:
 __dead static void
 usage_clone(void)
 {
-	fprintf(stderr, "usage: %s clone [-q] [-v] repository-url "
+	fprintf(stderr, "usage: %s clone [-m] [-q] [-v] repository-url "
 	    "[target-directory]\n", getprogname());
 	exit(1);
 }
@@ -903,13 +903,16 @@ cmd_clone(int argc, char *argv[])
 	char *gitconfig = NULL;
 	FILE *gitconfig_file = NULL;
 	ssize_t n;
-	int verbosity = 0;
+	int verbosity = 0, mirror_references = 0;
 
 	TAILQ_INIT(&refs);
 	TAILQ_INIT(&symrefs);
 
-	while ((ch = getopt(argc, argv, "vq")) != -1) {
+	while ((ch = getopt(argc, argv, "mvq")) != -1) {
 		switch (ch) {
+		case 'm':
+			mirror_references = 1;
+			break;
 		case 'v':
 			if (verbosity < 0)
 				verbosity = 0;
@@ -1022,14 +1025,25 @@ cmd_clone(int argc, char *argv[])
 		error = got_error_from_errno2("fopen", gitconfig_path);
 		goto done;
 	}
-	if (asprintf(&gitconfig,
-	    "[remote \"%s\"]\n"
-	    "\turl = %s\n"
-	    "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
-	    GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
-	    GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
-		error = got_error_from_errno("asprintf");
-		goto done;
+	if (mirror_references) {
+		if (asprintf(&gitconfig,
+		    "[remote \"%s\"]\n"
+		    "\turl = %s\n"
+		    "\tmirror = true\n",
+		    GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
+			error = got_error_from_errno("asprintf");
+			goto done;
+		}
+	} else {
+		if (asprintf(&gitconfig,
+		    "[remote \"%s\"]\n"
+		    "\turl = %s\n"
+		    "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
+		    GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
+		    GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
+			error = got_error_from_errno("asprintf");
+			goto done;
+		}
 	}
 	n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
 	if (n != strlen(gitconfig)) {
@@ -1042,8 +1056,8 @@ cmd_clone(int argc, char *argv[])
 	fpa.last_p_resolved = -1;
 	fpa.verbosity = verbosity;
 	error = got_fetch_pack(&pack_hash, &refs, &symrefs,
-	    GOT_FETCH_DEFAULT_REMOTE_NAME, fetchfd, repo,
-	    fetch_progress, &fpa);
+	    GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
+	    fetchfd, repo, fetch_progress, &fpa);
 	if (error)
 		goto done;
 
@@ -1069,6 +1083,9 @@ cmd_clone(int argc, char *argv[])
 		if (error)
 			goto done;
 
+		if (mirror_references)
+			continue;
+
 		if (strncmp("refs/heads/", refname, 11) != 0)
 			continue;
 
@@ -1118,7 +1135,8 @@ cmd_clone(int argc, char *argv[])
 	}
 
 	if (verbosity >= 0)
-		printf("Created cloned repository '%s'\n", repo_path);
+		printf("Created %s repository '%s'\n",
+		    mirror_references ? "mirrored" : "cloned", repo_path);
 done:
 	if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
 		error = got_error_from_errno("close");
@@ -1372,7 +1390,7 @@ cmd_fetch(int argc, char *argv[])
 	fpa.last_p_resolved = -1;
 	fpa.verbosity = verbosity;
 	error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
-	    fetchfd, repo, fetch_progress, &fpa);
+	    remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
 	if (error)
 		goto done;
 
diff --git a/include/got_fetch.h b/include/got_fetch.h
index 43417f2..9df27fa 100644
--- a/include/got_fetch.h
+++ b/include/got_fetch.h
@@ -61,4 +61,4 @@ typedef const struct got_error *(*got_fetch_progress_cb)(void *,
  */
 const struct got_error *got_fetch_pack(struct got_object_id **,
 	struct got_pathlist_head *, struct got_pathlist_head *, const char *,
-	int, struct got_repository *, got_fetch_progress_cb, void *);
+	int, int, struct got_repository *, got_fetch_progress_cb, void *);
diff --git a/include/got_repository.h b/include/got_repository.h
index 106390c..1e0ca3b 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -51,6 +51,12 @@ const char *got_repo_get_gitconfig_owner(struct got_repository *);
 struct got_remote_repo {
 	char *name;
 	char *url;
+	
+	/*
+	 * If set, references are mirrored 1:1 into the local repository.
+	 * If not set, references are mapped into "refs/remotes/$name/".
+	 */
+	int mirror_references;
 };
 
 /* Obtain the list of remote repositories parsed from gitconfig. */ 
diff --git a/lib/fetch.c b/lib/fetch.c
index 132da7a..8edac2b 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -387,9 +387,9 @@ check_pack_hash(int fd, size_t sz, uint8_t *hcomp)
 
 const struct got_error*
 got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
-    struct got_pathlist_head *symrefs, const char *remote_name, int fetchfd,
-    struct got_repository *repo, got_fetch_progress_cb progress_cb,
-    void *progress_arg)
+    struct got_pathlist_head *symrefs, const char *remote_name,
+    int mirror_references, int fetchfd, struct got_repository *repo,
+    got_fetch_progress_cb progress_cb, void *progress_arg)
 {
 	int imsg_fetchfds[2], imsg_idxfds[2];
 	int packfd = -1, npackfd = -1, idxfd = -1, nidxfd = -1, nfetchfd = -1;
@@ -418,9 +418,12 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 	TAILQ_INIT(&have_refs);
 	SIMPLEQ_INIT(&my_refs);
 
-	if (asprintf(&ref_prefix, "refs/remotes/%s/", remote_name) == -1)
-		return got_error_from_errno("asprintf");
-	ref_prefixlen = strlen(ref_prefix);
+	if (!mirror_references) {
+		if (asprintf(&ref_prefix, "refs/remotes/%s/",
+		    remote_name) == -1)
+			return got_error_from_errno("asprintf");
+		ref_prefixlen = strlen(ref_prefix);
+	}
 
 	err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
 	if (err)
@@ -434,6 +437,23 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 			continue;
 
 		refname = got_ref_get_name(re->ref);
+
+		if (mirror_references) {
+			char *name;
+			err = got_ref_resolve(&id, repo, re->ref);
+			if (err)
+				goto done;
+			name = strdup(refname);
+			if (name == NULL) {
+				err = got_error_from_errno("strdup");
+				goto done;
+			}
+			err = got_pathlist_append(&have_refs, name, id);
+			if (err)
+				goto done;
+			continue;
+		}
+
 		if (strncmp("refs/tags/", refname, 10) == 0) {
 			char *tagname;
 
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 11db131..5d6fc07 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -341,6 +341,7 @@ struct got_imsg_traversed_commits {
 struct got_imsg_remote {
 	size_t name_len;
 	size_t url_len;
+	int mirror_references;
 
 	/* Followed by name_len + url_len data bytes. */
 };
diff --git a/lib/privsep.c b/lib/privsep.c
index d869c21..e0f4043 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -1935,6 +1935,13 @@ got_privsep_send_gitconfig_remotes(struct imsgbuf *ibuf,
 			ibuf_free(wbuf);
 			return err;
 		}
+		if (imsg_add(wbuf, &remotes[i].mirror_references,
+		    sizeof(iremote.mirror_references)) == -1) {
+			err = got_error_from_errno(
+			    "imsg_add GITCONFIG_REMOTE");
+			ibuf_free(wbuf);
+			return err;
+		}
 
 		wbuf->fd = -1;
 		imsg_close(ibuf, wbuf);
@@ -2023,6 +2030,7 @@ got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
 				free(remote->name);
 				break;
 			}
+			remote->mirror_references = iremote.mirror_references;
 			(*nremotes)++;
 			break;
 		default:
diff --git a/libexec/got-read-gitconfig/got-read-gitconfig.c b/libexec/got-read-gitconfig/got-read-gitconfig.c
index 4d1f4a8..6f203d1 100644
--- a/libexec/got-read-gitconfig/got-read-gitconfig.c
+++ b/libexec/got-read-gitconfig/got-read-gitconfig.c
@@ -108,7 +108,7 @@ gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
 
 	i = 0;
 	TAILQ_FOREACH(node, &sections->fields, link) {
-		char *name, *end;
+		char *name, *end, *mirror;
 
 		if (strncasecmp("remote \"", node->field, 8) != 0)
 			continue;
@@ -130,6 +130,16 @@ gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
 			goto done;
 		}
 
+		remotes[i].mirror_references = 0;
+		mirror = got_gitconfig_get_str(gitconfig, node->field,
+		    "mirror");
+		if (mirror != NULL &&
+		    (strcasecmp(mirror, "true") == 0 ||
+		    strcasecmp(mirror, "on") == 0 ||
+		    strcasecmp(mirror, "yes") == 0 ||
+		    strcmp(mirror, "1") == 0))
+			remotes[i].mirror_references = 1;
+
 		i++;
 	}