@@ -235,33 +235,50 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
235
235
return result ;
236
236
}
237
237
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 )
240
239
{
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 ;
249
242
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" ));
256
252
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 ;
259
259
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 ;
263
281
264
- /* write prerequisites */
265
282
argv_array_pushl (& rls .args ,
266
283
"rev-list" , "--boundary" , "--pretty=oneline" ,
267
284
NULL );
@@ -279,7 +296,7 @@ int create_bundle(struct bundle_header *header, const char *path,
279
296
if (!get_sha1_hex (buf .buf + 1 , sha1 )) {
280
297
struct object * object = parse_object_or_die (sha1 , buf .buf );
281
298
object -> flags |= UNINTERESTING ;
282
- add_pending_object (& revs , object , buf .buf );
299
+ add_pending_object (revs , object , buf .buf );
283
300
}
284
301
} else if (!get_sha1_hex (buf .buf , sha1 )) {
285
302
struct object * object = parse_object_or_die (sha1 , buf .buf );
@@ -290,17 +307,25 @@ int create_bundle(struct bundle_header *header, const char *path,
290
307
fclose (rls_fout );
291
308
if (finish_command (& rls ))
292
309
return error (_ ("rev-list died" ));
310
+ return 0 ;
311
+ }
293
312
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 ;
301
326
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 ;
304
329
unsigned char sha1 [20 ];
305
330
char * ref ;
306
331
const char * display_ref ;
@@ -315,7 +340,7 @@ int create_bundle(struct bundle_header *header, const char *path,
315
340
display_ref = (flag & REF_ISSYMREF ) ? e -> name : ref ;
316
341
317
342
if (e -> item -> type == OBJ_TAG &&
318
- !is_tag_in_date_range (e -> item , & revs )) {
343
+ !is_tag_in_date_range (e -> item , revs )) {
319
344
e -> item -> flags |= UNINTERESTING ;
320
345
continue ;
321
346
}
@@ -361,7 +386,7 @@ int create_bundle(struct bundle_header *header, const char *path,
361
386
*/
362
387
obj = parse_object_or_die (sha1 , e -> name );
363
388
obj -> flags |= SHOWN ;
364
- add_pending_object (& revs , obj , e -> name );
389
+ add_pending_object (revs , obj , e -> name );
365
390
}
366
391
free (ref );
367
392
continue ;
@@ -374,41 +399,56 @@ int create_bundle(struct bundle_header *header, const char *path,
374
399
write_or_die (bundle_fd , "\n" , 1 );
375
400
free (ref );
376
401
}
377
- if (!ref_count )
378
- die (_ ("Refusing to create empty bundle." ));
379
402
380
403
/* end header */
381
404
write_or_die (bundle_fd , "\n" , 1 );
405
+ return ref_count ;
406
+ }
382
407
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 ;
394
416
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 ;
401
451
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" ));
412
452
if (!bundle_to_stdout ) {
413
453
if (commit_lock_file (& lock ))
414
454
die_errno (_ ("cannot create '%s'" ), path );
0 commit comments