bundle: split out a helper function to compute and write prerequisites

The new helper compute_and_write_prerequistes() is ugly, but it cannot be avoided. Ideally we should avoid a function that computes and does I/O at the same time, but the prerequisites lines in the output needs the human readable title only to help the recipient of the bundle. The code copies them straight from the rev-list output and immediately discards as no other internal computation needs that information. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 30, 2014 at 11:01 UTC e8eb25122e2cf966bf774fd0e3596d1b3dc93be6
1 file changed +35 -24
bundle.c
+35 -24
@@ -270,33 +270,15 @@ static int write_pack_data(int bundle_fd, struct lock_file *lock, struct rev_inf
270 return 0;
271 }
272
273 -int create_bundle(struct bundle_header *header, const char *path,
274 - int argc, const char **argv)
273 +static int compute_and_write_prerequisites(int bundle_fd,
274 + struct rev_info *revs,
275 + int argc, const char **argv)
276 {
276 - static struct lock_file lock;
277 - int bundle_fd = -1;
278 - int bundle_to_stdout;
279 - int i, ref_count = 0;
280 - struct strbuf buf = STRBUF_INIT;
281 - struct rev_info revs;
277 struct child_process rls = CHILD_PROCESS_INIT;
278 + struct strbuf buf = STRBUF_INIT;
279 FILE *rls_fout;
280 + int i;
281
285 - bundle_to_stdout = !strcmp(path, "-");
286 - if (bundle_to_stdout)
287 - bundle_fd = 1;
288 - else
289 - bundle_fd = hold_lock_file_for_update(&lock, path,
290 - LOCK_DIE_ON_ERROR);
291 -
292 - /* write signature */
293 - write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
294 -
295 - /* init revs to list objects for pack-objects later */
296 - save_commit_buffer = 0;
297 - init_revisions(&revs, NULL);
298 -
299 - /* write prerequisites */
282 argv_array_pushl(&rls.args,
283 "rev-list", "--boundary", "--pretty=oneline",
284 NULL);
@@ -314,7 +296,7 @@ int create_bundle(struct bundle_header *header, const char *path,
296 if (!get_sha1_hex(buf.buf + 1, sha1)) {
297 struct object *object = parse_object_or_die(sha1, buf.buf);
298 object->flags |= UNINTERESTING;
317 - add_pending_object(&revs, object, buf.buf);
299 + add_pending_object(revs, object, buf.buf);
300 }
301 } else if (!get_sha1_hex(buf.buf, sha1)) {
302 struct object *object = parse_object_or_die(sha1, buf.buf);
@@ -325,6 +307,35 @@ int create_bundle(struct bundle_header *header, const char *path,
307 fclose(rls_fout);
308 if (finish_command(&rls))
309 return error(_("rev-list died"));
310 + return 0;
311 +}
312 +
313 +int create_bundle(struct bundle_header *header, const char *path,
314 + int argc, const char **argv)
315 +{
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);