@@ -430,7 +430,6 @@ static int reftable_ref_record_is_deletion_void(const void *p)
430
430
(const struct reftable_ref_record * )p );
431
431
}
432
432
433
-
434
433
static int reftable_ref_record_equal_void (const void * a ,
435
434
const void * b , int hash_size )
436
435
{
@@ -439,6 +438,13 @@ static int reftable_ref_record_equal_void(const void *a,
439
438
return reftable_ref_record_equal (ra , rb , hash_size );
440
439
}
441
440
441
+ static int reftable_ref_record_cmp_void (const void * _a , const void * _b )
442
+ {
443
+ const struct reftable_ref_record * a = _a ;
444
+ const struct reftable_ref_record * b = _b ;
445
+ return strcmp (a -> refname , b -> refname );
446
+ }
447
+
442
448
static void reftable_ref_record_print_void (const void * rec ,
443
449
int hash_size )
444
450
{
@@ -455,6 +461,7 @@ static struct reftable_record_vtable reftable_ref_record_vtable = {
455
461
.release = & reftable_ref_record_release_void ,
456
462
.is_deletion = & reftable_ref_record_is_deletion_void ,
457
463
.equal = & reftable_ref_record_equal_void ,
464
+ .cmp = & reftable_ref_record_cmp_void ,
458
465
.print = & reftable_ref_record_print_void ,
459
466
};
460
467
@@ -625,6 +632,25 @@ static int reftable_obj_record_equal_void(const void *a, const void *b, int hash
625
632
return 1 ;
626
633
}
627
634
635
+ static int reftable_obj_record_cmp_void (const void * _a , const void * _b )
636
+ {
637
+ const struct reftable_obj_record * a = _a ;
638
+ const struct reftable_obj_record * b = _b ;
639
+ int cmp ;
640
+
641
+ cmp = memcmp (a -> hash_prefix , b -> hash_prefix ,
642
+ a -> hash_prefix_len > b -> hash_prefix_len ?
643
+ a -> hash_prefix_len : b -> hash_prefix_len );
644
+ if (cmp )
645
+ return cmp ;
646
+
647
+ /*
648
+ * When the prefix is the same then the object record that is longer is
649
+ * considered to be bigger.
650
+ */
651
+ return a -> hash_prefix_len - b -> hash_prefix_len ;
652
+ }
653
+
628
654
static struct reftable_record_vtable reftable_obj_record_vtable = {
629
655
.key = & reftable_obj_record_key ,
630
656
.type = BLOCK_TYPE_OBJ ,
@@ -635,6 +661,7 @@ static struct reftable_record_vtable reftable_obj_record_vtable = {
635
661
.release = & reftable_obj_record_release ,
636
662
.is_deletion = & not_a_deletion ,
637
663
.equal = & reftable_obj_record_equal_void ,
664
+ .cmp = & reftable_obj_record_cmp_void ,
638
665
.print = & reftable_obj_record_print ,
639
666
};
640
667
@@ -953,6 +980,22 @@ static int reftable_log_record_equal_void(const void *a,
953
980
hash_size );
954
981
}
955
982
983
+ static int reftable_log_record_cmp_void (const void * _a , const void * _b )
984
+ {
985
+ const struct reftable_log_record * a = _a ;
986
+ const struct reftable_log_record * b = _b ;
987
+ int cmp = strcmp (a -> refname , b -> refname );
988
+ if (cmp )
989
+ return cmp ;
990
+
991
+ /*
992
+ * Note that the comparison here is reversed. This is because the
993
+ * update index is reversed when comparing keys. For reference, see how
994
+ * we handle this in reftable_log_record_key()`.
995
+ */
996
+ return b -> update_index - a -> update_index ;
997
+ }
998
+
956
999
int reftable_log_record_equal (const struct reftable_log_record * a ,
957
1000
const struct reftable_log_record * b , int hash_size )
958
1001
{
@@ -1002,6 +1045,7 @@ static struct reftable_record_vtable reftable_log_record_vtable = {
1002
1045
.release = & reftable_log_record_release_void ,
1003
1046
.is_deletion = & reftable_log_record_is_deletion_void ,
1004
1047
.equal = & reftable_log_record_equal_void ,
1048
+ .cmp = & reftable_log_record_cmp_void ,
1005
1049
.print = & reftable_log_record_print_void ,
1006
1050
};
1007
1051
@@ -1077,6 +1121,13 @@ static int reftable_index_record_equal(const void *a, const void *b, int hash_si
1077
1121
return ia -> offset == ib -> offset && !strbuf_cmp (& ia -> last_key , & ib -> last_key );
1078
1122
}
1079
1123
1124
+ static int reftable_index_record_cmp (const void * _a , const void * _b )
1125
+ {
1126
+ const struct reftable_index_record * a = _a ;
1127
+ const struct reftable_index_record * b = _b ;
1128
+ return strbuf_cmp (& a -> last_key , & b -> last_key );
1129
+ }
1130
+
1080
1131
static void reftable_index_record_print (const void * rec , int hash_size )
1081
1132
{
1082
1133
const struct reftable_index_record * idx = rec ;
@@ -1094,6 +1145,7 @@ static struct reftable_record_vtable reftable_index_record_vtable = {
1094
1145
.release = & reftable_index_record_release ,
1095
1146
.is_deletion = & not_a_deletion ,
1096
1147
.equal = & reftable_index_record_equal ,
1148
+ .cmp = & reftable_index_record_cmp ,
1097
1149
.print = & reftable_index_record_print ,
1098
1150
};
1099
1151
@@ -1147,6 +1199,14 @@ int reftable_record_is_deletion(struct reftable_record *rec)
1147
1199
reftable_record_data (rec ));
1148
1200
}
1149
1201
1202
+ int reftable_record_cmp (struct reftable_record * a , struct reftable_record * b )
1203
+ {
1204
+ if (a -> type != b -> type )
1205
+ BUG ("cannot compare reftable records of different type" );
1206
+ return reftable_record_vtable (a )-> cmp (
1207
+ reftable_record_data (a ), reftable_record_data (b ));
1208
+ }
1209
+
1150
1210
int reftable_record_equal (struct reftable_record * a , struct reftable_record * b , int hash_size )
1151
1211
{
1152
1212
if (a -> type != b -> type )
0 commit comments