bundle: split out a helper function to create pack data
The create_bundle() function, while it does one single logical thing, takes a rather large implementation to do so. Let's start separating what it does into smaller steps to make it easier to see what is going on. This is a first step to separate out the actual pack-data generation, after the earlier part of the function figures out which part of the history to place in the bundle. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Oct 30, 2014 at 10:45 UTC
5e626b91d4a5d2cfee8747facd53d7661f1f9112
1 file changed
+37
-27
bundle.c
+37
-27
@@ -235,6 +235,41 @@ out:
235
return result;
236
}
237
238
+static int write_pack_data(int bundle_fd, struct lock_file *lock, struct rev_info *revs)
239
+{
240
+ struct child_process pack_objects = CHILD_PROCESS_INIT;
241
+ int i;
242
+
243
+ argv_array_pushl(&pack_objects.args,
244
+ "pack-objects", "--all-progress-implied",
245
+ "--stdout", "--thin", "--delta-base-offset",
246
+ NULL);
247
+ pack_objects.in = -1;
248
+ pack_objects.out = bundle_fd;
249
+ pack_objects.git_cmd = 1;
250
+ if (start_command(&pack_objects))
251
+ return error(_("Could not spawn pack-objects"));
252
+
253
+ /*
254
+ * start_command closed bundle_fd if it was > 1
255
+ * so set the lock fd to -1 so commit_lock_file()
256
+ * won't fail trying to close it.
257
+ */
258
+ lock->fd = -1;
259
+
260
+ for (i = 0; i < revs->pending.nr; i++) {
261
+ struct object *object = revs->pending.objects[i].item;
262
+ if (object->flags & UNINTERESTING)
263
+ write_or_die(pack_objects.in, "^", 1);
264
+ write_or_die(pack_objects.in, sha1_to_hex(object->sha1), 40);
265
+ write_or_die(pack_objects.in, "\n", 1);
266
+ }
267
+ close(pack_objects.in);
268
+ if (finish_command(&pack_objects))
269
+ return error(_("pack-objects died"));
270
+ return 0;
271
+}
272
+
273
int create_bundle(struct bundle_header *header, const char *path,
274
int argc, const char **argv)
275
{
@@ -381,34 +416,9 @@ int create_bundle(struct bundle_header *header, const char *path,
416
write_or_die(bundle_fd, "\n", 1);
417
418
/* write pack */
384
- child_process_init(&rls);
385
- argv_array_pushl(&rls.args,
386
- "pack-objects", "--all-progress-implied",
387
- "--stdout", "--thin", "--delta-base-offset",
388
- NULL);
389
- rls.in = -1;
390
- rls.out = bundle_fd;
391
- rls.git_cmd = 1;
392
- if (start_command(&rls))
393
- return error(_("Could not spawn pack-objects"));
394
-
395
- /*
396
- * start_command closed bundle_fd if it was > 1
397
- * so set the lock fd to -1 so commit_lock_file()
398
- * won't fail trying to close it.
399
- */
400
- lock.fd = -1;
419
+ if (write_pack_data(bundle_fd, &lock, &revs))
420
+ return -1;
421
402
- for (i = 0; i < revs.pending.nr; i++) {
403
- struct object *object = revs.pending.objects[i].item;
404
- if (object->flags & UNINTERESTING)
405
- write_or_die(rls.in, "^", 1);
406
- write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
407
- write_or_die(rls.in, "\n", 1);
408
- }
409
- close(rls.in);
410
- if (finish_command(&rls))
411
- return error(_("pack-objects died"));
422
if (!bundle_to_stdout) {
423
if (commit_lock_file(&lock))
424
die_errno(_("cannot create '%s'"), path);