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