bundle: split out a helper function to create pack data
authorJunio C Hamano <gitster@pobox.com>
Thu, 30 Oct 2014 17:45:41 +0000 (10:45 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Oct 2014 21:45:52 +0000 (14:45 -0700)
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>
bundle.c
index c846092e9cf99e94dd6ef3e2731c06b7107ec273..9c87532ff7e8dfa87695705cd65d986a371e8ae6 100644 (file)
--- a/bundle.c
+++ b/bundle.c
@@ -235,6 +235,41 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
        return result;
 }
 
+static int write_pack_data(int bundle_fd, struct lock_file *lock, struct rev_info *revs)
+{
+       struct child_process pack_objects = CHILD_PROCESS_INIT;
+       int i;
+
+       argv_array_pushl(&pack_objects.args,
+                        "pack-objects", "--all-progress-implied",
+                        "--stdout", "--thin", "--delta-base-offset",
+                        NULL);
+       pack_objects.in = -1;
+       pack_objects.out = bundle_fd;
+       pack_objects.git_cmd = 1;
+       if (start_command(&pack_objects))
+               return error(_("Could not spawn pack-objects"));
+
+       /*
+        * start_command closed bundle_fd if it was > 1
+        * so set the lock fd to -1 so commit_lock_file()
+        * won't fail trying to close it.
+        */
+       lock->fd = -1;
+
+       for (i = 0; i < revs->pending.nr; i++) {
+               struct object *object = revs->pending.objects[i].item;
+               if (object->flags & UNINTERESTING)
+                       write_or_die(pack_objects.in, "^", 1);
+               write_or_die(pack_objects.in, sha1_to_hex(object->sha1), 40);
+               write_or_die(pack_objects.in, "\n", 1);
+       }
+       close(pack_objects.in);
+       if (finish_command(&pack_objects))
+               return error(_("pack-objects died"));
+       return 0;
+}
+
 int create_bundle(struct bundle_header *header, const char *path,
                  int argc, const char **argv)
 {
@@ -381,34 +416,9 @@ int create_bundle(struct bundle_header *header, const char *path,
        write_or_die(bundle_fd, "\n", 1);
 
        /* write pack */
-       child_process_init(&rls);
-       argv_array_pushl(&rls.args,
-                        "pack-objects", "--all-progress-implied",
-                        "--stdout", "--thin", "--delta-base-offset",
-                        NULL);
-       rls.in = -1;
-       rls.out = bundle_fd;
-       rls.git_cmd = 1;
-       if (start_command(&rls))
-               return error(_("Could not spawn pack-objects"));
-
-       /*
-        * start_command closed bundle_fd if it was > 1
-        * so set the lock fd to -1 so commit_lock_file()
-        * won't fail trying to close it.
-        */
-       lock.fd = -1;
+       if (write_pack_data(bundle_fd, &lock, &revs))
+               return -1;
 
-       for (i = 0; i < revs.pending.nr; i++) {
-               struct object *object = revs.pending.objects[i].item;
-               if (object->flags & UNINTERESTING)
-                       write_or_die(rls.in, "^", 1);
-               write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
-               write_or_die(rls.in, "\n", 1);
-       }
-       close(rls.in);
-       if (finish_command(&rls))
-               return error(_("pack-objects died"));
        if (!bundle_to_stdout) {
                if (commit_lock_file(&lock))
                        die_errno(_("cannot create '%s'"), path);