bundle: split out ref writing from bundle_create

The bundle_create() function has a number of logical steps: process the input, write the refs, and write the packfile. Recent commits split the first and third into separate sub-functions. It's worth splitting the middle step out, too, if only because it makes the progression of the steps more obvious. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 30, 2014 at 17:35 UTC d9362ef9b91ea2dccc80553dbcb37dc6c5d78548
1 file changed +58 -39
bundle.c
+58 -39
@@ -310,43 +310,22 @@ static int compute_and_write_prerequisites(int bundle_fd,
310 return 0;
311 }
312
313 -int create_bundle(struct bundle_header *header, const char *path,
314 - int argc, const char **argv)
313 +/*
314 + * Write out bundle refs based on the tips already
315 + * parsed into revs.pending. As a side effect, may
316 + * manipulate revs.pending to include additional
317 + * necessary objects (like tags).
318 + *
319 + * Returns the number of refs written, or negative
320 + * on error.
321 + */
322 +static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
323 {
316 - static struct lock_file lock;
317 - int bundle_fd = -1;
318 - int bundle_to_stdout;
319 - int i, ref_count = 0;
320 - struct rev_info revs;
321 -
322 - bundle_to_stdout = !strcmp(path, "-");
323 - if (bundle_to_stdout)
324 - bundle_fd = 1;
325 - else
326 - bundle_fd = hold_lock_file_for_update(&lock, path,
327 - LOCK_DIE_ON_ERROR);
328 -
329 - /* write signature */
330 - write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
331 -
332 - /* init revs to list objects for pack-objects later */
333 - save_commit_buffer = 0;
334 - init_revisions(&revs, NULL);
335 -
336 - /* write prerequisites */
337 - if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
338 - return -1;
339 -
340 - /* write references */
341 - argc = setup_revisions(argc, argv, &revs, NULL);
342 -
343 - if (argc > 1)
344 - return error(_("unrecognized argument: %s"), argv[1]);
345 -
346 - object_array_remove_duplicates(&revs.pending);
324 + int i;
325 + int ref_count = 0;
326
348 - for (i = 0; i < revs.pending.nr; i++) {
349 - struct object_array_entry *e = revs.pending.objects + i;
327 + for (i = 0; i < revs->pending.nr; i++) {
328 + struct object_array_entry *e = revs->pending.objects + i;
329 unsigned char sha1[20];
330 char *ref;
331 const char *display_ref;
@@ -361,7 +340,7 @@ int create_bundle(struct bundle_header *header, const char *path,
340 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
341
342 if (e->item->type == OBJ_TAG &&
364 - !is_tag_in_date_range(e->item, &revs)) {
343 + !is_tag_in_date_range(e->item, revs)) {
344 e->item->flags |= UNINTERESTING;
345 continue;
346 }
@@ -407,7 +386,7 @@ int create_bundle(struct bundle_header *header, const char *path,
386 */
387 obj = parse_object_or_die(sha1, e->name);
388 obj->flags |= SHOWN;
410 - add_pending_object(&revs, obj, e->name);
389 + add_pending_object(revs, obj, e->name);
390 }
391 free(ref);
392 continue;
@@ -420,11 +399,51 @@ int create_bundle(struct bundle_header *header, const char *path,
399 write_or_die(bundle_fd, "\n", 1);
400 free(ref);
401 }
423 - if (!ref_count)
424 - die(_("Refusing to create empty bundle."));
402
403 /* end header */
404 write_or_die(bundle_fd, "\n", 1);
405 + return ref_count;
406 +}
407 +
408 +int create_bundle(struct bundle_header *header, const char *path,
409 + int argc, const char **argv)
410 +{
411 + static struct lock_file lock;
412 + int bundle_fd = -1;
413 + int bundle_to_stdout;
414 + int ref_count = 0;
415 + struct rev_info revs;
416 +
417 + bundle_to_stdout = !strcmp(path, "-");
418 + if (bundle_to_stdout)
419 + bundle_fd = 1;
420 + else
421 + bundle_fd = hold_lock_file_for_update(&lock, path,
422 + LOCK_DIE_ON_ERROR);
423 +
424 + /* write signature */
425 + write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
426 +
427 + /* init revs to list objects for pack-objects later */
428 + save_commit_buffer = 0;
429 + init_revisions(&revs, NULL);
430 +
431 + /* write prerequisites */
432 + if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
433 + return -1;
434 +
435 + argc = setup_revisions(argc, argv, &revs, NULL);
436 +
437 + if (argc > 1)
438 + return error(_("unrecognized argument: %s"), argv[1]);
439 +
440 + object_array_remove_duplicates(&revs.pending);
441 +
442 + ref_count = write_bundle_refs(bundle_fd, &revs);
443 + if (!ref_count)
444 + die(_("Refusing to create empty bundle."));
445 + else if (ref_count < 0)
446 + return -1;
447
448 /* write pack */
449 if (write_pack_data(bundle_fd, &lock, &revs))