sha1-name.c: store and use repo in struct disambiguate_state

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 16, 2019 at 16:33 UTC ef9b0370da656eb11db55262cb8fb96a319e662b
1 file changed +59 -36
sha1-name.c
+59 -36
@@ -17,13 +17,14 @@
17
18 static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
19
20 -typedef int (*disambiguate_hint_fn)(const struct object_id *, void *);
20 +typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
21
22 struct disambiguate_state {
23 int len; /* length of prefix in hex chars */
24 char hex_pfx[GIT_MAX_HEXSZ + 1];
25 struct object_id bin_pfx;
26
27 + struct repository *repo;
28 disambiguate_hint_fn fn;
29 void *cb_data;
30 struct object_id candidate;
@@ -38,7 +39,7 @@ struct disambiguate_state {
39 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
40 {
41 if (ds->always_call_fn) {
41 - ds->ambiguous = ds->fn(current, ds->cb_data) ? 1 : 0;
42 + ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
43 return;
44 }
45 if (!ds->candidate_exists) {
@@ -58,7 +59,7 @@ static void update_candidates(struct disambiguate_state *ds, const struct object
59 }
60
61 if (!ds->candidate_checked) {
61 - ds->candidate_ok = ds->fn(&ds->candidate, ds->cb_data);
62 + ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
63 ds->disambiguate_fn_used = 1;
64 ds->candidate_checked = 1;
65 }
@@ -71,7 +72,7 @@ static void update_candidates(struct disambiguate_state *ds, const struct object
72 }
73
74 /* if we reach this point, we know ds->candidate satisfies fn */
74 - if (ds->fn(current, ds->cb_data)) {
75 + if (ds->fn(ds->repo, current, ds->cb_data)) {
76 /*
77 * if both current and candidate satisfy fn, we cannot
78 * disambiguate.
@@ -89,9 +90,7 @@ static void find_short_object_filename(struct disambiguate_state *ds)
90 {
91 struct object_directory *odb;
92
92 - for (odb = the_repository->objects->odb;
93 - odb && !ds->ambiguous;
94 - odb = odb->next) {
93 + for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next) {
94 int pos;
95 struct oid_array *loose_objects;
96
@@ -182,10 +181,10 @@ static void find_short_packed_object(struct disambiguate_state *ds)
181 struct multi_pack_index *m;
182 struct packed_git *p;
183
185 - for (m = get_multi_pack_index(the_repository); m && !ds->ambiguous;
184 + for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
185 m = m->next)
186 unique_in_midx(m, ds);
188 - for (p = get_packed_git(the_repository); p && !ds->ambiguous;
187 + for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
188 p = p->next)
189 unique_in_pack(p, ds);
190 }
@@ -215,7 +214,7 @@ static int finish_object_disambiguation(struct disambiguate_state *ds,
214 * same repository!
215 */
216 ds->candidate_ok = (!ds->disambiguate_fn_used ||
218 - ds->fn(&ds->candidate, ds->cb_data));
217 + ds->fn(ds->repo, &ds->candidate, ds->cb_data));
218
219 if (!ds->candidate_ok)
220 return SHORT_NAME_AMBIGUOUS;
@@ -224,59 +223,67 @@ static int finish_object_disambiguation(struct disambiguate_state *ds,
223 return 0;
224 }
225
227 -static int disambiguate_commit_only(const struct object_id *oid, void *cb_data_unused)
226 +static int disambiguate_commit_only(struct repository *r,
227 + const struct object_id *oid,
228 + void *cb_data_unused)
229 {
229 - int kind = oid_object_info(the_repository, oid, NULL);
230 + int kind = oid_object_info(r, oid, NULL);
231 return kind == OBJ_COMMIT;
232 }
233
233 -static int disambiguate_committish_only(const struct object_id *oid, void *cb_data_unused)
234 +static int disambiguate_committish_only(struct repository *r,
235 + const struct object_id *oid,
236 + void *cb_data_unused)
237 {
238 struct object *obj;
239 int kind;
240
238 - kind = oid_object_info(the_repository, oid, NULL);
241 + kind = oid_object_info(r, oid, NULL);
242 if (kind == OBJ_COMMIT)
243 return 1;
244 if (kind != OBJ_TAG)
245 return 0;
246
247 /* We need to do this the hard way... */
245 - obj = deref_tag(the_repository, parse_object(the_repository, oid),
246 - NULL, 0);
248 + obj = deref_tag(r, parse_object(r, oid), NULL, 0);
249 if (obj && obj->type == OBJ_COMMIT)
250 return 1;
251 return 0;
252 }
253
252 -static int disambiguate_tree_only(const struct object_id *oid, void *cb_data_unused)
254 +static int disambiguate_tree_only(struct repository *r,
255 + const struct object_id *oid,
256 + void *cb_data_unused)
257 {
254 - int kind = oid_object_info(the_repository, oid, NULL);
258 + int kind = oid_object_info(r, oid, NULL);
259 return kind == OBJ_TREE;
260 }
261
258 -static int disambiguate_treeish_only(const struct object_id *oid, void *cb_data_unused)
262 +static int disambiguate_treeish_only(struct repository *r,
263 + const struct object_id *oid,
264 + void *cb_data_unused)
265 {
266 struct object *obj;
267 int kind;
268
263 - kind = oid_object_info(the_repository, oid, NULL);
269 + kind = oid_object_info(r, oid, NULL);
270 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
271 return 1;
272 if (kind != OBJ_TAG)
273 return 0;
274
275 /* We need to do this the hard way... */
270 - obj = deref_tag(the_repository, parse_object(the_repository, oid),
271 - NULL, 0);
276 + obj = deref_tag(r, parse_object(r, oid), NULL, 0);
277 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
278 return 1;
279 return 0;
280 }
281
277 -static int disambiguate_blob_only(const struct object_id *oid, void *cb_data_unused)
282 +static int disambiguate_blob_only(struct repository *r,
283 + const struct object_id *oid,
284 + void *cb_data_unused)
285 {
279 - int kind = oid_object_info(the_repository, oid, NULL);
286 + int kind = oid_object_info(r, oid, NULL);
287 return kind == OBJ_BLOB;
288 }
289
@@ -310,7 +317,8 @@ int set_disambiguate_hint_config(const char *var, const char *value)
317 return error("unknown hint type for '%s': %s", var, value);
318 }
319
313 -static int init_object_disambiguation(const char *name, int len,
320 +static int init_object_disambiguation(struct repository *r,
321 + const char *name, int len,
322 struct disambiguate_state *ds)
323 {
324 int i;
@@ -341,7 +349,8 @@ static int init_object_disambiguation(const char *name, int len,
349
350 ds->len = len;
351 ds->hex_pfx[len] = '\0';
344 - prepare_alt_odb(the_repository);
352 + ds->repo = r;
353 + prepare_alt_odb(r);
354 return 0;
355 }
356
@@ -351,25 +360,25 @@ static int show_ambiguous_object(const struct object_id *oid, void *data)
360 struct strbuf desc = STRBUF_INIT;
361 int type;
362
354 - if (ds->fn && !ds->fn(oid, ds->cb_data))
363 + if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
364 return 0;
365
357 - type = oid_object_info(the_repository, oid, NULL);
366 + type = oid_object_info(ds->repo, oid, NULL);
367 if (type == OBJ_COMMIT) {
359 - struct commit *commit = lookup_commit(the_repository, oid);
368 + struct commit *commit = lookup_commit(ds->repo, oid);
369 if (commit) {
370 struct pretty_print_context pp = {0};
371 pp.date_mode.type = DATE_SHORT;
372 format_commit_message(commit, " %ad - %s", &desc, &pp);
373 }
374 } else if (type == OBJ_TAG) {
366 - struct tag *tag = lookup_tag(the_repository, oid);
375 + struct tag *tag = lookup_tag(ds->repo, oid);
376 if (!parse_tag(tag) && tag->tag)
377 strbuf_addf(&desc, " %s", tag->tag);
378 }
379
380 advise(" %s %s%s",
372 - find_unique_abbrev(oid, DEFAULT_ABBREV),
381 + repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV),
382 type_name(type) ? type_name(type) : "unknown type",
383 desc.buf);
384
@@ -383,6 +392,13 @@ static int collect_ambiguous(const struct object_id *oid, void *data)
392 return 0;
393 }
394
395 +static int repo_collect_ambiguous(struct repository *r,
396 + const struct object_id *oid,
397 + void *data)
398 +{
399 + return collect_ambiguous(oid, data);
400 +}
401 +
402 static struct repository *sort_ambiguous_repo;
403 static int sort_ambiguous(const void *a, const void *b)
404 {
@@ -428,7 +444,7 @@ static enum get_oid_result get_short_oid(const char *name, int len,
444 struct disambiguate_state ds;
445 int quietly = !!(flags & GET_OID_QUIETLY);
446
431 - if (init_object_disambiguation(name, len, &ds) < 0)
447 + if (init_object_disambiguation(the_repository, name, len, &ds) < 0)
448 return -1;
449
450 if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
@@ -483,11 +499,11 @@ int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
499 struct disambiguate_state ds;
500 int ret;
501
486 - if (init_object_disambiguation(prefix, strlen(prefix), &ds) < 0)
502 + if (init_object_disambiguation(the_repository, prefix, strlen(prefix), &ds) < 0)
503 return -1;
504
505 ds.always_call_fn = 1;
490 - ds.fn = collect_ambiguous;
506 + ds.fn = repo_collect_ambiguous;
507 ds.cb_data = &collect;
508 find_short_object_filename(&ds);
509 find_short_packed_object(&ds);
@@ -543,6 +559,13 @@ static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
559 return 0;
560 }
561
562 +static int repo_extend_abbrev_len(struct repository *r,
563 + const struct object_id *oid,
564 + void *cb_data)
565 +{
566 + return extend_abbrev_len(oid, cb_data);
567 +}
568 +
569 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
570 struct min_abbrev_data *mad)
571 {
@@ -668,10 +691,10 @@ int repo_find_unique_abbrev_r(struct repository *r, char *hex,
691
692 find_abbrev_len_packed(&mad);
693
671 - if (init_object_disambiguation(hex, mad.cur_len, &ds) < 0)
694 + if (init_object_disambiguation(r, hex, mad.cur_len, &ds) < 0)
695 return -1;
696
674 - ds.fn = extend_abbrev_len;
697 + ds.fn = repo_extend_abbrev_len;
698 ds.always_call_fn = 1;
699 ds.cb_data = (void *)&mad;
700