Line | Count | Source (jump to first uncovered line) |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "object-name.h" |
5 | | #include "advice.h" |
6 | | #include "config.h" |
7 | | #include "environment.h" |
8 | | #include "gettext.h" |
9 | | #include "hex.h" |
10 | | #include "tag.h" |
11 | | #include "commit.h" |
12 | | #include "tree.h" |
13 | | #include "tree-walk.h" |
14 | | #include "refs.h" |
15 | | #include "remote.h" |
16 | | #include "dir.h" |
17 | | #include "oid-array.h" |
18 | | #include "oidtree.h" |
19 | | #include "packfile.h" |
20 | | #include "pretty.h" |
21 | | #include "object-store-ll.h" |
22 | | #include "read-cache-ll.h" |
23 | | #include "repository.h" |
24 | | #include "setup.h" |
25 | | #include "midx.h" |
26 | | #include "commit-reach.h" |
27 | | #include "date.h" |
28 | | #include "object-file-convert.h" |
29 | | |
30 | | static int get_oid_oneline(struct repository *r, const char *, struct object_id *, |
31 | | const struct commit_list *); |
32 | | |
33 | | typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *); |
34 | | |
35 | | struct disambiguate_state { |
36 | | int len; /* length of prefix in hex chars */ |
37 | | char hex_pfx[GIT_MAX_HEXSZ + 1]; |
38 | | struct object_id bin_pfx; |
39 | | |
40 | | struct repository *repo; |
41 | | disambiguate_hint_fn fn; |
42 | | void *cb_data; |
43 | | struct object_id candidate; |
44 | | unsigned candidate_exists:1; |
45 | | unsigned candidate_checked:1; |
46 | | unsigned candidate_ok:1; |
47 | | unsigned disambiguate_fn_used:1; |
48 | | unsigned ambiguous:1; |
49 | | unsigned always_call_fn:1; |
50 | | }; |
51 | | |
52 | | static void update_candidates(struct disambiguate_state *ds, const struct object_id *current) |
53 | 0 | { |
54 | | /* The hash algorithm of current has already been filtered */ |
55 | 0 | if (ds->always_call_fn) { |
56 | 0 | ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0; |
57 | 0 | return; |
58 | 0 | } |
59 | 0 | if (!ds->candidate_exists) { |
60 | | /* this is the first candidate */ |
61 | 0 | oidcpy(&ds->candidate, current); |
62 | 0 | ds->candidate_exists = 1; |
63 | 0 | return; |
64 | 0 | } else if (oideq(&ds->candidate, current)) { |
65 | | /* the same as what we already have seen */ |
66 | 0 | return; |
67 | 0 | } |
68 | | |
69 | 0 | if (!ds->fn) { |
70 | | /* cannot disambiguate between ds->candidate and current */ |
71 | 0 | ds->ambiguous = 1; |
72 | 0 | return; |
73 | 0 | } |
74 | | |
75 | 0 | if (!ds->candidate_checked) { |
76 | 0 | ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data); |
77 | 0 | ds->disambiguate_fn_used = 1; |
78 | 0 | ds->candidate_checked = 1; |
79 | 0 | } |
80 | |
|
81 | 0 | if (!ds->candidate_ok) { |
82 | | /* discard the candidate; we know it does not satisfy fn */ |
83 | 0 | oidcpy(&ds->candidate, current); |
84 | 0 | ds->candidate_checked = 0; |
85 | 0 | return; |
86 | 0 | } |
87 | | |
88 | | /* if we reach this point, we know ds->candidate satisfies fn */ |
89 | 0 | if (ds->fn(ds->repo, current, ds->cb_data)) { |
90 | | /* |
91 | | * if both current and candidate satisfy fn, we cannot |
92 | | * disambiguate. |
93 | | */ |
94 | 0 | ds->candidate_ok = 0; |
95 | 0 | ds->ambiguous = 1; |
96 | 0 | } |
97 | | |
98 | | /* otherwise, current can be discarded and candidate is still good */ |
99 | 0 | } |
100 | | |
101 | | static int match_hash(unsigned, const unsigned char *, const unsigned char *); |
102 | | |
103 | | static enum cb_next match_prefix(const struct object_id *oid, void *arg) |
104 | 0 | { |
105 | 0 | struct disambiguate_state *ds = arg; |
106 | | /* no need to call match_hash, oidtree_each did prefix match */ |
107 | 0 | update_candidates(ds, oid); |
108 | 0 | return ds->ambiguous ? CB_BREAK : CB_CONTINUE; |
109 | 0 | } |
110 | | |
111 | | static void find_short_object_filename(struct disambiguate_state *ds) |
112 | 0 | { |
113 | 0 | struct object_directory *odb; |
114 | |
|
115 | 0 | for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next) |
116 | 0 | oidtree_each(odb_loose_cache(odb, &ds->bin_pfx), |
117 | 0 | &ds->bin_pfx, ds->len, match_prefix, ds); |
118 | 0 | } |
119 | | |
120 | | static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b) |
121 | 0 | { |
122 | 0 | do { |
123 | 0 | if (*a != *b) |
124 | 0 | return 0; |
125 | 0 | a++; |
126 | 0 | b++; |
127 | 0 | len -= 2; |
128 | 0 | } while (len > 1); |
129 | 0 | if (len) |
130 | 0 | if ((*a ^ *b) & 0xf0) |
131 | 0 | return 0; |
132 | 0 | return 1; |
133 | 0 | } |
134 | | |
135 | | static void unique_in_midx(struct multi_pack_index *m, |
136 | | struct disambiguate_state *ds) |
137 | 0 | { |
138 | 0 | for (; m; m = m->base_midx) { |
139 | 0 | uint32_t num, i, first = 0; |
140 | 0 | const struct object_id *current = NULL; |
141 | 0 | int len = ds->len > ds->repo->hash_algo->hexsz ? |
142 | 0 | ds->repo->hash_algo->hexsz : ds->len; |
143 | |
|
144 | 0 | if (!m->num_objects) |
145 | 0 | continue; |
146 | | |
147 | 0 | num = m->num_objects + m->num_objects_in_base; |
148 | |
|
149 | 0 | bsearch_one_midx(&ds->bin_pfx, m, &first); |
150 | | |
151 | | /* |
152 | | * At this point, "first" is the location of the lowest |
153 | | * object with an object name that could match |
154 | | * "bin_pfx". See if we have 0, 1 or more objects that |
155 | | * actually match(es). |
156 | | */ |
157 | 0 | for (i = first; i < num && !ds->ambiguous; i++) { |
158 | 0 | struct object_id oid; |
159 | 0 | current = nth_midxed_object_oid(&oid, m, i); |
160 | 0 | if (!match_hash(len, ds->bin_pfx.hash, current->hash)) |
161 | 0 | break; |
162 | 0 | update_candidates(ds, current); |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |
166 | | |
167 | | static void unique_in_pack(struct packed_git *p, |
168 | | struct disambiguate_state *ds) |
169 | 0 | { |
170 | 0 | uint32_t num, i, first = 0; |
171 | 0 | int len = ds->len > ds->repo->hash_algo->hexsz ? |
172 | 0 | ds->repo->hash_algo->hexsz : ds->len; |
173 | |
|
174 | 0 | if (p->multi_pack_index) |
175 | 0 | return; |
176 | | |
177 | 0 | if (open_pack_index(p) || !p->num_objects) |
178 | 0 | return; |
179 | | |
180 | 0 | num = p->num_objects; |
181 | 0 | bsearch_pack(&ds->bin_pfx, p, &first); |
182 | | |
183 | | /* |
184 | | * At this point, "first" is the location of the lowest object |
185 | | * with an object name that could match "bin_pfx". See if we have |
186 | | * 0, 1 or more objects that actually match(es). |
187 | | */ |
188 | 0 | for (i = first; i < num && !ds->ambiguous; i++) { |
189 | 0 | struct object_id oid; |
190 | 0 | nth_packed_object_id(&oid, p, i); |
191 | 0 | if (!match_hash(len, ds->bin_pfx.hash, oid.hash)) |
192 | 0 | break; |
193 | 0 | update_candidates(ds, &oid); |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | | static void find_short_packed_object(struct disambiguate_state *ds) |
198 | 0 | { |
199 | 0 | struct multi_pack_index *m; |
200 | 0 | struct packed_git *p; |
201 | | |
202 | | /* Skip, unless oids from the storage hash algorithm are wanted */ |
203 | 0 | if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo)) |
204 | 0 | return; |
205 | | |
206 | 0 | for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous; |
207 | 0 | m = m->next) |
208 | 0 | unique_in_midx(m, ds); |
209 | 0 | for (p = get_packed_git(ds->repo); p && !ds->ambiguous; |
210 | 0 | p = p->next) |
211 | 0 | unique_in_pack(p, ds); |
212 | 0 | } |
213 | | |
214 | | static int finish_object_disambiguation(struct disambiguate_state *ds, |
215 | | struct object_id *oid) |
216 | 0 | { |
217 | 0 | if (ds->ambiguous) |
218 | 0 | return SHORT_NAME_AMBIGUOUS; |
219 | | |
220 | 0 | if (!ds->candidate_exists) |
221 | 0 | return MISSING_OBJECT; |
222 | | |
223 | 0 | if (!ds->candidate_checked) |
224 | | /* |
225 | | * If this is the only candidate, there is no point |
226 | | * calling the disambiguation hint callback. |
227 | | * |
228 | | * On the other hand, if the current candidate |
229 | | * replaced an earlier candidate that did _not_ pass |
230 | | * the disambiguation hint callback, then we do have |
231 | | * more than one objects that match the short name |
232 | | * given, so we should make sure this one matches; |
233 | | * otherwise, if we discovered this one and the one |
234 | | * that we previously discarded in the reverse order, |
235 | | * we would end up showing different results in the |
236 | | * same repository! |
237 | | */ |
238 | 0 | ds->candidate_ok = (!ds->disambiguate_fn_used || |
239 | 0 | ds->fn(ds->repo, &ds->candidate, ds->cb_data)); |
240 | |
|
241 | 0 | if (!ds->candidate_ok) |
242 | 0 | return SHORT_NAME_AMBIGUOUS; |
243 | | |
244 | 0 | oidcpy(oid, &ds->candidate); |
245 | 0 | return 0; |
246 | 0 | } |
247 | | |
248 | | static int disambiguate_commit_only(struct repository *r, |
249 | | const struct object_id *oid, |
250 | | void *cb_data UNUSED) |
251 | 0 | { |
252 | 0 | int kind = oid_object_info(r, oid, NULL); |
253 | 0 | return kind == OBJ_COMMIT; |
254 | 0 | } |
255 | | |
256 | | static int disambiguate_committish_only(struct repository *r, |
257 | | const struct object_id *oid, |
258 | | void *cb_data UNUSED) |
259 | 0 | { |
260 | 0 | struct object *obj; |
261 | 0 | int kind; |
262 | |
|
263 | 0 | kind = oid_object_info(r, oid, NULL); |
264 | 0 | if (kind == OBJ_COMMIT) |
265 | 0 | return 1; |
266 | 0 | if (kind != OBJ_TAG) |
267 | 0 | return 0; |
268 | | |
269 | | /* We need to do this the hard way... */ |
270 | 0 | obj = deref_tag(r, parse_object(r, oid), NULL, 0); |
271 | 0 | if (obj && obj->type == OBJ_COMMIT) |
272 | 0 | return 1; |
273 | 0 | return 0; |
274 | 0 | } |
275 | | |
276 | | static int disambiguate_tree_only(struct repository *r, |
277 | | const struct object_id *oid, |
278 | | void *cb_data UNUSED) |
279 | 0 | { |
280 | 0 | int kind = oid_object_info(r, oid, NULL); |
281 | 0 | return kind == OBJ_TREE; |
282 | 0 | } |
283 | | |
284 | | static int disambiguate_treeish_only(struct repository *r, |
285 | | const struct object_id *oid, |
286 | | void *cb_data UNUSED) |
287 | 0 | { |
288 | 0 | struct object *obj; |
289 | 0 | int kind; |
290 | |
|
291 | 0 | kind = oid_object_info(r, oid, NULL); |
292 | 0 | if (kind == OBJ_TREE || kind == OBJ_COMMIT) |
293 | 0 | return 1; |
294 | 0 | if (kind != OBJ_TAG) |
295 | 0 | return 0; |
296 | | |
297 | | /* We need to do this the hard way... */ |
298 | 0 | obj = deref_tag(r, parse_object(r, oid), NULL, 0); |
299 | 0 | if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT)) |
300 | 0 | return 1; |
301 | 0 | return 0; |
302 | 0 | } |
303 | | |
304 | | static int disambiguate_blob_only(struct repository *r, |
305 | | const struct object_id *oid, |
306 | | void *cb_data UNUSED) |
307 | 0 | { |
308 | 0 | int kind = oid_object_info(r, oid, NULL); |
309 | 0 | return kind == OBJ_BLOB; |
310 | 0 | } |
311 | | |
312 | | static disambiguate_hint_fn default_disambiguate_hint; |
313 | | |
314 | | int set_disambiguate_hint_config(const char *var, const char *value) |
315 | 0 | { |
316 | 0 | static const struct { |
317 | 0 | const char *name; |
318 | 0 | disambiguate_hint_fn fn; |
319 | 0 | } hints[] = { |
320 | 0 | { "none", NULL }, |
321 | 0 | { "commit", disambiguate_commit_only }, |
322 | 0 | { "committish", disambiguate_committish_only }, |
323 | 0 | { "tree", disambiguate_tree_only }, |
324 | 0 | { "treeish", disambiguate_treeish_only }, |
325 | 0 | { "blob", disambiguate_blob_only } |
326 | 0 | }; |
327 | 0 | int i; |
328 | |
|
329 | 0 | if (!value) |
330 | 0 | return config_error_nonbool(var); |
331 | | |
332 | 0 | for (i = 0; i < ARRAY_SIZE(hints); i++) { |
333 | 0 | if (!strcasecmp(value, hints[i].name)) { |
334 | 0 | default_disambiguate_hint = hints[i].fn; |
335 | 0 | return 0; |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | 0 | return error("unknown hint type for '%s': %s", var, value); |
340 | 0 | } |
341 | | |
342 | | static int init_object_disambiguation(struct repository *r, |
343 | | const char *name, int len, |
344 | | const struct git_hash_algo *algo, |
345 | | struct disambiguate_state *ds) |
346 | 0 | { |
347 | 0 | int i; |
348 | |
|
349 | 0 | if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ) |
350 | 0 | return -1; |
351 | | |
352 | 0 | memset(ds, 0, sizeof(*ds)); |
353 | |
|
354 | 0 | for (i = 0; i < len ;i++) { |
355 | 0 | unsigned char c = name[i]; |
356 | 0 | unsigned char val; |
357 | 0 | if (c >= '0' && c <= '9') |
358 | 0 | val = c - '0'; |
359 | 0 | else if (c >= 'a' && c <= 'f') |
360 | 0 | val = c - 'a' + 10; |
361 | 0 | else if (c >= 'A' && c <='F') { |
362 | 0 | val = c - 'A' + 10; |
363 | 0 | c -= 'A' - 'a'; |
364 | 0 | } |
365 | 0 | else |
366 | 0 | return -1; |
367 | 0 | ds->hex_pfx[i] = c; |
368 | 0 | if (!(i & 1)) |
369 | 0 | val <<= 4; |
370 | 0 | ds->bin_pfx.hash[i >> 1] |= val; |
371 | 0 | } |
372 | | |
373 | 0 | ds->len = len; |
374 | 0 | ds->hex_pfx[len] = '\0'; |
375 | 0 | ds->repo = r; |
376 | 0 | ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN; |
377 | 0 | prepare_alt_odb(r); |
378 | 0 | return 0; |
379 | 0 | } |
380 | | |
381 | | struct ambiguous_output { |
382 | | const struct disambiguate_state *ds; |
383 | | struct strbuf advice; |
384 | | struct strbuf sb; |
385 | | }; |
386 | | |
387 | | static int show_ambiguous_object(const struct object_id *oid, void *data) |
388 | 0 | { |
389 | 0 | struct ambiguous_output *state = data; |
390 | 0 | const struct disambiguate_state *ds = state->ds; |
391 | 0 | struct strbuf *advice = &state->advice; |
392 | 0 | struct strbuf *sb = &state->sb; |
393 | 0 | int type; |
394 | 0 | const char *hash; |
395 | |
|
396 | 0 | if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data)) |
397 | 0 | return 0; |
398 | | |
399 | 0 | hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV); |
400 | 0 | type = oid_object_info(ds->repo, oid, NULL); |
401 | |
|
402 | 0 | if (type < 0) { |
403 | | /* |
404 | | * TRANSLATORS: This is a line of ambiguous object |
405 | | * output shown when we cannot look up or parse the |
406 | | * object in question. E.g. "deadbeef [bad object]". |
407 | | */ |
408 | 0 | strbuf_addf(sb, _("%s [bad object]"), hash); |
409 | 0 | goto out; |
410 | 0 | } |
411 | | |
412 | 0 | assert(type == OBJ_TREE || type == OBJ_COMMIT || |
413 | 0 | type == OBJ_BLOB || type == OBJ_TAG); |
414 | | |
415 | 0 | if (type == OBJ_COMMIT) { |
416 | 0 | struct strbuf date = STRBUF_INIT; |
417 | 0 | struct strbuf msg = STRBUF_INIT; |
418 | 0 | struct commit *commit = lookup_commit(ds->repo, oid); |
419 | |
|
420 | 0 | if (commit) { |
421 | 0 | struct pretty_print_context pp = {0}; |
422 | 0 | pp.date_mode.type = DATE_SHORT; |
423 | 0 | repo_format_commit_message(the_repository, commit, |
424 | 0 | "%ad", &date, &pp); |
425 | 0 | repo_format_commit_message(the_repository, commit, |
426 | 0 | "%s", &msg, &pp); |
427 | 0 | } |
428 | | |
429 | | /* |
430 | | * TRANSLATORS: This is a line of ambiguous commit |
431 | | * object output. E.g.: |
432 | | * |
433 | | * "deadbeef commit 2021-01-01 - Some Commit Message" |
434 | | */ |
435 | 0 | strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf, |
436 | 0 | msg.buf); |
437 | |
|
438 | 0 | strbuf_release(&date); |
439 | 0 | strbuf_release(&msg); |
440 | 0 | } else if (type == OBJ_TAG) { |
441 | 0 | struct tag *tag = lookup_tag(ds->repo, oid); |
442 | |
|
443 | 0 | if (!parse_tag(tag) && tag->tag) { |
444 | | /* |
445 | | * TRANSLATORS: This is a line of ambiguous |
446 | | * tag object output. E.g.: |
447 | | * |
448 | | * "deadbeef tag 2022-01-01 - Some Tag Message" |
449 | | * |
450 | | * The second argument is the YYYY-MM-DD found |
451 | | * in the tag. |
452 | | * |
453 | | * The third argument is the "tag" string |
454 | | * from object.c. |
455 | | */ |
456 | 0 | strbuf_addf(sb, _("%s tag %s - %s"), hash, |
457 | 0 | show_date(tag->date, 0, DATE_MODE(SHORT)), |
458 | 0 | tag->tag); |
459 | 0 | } else { |
460 | | /* |
461 | | * TRANSLATORS: This is a line of ambiguous |
462 | | * tag object output where we couldn't parse |
463 | | * the tag itself. E.g.: |
464 | | * |
465 | | * "deadbeef [bad tag, could not parse it]" |
466 | | */ |
467 | 0 | strbuf_addf(sb, _("%s [bad tag, could not parse it]"), |
468 | 0 | hash); |
469 | 0 | } |
470 | 0 | } else if (type == OBJ_TREE) { |
471 | | /* |
472 | | * TRANSLATORS: This is a line of ambiguous <type> |
473 | | * object output. E.g. "deadbeef tree". |
474 | | */ |
475 | 0 | strbuf_addf(sb, _("%s tree"), hash); |
476 | 0 | } else if (type == OBJ_BLOB) { |
477 | | /* |
478 | | * TRANSLATORS: This is a line of ambiguous <type> |
479 | | * object output. E.g. "deadbeef blob". |
480 | | */ |
481 | 0 | strbuf_addf(sb, _("%s blob"), hash); |
482 | 0 | } |
483 | | |
484 | |
|
485 | 0 | out: |
486 | | /* |
487 | | * TRANSLATORS: This is line item of ambiguous object output |
488 | | * from describe_ambiguous_object() above. For RTL languages |
489 | | * you'll probably want to swap the "%s" and leading " " space |
490 | | * around. |
491 | | */ |
492 | 0 | strbuf_addf(advice, _(" %s\n"), sb->buf); |
493 | |
|
494 | 0 | strbuf_reset(sb); |
495 | 0 | return 0; |
496 | 0 | } |
497 | | |
498 | | static int collect_ambiguous(const struct object_id *oid, void *data) |
499 | 0 | { |
500 | 0 | oid_array_append(data, oid); |
501 | 0 | return 0; |
502 | 0 | } |
503 | | |
504 | | static int repo_collect_ambiguous(struct repository *r UNUSED, |
505 | | const struct object_id *oid, |
506 | | void *data) |
507 | 0 | { |
508 | 0 | return collect_ambiguous(oid, data); |
509 | 0 | } |
510 | | |
511 | | static int sort_ambiguous(const void *va, const void *vb, void *ctx) |
512 | 0 | { |
513 | 0 | struct repository *sort_ambiguous_repo = ctx; |
514 | 0 | const struct object_id *a = va, *b = vb; |
515 | 0 | int a_type = oid_object_info(sort_ambiguous_repo, a, NULL); |
516 | 0 | int b_type = oid_object_info(sort_ambiguous_repo, b, NULL); |
517 | 0 | int a_type_sort; |
518 | 0 | int b_type_sort; |
519 | | |
520 | | /* |
521 | | * Sorts by hash within the same object type, just as |
522 | | * oid_array_for_each_unique() would do. |
523 | | */ |
524 | 0 | if (a_type == b_type) { |
525 | 0 | if (a->algo == b->algo) |
526 | 0 | return oidcmp(a, b); |
527 | 0 | else |
528 | 0 | return a->algo > b->algo ? 1 : -1; |
529 | 0 | } |
530 | | |
531 | | /* |
532 | | * Between object types show tags, then commits, and finally |
533 | | * trees and blobs. |
534 | | * |
535 | | * The object_type enum is commit, tree, blob, tag, but we |
536 | | * want tag, commit, tree blob. Cleverly (perhaps too |
537 | | * cleverly) do that with modulus, since the enum assigns 1 to |
538 | | * commit, so tag becomes 0. |
539 | | */ |
540 | 0 | a_type_sort = a_type % 4; |
541 | 0 | b_type_sort = b_type % 4; |
542 | 0 | return a_type_sort > b_type_sort ? 1 : -1; |
543 | 0 | } |
544 | | |
545 | | static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a) |
546 | 0 | { |
547 | 0 | QSORT_S(a->oid, a->nr, sort_ambiguous, r); |
548 | 0 | } |
549 | | |
550 | | static enum get_oid_result get_short_oid(struct repository *r, |
551 | | const char *name, int len, |
552 | | struct object_id *oid, |
553 | | unsigned flags) |
554 | 0 | { |
555 | 0 | int status; |
556 | 0 | struct disambiguate_state ds; |
557 | 0 | int quietly = !!(flags & GET_OID_QUIETLY); |
558 | 0 | const struct git_hash_algo *algo = r->hash_algo; |
559 | |
|
560 | 0 | if (flags & GET_OID_HASH_ANY) |
561 | 0 | algo = NULL; |
562 | |
|
563 | 0 | if (init_object_disambiguation(r, name, len, algo, &ds) < 0) |
564 | 0 | return -1; |
565 | | |
566 | 0 | if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS)) |
567 | 0 | BUG("multiple get_short_oid disambiguator flags"); |
568 | | |
569 | 0 | if (flags & GET_OID_COMMIT) |
570 | 0 | ds.fn = disambiguate_commit_only; |
571 | 0 | else if (flags & GET_OID_COMMITTISH) |
572 | 0 | ds.fn = disambiguate_committish_only; |
573 | 0 | else if (flags & GET_OID_TREE) |
574 | 0 | ds.fn = disambiguate_tree_only; |
575 | 0 | else if (flags & GET_OID_TREEISH) |
576 | 0 | ds.fn = disambiguate_treeish_only; |
577 | 0 | else if (flags & GET_OID_BLOB) |
578 | 0 | ds.fn = disambiguate_blob_only; |
579 | 0 | else |
580 | 0 | ds.fn = default_disambiguate_hint; |
581 | |
|
582 | 0 | find_short_object_filename(&ds); |
583 | 0 | find_short_packed_object(&ds); |
584 | 0 | status = finish_object_disambiguation(&ds, oid); |
585 | | |
586 | | /* |
587 | | * If we didn't find it, do the usual reprepare() slow-path, |
588 | | * since the object may have recently been added to the repository |
589 | | * or migrated from loose to packed. |
590 | | */ |
591 | 0 | if (status == MISSING_OBJECT) { |
592 | 0 | reprepare_packed_git(r); |
593 | 0 | find_short_object_filename(&ds); |
594 | 0 | find_short_packed_object(&ds); |
595 | 0 | status = finish_object_disambiguation(&ds, oid); |
596 | 0 | } |
597 | |
|
598 | 0 | if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) { |
599 | 0 | struct oid_array collect = OID_ARRAY_INIT; |
600 | 0 | struct ambiguous_output out = { |
601 | 0 | .ds = &ds, |
602 | 0 | .sb = STRBUF_INIT, |
603 | 0 | .advice = STRBUF_INIT, |
604 | 0 | }; |
605 | |
|
606 | 0 | error(_("short object ID %s is ambiguous"), ds.hex_pfx); |
607 | | |
608 | | /* |
609 | | * We may still have ambiguity if we simply saw a series of |
610 | | * candidates that did not satisfy our hint function. In |
611 | | * that case, we still want to show them, so disable the hint |
612 | | * function entirely. |
613 | | */ |
614 | 0 | if (!ds.ambiguous) |
615 | 0 | ds.fn = NULL; |
616 | |
|
617 | 0 | repo_for_each_abbrev(r, ds.hex_pfx, algo, collect_ambiguous, &collect); |
618 | 0 | sort_ambiguous_oid_array(r, &collect); |
619 | |
|
620 | 0 | if (oid_array_for_each(&collect, show_ambiguous_object, &out)) |
621 | 0 | BUG("show_ambiguous_object shouldn't return non-zero"); |
622 | | |
623 | | /* |
624 | | * TRANSLATORS: The argument is the list of ambiguous |
625 | | * objects composed in show_ambiguous_object(). See |
626 | | * its "TRANSLATORS" comments for details. |
627 | | */ |
628 | 0 | advise(_("The candidates are:\n%s"), out.advice.buf); |
629 | |
|
630 | 0 | oid_array_clear(&collect); |
631 | 0 | strbuf_release(&out.advice); |
632 | 0 | strbuf_release(&out.sb); |
633 | 0 | } |
634 | | |
635 | 0 | return status; |
636 | 0 | } |
637 | | |
638 | | int repo_for_each_abbrev(struct repository *r, const char *prefix, |
639 | | const struct git_hash_algo *algo, |
640 | | each_abbrev_fn fn, void *cb_data) |
641 | 0 | { |
642 | 0 | struct oid_array collect = OID_ARRAY_INIT; |
643 | 0 | struct disambiguate_state ds; |
644 | 0 | int ret; |
645 | |
|
646 | 0 | if (init_object_disambiguation(r, prefix, strlen(prefix), algo, &ds) < 0) |
647 | 0 | return -1; |
648 | | |
649 | 0 | ds.always_call_fn = 1; |
650 | 0 | ds.fn = repo_collect_ambiguous; |
651 | 0 | ds.cb_data = &collect; |
652 | 0 | find_short_object_filename(&ds); |
653 | 0 | find_short_packed_object(&ds); |
654 | |
|
655 | 0 | ret = oid_array_for_each_unique(&collect, fn, cb_data); |
656 | 0 | oid_array_clear(&collect); |
657 | 0 | return ret; |
658 | 0 | } |
659 | | |
660 | | /* |
661 | | * Return the slot of the most-significant bit set in "val". There are various |
662 | | * ways to do this quickly with fls() or __builtin_clzl(), but speed is |
663 | | * probably not a big deal here. |
664 | | */ |
665 | | static unsigned msb(unsigned long val) |
666 | 0 | { |
667 | 0 | unsigned r = 0; |
668 | 0 | while (val >>= 1) |
669 | 0 | r++; |
670 | 0 | return r; |
671 | 0 | } |
672 | | |
673 | | struct min_abbrev_data { |
674 | | unsigned int init_len; |
675 | | unsigned int cur_len; |
676 | | char *hex; |
677 | | struct repository *repo; |
678 | | const struct object_id *oid; |
679 | | }; |
680 | | |
681 | | static inline char get_hex_char_from_oid(const struct object_id *oid, |
682 | | unsigned int pos) |
683 | 0 | { |
684 | 0 | static const char hex[] = "0123456789abcdef"; |
685 | |
|
686 | 0 | if ((pos & 1) == 0) |
687 | 0 | return hex[oid->hash[pos >> 1] >> 4]; |
688 | 0 | else |
689 | 0 | return hex[oid->hash[pos >> 1] & 0xf]; |
690 | 0 | } |
691 | | |
692 | | static int extend_abbrev_len(const struct object_id *oid, void *cb_data) |
693 | 0 | { |
694 | 0 | struct min_abbrev_data *mad = cb_data; |
695 | |
|
696 | 0 | unsigned int i = mad->init_len; |
697 | 0 | while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i)) |
698 | 0 | i++; |
699 | |
|
700 | 0 | if (i < GIT_MAX_RAWSZ && i >= mad->cur_len) |
701 | 0 | mad->cur_len = i + 1; |
702 | |
|
703 | 0 | return 0; |
704 | 0 | } |
705 | | |
706 | | static int repo_extend_abbrev_len(struct repository *r UNUSED, |
707 | | const struct object_id *oid, |
708 | | void *cb_data) |
709 | 0 | { |
710 | 0 | return extend_abbrev_len(oid, cb_data); |
711 | 0 | } |
712 | | |
713 | | static void find_abbrev_len_for_midx(struct multi_pack_index *m, |
714 | | struct min_abbrev_data *mad) |
715 | 0 | { |
716 | 0 | for (; m; m = m->base_midx) { |
717 | 0 | int match = 0; |
718 | 0 | uint32_t num, first = 0; |
719 | 0 | struct object_id oid; |
720 | 0 | const struct object_id *mad_oid; |
721 | |
|
722 | 0 | if (!m->num_objects) |
723 | 0 | continue; |
724 | | |
725 | 0 | num = m->num_objects + m->num_objects_in_base; |
726 | 0 | mad_oid = mad->oid; |
727 | 0 | match = bsearch_one_midx(mad_oid, m, &first); |
728 | | |
729 | | /* |
730 | | * first is now the position in the packfile where we |
731 | | * would insert mad->hash if it does not exist (or the |
732 | | * position of mad->hash if it does exist). Hence, we |
733 | | * consider a maximum of two objects nearby for the |
734 | | * abbreviation length. |
735 | | */ |
736 | 0 | mad->init_len = 0; |
737 | 0 | if (!match) { |
738 | 0 | if (nth_midxed_object_oid(&oid, m, first)) |
739 | 0 | extend_abbrev_len(&oid, mad); |
740 | 0 | } else if (first < num - 1) { |
741 | 0 | if (nth_midxed_object_oid(&oid, m, first + 1)) |
742 | 0 | extend_abbrev_len(&oid, mad); |
743 | 0 | } |
744 | 0 | if (first > 0) { |
745 | 0 | if (nth_midxed_object_oid(&oid, m, first - 1)) |
746 | 0 | extend_abbrev_len(&oid, mad); |
747 | 0 | } |
748 | 0 | mad->init_len = mad->cur_len; |
749 | 0 | } |
750 | 0 | } |
751 | | |
752 | | static void find_abbrev_len_for_pack(struct packed_git *p, |
753 | | struct min_abbrev_data *mad) |
754 | 0 | { |
755 | 0 | int match = 0; |
756 | 0 | uint32_t num, first = 0; |
757 | 0 | struct object_id oid; |
758 | 0 | const struct object_id *mad_oid; |
759 | |
|
760 | 0 | if (p->multi_pack_index) |
761 | 0 | return; |
762 | | |
763 | 0 | if (open_pack_index(p) || !p->num_objects) |
764 | 0 | return; |
765 | | |
766 | 0 | num = p->num_objects; |
767 | 0 | mad_oid = mad->oid; |
768 | 0 | match = bsearch_pack(mad_oid, p, &first); |
769 | | |
770 | | /* |
771 | | * first is now the position in the packfile where we would insert |
772 | | * mad->hash if it does not exist (or the position of mad->hash if |
773 | | * it does exist). Hence, we consider a maximum of two objects |
774 | | * nearby for the abbreviation length. |
775 | | */ |
776 | 0 | mad->init_len = 0; |
777 | 0 | if (!match) { |
778 | 0 | if (!nth_packed_object_id(&oid, p, first)) |
779 | 0 | extend_abbrev_len(&oid, mad); |
780 | 0 | } else if (first < num - 1) { |
781 | 0 | if (!nth_packed_object_id(&oid, p, first + 1)) |
782 | 0 | extend_abbrev_len(&oid, mad); |
783 | 0 | } |
784 | 0 | if (first > 0) { |
785 | 0 | if (!nth_packed_object_id(&oid, p, first - 1)) |
786 | 0 | extend_abbrev_len(&oid, mad); |
787 | 0 | } |
788 | 0 | mad->init_len = mad->cur_len; |
789 | 0 | } |
790 | | |
791 | | static void find_abbrev_len_packed(struct min_abbrev_data *mad) |
792 | 0 | { |
793 | 0 | struct multi_pack_index *m; |
794 | 0 | struct packed_git *p; |
795 | |
|
796 | 0 | for (m = get_multi_pack_index(mad->repo); m; m = m->next) |
797 | 0 | find_abbrev_len_for_midx(m, mad); |
798 | 0 | for (p = get_packed_git(mad->repo); p; p = p->next) |
799 | 0 | find_abbrev_len_for_pack(p, mad); |
800 | 0 | } |
801 | | |
802 | | void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, |
803 | | const struct object_id *oid, int abbrev_len) |
804 | 0 | { |
805 | 0 | int r; |
806 | 0 | strbuf_grow(sb, GIT_MAX_HEXSZ + 1); |
807 | 0 | r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len); |
808 | 0 | strbuf_setlen(sb, sb->len + r); |
809 | 0 | } |
810 | | |
811 | | void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, |
812 | | int abbrev_len) |
813 | 0 | { |
814 | 0 | strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len); |
815 | 0 | } |
816 | | |
817 | | int repo_find_unique_abbrev_r(struct repository *r, char *hex, |
818 | | const struct object_id *oid, int len) |
819 | 0 | { |
820 | 0 | const struct git_hash_algo *algo = |
821 | 0 | oid->algo ? &hash_algos[oid->algo] : r->hash_algo; |
822 | 0 | struct disambiguate_state ds; |
823 | 0 | struct min_abbrev_data mad; |
824 | 0 | struct object_id oid_ret; |
825 | 0 | const unsigned hexsz = algo->hexsz; |
826 | |
|
827 | 0 | if (len < 0) { |
828 | 0 | unsigned long count = repo_approximate_object_count(r); |
829 | | /* |
830 | | * Add one because the MSB only tells us the highest bit set, |
831 | | * not including the value of all the _other_ bits (so "15" |
832 | | * is only one off of 2^4, but the MSB is the 3rd bit. |
833 | | */ |
834 | 0 | len = msb(count) + 1; |
835 | | /* |
836 | | * We now know we have on the order of 2^len objects, which |
837 | | * expects a collision at 2^(len/2). But we also care about hex |
838 | | * chars, not bits, and there are 4 bits per hex. So all |
839 | | * together we need to divide by 2 and round up. |
840 | | */ |
841 | 0 | len = DIV_ROUND_UP(len, 2); |
842 | | /* |
843 | | * For very small repos, we stick with our regular fallback. |
844 | | */ |
845 | 0 | if (len < FALLBACK_DEFAULT_ABBREV) |
846 | 0 | len = FALLBACK_DEFAULT_ABBREV; |
847 | 0 | } |
848 | |
|
849 | 0 | oid_to_hex_r(hex, oid); |
850 | 0 | if (len >= hexsz || !len) |
851 | 0 | return hexsz; |
852 | | |
853 | 0 | mad.repo = r; |
854 | 0 | mad.init_len = len; |
855 | 0 | mad.cur_len = len; |
856 | 0 | mad.hex = hex; |
857 | 0 | mad.oid = oid; |
858 | |
|
859 | 0 | find_abbrev_len_packed(&mad); |
860 | |
|
861 | 0 | if (init_object_disambiguation(r, hex, mad.cur_len, algo, &ds) < 0) |
862 | 0 | return -1; |
863 | | |
864 | 0 | ds.fn = repo_extend_abbrev_len; |
865 | 0 | ds.always_call_fn = 1; |
866 | 0 | ds.cb_data = (void *)&mad; |
867 | |
|
868 | 0 | find_short_object_filename(&ds); |
869 | 0 | (void)finish_object_disambiguation(&ds, &oid_ret); |
870 | |
|
871 | 0 | hex[mad.cur_len] = 0; |
872 | 0 | return mad.cur_len; |
873 | 0 | } |
874 | | |
875 | | const char *repo_find_unique_abbrev(struct repository *r, |
876 | | const struct object_id *oid, |
877 | | int len) |
878 | 0 | { |
879 | 0 | static int bufno; |
880 | 0 | static char hexbuffer[4][GIT_MAX_HEXSZ + 1]; |
881 | 0 | char *hex = hexbuffer[bufno]; |
882 | 0 | bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer); |
883 | 0 | repo_find_unique_abbrev_r(r, hex, oid, len); |
884 | 0 | return hex; |
885 | 0 | } |
886 | | |
887 | | static int ambiguous_path(const char *path, int len) |
888 | 0 | { |
889 | 0 | int slash = 1; |
890 | 0 | int cnt; |
891 | |
|
892 | 0 | for (cnt = 0; cnt < len; cnt++) { |
893 | 0 | switch (*path++) { |
894 | 0 | case '\0': |
895 | 0 | break; |
896 | 0 | case '/': |
897 | 0 | if (slash) |
898 | 0 | break; |
899 | 0 | slash = 1; |
900 | 0 | continue; |
901 | 0 | case '.': |
902 | 0 | continue; |
903 | 0 | default: |
904 | 0 | slash = 0; |
905 | 0 | continue; |
906 | 0 | } |
907 | 0 | break; |
908 | 0 | } |
909 | 0 | return slash; |
910 | 0 | } |
911 | | |
912 | | static inline int at_mark(const char *string, int len, |
913 | | const char **suffix, int nr) |
914 | 0 | { |
915 | 0 | int i; |
916 | |
|
917 | 0 | for (i = 0; i < nr; i++) { |
918 | 0 | int suffix_len = strlen(suffix[i]); |
919 | 0 | if (suffix_len <= len |
920 | 0 | && !strncasecmp(string, suffix[i], suffix_len)) |
921 | 0 | return suffix_len; |
922 | 0 | } |
923 | 0 | return 0; |
924 | 0 | } |
925 | | |
926 | | static inline int upstream_mark(const char *string, int len) |
927 | 0 | { |
928 | 0 | const char *suffix[] = { "@{upstream}", "@{u}" }; |
929 | 0 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); |
930 | 0 | } |
931 | | |
932 | | static inline int push_mark(const char *string, int len) |
933 | 0 | { |
934 | 0 | const char *suffix[] = { "@{push}" }; |
935 | 0 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); |
936 | 0 | } |
937 | | |
938 | | static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags); |
939 | | static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf); |
940 | | |
941 | | static int get_oid_basic(struct repository *r, const char *str, int len, |
942 | | struct object_id *oid, unsigned int flags) |
943 | 0 | { |
944 | 0 | static const char *warn_msg = "refname '%.*s' is ambiguous."; |
945 | 0 | static const char *object_name_msg = N_( |
946 | 0 | "Git normally never creates a ref that ends with 40 hex characters\n" |
947 | 0 | "because it will be ignored when you just specify 40-hex. These refs\n" |
948 | 0 | "may be created by mistake. For example,\n" |
949 | 0 | "\n" |
950 | 0 | " git switch -c $br $(git rev-parse ...)\n" |
951 | 0 | "\n" |
952 | 0 | "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n" |
953 | 0 | "examine these refs and maybe delete them. Turn this message off by\n" |
954 | 0 | "running \"git config advice.objectNameWarning false\""); |
955 | 0 | struct object_id tmp_oid; |
956 | 0 | char *real_ref = NULL; |
957 | 0 | int refs_found = 0; |
958 | 0 | int at, reflog_len, nth_prior = 0; |
959 | 0 | int fatal = !(flags & GET_OID_QUIETLY); |
960 | |
|
961 | 0 | if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) { |
962 | 0 | if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) { |
963 | 0 | refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0); |
964 | 0 | if (refs_found > 0) { |
965 | 0 | warning(warn_msg, len, str); |
966 | 0 | if (advice_enabled(ADVICE_OBJECT_NAME_WARNING)) |
967 | 0 | fprintf(stderr, "%s\n", _(object_name_msg)); |
968 | 0 | } |
969 | 0 | free(real_ref); |
970 | 0 | } |
971 | 0 | return 0; |
972 | 0 | } |
973 | | |
974 | | /* basic@{time or number or -number} format to query ref-log */ |
975 | 0 | reflog_len = at = 0; |
976 | 0 | if (len && str[len-1] == '}') { |
977 | 0 | for (at = len-4; at >= 0; at--) { |
978 | 0 | if (str[at] == '@' && str[at+1] == '{') { |
979 | 0 | if (str[at+2] == '-') { |
980 | 0 | if (at != 0) |
981 | | /* @{-N} not at start */ |
982 | 0 | return -1; |
983 | 0 | nth_prior = 1; |
984 | 0 | continue; |
985 | 0 | } |
986 | 0 | if (!upstream_mark(str + at, len - at) && |
987 | 0 | !push_mark(str + at, len - at)) { |
988 | 0 | reflog_len = (len-1) - (at+2); |
989 | 0 | len = at; |
990 | 0 | } |
991 | 0 | break; |
992 | 0 | } |
993 | 0 | } |
994 | 0 | } |
995 | | |
996 | | /* Accept only unambiguous ref paths. */ |
997 | 0 | if (len && ambiguous_path(str, len)) |
998 | 0 | return -1; |
999 | | |
1000 | 0 | if (nth_prior) { |
1001 | 0 | struct strbuf buf = STRBUF_INIT; |
1002 | 0 | int detached; |
1003 | |
|
1004 | 0 | if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) { |
1005 | 0 | detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid)); |
1006 | 0 | strbuf_release(&buf); |
1007 | 0 | if (detached) |
1008 | 0 | return 0; |
1009 | 0 | } |
1010 | 0 | } |
1011 | | |
1012 | 0 | if (!len && reflog_len) |
1013 | | /* allow "@{...}" to mean the current branch reflog */ |
1014 | 0 | refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal); |
1015 | 0 | else if (reflog_len) |
1016 | 0 | refs_found = repo_dwim_log(r, str, len, oid, &real_ref); |
1017 | 0 | else |
1018 | 0 | refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal); |
1019 | |
|
1020 | 0 | if (!refs_found) |
1021 | 0 | return -1; |
1022 | | |
1023 | 0 | if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) && |
1024 | 0 | (refs_found > 1 || |
1025 | 0 | !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY))) |
1026 | 0 | warning(warn_msg, len, str); |
1027 | |
|
1028 | 0 | if (reflog_len) { |
1029 | 0 | int nth, i; |
1030 | 0 | timestamp_t at_time; |
1031 | 0 | timestamp_t co_time; |
1032 | 0 | int co_tz, co_cnt; |
1033 | | |
1034 | | /* Is it asking for N-th entry, or approxidate? */ |
1035 | 0 | for (i = nth = 0; 0 <= nth && i < reflog_len; i++) { |
1036 | 0 | char ch = str[at+2+i]; |
1037 | 0 | if ('0' <= ch && ch <= '9') |
1038 | 0 | nth = nth * 10 + ch - '0'; |
1039 | 0 | else |
1040 | 0 | nth = -1; |
1041 | 0 | } |
1042 | 0 | if (100000000 <= nth) { |
1043 | 0 | at_time = nth; |
1044 | 0 | nth = -1; |
1045 | 0 | } else if (0 <= nth) |
1046 | 0 | at_time = 0; |
1047 | 0 | else { |
1048 | 0 | int errors = 0; |
1049 | 0 | char *tmp = xstrndup(str + at + 2, reflog_len); |
1050 | 0 | at_time = approxidate_careful(tmp, &errors); |
1051 | 0 | free(tmp); |
1052 | 0 | if (errors) { |
1053 | 0 | free(real_ref); |
1054 | 0 | return -1; |
1055 | 0 | } |
1056 | 0 | } |
1057 | 0 | if (read_ref_at(get_main_ref_store(r), |
1058 | 0 | real_ref, flags, at_time, nth, oid, NULL, |
1059 | 0 | &co_time, &co_tz, &co_cnt)) { |
1060 | 0 | if (!len) { |
1061 | 0 | if (!skip_prefix(real_ref, "refs/heads/", &str)) |
1062 | 0 | str = "HEAD"; |
1063 | 0 | len = strlen(str); |
1064 | 0 | } |
1065 | 0 | if (at_time) { |
1066 | 0 | if (!(flags & GET_OID_QUIETLY)) { |
1067 | 0 | warning(_("log for '%.*s' only goes back to %s"), |
1068 | 0 | len, str, |
1069 | 0 | show_date(co_time, co_tz, DATE_MODE(RFC2822))); |
1070 | 0 | } |
1071 | 0 | } else if (nth == co_cnt && !is_null_oid(oid)) { |
1072 | | /* |
1073 | | * We were asked for the Nth reflog (counting |
1074 | | * from 0), but there were only N entries. |
1075 | | * read_ref_at() will have returned "1" to tell |
1076 | | * us it did not find an entry, but it did |
1077 | | * still fill in the oid with the "old" value, |
1078 | | * which we can use. |
1079 | | */ |
1080 | 0 | } else { |
1081 | 0 | if (flags & GET_OID_QUIETLY) { |
1082 | 0 | exit(128); |
1083 | 0 | } |
1084 | 0 | die(_("log for '%.*s' only has %d entries"), |
1085 | 0 | len, str, co_cnt); |
1086 | 0 | } |
1087 | 0 | } |
1088 | 0 | } |
1089 | | |
1090 | 0 | free(real_ref); |
1091 | 0 | return 0; |
1092 | 0 | } |
1093 | | |
1094 | | static enum get_oid_result get_parent(struct repository *r, |
1095 | | const char *name, int len, |
1096 | | struct object_id *result, int idx) |
1097 | 0 | { |
1098 | 0 | struct object_id oid; |
1099 | 0 | enum get_oid_result ret = get_oid_1(r, name, len, &oid, |
1100 | 0 | GET_OID_COMMITTISH); |
1101 | 0 | struct commit *commit; |
1102 | 0 | struct commit_list *p; |
1103 | |
|
1104 | 0 | if (ret) |
1105 | 0 | return ret; |
1106 | 0 | commit = lookup_commit_reference(r, &oid); |
1107 | 0 | if (repo_parse_commit(r, commit)) |
1108 | 0 | return MISSING_OBJECT; |
1109 | 0 | if (!idx) { |
1110 | 0 | oidcpy(result, &commit->object.oid); |
1111 | 0 | return FOUND; |
1112 | 0 | } |
1113 | 0 | p = commit->parents; |
1114 | 0 | while (p) { |
1115 | 0 | if (!--idx) { |
1116 | 0 | oidcpy(result, &p->item->object.oid); |
1117 | 0 | return FOUND; |
1118 | 0 | } |
1119 | 0 | p = p->next; |
1120 | 0 | } |
1121 | 0 | return MISSING_OBJECT; |
1122 | 0 | } |
1123 | | |
1124 | | static enum get_oid_result get_nth_ancestor(struct repository *r, |
1125 | | const char *name, int len, |
1126 | | struct object_id *result, |
1127 | | int generation) |
1128 | 0 | { |
1129 | 0 | struct object_id oid; |
1130 | 0 | struct commit *commit; |
1131 | 0 | int ret; |
1132 | |
|
1133 | 0 | ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH); |
1134 | 0 | if (ret) |
1135 | 0 | return ret; |
1136 | 0 | commit = lookup_commit_reference(r, &oid); |
1137 | 0 | if (!commit) |
1138 | 0 | return MISSING_OBJECT; |
1139 | | |
1140 | 0 | while (generation--) { |
1141 | 0 | if (repo_parse_commit(r, commit) || !commit->parents) |
1142 | 0 | return MISSING_OBJECT; |
1143 | 0 | commit = commit->parents->item; |
1144 | 0 | } |
1145 | 0 | oidcpy(result, &commit->object.oid); |
1146 | 0 | return FOUND; |
1147 | 0 | } |
1148 | | |
1149 | | struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen, |
1150 | | struct object *o, enum object_type expected_type) |
1151 | 0 | { |
1152 | 0 | if (name && !namelen) |
1153 | 0 | namelen = strlen(name); |
1154 | 0 | while (1) { |
1155 | 0 | if (!o || (!o->parsed && !parse_object(r, &o->oid))) |
1156 | 0 | return NULL; |
1157 | 0 | if (expected_type == OBJ_ANY || o->type == expected_type) |
1158 | 0 | return o; |
1159 | 0 | if (o->type == OBJ_TAG) |
1160 | 0 | o = ((struct tag*) o)->tagged; |
1161 | 0 | else if (o->type == OBJ_COMMIT) |
1162 | 0 | o = &(repo_get_commit_tree(r, ((struct commit *)o))->object); |
1163 | 0 | else { |
1164 | 0 | if (name) |
1165 | 0 | error("%.*s: expected %s type, but the object " |
1166 | 0 | "dereferences to %s type", |
1167 | 0 | namelen, name, type_name(expected_type), |
1168 | 0 | type_name(o->type)); |
1169 | 0 | return NULL; |
1170 | 0 | } |
1171 | 0 | } |
1172 | 0 | } |
1173 | | |
1174 | | static int peel_onion(struct repository *r, const char *name, int len, |
1175 | | struct object_id *oid, unsigned lookup_flags) |
1176 | 0 | { |
1177 | 0 | struct object_id outer; |
1178 | 0 | const char *sp; |
1179 | 0 | unsigned int expected_type = 0; |
1180 | 0 | struct object *o; |
1181 | | |
1182 | | /* |
1183 | | * "ref^{type}" dereferences ref repeatedly until you cannot |
1184 | | * dereference anymore, or you get an object of given type, |
1185 | | * whichever comes first. "ref^{}" means just dereference |
1186 | | * tags until you get a non-tag. "ref^0" is a shorthand for |
1187 | | * "ref^{commit}". "commit^{tree}" could be used to find the |
1188 | | * top-level tree of the given commit. |
1189 | | */ |
1190 | 0 | if (len < 4 || name[len-1] != '}') |
1191 | 0 | return -1; |
1192 | | |
1193 | 0 | for (sp = name + len - 1; name <= sp; sp--) { |
1194 | 0 | int ch = *sp; |
1195 | 0 | if (ch == '{' && name < sp && sp[-1] == '^') |
1196 | 0 | break; |
1197 | 0 | } |
1198 | 0 | if (sp <= name) |
1199 | 0 | return -1; |
1200 | | |
1201 | 0 | sp++; /* beginning of type name, or closing brace for empty */ |
1202 | 0 | if (starts_with(sp, "commit}")) |
1203 | 0 | expected_type = OBJ_COMMIT; |
1204 | 0 | else if (starts_with(sp, "tag}")) |
1205 | 0 | expected_type = OBJ_TAG; |
1206 | 0 | else if (starts_with(sp, "tree}")) |
1207 | 0 | expected_type = OBJ_TREE; |
1208 | 0 | else if (starts_with(sp, "blob}")) |
1209 | 0 | expected_type = OBJ_BLOB; |
1210 | 0 | else if (starts_with(sp, "object}")) |
1211 | 0 | expected_type = OBJ_ANY; |
1212 | 0 | else if (sp[0] == '}') |
1213 | 0 | expected_type = OBJ_NONE; |
1214 | 0 | else if (sp[0] == '/') |
1215 | 0 | expected_type = OBJ_COMMIT; |
1216 | 0 | else |
1217 | 0 | return -1; |
1218 | | |
1219 | 0 | lookup_flags &= ~GET_OID_DISAMBIGUATORS; |
1220 | 0 | if (expected_type == OBJ_COMMIT) |
1221 | 0 | lookup_flags |= GET_OID_COMMITTISH; |
1222 | 0 | else if (expected_type == OBJ_TREE) |
1223 | 0 | lookup_flags |= GET_OID_TREEISH; |
1224 | |
|
1225 | 0 | if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags)) |
1226 | 0 | return -1; |
1227 | | |
1228 | 0 | o = parse_object(r, &outer); |
1229 | 0 | if (!o) |
1230 | 0 | return -1; |
1231 | 0 | if (!expected_type) { |
1232 | 0 | o = deref_tag(r, o, name, sp - name - 2); |
1233 | 0 | if (!o || (!o->parsed && !parse_object(r, &o->oid))) |
1234 | 0 | return -1; |
1235 | 0 | oidcpy(oid, &o->oid); |
1236 | 0 | return 0; |
1237 | 0 | } |
1238 | | |
1239 | | /* |
1240 | | * At this point, the syntax look correct, so |
1241 | | * if we do not get the needed object, we should |
1242 | | * barf. |
1243 | | */ |
1244 | 0 | o = repo_peel_to_type(r, name, len, o, expected_type); |
1245 | 0 | if (!o) |
1246 | 0 | return -1; |
1247 | | |
1248 | 0 | oidcpy(oid, &o->oid); |
1249 | 0 | if (sp[0] == '/') { |
1250 | | /* "$commit^{/foo}" */ |
1251 | 0 | char *prefix; |
1252 | 0 | int ret; |
1253 | 0 | struct commit_list *list = NULL; |
1254 | | |
1255 | | /* |
1256 | | * $commit^{/}. Some regex implementation may reject. |
1257 | | * We don't need regex anyway. '' pattern always matches. |
1258 | | */ |
1259 | 0 | if (sp[1] == '}') |
1260 | 0 | return 0; |
1261 | | |
1262 | 0 | prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1)); |
1263 | 0 | commit_list_insert((struct commit *)o, &list); |
1264 | 0 | ret = get_oid_oneline(r, prefix, oid, list); |
1265 | |
|
1266 | 0 | free_commit_list(list); |
1267 | 0 | free(prefix); |
1268 | 0 | return ret; |
1269 | 0 | } |
1270 | 0 | return 0; |
1271 | 0 | } |
1272 | | |
1273 | | static int get_describe_name(struct repository *r, |
1274 | | const char *name, int len, |
1275 | | struct object_id *oid) |
1276 | 0 | { |
1277 | 0 | const char *cp; |
1278 | 0 | unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT; |
1279 | |
|
1280 | 0 | for (cp = name + len - 1; name + 2 <= cp; cp--) { |
1281 | 0 | char ch = *cp; |
1282 | 0 | if (!isxdigit(ch)) { |
1283 | | /* We must be looking at g in "SOMETHING-g" |
1284 | | * for it to be describe output. |
1285 | | */ |
1286 | 0 | if (ch == 'g' && cp[-1] == '-') { |
1287 | 0 | cp++; |
1288 | 0 | len -= cp - name; |
1289 | 0 | return get_short_oid(r, |
1290 | 0 | cp, len, oid, flags); |
1291 | 0 | } |
1292 | 0 | } |
1293 | 0 | } |
1294 | 0 | return -1; |
1295 | 0 | } |
1296 | | |
1297 | | static enum get_oid_result get_oid_1(struct repository *r, |
1298 | | const char *name, int len, |
1299 | | struct object_id *oid, |
1300 | | unsigned lookup_flags) |
1301 | 0 | { |
1302 | 0 | int ret, has_suffix; |
1303 | 0 | const char *cp; |
1304 | | |
1305 | | /* |
1306 | | * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1". |
1307 | | */ |
1308 | 0 | has_suffix = 0; |
1309 | 0 | for (cp = name + len - 1; name <= cp; cp--) { |
1310 | 0 | int ch = *cp; |
1311 | 0 | if ('0' <= ch && ch <= '9') |
1312 | 0 | continue; |
1313 | 0 | if (ch == '~' || ch == '^') |
1314 | 0 | has_suffix = ch; |
1315 | 0 | break; |
1316 | 0 | } |
1317 | |
|
1318 | 0 | if (has_suffix) { |
1319 | 0 | unsigned int num = 0; |
1320 | 0 | int len1 = cp - name; |
1321 | 0 | cp++; |
1322 | 0 | while (cp < name + len) { |
1323 | 0 | unsigned int digit = *cp++ - '0'; |
1324 | 0 | if (unsigned_mult_overflows(num, 10)) |
1325 | 0 | return MISSING_OBJECT; |
1326 | 0 | num *= 10; |
1327 | 0 | if (unsigned_add_overflows(num, digit)) |
1328 | 0 | return MISSING_OBJECT; |
1329 | 0 | num += digit; |
1330 | 0 | } |
1331 | 0 | if (!num && len1 == len - 1) |
1332 | 0 | num = 1; |
1333 | 0 | else if (num > INT_MAX) |
1334 | 0 | return MISSING_OBJECT; |
1335 | 0 | if (has_suffix == '^') |
1336 | 0 | return get_parent(r, name, len1, oid, num); |
1337 | | /* else if (has_suffix == '~') -- goes without saying */ |
1338 | 0 | return get_nth_ancestor(r, name, len1, oid, num); |
1339 | 0 | } |
1340 | | |
1341 | 0 | ret = peel_onion(r, name, len, oid, lookup_flags); |
1342 | 0 | if (!ret) |
1343 | 0 | return FOUND; |
1344 | | |
1345 | 0 | ret = get_oid_basic(r, name, len, oid, lookup_flags); |
1346 | 0 | if (!ret) |
1347 | 0 | return FOUND; |
1348 | | |
1349 | | /* It could be describe output that is "SOMETHING-gXXXX" */ |
1350 | 0 | ret = get_describe_name(r, name, len, oid); |
1351 | 0 | if (!ret) |
1352 | 0 | return FOUND; |
1353 | | |
1354 | 0 | return get_short_oid(r, name, len, oid, lookup_flags); |
1355 | 0 | } |
1356 | | |
1357 | | /* |
1358 | | * This interprets names like ':/Initial revision of "git"' by searching |
1359 | | * through history and returning the first commit whose message starts |
1360 | | * the given regular expression. |
1361 | | * |
1362 | | * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'. |
1363 | | * |
1364 | | * For a literal '!' character at the beginning of a pattern, you have to repeat |
1365 | | * that, like: ':/!!foo' |
1366 | | * |
1367 | | * For future extension, all other sequences beginning with ':/!' are reserved. |
1368 | | */ |
1369 | | |
1370 | | /* Remember to update object flag allocation in object.h */ |
1371 | 0 | #define ONELINE_SEEN (1u<<20) |
1372 | | |
1373 | | struct handle_one_ref_cb { |
1374 | | struct repository *repo; |
1375 | | struct commit_list **list; |
1376 | | }; |
1377 | | |
1378 | | static int handle_one_ref(const char *path, const char *referent UNUSED, const struct object_id *oid, |
1379 | | int flag UNUSED, |
1380 | | void *cb_data) |
1381 | 0 | { |
1382 | 0 | struct handle_one_ref_cb *cb = cb_data; |
1383 | 0 | struct commit_list **list = cb->list; |
1384 | 0 | struct object *object = parse_object(cb->repo, oid); |
1385 | 0 | if (!object) |
1386 | 0 | return 0; |
1387 | 0 | if (object->type == OBJ_TAG) { |
1388 | 0 | object = deref_tag(cb->repo, object, path, |
1389 | 0 | strlen(path)); |
1390 | 0 | if (!object) |
1391 | 0 | return 0; |
1392 | 0 | } |
1393 | 0 | if (object->type != OBJ_COMMIT) |
1394 | 0 | return 0; |
1395 | 0 | commit_list_insert((struct commit *)object, list); |
1396 | 0 | return 0; |
1397 | 0 | } |
1398 | | |
1399 | | static int get_oid_oneline(struct repository *r, |
1400 | | const char *prefix, struct object_id *oid, |
1401 | | const struct commit_list *list) |
1402 | 0 | { |
1403 | 0 | struct commit_list *copy = NULL; |
1404 | 0 | const struct commit_list *l; |
1405 | 0 | int found = 0; |
1406 | 0 | int negative = 0; |
1407 | 0 | regex_t regex; |
1408 | |
|
1409 | 0 | if (prefix[0] == '!') { |
1410 | 0 | prefix++; |
1411 | |
|
1412 | 0 | if (prefix[0] == '-') { |
1413 | 0 | prefix++; |
1414 | 0 | negative = 1; |
1415 | 0 | } else if (prefix[0] != '!') { |
1416 | 0 | return -1; |
1417 | 0 | } |
1418 | 0 | } |
1419 | | |
1420 | 0 | if (regcomp(®ex, prefix, REG_EXTENDED)) |
1421 | 0 | return -1; |
1422 | | |
1423 | 0 | for (l = list; l; l = l->next) { |
1424 | 0 | l->item->object.flags |= ONELINE_SEEN; |
1425 | 0 | commit_list_insert(l->item, ©); |
1426 | 0 | } |
1427 | 0 | while (copy) { |
1428 | 0 | const char *p, *buf; |
1429 | 0 | struct commit *commit; |
1430 | 0 | int matches; |
1431 | |
|
1432 | 0 | commit = pop_most_recent_commit(©, ONELINE_SEEN); |
1433 | 0 | if (!parse_object(r, &commit->object.oid)) |
1434 | 0 | continue; |
1435 | 0 | buf = repo_get_commit_buffer(r, commit, NULL); |
1436 | 0 | p = strstr(buf, "\n\n"); |
1437 | 0 | matches = negative ^ (p && !regexec(®ex, p + 2, 0, NULL, 0)); |
1438 | 0 | repo_unuse_commit_buffer(r, commit, buf); |
1439 | |
|
1440 | 0 | if (matches) { |
1441 | 0 | oidcpy(oid, &commit->object.oid); |
1442 | 0 | found = 1; |
1443 | 0 | break; |
1444 | 0 | } |
1445 | 0 | } |
1446 | 0 | regfree(®ex); |
1447 | 0 | for (l = list; l; l = l->next) |
1448 | 0 | clear_commit_marks(l->item, ONELINE_SEEN); |
1449 | 0 | free_commit_list(copy); |
1450 | 0 | return found ? 0 : -1; |
1451 | 0 | } |
1452 | | |
1453 | | struct grab_nth_branch_switch_cbdata { |
1454 | | int remaining; |
1455 | | struct strbuf *sb; |
1456 | | }; |
1457 | | |
1458 | | static int grab_nth_branch_switch(struct object_id *ooid UNUSED, |
1459 | | struct object_id *noid UNUSED, |
1460 | | const char *email UNUSED, |
1461 | | timestamp_t timestamp UNUSED, |
1462 | | int tz UNUSED, |
1463 | | const char *message, void *cb_data) |
1464 | 0 | { |
1465 | 0 | struct grab_nth_branch_switch_cbdata *cb = cb_data; |
1466 | 0 | const char *match = NULL, *target = NULL; |
1467 | 0 | size_t len; |
1468 | |
|
1469 | 0 | if (skip_prefix(message, "checkout: moving from ", &match)) |
1470 | 0 | target = strstr(match, " to "); |
1471 | |
|
1472 | 0 | if (!match || !target) |
1473 | 0 | return 0; |
1474 | 0 | if (--(cb->remaining) == 0) { |
1475 | 0 | len = target - match; |
1476 | 0 | strbuf_reset(cb->sb); |
1477 | 0 | strbuf_add(cb->sb, match, len); |
1478 | 0 | return 1; /* we are done */ |
1479 | 0 | } |
1480 | 0 | return 0; |
1481 | 0 | } |
1482 | | |
1483 | | /* |
1484 | | * Parse @{-N} syntax, return the number of characters parsed |
1485 | | * if successful; otherwise signal an error with negative value. |
1486 | | */ |
1487 | | static int interpret_nth_prior_checkout(struct repository *r, |
1488 | | const char *name, int namelen, |
1489 | | struct strbuf *buf) |
1490 | 0 | { |
1491 | 0 | long nth; |
1492 | 0 | int retval; |
1493 | 0 | struct grab_nth_branch_switch_cbdata cb; |
1494 | 0 | const char *brace; |
1495 | 0 | char *num_end; |
1496 | |
|
1497 | 0 | if (namelen < 4) |
1498 | 0 | return -1; |
1499 | 0 | if (name[0] != '@' || name[1] != '{' || name[2] != '-') |
1500 | 0 | return -1; |
1501 | 0 | brace = memchr(name, '}', namelen); |
1502 | 0 | if (!brace) |
1503 | 0 | return -1; |
1504 | 0 | nth = strtol(name + 3, &num_end, 10); |
1505 | 0 | if (num_end != brace) |
1506 | 0 | return -1; |
1507 | 0 | if (nth <= 0) |
1508 | 0 | return -1; |
1509 | 0 | cb.remaining = nth; |
1510 | 0 | cb.sb = buf; |
1511 | |
|
1512 | 0 | retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r), |
1513 | 0 | "HEAD", grab_nth_branch_switch, &cb); |
1514 | 0 | if (0 < retval) { |
1515 | 0 | retval = brace - name + 1; |
1516 | 0 | } else |
1517 | 0 | retval = 0; |
1518 | |
|
1519 | 0 | return retval; |
1520 | 0 | } |
1521 | | |
1522 | | int repo_get_oid_mb(struct repository *r, |
1523 | | const char *name, |
1524 | | struct object_id *oid) |
1525 | 0 | { |
1526 | 0 | struct commit *one, *two; |
1527 | 0 | struct commit_list *mbs = NULL; |
1528 | 0 | struct object_id oid_tmp; |
1529 | 0 | const char *dots; |
1530 | 0 | int st; |
1531 | |
|
1532 | 0 | dots = strstr(name, "..."); |
1533 | 0 | if (!dots) |
1534 | 0 | return repo_get_oid(r, name, oid); |
1535 | 0 | if (dots == name) |
1536 | 0 | st = repo_get_oid(r, "HEAD", &oid_tmp); |
1537 | 0 | else { |
1538 | 0 | struct strbuf sb; |
1539 | 0 | strbuf_init(&sb, dots - name); |
1540 | 0 | strbuf_add(&sb, name, dots - name); |
1541 | 0 | st = repo_get_oid_committish(r, sb.buf, &oid_tmp); |
1542 | 0 | strbuf_release(&sb); |
1543 | 0 | } |
1544 | 0 | if (st) |
1545 | 0 | return st; |
1546 | 0 | one = lookup_commit_reference_gently(r, &oid_tmp, 0); |
1547 | 0 | if (!one) |
1548 | 0 | return -1; |
1549 | | |
1550 | 0 | if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp)) |
1551 | 0 | return -1; |
1552 | 0 | two = lookup_commit_reference_gently(r, &oid_tmp, 0); |
1553 | 0 | if (!two) |
1554 | 0 | return -1; |
1555 | 0 | if (repo_get_merge_bases(r, one, two, &mbs) < 0) { |
1556 | 0 | free_commit_list(mbs); |
1557 | 0 | return -1; |
1558 | 0 | } |
1559 | 0 | if (!mbs || mbs->next) |
1560 | 0 | st = -1; |
1561 | 0 | else { |
1562 | 0 | st = 0; |
1563 | 0 | oidcpy(oid, &mbs->item->object.oid); |
1564 | 0 | } |
1565 | 0 | free_commit_list(mbs); |
1566 | 0 | return st; |
1567 | 0 | } |
1568 | | |
1569 | | /* parse @something syntax, when 'something' is not {.*} */ |
1570 | | static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf) |
1571 | 0 | { |
1572 | 0 | const char *next; |
1573 | |
|
1574 | 0 | if (len || name[1] == '{') |
1575 | 0 | return -1; |
1576 | | |
1577 | | /* make sure it's a single @, or @@{.*}, not @foo */ |
1578 | 0 | next = memchr(name + len + 1, '@', namelen - len - 1); |
1579 | 0 | if (next && next[1] != '{') |
1580 | 0 | return -1; |
1581 | 0 | if (!next) |
1582 | 0 | next = name + namelen; |
1583 | 0 | if (next != name + 1) |
1584 | 0 | return -1; |
1585 | | |
1586 | 0 | strbuf_reset(buf); |
1587 | 0 | strbuf_add(buf, "HEAD", 4); |
1588 | 0 | return 1; |
1589 | 0 | } |
1590 | | |
1591 | | static int reinterpret(struct repository *r, |
1592 | | const char *name, int namelen, int len, |
1593 | | struct strbuf *buf, unsigned allowed) |
1594 | 0 | { |
1595 | | /* we have extra data, which might need further processing */ |
1596 | 0 | struct strbuf tmp = STRBUF_INIT; |
1597 | 0 | int used = buf->len; |
1598 | 0 | int ret; |
1599 | 0 | struct interpret_branch_name_options options = { |
1600 | 0 | .allowed = allowed |
1601 | 0 | }; |
1602 | |
|
1603 | 0 | strbuf_add(buf, name + len, namelen - len); |
1604 | 0 | ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options); |
1605 | | /* that data was not interpreted, remove our cruft */ |
1606 | 0 | if (ret < 0) { |
1607 | 0 | strbuf_setlen(buf, used); |
1608 | 0 | return len; |
1609 | 0 | } |
1610 | 0 | strbuf_reset(buf); |
1611 | 0 | strbuf_addbuf(buf, &tmp); |
1612 | 0 | strbuf_release(&tmp); |
1613 | | /* tweak for size of {-N} versus expanded ref name */ |
1614 | 0 | return ret - used + len; |
1615 | 0 | } |
1616 | | |
1617 | | static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref) |
1618 | 0 | { |
1619 | 0 | char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0); |
1620 | 0 | strbuf_reset(buf); |
1621 | 0 | strbuf_addstr(buf, s); |
1622 | 0 | free(s); |
1623 | 0 | } |
1624 | | |
1625 | | static int branch_interpret_allowed(const char *refname, unsigned allowed) |
1626 | 0 | { |
1627 | 0 | if (!allowed) |
1628 | 0 | return 1; |
1629 | | |
1630 | 0 | if ((allowed & INTERPRET_BRANCH_LOCAL) && |
1631 | 0 | starts_with(refname, "refs/heads/")) |
1632 | 0 | return 1; |
1633 | 0 | if ((allowed & INTERPRET_BRANCH_REMOTE) && |
1634 | 0 | starts_with(refname, "refs/remotes/")) |
1635 | 0 | return 1; |
1636 | | |
1637 | 0 | return 0; |
1638 | 0 | } |
1639 | | |
1640 | | static int interpret_branch_mark(struct repository *r, |
1641 | | const char *name, int namelen, |
1642 | | int at, struct strbuf *buf, |
1643 | | int (*get_mark)(const char *, int), |
1644 | | const char *(*get_data)(struct branch *, |
1645 | | struct strbuf *), |
1646 | | const struct interpret_branch_name_options *options) |
1647 | 0 | { |
1648 | 0 | int len; |
1649 | 0 | struct branch *branch; |
1650 | 0 | struct strbuf err = STRBUF_INIT; |
1651 | 0 | const char *value; |
1652 | |
|
1653 | 0 | len = get_mark(name + at, namelen - at); |
1654 | 0 | if (!len) |
1655 | 0 | return -1; |
1656 | | |
1657 | 0 | if (memchr(name, ':', at)) |
1658 | 0 | return -1; |
1659 | | |
1660 | 0 | if (at) { |
1661 | 0 | char *name_str = xmemdupz(name, at); |
1662 | 0 | branch = branch_get(name_str); |
1663 | 0 | free(name_str); |
1664 | 0 | } else |
1665 | 0 | branch = branch_get(NULL); |
1666 | |
|
1667 | 0 | value = get_data(branch, &err); |
1668 | 0 | if (!value) { |
1669 | 0 | if (options->nonfatal_dangling_mark) { |
1670 | 0 | strbuf_release(&err); |
1671 | 0 | return -1; |
1672 | 0 | } else { |
1673 | 0 | die("%s", err.buf); |
1674 | 0 | } |
1675 | 0 | } |
1676 | | |
1677 | 0 | if (!branch_interpret_allowed(value, options->allowed)) |
1678 | 0 | return -1; |
1679 | | |
1680 | 0 | set_shortened_ref(r, buf, value); |
1681 | 0 | return len + at; |
1682 | 0 | } |
1683 | | |
1684 | | int repo_interpret_branch_name(struct repository *r, |
1685 | | const char *name, int namelen, |
1686 | | struct strbuf *buf, |
1687 | | const struct interpret_branch_name_options *options) |
1688 | 0 | { |
1689 | 0 | char *at; |
1690 | 0 | const char *start; |
1691 | 0 | int len; |
1692 | |
|
1693 | 0 | if (!namelen) |
1694 | 0 | namelen = strlen(name); |
1695 | |
|
1696 | 0 | if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) { |
1697 | 0 | len = interpret_nth_prior_checkout(r, name, namelen, buf); |
1698 | 0 | if (!len) { |
1699 | 0 | return len; /* syntax Ok, not enough switches */ |
1700 | 0 | } else if (len > 0) { |
1701 | 0 | if (len == namelen) |
1702 | 0 | return len; /* consumed all */ |
1703 | 0 | else |
1704 | 0 | return reinterpret(r, name, namelen, len, buf, |
1705 | 0 | options->allowed); |
1706 | 0 | } |
1707 | 0 | } |
1708 | | |
1709 | 0 | for (start = name; |
1710 | 0 | (at = memchr(start, '@', namelen - (start - name))); |
1711 | 0 | start = at + 1) { |
1712 | |
|
1713 | 0 | if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) { |
1714 | 0 | len = interpret_empty_at(name, namelen, at - name, buf); |
1715 | 0 | if (len > 0) |
1716 | 0 | return reinterpret(r, name, namelen, len, buf, |
1717 | 0 | options->allowed); |
1718 | 0 | } |
1719 | | |
1720 | 0 | len = interpret_branch_mark(r, name, namelen, at - name, buf, |
1721 | 0 | upstream_mark, branch_get_upstream, |
1722 | 0 | options); |
1723 | 0 | if (len > 0) |
1724 | 0 | return len; |
1725 | | |
1726 | 0 | len = interpret_branch_mark(r, name, namelen, at - name, buf, |
1727 | 0 | push_mark, branch_get_push, |
1728 | 0 | options); |
1729 | 0 | if (len > 0) |
1730 | 0 | return len; |
1731 | 0 | } |
1732 | | |
1733 | 0 | return -1; |
1734 | 0 | } |
1735 | | |
1736 | | void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) |
1737 | 0 | { |
1738 | 0 | int len = strlen(name); |
1739 | 0 | struct interpret_branch_name_options options = { |
1740 | 0 | .allowed = allowed |
1741 | 0 | }; |
1742 | 0 | int used = repo_interpret_branch_name(the_repository, name, len, sb, |
1743 | 0 | &options); |
1744 | |
|
1745 | 0 | if (used < 0) |
1746 | 0 | used = 0; |
1747 | 0 | strbuf_add(sb, name + used, len - used); |
1748 | 0 | } |
1749 | | |
1750 | | int strbuf_check_branch_ref(struct strbuf *sb, const char *name) |
1751 | 0 | { |
1752 | 0 | if (startup_info->have_repository) |
1753 | 0 | strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); |
1754 | 0 | else |
1755 | 0 | strbuf_addstr(sb, name); |
1756 | | |
1757 | | /* |
1758 | | * This splice must be done even if we end up rejecting the |
1759 | | * name; builtin/branch.c::copy_or_rename_branch() still wants |
1760 | | * to see what the name expanded to so that "branch -m" can be |
1761 | | * used as a tool to correct earlier mistakes. |
1762 | | */ |
1763 | 0 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
1764 | |
|
1765 | 0 | if (*name == '-' || |
1766 | 0 | !strcmp(sb->buf, "refs/heads/HEAD")) |
1767 | 0 | return -1; |
1768 | | |
1769 | 0 | return check_refname_format(sb->buf, 0); |
1770 | 0 | } |
1771 | | |
1772 | | void object_context_release(struct object_context *ctx) |
1773 | 0 | { |
1774 | 0 | free(ctx->path); |
1775 | 0 | strbuf_release(&ctx->symlink_path); |
1776 | 0 | } |
1777 | | |
1778 | | /* |
1779 | | * This is like "get_oid_basic()", except it allows "object ID expressions", |
1780 | | * notably "xyz^" for "parent of xyz" |
1781 | | */ |
1782 | | int repo_get_oid(struct repository *r, const char *name, struct object_id *oid) |
1783 | 0 | { |
1784 | 0 | struct object_context unused; |
1785 | 0 | int ret = get_oid_with_context(r, name, 0, oid, &unused); |
1786 | 0 | object_context_release(&unused); |
1787 | 0 | return ret; |
1788 | 0 | } |
1789 | | |
1790 | | /* |
1791 | | * This returns a non-zero value if the string (built using printf |
1792 | | * format and the given arguments) is not a valid object. |
1793 | | */ |
1794 | | int get_oidf(struct object_id *oid, const char *fmt, ...) |
1795 | 0 | { |
1796 | 0 | va_list ap; |
1797 | 0 | int ret; |
1798 | 0 | struct strbuf sb = STRBUF_INIT; |
1799 | |
|
1800 | 0 | va_start(ap, fmt); |
1801 | 0 | strbuf_vaddf(&sb, fmt, ap); |
1802 | 0 | va_end(ap); |
1803 | |
|
1804 | 0 | ret = repo_get_oid(the_repository, sb.buf, oid); |
1805 | 0 | strbuf_release(&sb); |
1806 | |
|
1807 | 0 | return ret; |
1808 | 0 | } |
1809 | | |
1810 | | /* |
1811 | | * Many callers know that the user meant to name a commit-ish by |
1812 | | * syntactical positions where the object name appears. Calling this |
1813 | | * function allows the machinery to disambiguate shorter-than-unique |
1814 | | * abbreviated object names between commit-ish and others. |
1815 | | * |
1816 | | * Note that this does NOT error out when the named object is not a |
1817 | | * commit-ish. It is merely to give a hint to the disambiguation |
1818 | | * machinery. |
1819 | | */ |
1820 | | int repo_get_oid_committish(struct repository *r, |
1821 | | const char *name, |
1822 | | struct object_id *oid) |
1823 | 0 | { |
1824 | 0 | struct object_context unused; |
1825 | 0 | int ret = get_oid_with_context(r, name, GET_OID_COMMITTISH, |
1826 | 0 | oid, &unused); |
1827 | 0 | object_context_release(&unused); |
1828 | 0 | return ret; |
1829 | 0 | } |
1830 | | |
1831 | | int repo_get_oid_treeish(struct repository *r, |
1832 | | const char *name, |
1833 | | struct object_id *oid) |
1834 | 0 | { |
1835 | 0 | struct object_context unused; |
1836 | 0 | int ret = get_oid_with_context(r, name, GET_OID_TREEISH, |
1837 | 0 | oid, &unused); |
1838 | 0 | object_context_release(&unused); |
1839 | 0 | return ret; |
1840 | 0 | } |
1841 | | |
1842 | | int repo_get_oid_commit(struct repository *r, |
1843 | | const char *name, |
1844 | | struct object_id *oid) |
1845 | 0 | { |
1846 | 0 | struct object_context unused; |
1847 | 0 | int ret = get_oid_with_context(r, name, GET_OID_COMMIT, |
1848 | 0 | oid, &unused); |
1849 | 0 | object_context_release(&unused); |
1850 | 0 | return ret; |
1851 | 0 | } |
1852 | | |
1853 | | int repo_get_oid_tree(struct repository *r, |
1854 | | const char *name, |
1855 | | struct object_id *oid) |
1856 | 0 | { |
1857 | 0 | struct object_context unused; |
1858 | 0 | int ret = get_oid_with_context(r, name, GET_OID_TREE, |
1859 | 0 | oid, &unused); |
1860 | 0 | object_context_release(&unused); |
1861 | 0 | return ret; |
1862 | 0 | } |
1863 | | |
1864 | | int repo_get_oid_blob(struct repository *r, |
1865 | | const char *name, |
1866 | | struct object_id *oid) |
1867 | 0 | { |
1868 | 0 | struct object_context unused; |
1869 | 0 | int ret = get_oid_with_context(r, name, GET_OID_BLOB, |
1870 | 0 | oid, &unused); |
1871 | 0 | object_context_release(&unused); |
1872 | 0 | return ret; |
1873 | 0 | } |
1874 | | |
1875 | | /* Must be called only when object_name:filename doesn't exist. */ |
1876 | | static void diagnose_invalid_oid_path(struct repository *r, |
1877 | | const char *prefix, |
1878 | | const char *filename, |
1879 | | const struct object_id *tree_oid, |
1880 | | const char *object_name, |
1881 | | int object_name_len) |
1882 | 0 | { |
1883 | 0 | struct object_id oid; |
1884 | 0 | unsigned short mode; |
1885 | |
|
1886 | 0 | if (!prefix) |
1887 | 0 | prefix = ""; |
1888 | |
|
1889 | 0 | if (file_exists(filename)) |
1890 | 0 | die(_("path '%s' exists on disk, but not in '%.*s'"), |
1891 | 0 | filename, object_name_len, object_name); |
1892 | 0 | if (is_missing_file_error(errno)) { |
1893 | 0 | char *fullname = xstrfmt("%s%s", prefix, filename); |
1894 | |
|
1895 | 0 | if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) { |
1896 | 0 | die(_("path '%s' exists, but not '%s'\n" |
1897 | 0 | "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"), |
1898 | 0 | fullname, |
1899 | 0 | filename, |
1900 | 0 | object_name_len, object_name, |
1901 | 0 | fullname, |
1902 | 0 | object_name_len, object_name, |
1903 | 0 | filename); |
1904 | 0 | } |
1905 | 0 | die(_("path '%s' does not exist in '%.*s'"), |
1906 | 0 | filename, object_name_len, object_name); |
1907 | 0 | } |
1908 | 0 | } |
1909 | | |
1910 | | /* Must be called only when :stage:filename doesn't exist. */ |
1911 | | static void diagnose_invalid_index_path(struct repository *r, |
1912 | | int stage, |
1913 | | const char *prefix, |
1914 | | const char *filename) |
1915 | 0 | { |
1916 | 0 | struct index_state *istate = r->index; |
1917 | 0 | const struct cache_entry *ce; |
1918 | 0 | int pos; |
1919 | 0 | unsigned namelen = strlen(filename); |
1920 | 0 | struct strbuf fullname = STRBUF_INIT; |
1921 | |
|
1922 | 0 | if (!prefix) |
1923 | 0 | prefix = ""; |
1924 | | |
1925 | | /* Wrong stage number? */ |
1926 | 0 | pos = index_name_pos(istate, filename, namelen); |
1927 | 0 | if (pos < 0) |
1928 | 0 | pos = -pos - 1; |
1929 | 0 | if (pos < istate->cache_nr) { |
1930 | 0 | ce = istate->cache[pos]; |
1931 | 0 | if (!S_ISSPARSEDIR(ce->ce_mode) && |
1932 | 0 | ce_namelen(ce) == namelen && |
1933 | 0 | !memcmp(ce->name, filename, namelen)) |
1934 | 0 | die(_("path '%s' is in the index, but not at stage %d\n" |
1935 | 0 | "hint: Did you mean ':%d:%s'?"), |
1936 | 0 | filename, stage, |
1937 | 0 | ce_stage(ce), filename); |
1938 | 0 | } |
1939 | | |
1940 | | /* Confusion between relative and absolute filenames? */ |
1941 | 0 | strbuf_addstr(&fullname, prefix); |
1942 | 0 | strbuf_addstr(&fullname, filename); |
1943 | 0 | pos = index_name_pos(istate, fullname.buf, fullname.len); |
1944 | 0 | if (pos < 0) |
1945 | 0 | pos = -pos - 1; |
1946 | 0 | if (pos < istate->cache_nr) { |
1947 | 0 | ce = istate->cache[pos]; |
1948 | 0 | if (!S_ISSPARSEDIR(ce->ce_mode) && |
1949 | 0 | ce_namelen(ce) == fullname.len && |
1950 | 0 | !memcmp(ce->name, fullname.buf, fullname.len)) |
1951 | 0 | die(_("path '%s' is in the index, but not '%s'\n" |
1952 | 0 | "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"), |
1953 | 0 | fullname.buf, filename, |
1954 | 0 | ce_stage(ce), fullname.buf, |
1955 | 0 | ce_stage(ce), filename); |
1956 | 0 | } |
1957 | | |
1958 | 0 | if (repo_file_exists(r, filename)) |
1959 | 0 | die(_("path '%s' exists on disk, but not in the index"), filename); |
1960 | 0 | if (is_missing_file_error(errno)) |
1961 | 0 | die(_("path '%s' does not exist (neither on disk nor in the index)"), |
1962 | 0 | filename); |
1963 | | |
1964 | 0 | strbuf_release(&fullname); |
1965 | 0 | } |
1966 | | |
1967 | | |
1968 | | static char *resolve_relative_path(struct repository *r, const char *rel) |
1969 | 0 | { |
1970 | 0 | if (!starts_with(rel, "./") && !starts_with(rel, "../")) |
1971 | 0 | return NULL; |
1972 | | |
1973 | 0 | if (r != the_repository || !is_inside_work_tree()) |
1974 | 0 | die(_("relative path syntax can't be used outside working tree")); |
1975 | | |
1976 | | /* die() inside prefix_path() if resolved path is outside worktree */ |
1977 | 0 | return prefix_path(startup_info->prefix, |
1978 | 0 | startup_info->prefix ? strlen(startup_info->prefix) : 0, |
1979 | 0 | rel); |
1980 | 0 | } |
1981 | | |
1982 | | static int reject_tree_in_index(struct repository *repo, |
1983 | | int only_to_die, |
1984 | | const struct cache_entry *ce, |
1985 | | int stage, |
1986 | | const char *prefix, |
1987 | | const char *cp) |
1988 | 0 | { |
1989 | 0 | if (!S_ISSPARSEDIR(ce->ce_mode)) |
1990 | 0 | return 0; |
1991 | 0 | if (only_to_die) |
1992 | 0 | diagnose_invalid_index_path(repo, stage, prefix, cp); |
1993 | 0 | return -1; |
1994 | 0 | } |
1995 | | |
1996 | | static enum get_oid_result get_oid_with_context_1(struct repository *repo, |
1997 | | const char *name, |
1998 | | unsigned flags, |
1999 | | const char *prefix, |
2000 | | struct object_id *oid, |
2001 | | struct object_context *oc) |
2002 | 0 | { |
2003 | 0 | int ret, bracket_depth; |
2004 | 0 | int namelen = strlen(name); |
2005 | 0 | const char *cp; |
2006 | 0 | int only_to_die = flags & GET_OID_ONLY_TO_DIE; |
2007 | |
|
2008 | 0 | memset(oc, 0, sizeof(*oc)); |
2009 | 0 | oc->mode = S_IFINVALID; |
2010 | 0 | strbuf_init(&oc->symlink_path, 0); |
2011 | 0 | ret = get_oid_1(repo, name, namelen, oid, flags); |
2012 | 0 | if (!ret && flags & GET_OID_REQUIRE_PATH) |
2013 | 0 | die(_("<object>:<path> required, only <object> '%s' given"), |
2014 | 0 | name); |
2015 | 0 | if (!ret) |
2016 | 0 | return ret; |
2017 | | /* |
2018 | | * tree:path --> object name of path in tree |
2019 | | * :path -> object name of absolute path in index |
2020 | | * :./path -> object name of path relative to cwd in index |
2021 | | * :[0-3]:path -> object name of path in index at stage |
2022 | | * :/foo -> recent commit matching foo |
2023 | | */ |
2024 | 0 | if (name[0] == ':') { |
2025 | 0 | int stage = 0; |
2026 | 0 | const struct cache_entry *ce; |
2027 | 0 | char *new_path = NULL; |
2028 | 0 | int pos; |
2029 | 0 | if (!only_to_die && namelen > 2 && name[1] == '/') { |
2030 | 0 | struct handle_one_ref_cb cb; |
2031 | 0 | struct commit_list *list = NULL; |
2032 | |
|
2033 | 0 | cb.repo = repo; |
2034 | 0 | cb.list = &list; |
2035 | 0 | refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb); |
2036 | 0 | refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb); |
2037 | 0 | commit_list_sort_by_date(&list); |
2038 | 0 | ret = get_oid_oneline(repo, name + 2, oid, list); |
2039 | |
|
2040 | 0 | free_commit_list(list); |
2041 | 0 | return ret; |
2042 | 0 | } |
2043 | 0 | if (namelen < 3 || |
2044 | 0 | name[2] != ':' || |
2045 | 0 | name[1] < '0' || '3' < name[1]) |
2046 | 0 | cp = name + 1; |
2047 | 0 | else { |
2048 | 0 | stage = name[1] - '0'; |
2049 | 0 | cp = name + 3; |
2050 | 0 | } |
2051 | 0 | new_path = resolve_relative_path(repo, cp); |
2052 | 0 | if (!new_path) { |
2053 | 0 | namelen = namelen - (cp - name); |
2054 | 0 | } else { |
2055 | 0 | cp = new_path; |
2056 | 0 | namelen = strlen(cp); |
2057 | 0 | } |
2058 | |
|
2059 | 0 | if (flags & GET_OID_RECORD_PATH) |
2060 | 0 | oc->path = xstrdup(cp); |
2061 | |
|
2062 | 0 | if (!repo->index || !repo->index->cache) |
2063 | 0 | repo_read_index(repo); |
2064 | 0 | pos = index_name_pos(repo->index, cp, namelen); |
2065 | 0 | if (pos < 0) |
2066 | 0 | pos = -pos - 1; |
2067 | 0 | while (pos < repo->index->cache_nr) { |
2068 | 0 | ce = repo->index->cache[pos]; |
2069 | 0 | if (ce_namelen(ce) != namelen || |
2070 | 0 | memcmp(ce->name, cp, namelen)) |
2071 | 0 | break; |
2072 | 0 | if (ce_stage(ce) == stage) { |
2073 | 0 | free(new_path); |
2074 | 0 | if (reject_tree_in_index(repo, only_to_die, ce, |
2075 | 0 | stage, prefix, cp)) |
2076 | 0 | return -1; |
2077 | 0 | oidcpy(oid, &ce->oid); |
2078 | 0 | oc->mode = ce->ce_mode; |
2079 | 0 | return 0; |
2080 | 0 | } |
2081 | 0 | pos++; |
2082 | 0 | } |
2083 | 0 | if (only_to_die && name[1] && name[1] != '/') |
2084 | 0 | diagnose_invalid_index_path(repo, stage, prefix, cp); |
2085 | 0 | free(new_path); |
2086 | 0 | return -1; |
2087 | 0 | } |
2088 | 0 | for (cp = name, bracket_depth = 0; *cp; cp++) { |
2089 | 0 | if (*cp == '{') |
2090 | 0 | bracket_depth++; |
2091 | 0 | else if (bracket_depth && *cp == '}') |
2092 | 0 | bracket_depth--; |
2093 | 0 | else if (!bracket_depth && *cp == ':') |
2094 | 0 | break; |
2095 | 0 | } |
2096 | 0 | if (*cp == ':') { |
2097 | 0 | struct object_id tree_oid; |
2098 | 0 | int len = cp - name; |
2099 | 0 | unsigned sub_flags = flags; |
2100 | |
|
2101 | 0 | sub_flags &= ~GET_OID_DISAMBIGUATORS; |
2102 | 0 | sub_flags |= GET_OID_TREEISH; |
2103 | |
|
2104 | 0 | if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) { |
2105 | 0 | const char *filename = cp+1; |
2106 | 0 | char *new_filename = NULL; |
2107 | |
|
2108 | 0 | new_filename = resolve_relative_path(repo, filename); |
2109 | 0 | if (new_filename) |
2110 | 0 | filename = new_filename; |
2111 | 0 | if (flags & GET_OID_FOLLOW_SYMLINKS) { |
2112 | 0 | ret = get_tree_entry_follow_symlinks(repo, &tree_oid, |
2113 | 0 | filename, oid, &oc->symlink_path, |
2114 | 0 | &oc->mode); |
2115 | 0 | } else { |
2116 | 0 | ret = get_tree_entry(repo, &tree_oid, filename, oid, |
2117 | 0 | &oc->mode); |
2118 | 0 | if (ret && only_to_die) { |
2119 | 0 | diagnose_invalid_oid_path(repo, prefix, |
2120 | 0 | filename, |
2121 | 0 | &tree_oid, |
2122 | 0 | name, len); |
2123 | 0 | } |
2124 | 0 | } |
2125 | 0 | if (flags & GET_OID_RECORD_PATH) |
2126 | 0 | oc->path = xstrdup(filename); |
2127 | |
|
2128 | 0 | free(new_filename); |
2129 | 0 | return ret; |
2130 | 0 | } else { |
2131 | 0 | if (only_to_die) |
2132 | 0 | die(_("invalid object name '%.*s'."), len, name); |
2133 | 0 | } |
2134 | 0 | } |
2135 | 0 | return ret; |
2136 | 0 | } |
2137 | | |
2138 | | /* |
2139 | | * Call this function when you know "name" given by the end user must |
2140 | | * name an object but it doesn't; the function _may_ die with a better |
2141 | | * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not |
2142 | | * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case |
2143 | | * you have a chance to diagnose the error further. |
2144 | | */ |
2145 | | void maybe_die_on_misspelt_object_name(struct repository *r, |
2146 | | const char *name, |
2147 | | const char *prefix) |
2148 | 0 | { |
2149 | 0 | struct object_context oc; |
2150 | 0 | struct object_id oid; |
2151 | 0 | get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY, |
2152 | 0 | prefix, &oid, &oc); |
2153 | 0 | object_context_release(&oc); |
2154 | 0 | } |
2155 | | |
2156 | | enum get_oid_result get_oid_with_context(struct repository *repo, |
2157 | | const char *str, |
2158 | | unsigned flags, |
2159 | | struct object_id *oid, |
2160 | | struct object_context *oc) |
2161 | 0 | { |
2162 | 0 | if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE) |
2163 | 0 | BUG("incompatible flags for get_oid_with_context"); |
2164 | 0 | return get_oid_with_context_1(repo, str, flags, NULL, oid, oc); |
2165 | 0 | } |