sha1_name: convert disambiguate_hint_fn to take object_id
Convert this function pointer type and the functions that implement it to take a struct object_id. Introduce a temporary in show_ambiguous_object to avoid having to convert for_each_abbrev at this point. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committed
Mar 26, 2017 at 16:01 UTC
d2b7d9c7edc549e7ec19f4a7681adc7bfbaa6b89
1 file changed
+34
-30
sha1_name.c
+34
-30
@@ -11,7 +11,7 @@
11
12
static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
13
14
-typedef int (*disambiguate_hint_fn)(const unsigned char *, void *);
14
+typedef int (*disambiguate_hint_fn)(const struct object_id *, void *);
15
16
struct disambiguate_state {
17
int len; /* length of prefix in hex chars */
@@ -29,7 +29,7 @@ struct disambiguate_state {
29
unsigned always_call_fn:1;
30
};
31
32
-static void update_candidates(struct disambiguate_state *ds, const unsigned char *current)
32
+static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
33
{
34
if (ds->always_call_fn) {
35
ds->ambiguous = ds->fn(current, ds->cb_data) ? 1 : 0;
@@ -37,10 +37,10 @@ static void update_candidates(struct disambiguate_state *ds, const unsigned char
37
}
38
if (!ds->candidate_exists) {
39
/* this is the first candidate */
40
- hashcpy(ds->candidate.hash, current);
40
+ oidcpy(&ds->candidate, current);
41
ds->candidate_exists = 1;
42
return;
43
- } else if (!hashcmp(ds->candidate.hash, current)) {
43
+ } else if (!oidcmp(&ds->candidate, current)) {
44
/* the same as what we already have seen */
45
return;
46
}
@@ -52,14 +52,14 @@ static void update_candidates(struct disambiguate_state *ds, const unsigned char
52
}
53
54
if (!ds->candidate_checked) {
55
- ds->candidate_ok = ds->fn(ds->candidate.hash, ds->cb_data);
55
+ ds->candidate_ok = ds->fn(&ds->candidate, ds->cb_data);
56
ds->disambiguate_fn_used = 1;
57
ds->candidate_checked = 1;
58
}
59
60
if (!ds->candidate_ok) {
61
/* discard the candidate; we know it does not satisfy fn */
62
- hashcpy(ds->candidate.hash, current);
62
+ oidcpy(&ds->candidate, current);
63
ds->candidate_checked = 0;
64
return;
65
}
@@ -107,15 +107,15 @@ static void find_short_object_filename(struct disambiguate_state *ds)
107
continue;
108
109
while (!ds->ambiguous && (de = readdir(dir)) != NULL) {
110
- unsigned char sha1[20];
110
+ struct object_id oid;
111
112
- if (strlen(de->d_name) != 38)
112
+ if (strlen(de->d_name) != GIT_SHA1_HEXSZ - 2)
113
continue;
114
if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2))
115
continue;
116
- memcpy(hex + 2, de->d_name, 38);
117
- if (!get_sha1_hex(hex, sha1))
118
- update_candidates(ds, sha1);
116
+ memcpy(hex + 2, de->d_name, GIT_SHA1_HEXSZ - 2);
117
+ if (!get_oid_hex(hex, &oid))
118
+ update_candidates(ds, &oid);
119
}
120
closedir(dir);
121
}
@@ -140,7 +140,7 @@ static void unique_in_pack(struct packed_git *p,
140
struct disambiguate_state *ds)
141
{
142
uint32_t num, last, i, first = 0;
143
- const unsigned char *current = NULL;
143
+ const struct object_id *current = NULL;
144
145
open_pack_index(p);
146
num = p->num_objects;
@@ -169,8 +169,9 @@ static void unique_in_pack(struct packed_git *p,
169
* 0, 1 or more objects that actually match(es).
170
*/
171
for (i = first; i < num && !ds->ambiguous; i++) {
172
- current = nth_packed_object_sha1(p, i);
173
- if (!match_sha(ds->len, ds->bin_pfx.hash, current))
172
+ struct object_id oid;
173
+ current = nth_packed_object_oid(&oid, p, i);
174
+ if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash))
175
break;
176
update_candidates(ds, current);
177
}
@@ -213,7 +214,7 @@ static int finish_object_disambiguation(struct disambiguate_state *ds,
214
* same repository!
215
*/
216
ds->candidate_ok = (!ds->disambiguate_fn_used ||
216
- ds->fn(ds->candidate.hash, ds->cb_data));
217
+ ds->fn(&ds->candidate, ds->cb_data));
218
219
if (!ds->candidate_ok)
220
return SHORT_NAME_AMBIGUOUS;
@@ -222,57 +223,57 @@ static int finish_object_disambiguation(struct disambiguate_state *ds,
223
return 0;
224
}
225
225
-static int disambiguate_commit_only(const unsigned char *sha1, void *cb_data_unused)
226
+static int disambiguate_commit_only(const struct object_id *oid, void *cb_data_unused)
227
{
227
- int kind = sha1_object_info(sha1, NULL);
228
+ int kind = sha1_object_info(oid->hash, NULL);
229
return kind == OBJ_COMMIT;
230
}
231
231
-static int disambiguate_committish_only(const unsigned char *sha1, void *cb_data_unused)
232
+static int disambiguate_committish_only(const struct object_id *oid, void *cb_data_unused)
233
{
234
struct object *obj;
235
int kind;
236
236
- kind = sha1_object_info(sha1, NULL);
237
+ kind = sha1_object_info(oid->hash, NULL);
238
if (kind == OBJ_COMMIT)
239
return 1;
240
if (kind != OBJ_TAG)
241
return 0;
242
243
/* We need to do this the hard way... */
243
- obj = deref_tag(parse_object(sha1), NULL, 0);
244
+ obj = deref_tag(parse_object(oid->hash), NULL, 0);
245
if (obj && obj->type == OBJ_COMMIT)
246
return 1;
247
return 0;
248
}
249
249
-static int disambiguate_tree_only(const unsigned char *sha1, void *cb_data_unused)
250
+static int disambiguate_tree_only(const struct object_id *oid, void *cb_data_unused)
251
{
251
- int kind = sha1_object_info(sha1, NULL);
252
+ int kind = sha1_object_info(oid->hash, NULL);
253
return kind == OBJ_TREE;
254
}
255
255
-static int disambiguate_treeish_only(const unsigned char *sha1, void *cb_data_unused)
256
+static int disambiguate_treeish_only(const struct object_id *oid, void *cb_data_unused)
257
{
258
struct object *obj;
259
int kind;
260
260
- kind = sha1_object_info(sha1, NULL);
261
+ kind = sha1_object_info(oid->hash, NULL);
262
if (kind == OBJ_TREE || kind == OBJ_COMMIT)
263
return 1;
264
if (kind != OBJ_TAG)
265
return 0;
266
267
/* We need to do this the hard way... */
267
- obj = deref_tag(parse_object(sha1), NULL, 0);
268
+ obj = deref_tag(parse_object(oid->hash), NULL, 0);
269
if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
270
return 1;
271
return 0;
272
}
273
273
-static int disambiguate_blob_only(const unsigned char *sha1, void *cb_data_unused)
274
+static int disambiguate_blob_only(const struct object_id *oid, void *cb_data_unused)
275
{
275
- int kind = sha1_object_info(sha1, NULL);
276
+ int kind = sha1_object_info(oid->hash, NULL);
277
return kind == OBJ_BLOB;
278
}
279
@@ -344,10 +345,13 @@ static int init_object_disambiguation(const char *name, int len,
345
static int show_ambiguous_object(const unsigned char *sha1, void *data)
346
{
347
const struct disambiguate_state *ds = data;
348
+ struct object_id oid;
349
struct strbuf desc = STRBUF_INIT;
350
int type;
351
350
- if (ds->fn && !ds->fn(sha1, ds->cb_data))
352
+
353
+ hashcpy(oid.hash, sha1);
354
+ if (ds->fn && !ds->fn(&oid, ds->cb_data))
355
return 0;
356
357
type = sha1_object_info(sha1, NULL);
@@ -422,9 +426,9 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
426
return status;
427
}
428
425
-static int collect_ambiguous(const unsigned char *sha1, void *data)
429
+static int collect_ambiguous(const struct object_id *oid, void *data)
430
{
427
- sha1_array_append(data, sha1);
431
+ sha1_array_append(data, oid->hash);
432
return 0;
433
}
434