25
25
#include "midx.h"
26
26
#include "commit-reach.h"
27
27
#include "date.h"
28
+ #include "object-file-convert.h"
28
29
29
30
static int get_oid_oneline (struct repository * r , const char * , struct object_id * , struct commit_list * );
30
31
@@ -49,6 +50,7 @@ struct disambiguate_state {
49
50
50
51
static void update_candidates (struct disambiguate_state * ds , const struct object_id * current )
51
52
{
53
+ /* The hash algorithm of current has already been filtered */
52
54
if (ds -> always_call_fn ) {
53
55
ds -> ambiguous = ds -> fn (ds -> repo , current , ds -> cb_data ) ? 1 : 0 ;
54
56
return ;
@@ -134,6 +136,8 @@ static void unique_in_midx(struct multi_pack_index *m,
134
136
{
135
137
uint32_t num , i , first = 0 ;
136
138
const struct object_id * current = NULL ;
139
+ int len = ds -> len > ds -> repo -> hash_algo -> hexsz ?
140
+ ds -> repo -> hash_algo -> hexsz : ds -> len ;
137
141
num = m -> num_objects ;
138
142
139
143
if (!num )
@@ -149,7 +153,7 @@ static void unique_in_midx(struct multi_pack_index *m,
149
153
for (i = first ; i < num && !ds -> ambiguous ; i ++ ) {
150
154
struct object_id oid ;
151
155
current = nth_midxed_object_oid (& oid , m , i );
152
- if (!match_hash (ds -> len , ds -> bin_pfx .hash , current -> hash ))
156
+ if (!match_hash (len , ds -> bin_pfx .hash , current -> hash ))
153
157
break ;
154
158
update_candidates (ds , current );
155
159
}
@@ -159,6 +163,8 @@ static void unique_in_pack(struct packed_git *p,
159
163
struct disambiguate_state * ds )
160
164
{
161
165
uint32_t num , i , first = 0 ;
166
+ int len = ds -> len > ds -> repo -> hash_algo -> hexsz ?
167
+ ds -> repo -> hash_algo -> hexsz : ds -> len ;
162
168
163
169
if (p -> multi_pack_index )
164
170
return ;
@@ -177,7 +183,7 @@ static void unique_in_pack(struct packed_git *p,
177
183
for (i = first ; i < num && !ds -> ambiguous ; i ++ ) {
178
184
struct object_id oid ;
179
185
nth_packed_object_id (& oid , p , i );
180
- if (!match_hash (ds -> len , ds -> bin_pfx .hash , oid .hash ))
186
+ if (!match_hash (len , ds -> bin_pfx .hash , oid .hash ))
181
187
break ;
182
188
update_candidates (ds , & oid );
183
189
}
@@ -188,6 +194,10 @@ static void find_short_packed_object(struct disambiguate_state *ds)
188
194
struct multi_pack_index * m ;
189
195
struct packed_git * p ;
190
196
197
+ /* Skip, unless oids from the storage hash algorithm are wanted */
198
+ if (ds -> bin_pfx .algo && (& hash_algos [ds -> bin_pfx .algo ] != ds -> repo -> hash_algo ))
199
+ return ;
200
+
191
201
for (m = get_multi_pack_index (ds -> repo ); m && !ds -> ambiguous ;
192
202
m = m -> next )
193
203
unique_in_midx (m , ds );
@@ -326,11 +336,12 @@ int set_disambiguate_hint_config(const char *var, const char *value)
326
336
327
337
static int init_object_disambiguation (struct repository * r ,
328
338
const char * name , int len ,
339
+ const struct git_hash_algo * algo ,
329
340
struct disambiguate_state * ds )
330
341
{
331
342
int i ;
332
343
333
- if (len < MINIMUM_ABBREV || len > the_hash_algo -> hexsz )
344
+ if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ )
334
345
return -1 ;
335
346
336
347
memset (ds , 0 , sizeof (* ds ));
@@ -357,6 +368,7 @@ static int init_object_disambiguation(struct repository *r,
357
368
ds -> len = len ;
358
369
ds -> hex_pfx [len ] = '\0' ;
359
370
ds -> repo = r ;
371
+ ds -> bin_pfx .algo = algo ? hash_algo_by_ptr (algo ) : GIT_HASH_UNKNOWN ;
360
372
prepare_alt_odb (r );
361
373
return 0 ;
362
374
}
@@ -491,9 +503,10 @@ static int repo_collect_ambiguous(struct repository *r UNUSED,
491
503
return collect_ambiguous (oid , data );
492
504
}
493
505
494
- static int sort_ambiguous (const void * a , const void * b , void * ctx )
506
+ static int sort_ambiguous (const void * va , const void * vb , void * ctx )
495
507
{
496
508
struct repository * sort_ambiguous_repo = ctx ;
509
+ const struct object_id * a = va , * b = vb ;
497
510
int a_type = oid_object_info (sort_ambiguous_repo , a , NULL );
498
511
int b_type = oid_object_info (sort_ambiguous_repo , b , NULL );
499
512
int a_type_sort ;
@@ -503,8 +516,12 @@ static int sort_ambiguous(const void *a, const void *b, void *ctx)
503
516
* Sorts by hash within the same object type, just as
504
517
* oid_array_for_each_unique() would do.
505
518
*/
506
- if (a_type == b_type )
507
- return oidcmp (a , b );
519
+ if (a_type == b_type ) {
520
+ if (a -> algo == b -> algo )
521
+ return oidcmp (a , b );
522
+ else
523
+ return a -> algo > b -> algo ? 1 : -1 ;
524
+ }
508
525
509
526
/*
510
527
* Between object types show tags, then commits, and finally
@@ -533,8 +550,12 @@ static enum get_oid_result get_short_oid(struct repository *r,
533
550
int status ;
534
551
struct disambiguate_state ds ;
535
552
int quietly = !!(flags & GET_OID_QUIETLY );
553
+ const struct git_hash_algo * algo = r -> hash_algo ;
554
+
555
+ if (flags & GET_OID_HASH_ANY )
556
+ algo = NULL ;
536
557
537
- if (init_object_disambiguation (r , name , len , & ds ) < 0 )
558
+ if (init_object_disambiguation (r , name , len , algo , & ds ) < 0 )
538
559
return -1 ;
539
560
540
561
if (HAS_MULTI_BITS (flags & GET_OID_DISAMBIGUATORS ))
@@ -588,7 +609,7 @@ static enum get_oid_result get_short_oid(struct repository *r,
588
609
if (!ds .ambiguous )
589
610
ds .fn = NULL ;
590
611
591
- repo_for_each_abbrev (r , ds .hex_pfx , collect_ambiguous , & collect );
612
+ repo_for_each_abbrev (r , ds .hex_pfx , algo , collect_ambiguous , & collect );
592
613
sort_ambiguous_oid_array (r , & collect );
593
614
594
615
if (oid_array_for_each (& collect , show_ambiguous_object , & out ))
@@ -610,13 +631,14 @@ static enum get_oid_result get_short_oid(struct repository *r,
610
631
}
611
632
612
633
int repo_for_each_abbrev (struct repository * r , const char * prefix ,
634
+ const struct git_hash_algo * algo ,
613
635
each_abbrev_fn fn , void * cb_data )
614
636
{
615
637
struct oid_array collect = OID_ARRAY_INIT ;
616
638
struct disambiguate_state ds ;
617
639
int ret ;
618
640
619
- if (init_object_disambiguation (r , prefix , strlen (prefix ), & ds ) < 0 )
641
+ if (init_object_disambiguation (r , prefix , strlen (prefix ), algo , & ds ) < 0 )
620
642
return -1 ;
621
643
622
644
ds .always_call_fn = 1 ;
@@ -787,10 +809,12 @@ void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
787
809
int repo_find_unique_abbrev_r (struct repository * r , char * hex ,
788
810
const struct object_id * oid , int len )
789
811
{
812
+ const struct git_hash_algo * algo =
813
+ oid -> algo ? & hash_algos [oid -> algo ] : r -> hash_algo ;
790
814
struct disambiguate_state ds ;
791
815
struct min_abbrev_data mad ;
792
816
struct object_id oid_ret ;
793
- const unsigned hexsz = r -> hash_algo -> hexsz ;
817
+ const unsigned hexsz = algo -> hexsz ;
794
818
795
819
if (len < 0 ) {
796
820
unsigned long count = repo_approximate_object_count (r );
@@ -826,7 +850,7 @@ int repo_find_unique_abbrev_r(struct repository *r, char *hex,
826
850
827
851
find_abbrev_len_packed (& mad );
828
852
829
- if (init_object_disambiguation (r , hex , mad .cur_len , & ds ) < 0 )
853
+ if (init_object_disambiguation (r , hex , mad .cur_len , algo , & ds ) < 0 )
830
854
return -1 ;
831
855
832
856
ds .fn = repo_extend_abbrev_len ;
0 commit comments