Skip to content

Commit 6b55f8b

Browse files
committed
Merge branch 'rs/use-child-process-init-more'
* rs/use-child-process-init-more: bundle: split out ref writing from bundle_create bundle: split out a helper function to compute and write prerequisites bundle: split out a helper function to create pack data use child_process_init() to initialize struct child_process variables
2 parents e44da1b + d9362ef commit 6b55f8b

File tree

4 files changed

+106
-66
lines changed

4 files changed

+106
-66
lines changed

bundle.c

Lines changed: 103 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -235,33 +235,50 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
235235
return result;
236236
}
237237

238-
int create_bundle(struct bundle_header *header, const char *path,
239-
int argc, const char **argv)
238+
static int write_pack_data(int bundle_fd, struct lock_file *lock, struct rev_info *revs)
240239
{
241-
static struct lock_file lock;
242-
int bundle_fd = -1;
243-
int bundle_to_stdout;
244-
int i, ref_count = 0;
245-
struct strbuf buf = STRBUF_INIT;
246-
struct rev_info revs;
247-
struct child_process rls = CHILD_PROCESS_INIT;
248-
FILE *rls_fout;
240+
struct child_process pack_objects = CHILD_PROCESS_INIT;
241+
int i;
249242

250-
bundle_to_stdout = !strcmp(path, "-");
251-
if (bundle_to_stdout)
252-
bundle_fd = 1;
253-
else
254-
bundle_fd = hold_lock_file_for_update(&lock, path,
255-
LOCK_DIE_ON_ERROR);
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"));
256252

257-
/* write signature */
258-
write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
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;
259259

260-
/* init revs to list objects for pack-objects later */
261-
save_commit_buffer = 0;
262-
init_revisions(&revs, NULL);
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+
static int compute_and_write_prerequisites(int bundle_fd,
274+
struct rev_info *revs,
275+
int argc, const char **argv)
276+
{
277+
struct child_process rls = CHILD_PROCESS_INIT;
278+
struct strbuf buf = STRBUF_INIT;
279+
FILE *rls_fout;
280+
int i;
263281

264-
/* write prerequisites */
265282
argv_array_pushl(&rls.args,
266283
"rev-list", "--boundary", "--pretty=oneline",
267284
NULL);
@@ -279,7 +296,7 @@ int create_bundle(struct bundle_header *header, const char *path,
279296
if (!get_sha1_hex(buf.buf + 1, sha1)) {
280297
struct object *object = parse_object_or_die(sha1, buf.buf);
281298
object->flags |= UNINTERESTING;
282-
add_pending_object(&revs, object, buf.buf);
299+
add_pending_object(revs, object, buf.buf);
283300
}
284301
} else if (!get_sha1_hex(buf.buf, sha1)) {
285302
struct object *object = parse_object_or_die(sha1, buf.buf);
@@ -290,17 +307,25 @@ int create_bundle(struct bundle_header *header, const char *path,
290307
fclose(rls_fout);
291308
if (finish_command(&rls))
292309
return error(_("rev-list died"));
310+
return 0;
311+
}
293312

294-
/* write references */
295-
argc = setup_revisions(argc, argv, &revs, NULL);
296-
297-
if (argc > 1)
298-
return error(_("unrecognized argument: %s"), argv[1]);
299-
300-
object_array_remove_duplicates(&revs.pending);
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+
{
324+
int i;
325+
int ref_count = 0;
301326

302-
for (i = 0; i < revs.pending.nr; i++) {
303-
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;
304329
unsigned char sha1[20];
305330
char *ref;
306331
const char *display_ref;
@@ -315,7 +340,7 @@ int create_bundle(struct bundle_header *header, const char *path,
315340
display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
316341

317342
if (e->item->type == OBJ_TAG &&
318-
!is_tag_in_date_range(e->item, &revs)) {
343+
!is_tag_in_date_range(e->item, revs)) {
319344
e->item->flags |= UNINTERESTING;
320345
continue;
321346
}
@@ -361,7 +386,7 @@ int create_bundle(struct bundle_header *header, const char *path,
361386
*/
362387
obj = parse_object_or_die(sha1, e->name);
363388
obj->flags |= SHOWN;
364-
add_pending_object(&revs, obj, e->name);
389+
add_pending_object(revs, obj, e->name);
365390
}
366391
free(ref);
367392
continue;
@@ -374,41 +399,56 @@ int create_bundle(struct bundle_header *header, const char *path,
374399
write_or_die(bundle_fd, "\n", 1);
375400
free(ref);
376401
}
377-
if (!ref_count)
378-
die(_("Refusing to create empty bundle."));
379402

380403
/* end header */
381404
write_or_die(bundle_fd, "\n", 1);
405+
return ref_count;
406+
}
382407

383-
/* write pack */
384-
memset(&rls, 0, sizeof(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"));
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;
394416

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;
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))
450+
return -1;
401451

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"));
412452
if (!bundle_to_stdout) {
413453
if (commit_lock_file(&lock))
414454
die_errno(_("cannot create '%s'"), path);

column.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ int run_column_filter(int colopts, const struct column_options *opts)
374374
if (fd_out != -1)
375375
return -1;
376376

377-
memset(&column_process, 0, sizeof(column_process));
377+
child_process_init(&column_process);
378378
argv = &column_process.args;
379379

380380
argv_array_push(argv, "column");

trailer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ static const char *apply_command(const char *command, const char *arg)
237237
strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
238238

239239
argv[0] = cmd.buf;
240-
memset(&cp, 0, sizeof(cp));
240+
child_process_init(&cp);
241241
cp.argv = argv;
242242
cp.env = local_repo_env;
243243
cp.no_stdin = 1;

transport-helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ static int get_exporter(struct transport *transport,
414414
struct child_process *helper = get_helper(transport);
415415
int i;
416416

417-
memset(fastexport, 0, sizeof(*fastexport));
417+
child_process_init(fastexport);
418418

419419
/* we need to duplicate helper->in because we want to use it after
420420
* fastexport is done with it. */

0 commit comments

Comments
 (0)