@@ -310,43 +310,22 @@ static int compute_and_write_prerequisites(int bundle_fd,
310310 return 0 ;
311311}
312312
313- int create_bundle (struct bundle_header * header , const char * path ,
314- int argc , const char * * argv )
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 )
315323{
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 );
342-
343- if (argc > 1 )
344- return error (_ ("unrecognized argument: %s" ), argv [1 ]);
345-
346- object_array_remove_duplicates (& revs .pending );
324+ int i ;
325+ int ref_count = 0 ;
347326
348- for (i = 0 ; i < revs . pending .nr ; i ++ ) {
349- 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 ;
350329 unsigned char sha1 [20 ];
351330 char * ref ;
352331 const char * display_ref ;
@@ -361,7 +340,7 @@ int create_bundle(struct bundle_header *header, const char *path,
361340 display_ref = (flag & REF_ISSYMREF ) ? e -> name : ref ;
362341
363342 if (e -> item -> type == OBJ_TAG &&
364- !is_tag_in_date_range (e -> item , & revs )) {
343+ !is_tag_in_date_range (e -> item , revs )) {
365344 e -> item -> flags |= UNINTERESTING ;
366345 continue ;
367346 }
@@ -407,7 +386,7 @@ int create_bundle(struct bundle_header *header, const char *path,
407386 */
408387 obj = parse_object_or_die (sha1 , e -> name );
409388 obj -> flags |= SHOWN ;
410- add_pending_object (& revs , obj , e -> name );
389+ add_pending_object (revs , obj , e -> name );
411390 }
412391 free (ref );
413392 continue ;
@@ -420,11 +399,51 @@ int create_bundle(struct bundle_header *header, const char *path,
420399 write_or_die (bundle_fd , "\n" , 1 );
421400 free (ref );
422401 }
423- if (!ref_count )
424- die (_ ("Refusing to create empty bundle." ));
425402
426403 /* end header */
427404 write_or_die (bundle_fd , "\n" , 1 );
405+ return ref_count ;
406+ }
407+
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 ;
416+
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 ;
428447
429448 /* write pack */
430449 if (write_pack_data (bundle_fd , & lock , & revs ))
0 commit comments