@@ -13,9 +13,13 @@ static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *)
13
13
typedef int (* disambiguate_hint_fn )(const unsigned char * , void * );
14
14
15
15
struct disambiguate_state {
16
+ int len ; /* length of prefix in hex chars */
17
+ char hex_pfx [GIT_SHA1_HEXSZ ];
18
+ unsigned char bin_pfx [GIT_SHA1_RAWSZ ];
19
+
16
20
disambiguate_hint_fn fn ;
17
21
void * cb_data ;
18
- unsigned char candidate [20 ];
22
+ unsigned char candidate [GIT_SHA1_RAWSZ ];
19
23
unsigned candidate_exists :1 ;
20
24
unsigned candidate_checked :1 ;
21
25
unsigned candidate_ok :1 ;
@@ -72,10 +76,10 @@ static void update_candidates(struct disambiguate_state *ds, const unsigned char
72
76
/* otherwise, current can be discarded and candidate is still good */
73
77
}
74
78
75
- static void find_short_object_filename (int len , const char * hex_pfx , struct disambiguate_state * ds )
79
+ static void find_short_object_filename (struct disambiguate_state * ds )
76
80
{
77
81
struct alternate_object_database * alt ;
78
- char hex [40 ];
82
+ char hex [GIT_SHA1_HEXSZ ];
79
83
static struct alternate_object_database * fakeent ;
80
84
81
85
if (!fakeent ) {
@@ -95,15 +99,15 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa
95
99
}
96
100
fakeent -> next = alt_odb_list ;
97
101
98
- xsnprintf (hex , sizeof (hex ), "%.2s" , hex_pfx );
102
+ xsnprintf (hex , sizeof (hex ), "%.2s" , ds -> hex_pfx );
99
103
for (alt = fakeent ; alt && !ds -> ambiguous ; alt = alt -> next ) {
100
104
struct dirent * de ;
101
105
DIR * dir ;
102
106
/*
103
107
* every alt_odb struct has 42 extra bytes after the base
104
108
* for exactly this purpose
105
109
*/
106
- xsnprintf (alt -> name , 42 , "%.2s/" , hex_pfx );
110
+ xsnprintf (alt -> name , 42 , "%.2s/" , ds -> hex_pfx );
107
111
dir = opendir (alt -> base );
108
112
if (!dir )
109
113
continue ;
@@ -113,7 +117,7 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa
113
117
114
118
if (strlen (de -> d_name ) != 38 )
115
119
continue ;
116
- if (memcmp (de -> d_name , hex_pfx + 2 , len - 2 ))
120
+ if (memcmp (de -> d_name , ds -> hex_pfx + 2 , ds -> len - 2 ))
117
121
continue ;
118
122
memcpy (hex + 2 , de -> d_name , 38 );
119
123
if (!get_sha1_hex (hex , sha1 ))
@@ -138,9 +142,7 @@ static int match_sha(unsigned len, const unsigned char *a, const unsigned char *
138
142
return 1 ;
139
143
}
140
144
141
- static void unique_in_pack (int len ,
142
- const unsigned char * bin_pfx ,
143
- struct packed_git * p ,
145
+ static void unique_in_pack (struct packed_git * p ,
144
146
struct disambiguate_state * ds )
145
147
{
146
148
uint32_t num , last , i , first = 0 ;
@@ -155,7 +157,7 @@ static void unique_in_pack(int len,
155
157
int cmp ;
156
158
157
159
current = nth_packed_object_sha1 (p , mid );
158
- cmp = hashcmp (bin_pfx , current );
160
+ cmp = hashcmp (ds -> bin_pfx , current );
159
161
if (!cmp ) {
160
162
first = mid ;
161
163
break ;
@@ -174,20 +176,19 @@ static void unique_in_pack(int len,
174
176
*/
175
177
for (i = first ; i < num && !ds -> ambiguous ; i ++ ) {
176
178
current = nth_packed_object_sha1 (p , i );
177
- if (!match_sha (len , bin_pfx , current ))
179
+ if (!match_sha (ds -> len , ds -> bin_pfx , current ))
178
180
break ;
179
181
update_candidates (ds , current );
180
182
}
181
183
}
182
184
183
- static void find_short_packed_object (int len , const unsigned char * bin_pfx ,
184
- struct disambiguate_state * ds )
185
+ static void find_short_packed_object (struct disambiguate_state * ds )
185
186
{
186
187
struct packed_git * p ;
187
188
188
189
prepare_packed_git ();
189
190
for (p = packed_git ; p && !ds -> ambiguous ; p = p -> next )
190
- unique_in_pack (len , bin_pfx , p , ds );
191
+ unique_in_pack (p , ds );
191
192
}
192
193
193
194
#define SHORT_NAME_NOT_FOUND (-1)
@@ -281,14 +282,17 @@ static int disambiguate_blob_only(const unsigned char *sha1, void *cb_data_unuse
281
282
return kind == OBJ_BLOB ;
282
283
}
283
284
284
- static int prepare_prefixes (const char * name , int len ,
285
- unsigned char * bin_pfx ,
286
- char * hex_pfx )
285
+ static int init_object_disambiguation (const char * name , int len ,
286
+ struct disambiguate_state * ds )
287
287
{
288
288
int i ;
289
289
290
- hashclr (bin_pfx );
291
- memset (hex_pfx , 'x' , 40 );
290
+ if (len < MINIMUM_ABBREV || len > GIT_SHA1_HEXSZ )
291
+ return -1 ;
292
+
293
+ memset (ds , 0 , sizeof (* ds ));
294
+ memset (ds -> hex_pfx , 'x' , GIT_SHA1_HEXSZ );
295
+
292
296
for (i = 0 ; i < len ;i ++ ) {
293
297
unsigned char c = name [i ];
294
298
unsigned char val ;
@@ -302,32 +306,27 @@ static int prepare_prefixes(const char *name, int len,
302
306
}
303
307
else
304
308
return -1 ;
305
- hex_pfx [i ] = c ;
309
+ ds -> hex_pfx [i ] = c ;
306
310
if (!(i & 1 ))
307
311
val <<= 4 ;
308
- bin_pfx [i >> 1 ] |= val ;
312
+ ds -> bin_pfx [i >> 1 ] |= val ;
309
313
}
314
+
315
+ ds -> len = len ;
316
+ prepare_alt_odb ();
310
317
return 0 ;
311
318
}
312
319
313
320
static int get_short_sha1 (const char * name , int len , unsigned char * sha1 ,
314
321
unsigned flags )
315
322
{
316
323
int status ;
317
- char hex_pfx [40 ];
318
- unsigned char bin_pfx [20 ];
319
324
struct disambiguate_state ds ;
320
325
int quietly = !!(flags & GET_SHA1_QUIETLY );
321
326
322
- if (len < MINIMUM_ABBREV || len > 40 )
323
- return -1 ;
324
- if (prepare_prefixes (name , len , bin_pfx , hex_pfx ) < 0 )
327
+ if (init_object_disambiguation (name , len , & ds ) < 0 )
325
328
return -1 ;
326
329
327
- prepare_alt_odb ();
328
-
329
- memset (& ds , 0 , sizeof (ds ));
330
-
331
330
if (HAS_MULTI_BITS (flags & GET_SHA1_DISAMBIGUATORS ))
332
331
die ("BUG: multiple get_short_sha1 disambiguator flags" );
333
332
@@ -342,36 +341,28 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
342
341
else if (flags & GET_SHA1_BLOB )
343
342
ds .fn = disambiguate_blob_only ;
344
343
345
- find_short_object_filename (len , hex_pfx , & ds );
346
- find_short_packed_object (len , bin_pfx , & ds );
344
+ find_short_object_filename (& ds );
345
+ find_short_packed_object (& ds );
347
346
status = finish_object_disambiguation (& ds , sha1 );
348
347
349
348
if (!quietly && (status == SHORT_NAME_AMBIGUOUS ))
350
- return error ("short SHA1 %.*s is ambiguous." , len , hex_pfx );
349
+ return error ("short SHA1 %.*s is ambiguous." , ds . len , ds . hex_pfx );
351
350
return status ;
352
351
}
353
352
354
353
int for_each_abbrev (const char * prefix , each_abbrev_fn fn , void * cb_data )
355
354
{
356
- char hex_pfx [40 ];
357
- unsigned char bin_pfx [20 ];
358
355
struct disambiguate_state ds ;
359
- int len = strlen (prefix );
360
356
361
- if (len < MINIMUM_ABBREV || len > 40 )
357
+ if (init_object_disambiguation ( prefix , strlen ( prefix ), & ds ) < 0 )
362
358
return -1 ;
363
- if (prepare_prefixes (prefix , len , bin_pfx , hex_pfx ) < 0 )
364
- return -1 ;
365
-
366
- prepare_alt_odb ();
367
359
368
- memset (& ds , 0 , sizeof (ds ));
369
360
ds .always_call_fn = 1 ;
370
361
ds .cb_data = cb_data ;
371
362
ds .fn = fn ;
372
363
373
- find_short_object_filename (len , hex_pfx , & ds );
374
- find_short_packed_object (len , bin_pfx , & ds );
364
+ find_short_object_filename (& ds );
365
+ find_short_packed_object (& ds );
375
366
return ds .ambiguous ;
376
367
}
377
368
0 commit comments