@@ -243,7 +243,7 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
243
243
}
244
244
245
245
246
- /* Write the pack data to bundle_fd, then close it if it is > 1. */
246
+ /* Write the pack data to bundle_fd */
247
247
static int write_pack_data (int bundle_fd , struct rev_info * revs )
248
248
{
249
249
struct child_process pack_objects = CHILD_PROCESS_INIT ;
@@ -256,6 +256,20 @@ static int write_pack_data(int bundle_fd, struct rev_info *revs)
256
256
pack_objects .in = -1 ;
257
257
pack_objects .out = bundle_fd ;
258
258
pack_objects .git_cmd = 1 ;
259
+
260
+ /*
261
+ * start_command() will close our descriptor if it's >1. Duplicate it
262
+ * to avoid surprising the caller.
263
+ */
264
+ if (pack_objects .out > 1 ) {
265
+ pack_objects .out = dup (pack_objects .out );
266
+ if (pack_objects .out < 0 ) {
267
+ error_errno (_ ("unable to dup bundle descriptor" ));
268
+ child_process_clear (& pack_objects );
269
+ return -1 ;
270
+ }
271
+ }
272
+
259
273
if (start_command (& pack_objects ))
260
274
return error (_ ("Could not spawn pack-objects" ));
261
275
@@ -421,21 +435,10 @@ int create_bundle(struct bundle_header *header, const char *path,
421
435
bundle_to_stdout = !strcmp (path , "-" );
422
436
if (bundle_to_stdout )
423
437
bundle_fd = 1 ;
424
- else {
438
+ else
425
439
bundle_fd = hold_lock_file_for_update (& lock , path ,
426
440
LOCK_DIE_ON_ERROR );
427
441
428
- /*
429
- * write_pack_data() will close the fd passed to it,
430
- * but commit_lock_file() will also try to close the
431
- * lockfile's fd. So make a copy of the file
432
- * descriptor to avoid trying to close it twice.
433
- */
434
- bundle_fd = dup (bundle_fd );
435
- if (bundle_fd < 0 )
436
- die_errno ("unable to dup file descriptor" );
437
- }
438
-
439
442
/* write signature */
440
443
write_or_die (bundle_fd , bundle_signature , strlen (bundle_signature ));
441
444
@@ -457,29 +460,22 @@ int create_bundle(struct bundle_header *header, const char *path,
457
460
object_array_remove_duplicates (& revs .pending );
458
461
459
462
ref_count = write_bundle_refs (bundle_fd , & revs );
460
- if (ref_count <= 0 ) {
461
- if (! ref_count )
462
- error ( _ ( "Refusing to create empty bundle." ));
463
+ if (! ref_count )
464
+ die ( _ ( "Refusing to create empty bundle." ));
465
+ else if ( ref_count < 0 )
463
466
goto err ;
464
- }
465
467
466
468
/* write pack */
467
- if (write_pack_data (bundle_fd , & revs )) {
468
- bundle_fd = -1 ; /* already closed by the above call */
469
+ if (write_pack_data (bundle_fd , & revs ))
469
470
goto err ;
470
- }
471
471
472
472
if (!bundle_to_stdout ) {
473
473
if (commit_lock_file (& lock ))
474
474
die_errno (_ ("cannot create '%s'" ), path );
475
475
}
476
476
return 0 ;
477
477
err :
478
- if (!bundle_to_stdout ) {
479
- if (0 <= bundle_fd )
480
- close (bundle_fd );
481
- rollback_lock_file (& lock );
482
- }
478
+ rollback_lock_file (& lock );
483
479
return -1 ;
484
480
}
485
481
0 commit comments