Commit 659e7fbd6952b401014702a1e181642a9bba17df

Stefan Sperling 2020-03-20T15:55:08

fetch only HEAD branch and tags by default; add -a option to fetch everything

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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
diff --git a/got/got.1 b/got/got.1
index 2d0fb78..2b7971b 100644
--- a/got/got.1
+++ b/got/got.1
@@ -136,7 +136,7 @@ follows the globbing rules documented in
 .It Cm im
 Short alias for
 .Cm import .
-.It Cm clone Oo Fl q Oc Oo Fl v Oc Ar repository-URL Op Ar target-directory
+.It Cm clone Oo Fl a Oc Oo Fl q Oc Oo Fl v Oc Ar repository-URL Op Ar target-directory
 Clone a Git repository at the specified
 .Ar repository-URL
 into the specified
@@ -191,6 +191,10 @@ The options for
 .Cm got clone
 are as follows:
 .Bl -tag -width Ds
+.It Fl a
+Fetch all branches from the remote repository.
+If this option is not specified, a branch resolved via the repository's HEAD
+reference will be fetched.
 .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
@@ -236,7 +240,7 @@ The maximum is 3.
 .It Cm cl
 Short alias for
 .Cm clone .
-.It Cm fetch Oo Fl r Ar repository-path Oc Oo Fl q Oc Oo Fl v Oc Op Ar remote-repository-name
+.It Cm fetch Oo Fl a Oc Oo Fl r Ar repository-path Oc Oo Fl q Oc Oo Fl v Oc Op Ar remote-repository-name
 Fetch new changes from a remote repository.
 If no
 .Ar remote-repository-name
@@ -275,6 +279,10 @@ The options for
 .Cm got fetch
 are as follows:
 .Bl -tag -width Ds
+.It Fl a
+Fetch all branches from the remote repository.
+If this option is not specified, a branch resolved via the repository's HEAD
+reference will be fetched.
 .It Fl r Ar repository-path
 Use the repository at the specified path.
 If not specified, assume the repository is located at or above the current
diff --git a/got/got.c b/got/got.c
index 7436614..35b58f0 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 [-m] [-q] [-v] repository-url "
+	fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
 	    "[target-directory]\n", getprogname());
 	exit(1);
 }
@@ -903,13 +903,17 @@ cmd_clone(int argc, char *argv[])
 	char *gitconfig = NULL;
 	FILE *gitconfig_file = NULL;
 	ssize_t n;
-	int verbosity = 0, mirror_references = 0;
+	int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
+	struct got_reference *head_symref = NULL;
 
 	TAILQ_INIT(&refs);
 	TAILQ_INIT(&symrefs);
 
-	while ((ch = getopt(argc, argv, "mvq")) != -1) {
+	while ((ch = getopt(argc, argv, "amvq")) != -1) {
 		switch (ch) {
+		case 'a':
+			fetch_all_branches = 1;
+			break;
 		case 'm':
 			mirror_references = 1;
 			break;
@@ -1013,50 +1017,13 @@ cmd_clone(int argc, char *argv[])
 		printf("Connected to %s%s%s\n", host,
 		    port ? ":" : "", port ? port : "");
 
-	/* Create a config file git-fetch(1) can understand. */
-	gitconfig_path = got_repo_get_path_gitconfig(repo);
-	if (gitconfig_path == NULL) {
-		error = got_error_from_errno("got_repo_get_path_gitconfig");
-		goto done;
-	}
-	gitconfig_file = fopen(gitconfig_path, "a");
-	if (gitconfig_file == NULL) {
-		error = got_error_from_errno2("fopen", gitconfig_path);
-		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)) {
-		error = got_ferror(gitconfig_file, GOT_ERR_IO);
-		goto done;
-	}
-
 	fpa.last_scaled_size[0] = '\0';
 	fpa.last_p_indexed = -1;
 	fpa.last_p_resolved = -1;
 	fpa.verbosity = verbosity;
 	error = got_fetch_pack(&pack_hash, &refs, &symrefs,
 	    GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
-	    fetchfd, repo, fetch_progress, &fpa);
+	    fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
 	if (error)
 		goto done;
 
@@ -1105,7 +1072,7 @@ cmd_clone(int argc, char *argv[])
 
 	/* Set the HEAD reference if the server provided one. */
 	TAILQ_FOREACH(pe, &symrefs, entry) {
-		struct got_reference *symref, *target_ref;
+		struct got_reference *target_ref;
 		const char *refname = pe->path;
 		const char *target = pe->data;
 
@@ -1119,20 +1086,81 @@ cmd_clone(int argc, char *argv[])
 			goto done;
 		}
 
-		error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
+		error = got_ref_alloc_symref(&head_symref,
+		    GOT_REF_HEAD, target_ref);
 		got_ref_close(target_ref);
 		if (error)
 			goto done;
 
 		if (verbosity >= 0)
 			printf("Setting %s to %s\n", GOT_REF_HEAD,
-			    got_ref_get_symref_target(symref));
+			    got_ref_get_symref_target(head_symref));
 
-		error = got_ref_write(symref, repo);
-		got_ref_close(symref);
+		error = got_ref_write(head_symref, repo);
 		break;
 	}
 
+	/* Create a config file git-fetch(1) can understand. */
+	gitconfig_path = got_repo_get_path_gitconfig(repo);
+	if (gitconfig_path == NULL) {
+		error = got_error_from_errno("got_repo_get_path_gitconfig");
+		goto done;
+	}
+	gitconfig_file = fopen(gitconfig_path, "a");
+	if (gitconfig_file == NULL) {
+		error = got_error_from_errno2("fopen", gitconfig_path);
+		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 (fetch_all_branches) {
+		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;
+		}
+	} else {
+		const char *branchname;
+
+		/*
+		 * 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 (strncmp(branchname, "refs/heads/", 11) == 0)
+				branchname += 11;
+		} else
+			branchname = "*"; /* fall back to all branches */
+		if (asprintf(&gitconfig,
+		    "[remote \"%s\"]\n"
+		    "\turl = %s\n"
+		    "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
+		    GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
+		    branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
+		    branchname) == -1) {
+			error = got_error_from_errno("asprintf");
+			goto done;
+		}
+	}
+	n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
+	if (n != strlen(gitconfig)) {
+		error = got_ferror(gitconfig_file, GOT_ERR_IO);
+		goto done;
+	}
+
+
 	if (verbosity >= 0)
 		printf("Created %s repository '%s'\n",
 		    mirror_references ? "mirrored" : "cloned", repo_path);
@@ -1143,6 +1171,8 @@ done:
 		error = got_error_from_errno("fclose");
 	if (repo)
 		got_repo_close(repo);
+	if (head_symref)
+		got_ref_close(head_symref);
 	TAILQ_FOREACH(pe, &refs, entry) {
 		free((void *)pe->path);
 		free(pe->data);
@@ -1234,7 +1264,7 @@ done:
 __dead static void
 usage_fetch(void)
 {
-	fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
+	fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
 	    "[remote-repository-name]\n", getprogname());
 	exit(1);
 }
@@ -1257,13 +1287,16 @@ cmd_fetch(int argc, char *argv[])
 	struct got_object_id *pack_hash = NULL;
 	int i, ch, fetchfd = -1;
 	struct got_fetch_progress_arg fpa;
-	int verbosity = 0;
+	int verbosity = 0, fetch_all_branches = 0;
 
 	TAILQ_INIT(&refs);
 	TAILQ_INIT(&symrefs);
 
-	while ((ch = getopt(argc, argv, "r:vq")) != -1) {
+	while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
 		switch (ch) {
+		case 'a':
+			fetch_all_branches = 1;
+			break;
 		case 'r':
 			repo_path = realpath(optarg, NULL);
 			if (repo_path == NULL)
@@ -1390,7 +1423,8 @@ cmd_fetch(int argc, char *argv[])
 	fpa.last_p_resolved = -1;
 	fpa.verbosity = verbosity;
 	error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
-	    remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
+	    remote->mirror_references, fetch_all_branches, fetchfd, repo,
+	    fetch_progress, &fpa);
 	if (error)
 		goto done;
 
diff --git a/include/got_error.h b/include/got_error.h
index d29011a..7d8cd87 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -138,6 +138,7 @@
 #define GOT_ERR_ADDRINFO	121
 #define GOT_ERR_BAD_PACKET	122
 #define GOT_ERR_NO_REMOTE	123
+#define GOT_ERR_FETCH_NO_BRANCH	124
 
 static const struct got_error {
 	int code;
@@ -282,6 +283,7 @@ static const struct got_error {
 	{ GOT_ERR_ADDRINFO, "getaddrinfo failed" },
 	{ GOT_ERR_BAD_PACKET, "bad packet received" },
 	{ GOT_ERR_NO_REMOTE, "remote repository not found" },
+	{ GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
 };
 
 /*
diff --git a/include/got_fetch.h b/include/got_fetch.h
index 9df27fa..14f29c4 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, int, struct got_repository *, got_fetch_progress_cb, void *);
+	int, int, int, struct got_repository *, got_fetch_progress_cb, void *);
diff --git a/lib/fetch.c b/lib/fetch.c
index b5b03f3..e36d5b8 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -387,7 +387,8 @@ 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 mirror_references, int fetchfd, struct got_repository *repo,
+    int mirror_references, int fetch_all_branches, int fetchfd,
+    struct got_repository *repo,
     got_fetch_progress_cb progress_cb, void *progress_arg)
 {
 	int imsg_fetchfds[2], imsg_idxfds[2];
@@ -552,7 +553,8 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 		err = got_error_from_errno("dup");
 		goto done;
 	}
-	err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs);
+	err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
+	    fetch_all_branches);
 	if (err != NULL)
 		goto done;
 	nfetchfd = -1;
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 5d6fc07..3df98dc 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -246,10 +246,11 @@ struct got_imsg_fetch_have_ref {
 	/* Followed by name_len data bytes. */
 } __attribute__((__packed__));
 
-struct got_imsg_fetch_have_refs {
+struct got_imsg_fetch_request {
+	int fetch_all_branches;
 	size_t n_have_refs;
 	/* Followed by n_have_refs times of got_imsg_fetch_have_ref data. */
-};
+} __attribute__((__packed__));
 
 /* Structures for GOT_IMSG_FETCH_SYMREFS data. */
 struct got_imsg_fetch_symref {
@@ -389,7 +390,7 @@ const struct got_error *got_privsep_send_index_pack_done(struct imsgbuf *);
 const struct got_error *got_privsep_recv_index_progress(int *, int *, int *,
     int *, int *, struct imsgbuf *ibuf);
 const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int,
-    struct got_pathlist_head *);
+    struct got_pathlist_head *, int);
 const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
 const struct got_error *got_privsep_send_fetch_symrefs(struct imsgbuf *,
     struct got_pathlist_head *);
diff --git a/lib/privsep.c b/lib/privsep.c
index b1b5821..e658bf0 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -412,14 +412,14 @@ got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
 
 const struct got_error *
 got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
-   struct got_pathlist_head *have_refs)
+   struct got_pathlist_head *have_refs, int fetch_all_branches)
 {
 	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
 	size_t len, n_have_refs = 0;
 	struct got_pathlist_entry *pe;
 
-	len = sizeof(struct got_imsg_fetch_have_refs);
+	len = sizeof(struct got_imsg_fetch_request);
 	TAILQ_FOREACH(pe, have_refs, entry) {
 		len += sizeof(struct got_imsg_fetch_have_ref) + pe->path_len;
 		n_have_refs++;
@@ -435,7 +435,14 @@ got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
 		return got_error_from_errno("imsg_create FETCH_REQUEST");
 	}
 
-	/* Keep in sync with struct got_imsg_fetch_have_refs definition! */
+	/* Keep in sync with struct got_imsg_fetch_request definition! */
+	if (imsg_add(wbuf, &fetch_all_branches, sizeof(fetch_all_branches))
+	    == -1) {
+		err = got_error_from_errno("imsg_add FETCH_REQUEST");
+		ibuf_free(wbuf);
+		close(fd);
+		return err;
+	}
 	if (imsg_add(wbuf, &n_have_refs, sizeof(n_have_refs)) == -1) {
 		err = got_error_from_errno("imsg_add FETCH_REQUEST");
 		ibuf_free(wbuf);
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index 751733a..35e6b77 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -40,6 +40,7 @@
 #include "got_path.h"
 #include "got_version.h"
 #include "got_fetch.h"
+#include "got_reference.h"
 
 #include "got_lib_sha1.h"
 #include "got_lib_delta.h"
@@ -54,7 +55,6 @@
 
 struct got_object *indexed;
 static int chattygot;
-static char *fetchbranch;
 static struct got_object_id zhash = {.sha1={0}};
 
 static const struct got_error *
@@ -240,23 +240,15 @@ match_remote_ref(struct got_pathlist_head *have_refs,
 }
 
 static int
-match_branch(char *br, char *pat)
+match_branch(const char *branch, const char *wanted_branch)
 {
-	char name[128];
-
-	if (strstr(pat, "refs/heads") == pat) {
-		if (snprintf(name, sizeof(name), "%s", pat) >= sizeof(name))
-			return -1;
-	} else if (strstr(pat, "heads")) {
-		if (snprintf(name, sizeof(name), "refs/%s", pat)
-		    >= sizeof(name))
-			return -1;
-	} else {
-		if (snprintf(name, sizeof(name), "refs/heads/%s", pat)
-		    >= sizeof(name))
-			return -1;
-	}
-	return strcmp(br, name) == 0;
+	if (strncmp(branch, "refs/heads/", 11) != 0)
+		return 0;
+
+	if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
+		wanted_branch += 11;
+
+	return (strcmp(branch + 11, wanted_branch) == 0);
 }
 
 static const struct got_error *
@@ -490,7 +482,8 @@ fetch_error(const char *buf, size_t len)
 
 static const struct got_error *
 fetch_pack(int fd, int packfd, struct got_object_id *packid,
-    struct got_pathlist_head *have_refs, struct imsgbuf *ibuf)
+    struct got_pathlist_head *have_refs, int fetch_all_branches,
+    struct imsgbuf *ibuf)
 {
 	const struct got_error *err = NULL;
 	char buf[GOT_FETCH_PKTMAX];
@@ -501,9 +494,11 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
 	off_t packsz = 0, last_reported_packsz = 0;
 	char *id_str = NULL, *refname = NULL;
 	char *server_capabilities = NULL, *my_capabilities = NULL;
+	const char *default_branch = NULL;
 	struct got_pathlist_head symrefs;
 	struct got_pathlist_entry *pe;
 	int sent_my_capabilites = 0, have_sidebands = 0;
+	int found_branch = 0;
 
 	TAILQ_INIT(&symrefs);
 
@@ -546,12 +541,39 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
 			if (err)
 				goto done;
 			is_firstpkt = 0;
+			if (!fetch_all_branches) {
+				TAILQ_FOREACH(pe, &symrefs, entry) {
+					const char *name = pe->path;
+					const char *symref_target = pe->data;
+					if (strcmp(name, GOT_REF_HEAD) != 0)
+						continue;
+					default_branch = symref_target;
+					break;
+				}
+			}
 			continue;
 		}
 		if (strstr(refname, "^{}"))
 			continue;
-		if (fetchbranch && !match_branch(refname, fetchbranch))
+
+		if (chattygot)
+			fprintf(stderr, "%s: discovered remote ref %s\n",
+			    getprogname(), refname);
+
+		if (strncmp(refname, "refs/heads/", 11) == 0) {
+			if (default_branch != NULL &&
+			    !match_branch(refname, default_branch))
+				continue;
+			found_branch = 1;
+		} else if (strncmp(refname, "refs/tags/", 10) != 0) {
+			if (chattygot) {
+				fprintf(stderr, "%s: ignoring '%s' which is "
+				    "neither a branch nor a tag\n",
+				    getprogname(), refname);
+			}
 			continue;
+		}
+
 		if (refsz == nref + 1) {
 			refsz *= 2;
 			have = reallocarray(have, refsz, sizeof(have[0]));
@@ -585,16 +607,22 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
 				free(theirs);
 				goto done;
 			}
-			fprintf(stderr, "%s: discovered remote ref %s\n",
+			fprintf(stderr, "%s: %s will be fetched\n",
 			    getprogname(), refname);
 			fprintf(stderr, "%s: theirs=%s\n%s: mine=%s\n",
-			getprogname(), theirs, getprogname(), mine);
+			    getprogname(), theirs, getprogname(), mine);
 			free(theirs);
 			free(mine);
 		}
 		nref++;
 	}
 
+	/* Abort if we haven't found any branch to fetch. */
+	if (!found_branch) {
+		err = got_error(GOT_ERR_FETCH_NO_BRANCH);
+		goto done;
+	}
+
 	for (i = 0; i < nref; i++) {
 		if (got_object_id_cmp(&have[i], &want[i]) == 0)
 			continue;
@@ -810,9 +838,10 @@ main(int argc, char **argv)
 	struct imsg imsg;
 	struct got_pathlist_head have_refs;
 	struct got_pathlist_entry *pe;
-	struct got_imsg_fetch_have_refs *fetch_have_refs = NULL;
-	struct got_imsg_fetch_have_ref *href = NULL;
-	size_t datalen;
+	struct got_imsg_fetch_request *fetch_req = NULL;
+	struct got_imsg_fetch_have_ref href;
+	size_t datalen, remain;
+	size_t offset;
 #if 0
 	static int attached;
 	while (!attached)
@@ -847,50 +876,58 @@ main(int argc, char **argv)
 		goto done;
 	}
 	datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
-	if (datalen < sizeof(struct got_imsg_fetch_have_refs)) {
+	if (datalen < sizeof(struct got_imsg_fetch_request)) {
 		err = got_error(GOT_ERR_PRIVSEP_LEN);
 		goto done;
 	}
-	fetch_have_refs = (struct got_imsg_fetch_have_refs *)imsg.data;
-	if (datalen < sizeof(struct got_imsg_fetch_have_refs) +
+	fetch_req = (struct got_imsg_fetch_request *)imsg.data;
+	if (datalen < sizeof(*fetch_req) +
 	    sizeof(struct got_imsg_fetch_have_ref) *
-	    fetch_have_refs->n_have_refs) {
+	    fetch_req->n_have_refs) {
 		err = got_error(GOT_ERR_PRIVSEP_LEN);
 		goto done;
 	}
-	href = (struct got_imsg_fetch_have_ref *)(
-	    (uint8_t *)fetch_have_refs + sizeof(fetch_have_refs->n_have_refs));
-	for (i = 0; i < fetch_have_refs->n_have_refs; i++) {
+	offset = sizeof(*fetch_req);
+	remain = datalen;
+	for (i = 0; i < fetch_req->n_have_refs; i++) {
 		struct got_object_id *id;
 		char *refname;
 
-		if (datalen < sizeof(*href) + href->name_len) {
+		if (remain < sizeof(href) || offset > datalen) {
 			err = got_error(GOT_ERR_PRIVSEP_LEN);
 			goto done;
 		}
-		datalen -= sizeof(*href) + href->name_len;
-		refname = strndup((uint8_t *)href + sizeof(href->id) +
-		    sizeof(href->name_len), href->name_len);
+		memcpy(&href, imsg.data + offset, sizeof(href));
+		remain -= sizeof(href);
+		if (remain < href.name_len) {
+			err = got_error(GOT_ERR_PRIVSEP_LEN);
+			goto done;
+		}
+		remain -= href.name_len;
+		refname = malloc(href.name_len + 1);
 		if (refname == NULL) {
-			err = got_error_from_errno("strndump");
+			err = got_error_from_errno("malloc");
 			goto done;
 		}
+		offset += sizeof(href);
+		memcpy(refname, imsg.data + offset, href.name_len);
+		refname[href.name_len] = '\0';
+		offset += href.name_len;
+
 		id = malloc(sizeof(*id));
 		if (id == NULL) {
 			free(refname);
 			err = got_error_from_errno("malloc");
 			goto done;
 		}
-		memcpy(id->sha1, href->id, SHA1_DIGEST_LENGTH);
+		memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
 		err = got_pathlist_append(&have_refs, refname, id);
 		if (err) {
 			free(refname);
 			free(id);
 			goto done;
 		}
-		href = (struct got_imsg_fetch_have_ref *)(
-		    (uint8_t *)href + sizeof(*href) + href->name_len);
-		}
+	}
 	fetchfd = imsg.fd;
 
 	if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
@@ -910,7 +947,8 @@ main(int argc, char **argv)
 	}
 	packfd = imsg.fd;
 
-	err = fetch_pack(fetchfd, packfd, &packid, &have_refs, &ibuf);
+	err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
+	    fetch_req->fetch_all_branches, &ibuf);
 done:
 	TAILQ_FOREACH(pe, &have_refs, entry) {
 		free((char *)pe->path);