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