@@ -21,6 +21,7 @@ struct batch_options {
21
21
int print_contents ;
22
22
int buffer_output ;
23
23
int all_objects ;
24
+ int unordered ;
24
25
int cmdmode ; /* may be 'w' or 'c' for --filters or --textconv */
25
26
const char * format ;
26
27
};
@@ -337,11 +338,11 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
337
338
}
338
339
}
339
340
340
- static void batch_object_write (const char * obj_name , struct batch_options * opt ,
341
+ static void batch_object_write (const char * obj_name ,
342
+ struct strbuf * scratch ,
343
+ struct batch_options * opt ,
341
344
struct expand_data * data )
342
345
{
343
- struct strbuf buf = STRBUF_INIT ;
344
-
345
346
if (!data -> skip_object_info &&
346
347
oid_object_info_extended (the_repository , & data -> oid , & data -> info ,
347
348
OBJECT_INFO_LOOKUP_REPLACE ) < 0 ) {
@@ -351,18 +352,20 @@ static void batch_object_write(const char *obj_name, struct batch_options *opt,
351
352
return ;
352
353
}
353
354
354
- strbuf_expand ( & buf , opt -> format , expand_format , data );
355
- strbuf_addch ( & buf , '\n' );
356
- batch_write ( opt , buf . buf , buf . len );
357
- strbuf_release ( & buf );
355
+ strbuf_reset ( scratch );
356
+ strbuf_expand ( scratch , opt -> format , expand_format , data );
357
+ strbuf_addch ( scratch , '\n' );
358
+ batch_write ( opt , scratch -> buf , scratch -> len );
358
359
359
360
if (opt -> print_contents ) {
360
361
print_object_or_die (opt , data );
361
362
batch_write (opt , "\n" , 1 );
362
363
}
363
364
}
364
365
365
- static void batch_one_object (const char * obj_name , struct batch_options * opt ,
366
+ static void batch_one_object (const char * obj_name ,
367
+ struct strbuf * scratch ,
368
+ struct batch_options * opt ,
366
369
struct expand_data * data )
367
370
{
368
371
struct object_context ctx ;
@@ -404,42 +407,70 @@ static void batch_one_object(const char *obj_name, struct batch_options *opt,
404
407
return ;
405
408
}
406
409
407
- batch_object_write (obj_name , opt , data );
410
+ batch_object_write (obj_name , scratch , opt , data );
408
411
}
409
412
410
413
struct object_cb_data {
411
414
struct batch_options * opt ;
412
415
struct expand_data * expand ;
416
+ struct oidset * seen ;
417
+ struct strbuf * scratch ;
413
418
};
414
419
415
420
static int batch_object_cb (const struct object_id * oid , void * vdata )
416
421
{
417
422
struct object_cb_data * data = vdata ;
418
423
oidcpy (& data -> expand -> oid , oid );
419
- batch_object_write (NULL , data -> opt , data -> expand );
424
+ batch_object_write (NULL , data -> scratch , data -> opt , data -> expand );
420
425
return 0 ;
421
426
}
422
427
423
- static int batch_loose_object (const struct object_id * oid ,
424
- const char * path ,
425
- void * data )
428
+ static int collect_loose_object (const struct object_id * oid ,
429
+ const char * path ,
430
+ void * data )
426
431
{
427
432
oid_array_append (data , oid );
428
433
return 0 ;
429
434
}
430
435
431
- static int batch_packed_object (const struct object_id * oid ,
432
- struct packed_git * pack ,
433
- uint32_t pos ,
434
- void * data )
436
+ static int collect_packed_object (const struct object_id * oid ,
437
+ struct packed_git * pack ,
438
+ uint32_t pos ,
439
+ void * data )
435
440
{
436
441
oid_array_append (data , oid );
437
442
return 0 ;
438
443
}
439
444
445
+ static int batch_unordered_object (const struct object_id * oid , void * vdata )
446
+ {
447
+ struct object_cb_data * data = vdata ;
448
+
449
+ if (oidset_insert (data -> seen , oid ))
450
+ return 0 ;
451
+
452
+ return batch_object_cb (oid , data );
453
+ }
454
+
455
+ static int batch_unordered_loose (const struct object_id * oid ,
456
+ const char * path ,
457
+ void * data )
458
+ {
459
+ return batch_unordered_object (oid , data );
460
+ }
461
+
462
+ static int batch_unordered_packed (const struct object_id * oid ,
463
+ struct packed_git * pack ,
464
+ uint32_t pos ,
465
+ void * data )
466
+ {
467
+ return batch_unordered_object (oid , data );
468
+ }
469
+
440
470
static int batch_objects (struct batch_options * opt )
441
471
{
442
- struct strbuf buf = STRBUF_INIT ;
472
+ struct strbuf input = STRBUF_INIT ;
473
+ struct strbuf output = STRBUF_INIT ;
443
474
struct expand_data data ;
444
475
int save_warning ;
445
476
int retval = 0 ;
@@ -454,8 +485,9 @@ static int batch_objects(struct batch_options *opt)
454
485
*/
455
486
memset (& data , 0 , sizeof (data ));
456
487
data .mark_query = 1 ;
457
- strbuf_expand (& buf , opt -> format , expand_format , & data );
488
+ strbuf_expand (& output , opt -> format , expand_format , & data );
458
489
data .mark_query = 0 ;
490
+ strbuf_release (& output );
459
491
if (opt -> cmdmode )
460
492
data .split_on_whitespace = 1 ;
461
493
@@ -473,19 +505,37 @@ static int batch_objects(struct batch_options *opt)
473
505
data .info .typep = & data .type ;
474
506
475
507
if (opt -> all_objects ) {
476
- struct oid_array sa = OID_ARRAY_INIT ;
477
508
struct object_cb_data cb ;
478
509
479
- for_each_loose_object (batch_loose_object , & sa , 0 );
480
- for_each_packed_object (batch_packed_object , & sa , 0 );
481
510
if (repository_format_partial_clone )
482
511
warning ("This repository has extensions.partialClone set. Some objects may not be loaded." );
483
512
484
513
cb .opt = opt ;
485
514
cb .expand = & data ;
486
- oid_array_for_each_unique (& sa , batch_object_cb , & cb );
515
+ cb .scratch = & output ;
516
+
517
+ if (opt -> unordered ) {
518
+ struct oidset seen = OIDSET_INIT ;
519
+
520
+ cb .seen = & seen ;
521
+
522
+ for_each_loose_object (batch_unordered_loose , & cb , 0 );
523
+ for_each_packed_object (batch_unordered_packed , & cb ,
524
+ FOR_EACH_OBJECT_PACK_ORDER );
525
+
526
+ oidset_clear (& seen );
527
+ } else {
528
+ struct oid_array sa = OID_ARRAY_INIT ;
529
+
530
+ for_each_loose_object (collect_loose_object , & sa , 0 );
531
+ for_each_packed_object (collect_packed_object , & sa , 0 );
532
+
533
+ oid_array_for_each_unique (& sa , batch_object_cb , & cb );
534
+
535
+ oid_array_clear (& sa );
536
+ }
487
537
488
- oid_array_clear ( & sa );
538
+ strbuf_release ( & output );
489
539
return 0 ;
490
540
}
491
541
@@ -499,25 +549,26 @@ static int batch_objects(struct batch_options *opt)
499
549
save_warning = warn_on_object_refname_ambiguity ;
500
550
warn_on_object_refname_ambiguity = 0 ;
501
551
502
- while (strbuf_getline (& buf , stdin ) != EOF ) {
552
+ while (strbuf_getline (& input , stdin ) != EOF ) {
503
553
if (data .split_on_whitespace ) {
504
554
/*
505
555
* Split at first whitespace, tying off the beginning
506
556
* of the string and saving the remainder (or NULL) in
507
557
* data.rest.
508
558
*/
509
- char * p = strpbrk (buf .buf , " \t" );
559
+ char * p = strpbrk (input .buf , " \t" );
510
560
if (p ) {
511
561
while (* p && strchr (" \t" , * p ))
512
562
* p ++ = '\0' ;
513
563
}
514
564
data .rest = p ;
515
565
}
516
566
517
- batch_one_object (buf .buf , opt , & data );
567
+ batch_one_object (input .buf , & output , opt , & data );
518
568
}
519
569
520
- strbuf_release (& buf );
570
+ strbuf_release (& input );
571
+ strbuf_release (& output );
521
572
warn_on_object_refname_ambiguity = save_warning ;
522
573
return retval ;
523
574
}
@@ -586,6 +637,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
586
637
N_ ("follow in-tree symlinks (used with --batch or --batch-check)" )),
587
638
OPT_BOOL (0 , "batch-all-objects" , & batch .all_objects ,
588
639
N_ ("show all objects with --batch or --batch-check" )),
640
+ OPT_BOOL (0 , "unordered" , & batch .unordered ,
641
+ N_ ("do not order --batch-all-objects output" )),
589
642
OPT_END ()
590
643
};
591
644
0 commit comments