Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/name-rev.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / name-rev.c` 940 lines ยท 24404 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "builtin.h" 5 #include "environment.h" 6 #include "gettext.h" 7 #include "hex.h" 8 #include "config.h" 9 #include "commit.h" 10 #include "tag.h" 11 #include "refs.h" 12 #include "object-name.h" 13 #include "pager.h" 14 #include "parse-options.h" 15 #include "hash-lookup.h" 16 #include "commit-slab.h" 17 #include "commit-graph.h" 18 #include "wildmatch.h" 19 #include "mem-pool.h" 20 #include "pretty.h" 21 #include "revision.h" 22 #include "notes.h" 23 #include "write-or-die.h" 24 25 /* 26 * One day. See the 'name a rev shortly after epoch' test in t6120 when 27 * changing this value 28 */ 29 #define CUTOFF_DATE_SLOP 86400 30 31 struct rev_name { 32 const char *tip_name; 33 timestamp_t taggerdate; 34 int generation; 35 int distance; 36 int from_tag; 37 }; 38 39 define_commit_slab(commit_rev_name, struct rev_name); 40 41 static timestamp_t generation_cutoff = GENERATION_NUMBER_INFINITY; 42 static timestamp_t cutoff = TIME_MAX; 43 static struct commit_rev_name rev_names; 44 45 /* Disable the cutoff checks entirely */ 46 static void disable_cutoff(void) 47 { 48 generation_cutoff = 0; 49 cutoff = 0; 50 } 51 52 /* Cutoff searching any commits older than this one */ 53 static void set_commit_cutoff(struct commit *commit) 54 { 55 56 if (cutoff > commit->date) 57 cutoff = commit->date; 58 59 if (generation_cutoff) { 60 timestamp_t generation = commit_graph_generation(commit); 61 62 if (generation_cutoff > generation) 63 generation_cutoff = generation; 64 } 65 } 66 67 /* adjust the commit date cutoff with a slop to allow for slightly incorrect 68 * commit timestamps in case of clock skew. 69 */ 70 static void adjust_cutoff_timestamp_for_slop(void) 71 { 72 if (cutoff) { 73 /* check for underflow */ 74 if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP) 75 cutoff = cutoff - CUTOFF_DATE_SLOP; 76 else 77 cutoff = TIME_MIN; 78 } 79 } 80 81 /* Check if a commit is before the cutoff. Prioritize generation numbers 82 * first, but use the commit timestamp if we lack generation data. 83 */ 84 static int commit_is_before_cutoff(struct commit *commit) 85 { 86 if (generation_cutoff < GENERATION_NUMBER_INFINITY) 87 return generation_cutoff && 88 commit_graph_generation(commit) < generation_cutoff; 89 90 return commit->date < cutoff; 91 } 92 93 /* How many generations are maximally preferred over _one_ merge traversal? */ 94 #define MERGE_TRAVERSAL_WEIGHT 65535 95 96 static int is_valid_rev_name(const struct rev_name *name) 97 { 98 return name && name->tip_name; 99 } 100 101 static struct rev_name *get_commit_rev_name(const struct commit *commit) 102 { 103 struct rev_name *name = commit_rev_name_peek(&rev_names, commit); 104 105 return is_valid_rev_name(name) ? name : NULL; 106 } 107 108 static int effective_distance(int distance, int generation) 109 { 110 return distance + (generation > 0 ? MERGE_TRAVERSAL_WEIGHT : 0); 111 } 112 113 static int is_better_name(struct rev_name *name, 114 timestamp_t taggerdate, 115 int generation, 116 int distance, 117 int from_tag) 118 { 119 int name_distance = effective_distance(name->distance, name->generation); 120 int new_distance = effective_distance(distance, generation); 121 122 /* If both are tags, we prefer the nearer one. */ 123 if (from_tag && name->from_tag) 124 return name_distance > new_distance; 125 126 /* Favor a tag over a non-tag. */ 127 if (name->from_tag != from_tag) 128 return from_tag; 129 130 /* 131 * We are now looking at two non-tags. Tiebreak to favor 132 * shorter hops. 133 */ 134 if (name_distance != new_distance) 135 return name_distance > new_distance; 136 137 /* ... or tiebreak to favor older date */ 138 if (name->taggerdate != taggerdate) 139 return name->taggerdate > taggerdate; 140 141 /* keep the current one if we cannot decide */ 142 return 0; 143 } 144 145 static struct rev_name *create_or_update_name(struct commit *commit, 146 timestamp_t taggerdate, 147 int generation, int distance, 148 int from_tag) 149 { 150 struct rev_name *name = commit_rev_name_at(&rev_names, commit); 151 152 if (is_valid_rev_name(name) && 153 !is_better_name(name, taggerdate, generation, distance, from_tag)) 154 return NULL; 155 156 name->taggerdate = taggerdate; 157 name->generation = generation; 158 name->distance = distance; 159 name->from_tag = from_tag; 160 161 return name; 162 } 163 164 static char *get_parent_name(const struct rev_name *name, int parent_number, 165 struct mem_pool *string_pool) 166 { 167 size_t len; 168 169 strip_suffix(name->tip_name, "^0", &len); 170 if (name->generation > 0) { 171 return mem_pool_strfmt(string_pool, "%.*s~%d^%d", 172 (int)len, name->tip_name, 173 name->generation, parent_number); 174 } else { 175 return mem_pool_strfmt(string_pool, "%.*s^%d", 176 (int)len, name->tip_name, parent_number); 177 } 178 } 179 180 static void name_rev(struct commit *start_commit, 181 const char *tip_name, timestamp_t taggerdate, 182 int from_tag, int deref, struct mem_pool *string_pool) 183 { 184 struct commit_stack stack = COMMIT_STACK_INIT; 185 struct commit *commit; 186 struct commit_stack parents_to_queue = COMMIT_STACK_INIT; 187 struct rev_name *start_name; 188 189 repo_parse_commit(the_repository, start_commit); 190 if (commit_is_before_cutoff(start_commit)) 191 return; 192 193 start_name = create_or_update_name(start_commit, taggerdate, 0, 0, 194 from_tag); 195 if (!start_name) 196 return; 197 if (deref) 198 start_name->tip_name = mem_pool_strfmt(string_pool, "%s^0", 199 tip_name); 200 else 201 start_name->tip_name = mem_pool_strdup(string_pool, tip_name); 202 203 commit_stack_push(&stack, start_commit); 204 205 while ((commit = commit_stack_pop(&stack))) { 206 struct rev_name *name = get_commit_rev_name(commit); 207 struct commit_list *parents; 208 int parent_number = 1; 209 210 parents_to_queue.nr = 0; 211 212 for (parents = commit->parents; 213 parents; 214 parents = parents->next, parent_number++) { 215 struct commit *parent = parents->item; 216 struct rev_name *parent_name; 217 int generation, distance; 218 219 repo_parse_commit(the_repository, parent); 220 if (commit_is_before_cutoff(parent)) 221 continue; 222 223 if (parent_number > 1) { 224 generation = 0; 225 distance = name->distance + MERGE_TRAVERSAL_WEIGHT; 226 } else { 227 generation = name->generation + 1; 228 distance = name->distance + 1; 229 } 230 231 parent_name = create_or_update_name(parent, taggerdate, 232 generation, 233 distance, from_tag); 234 if (parent_name) { 235 if (parent_number > 1) 236 parent_name->tip_name = 237 get_parent_name(name, 238 parent_number, 239 string_pool); 240 else 241 parent_name->tip_name = name->tip_name; 242 commit_stack_push(&parents_to_queue, parent); 243 } 244 } 245 246 /* The first parent must come out first from the stack */ 247 while (parents_to_queue.nr) 248 commit_stack_push(&stack, 249 commit_stack_pop(&parents_to_queue)); 250 } 251 252 commit_stack_clear(&stack); 253 commit_stack_clear(&parents_to_queue); 254 } 255 256 static int subpath_matches(const char *path, const char *filter) 257 { 258 const char *subpath = path; 259 260 while (subpath) { 261 if (!wildmatch(filter, subpath, 0)) 262 return subpath - path; 263 subpath = strchr(subpath, '/'); 264 if (subpath) 265 subpath++; 266 } 267 return -1; 268 } 269 270 struct name_ref_data { 271 int tags_only; 272 int name_only; 273 struct string_list ref_filters; 274 struct string_list exclude_filters; 275 }; 276 277 struct pretty_format { 278 struct pretty_print_context ctx; 279 struct userformat_want want; 280 }; 281 282 enum command_type { 283 NAME_REV = 1, 284 FORMAT_REV = 2, 285 }; 286 287 enum stdin_mode { 288 TEXT = 1, 289 REVS = 2, 290 }; 291 292 struct command { 293 enum command_type type; 294 union { 295 int name_only; 296 struct pretty_format *pretty_format; 297 } u; 298 }; 299 300 static void init_name_rev_command(struct command *cmd, 301 int name_only) 302 { 303 cmd->type = NAME_REV; 304 cmd->u.name_only = name_only; 305 } 306 307 static void init_format_rev_command(struct command *cmd, 308 struct pretty_format *pretty_format) 309 { 310 cmd->type = FORMAT_REV; 311 cmd->u.pretty_format = pretty_format; 312 } 313 314 static struct tip_table { 315 struct tip_table_entry { 316 struct object_id oid; 317 const char *refname; 318 struct commit *commit; 319 timestamp_t taggerdate; 320 unsigned int from_tag:1; 321 unsigned int deref:1; 322 } *table; 323 int nr; 324 int alloc; 325 int sorted; 326 } tip_table; 327 328 static void add_to_tip_table(const struct object_id *oid, const char *refname, 329 int shorten_unambiguous, struct commit *commit, 330 timestamp_t taggerdate, int from_tag, int deref) 331 { 332 char *short_refname = NULL; 333 334 if (shorten_unambiguous) 335 short_refname = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), 336 refname, 0); 337 else if (skip_prefix(refname, "refs/heads/", &refname)) 338 ; /* refname already advanced */ 339 else 340 skip_prefix(refname, "refs/", &refname); 341 342 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc); 343 oidcpy(&tip_table.table[tip_table.nr].oid, oid); 344 tip_table.table[tip_table.nr].refname = short_refname ? 345 short_refname : xstrdup(refname); 346 tip_table.table[tip_table.nr].commit = commit; 347 tip_table.table[tip_table.nr].taggerdate = taggerdate; 348 tip_table.table[tip_table.nr].from_tag = from_tag; 349 tip_table.table[tip_table.nr].deref = deref; 350 tip_table.nr++; 351 tip_table.sorted = 0; 352 } 353 354 static int tipcmp(const void *a_, const void *b_) 355 { 356 const struct tip_table_entry *a = a_, *b = b_; 357 return oidcmp(&a->oid, &b->oid); 358 } 359 360 static int cmp_by_tag_and_age(const void *a_, const void *b_) 361 { 362 const struct tip_table_entry *a = a_, *b = b_; 363 int cmp; 364 365 /* Prefer tags. */ 366 cmp = b->from_tag - a->from_tag; 367 if (cmp) 368 return cmp; 369 370 /* Older is better. */ 371 if (a->taggerdate < b->taggerdate) 372 return -1; 373 return a->taggerdate != b->taggerdate; 374 } 375 376 static int name_ref(const struct reference *ref, void *cb_data) 377 { 378 struct object *o = parse_object(the_repository, ref->oid); 379 struct name_ref_data *data = cb_data; 380 int can_abbreviate_output = data->tags_only && data->name_only; 381 int deref = 0; 382 int from_tag = 0; 383 struct commit *commit = NULL; 384 timestamp_t taggerdate = TIME_MAX; 385 386 if (data->tags_only && !starts_with(ref->name, "refs/tags/")) 387 return 0; 388 389 if (data->exclude_filters.nr) { 390 struct string_list_item *item; 391 392 for_each_string_list_item(item, &data->exclude_filters) { 393 if (subpath_matches(ref->name, item->string) >= 0) 394 return 0; 395 } 396 } 397 398 if (data->ref_filters.nr) { 399 struct string_list_item *item; 400 int matched = 0; 401 402 /* See if any of the patterns match. */ 403 for_each_string_list_item(item, &data->ref_filters) { 404 /* 405 * Check all patterns even after finding a match, so 406 * that we can see if a match with a subpath exists. 407 * When a user asked for 'refs/tags/v*' and 'v1.*', 408 * both of which match, the user is showing her 409 * willingness to accept a shortened output by having 410 * the 'v1.*' in the acceptable refnames, so we 411 * shouldn't stop when seeing 'refs/tags/v1.4' matches 412 * 'refs/tags/v*'. We should show it as 'v1.4'. 413 */ 414 switch (subpath_matches(ref->name, item->string)) { 415 case -1: /* did not match */ 416 break; 417 case 0: /* matched fully */ 418 matched = 1; 419 break; 420 default: /* matched subpath */ 421 matched = 1; 422 can_abbreviate_output = 1; 423 break; 424 } 425 } 426 427 /* If none of the patterns matched, stop now */ 428 if (!matched) 429 return 0; 430 } 431 432 while (o && o->type == OBJ_TAG) { 433 struct tag *t = (struct tag *) o; 434 if (!t->tagged) 435 break; /* broken repository */ 436 o = parse_object(the_repository, &t->tagged->oid); 437 deref = 1; 438 taggerdate = t->date; 439 } 440 if (o && o->type == OBJ_COMMIT) { 441 commit = (struct commit *)o; 442 from_tag = starts_with(ref->name, "refs/tags/"); 443 if (taggerdate == TIME_MAX) 444 taggerdate = commit->date; 445 } 446 447 add_to_tip_table(ref->oid, ref->name, can_abbreviate_output, 448 commit, taggerdate, from_tag, deref); 449 return 0; 450 } 451 452 static void name_tips(struct mem_pool *string_pool) 453 { 454 int i; 455 456 /* 457 * Try to set better names first, so that worse ones spread 458 * less. 459 */ 460 QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age); 461 for (i = 0; i < tip_table.nr; i++) { 462 struct tip_table_entry *e = &tip_table.table[i]; 463 if (e->commit) { 464 name_rev(e->commit, e->refname, e->taggerdate, 465 e->from_tag, e->deref, string_pool); 466 } 467 } 468 } 469 470 static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_) 471 { 472 const struct tip_table_entry *table = table_; 473 return &table[ix].oid; 474 } 475 476 static const char *get_exact_ref_match(const struct object *o) 477 { 478 int found; 479 480 if (!tip_table.table || !tip_table.nr) 481 return NULL; 482 483 if (!tip_table.sorted) { 484 QSORT(tip_table.table, tip_table.nr, tipcmp); 485 tip_table.sorted = 1; 486 } 487 488 found = oid_pos(&o->oid, tip_table.table, tip_table.nr, 489 nth_tip_table_ent); 490 if (0 <= found) 491 return tip_table.table[found].refname; 492 return NULL; 493 } 494 495 /* may return a constant string or use "buf" as scratch space */ 496 static const char *get_rev_name(const struct object *o, struct strbuf *buf) 497 { 498 struct rev_name *n; 499 const struct commit *c; 500 501 if (o->type != OBJ_COMMIT) 502 return get_exact_ref_match(o); 503 c = (const struct commit *) o; 504 n = get_commit_rev_name(c); 505 if (!n) 506 return NULL; 507 508 if (!n->generation) { 509 return n->tip_name; 510 } else { 511 strbuf_reset(buf); 512 strbuf_addstr(buf, n->tip_name); 513 strbuf_strip_suffix(buf, "^0"); 514 strbuf_addf(buf, "~%d", n->generation); 515 return buf->buf; 516 } 517 } 518 519 static const char *get_format_rev(const struct commit *c, 520 struct pretty_format *format_ctx, 521 struct strbuf *buf) 522 { 523 strbuf_reset(buf); 524 525 if (format_ctx->want.notes) { 526 struct strbuf notebuf = STRBUF_INIT; 527 528 format_display_notes(&c->object.oid, ¬ebuf, 529 get_log_output_encoding(), 530 format_ctx->ctx.fmt == CMIT_FMT_USERFORMAT); 531 format_ctx->ctx.notes_message = strbuf_detach(¬ebuf, NULL); 532 } 533 534 pretty_print_commit(&format_ctx->ctx, c, buf); 535 FREE_AND_NULL(format_ctx->ctx.notes_message); 536 537 return buf->buf; 538 } 539 540 static void show_name(const struct object *obj, 541 const char *caller_name, 542 int always, int allow_undefined, int name_only) 543 { 544 const char *name; 545 const struct object_id *oid = &obj->oid; 546 struct strbuf buf = STRBUF_INIT; 547 548 if (!name_only) 549 printf("%s ", caller_name ? caller_name : oid_to_hex(oid)); 550 name = get_rev_name(obj, &buf); 551 if (name) 552 printf("%s\n", name); 553 else if (allow_undefined) 554 printf("undefined\n"); 555 else if (always) 556 printf("%s\n", 557 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV)); 558 else 559 die("cannot describe '%s'", oid_to_hex(oid)); 560 strbuf_release(&buf); 561 } 562 563 static char const * const name_rev_usage[] = { 564 N_("git name-rev [] ..."), 565 N_("git name-rev [] --all"), 566 N_("git name-rev [] --annotate-stdin"), 567 NULL 568 }; 569 570 static void name_rev_line(char *p, struct command *cmd) 571 { 572 struct strbuf buf = STRBUF_INIT; 573 int counter = 0; 574 char *p_start; 575 const unsigned hexsz = the_hash_algo->hexsz; 576 577 for (p_start = p; *p; p++) { 578 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f')) 579 if (!ishex(*p)) { 580 counter = 0; 581 } else if (++counter == hexsz && 582 !ishex(*(p + 1))) { 583 struct object_id oid; 584 const char *name = NULL; 585 char c = *(p + 1); 586 int p_len = p - p_start + 1; 587 struct object *o = NULL; 588 int oid_ret = 1; 589 590 counter = 0; 591 592 *(p + 1) = 0; 593 oid_ret = repo_get_oid(the_repository, p - (hexsz - 1), &oid); 594 *(p + 1) = c; 595 596 switch (cmd->type) { 597 case NAME_REV: 598 if (!oid_ret) 599 o = lookup_object(the_repository, &oid); 600 if (o) 601 name = get_rev_name(o, &buf); 602 if (!name) 603 continue; 604 if (cmd->u.name_only) 605 printf("%.*s%s", p_len - hexsz, p_start, name); 606 else 607 printf("%.*s (%s)", p_len, p_start, name); 608 break; 609 case FORMAT_REV: 610 if (!oid_ret) 611 o = parse_object(the_repository, &oid); 612 if (o && o->type == OBJ_COMMIT) 613 name = get_format_rev((const struct commit *)o, 614 cmd->u.pretty_format, 615 &buf); 616 if (name) 617 printf("%.*s%s", p_len - hexsz, p_start, name); 618 else 619 printf("%.*s", p_len, p_start); 620 break; 621 default: 622 BUG("uncovered case: %d", cmd->type); 623 } 624 625 p_start = p + 1; 626 } 627 } 628 629 /* flush */ 630 if (p_start != p) 631 fwrite(p_start, p - p_start, 1, stdout); 632 633 strbuf_release(&buf); 634 } 635 636 int cmd_name_rev(int argc, 637 const char **argv, 638 const char *prefix, 639 struct repository *repo UNUSED) 640 { 641 struct mem_pool string_pool; 642 struct object_array revs = OBJECT_ARRAY_INIT; 643 644 #ifndef WITH_BREAKING_CHANGES 645 int transform_stdin = 0; 646 #endif 647 int all = 0, annotate_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0; 648 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP }; 649 struct command cmd; 650 struct option opts[] = { 651 OPT_BOOL(0, "name-only", &data.name_only, N_("print only ref-based names (no object names)")), 652 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")), 653 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"), 654 N_("only use refs matching ")), 655 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"), 656 N_("ignore refs matching ")), 657 OPT_GROUP(""), 658 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")), 659 #ifndef WITH_BREAKING_CHANGES 660 OPT_BOOL_F(0, 661 "stdin", 662 &transform_stdin, 663 N_("deprecated: use --annotate-stdin instead"), 664 PARSE_OPT_HIDDEN), 665 #endif /* WITH_BREAKING_CHANGES */ 666 OPT_BOOL(0, "annotate-stdin", &annotate_stdin, N_("annotate text from stdin")), 667 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")), 668 OPT_BOOL(0, "always", &always, 669 N_("show abbreviated commit object as fallback")), 670 OPT_HIDDEN_BOOL(0, "peel-tag", &peel_tag, 671 N_("dereference tags in the input (internal use)")), 672 OPT_END(), 673 }; 674 675 mem_pool_init(&string_pool, 0); 676 init_commit_rev_name(&rev_names); 677 repo_config(the_repository, git_default_config, NULL); 678 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0); 679 init_name_rev_command(&cmd, data.name_only); 680 681 #ifndef WITH_BREAKING_CHANGES 682 if (transform_stdin) { 683 warning("--stdin is deprecated. Please use --annotate-stdin instead, " 684 "which is functionally equivalent.\n" 685 "This option will be removed in a future release."); 686 annotate_stdin = 1; 687 } 688 #endif 689 690 if (all + annotate_stdin + !!argc > 1) { 691 error("Specify either a list, or --all, not both!"); 692 usage_with_options(name_rev_usage, opts); 693 } 694 if (all || annotate_stdin) 695 disable_cutoff(); 696 697 for (; argc; argc--, argv++) { 698 struct object_id oid; 699 struct object *object; 700 struct commit *commit; 701 702 if (repo_get_oid(the_repository, *argv, &oid)) { 703 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n", 704 *argv); 705 continue; 706 } 707 708 commit = NULL; 709 object = parse_object(the_repository, &oid); 710 if (object) { 711 struct object *peeled = deref_tag(the_repository, 712 object, *argv, 0); 713 if (peeled && peeled->type == OBJ_COMMIT) 714 commit = (struct commit *)peeled; 715 } 716 717 if (!object) { 718 fprintf(stderr, "Could not get object for %s. Skipping.\n", 719 *argv); 720 continue; 721 } 722 723 if (commit) 724 set_commit_cutoff(commit); 725 726 if (peel_tag) { 727 if (!commit) { 728 fprintf(stderr, "Could not get commit for %s. Skipping.\n", 729 *argv); 730 continue; 731 } 732 object = (struct object *)commit; 733 } 734 add_object_array(object, *argv, &revs); 735 } 736 737 adjust_cutoff_timestamp_for_slop(); 738 739 refs_for_each_ref(get_main_ref_store(the_repository), name_ref, &data); 740 name_tips(&string_pool); 741 742 if (annotate_stdin) { 743 struct strbuf sb = STRBUF_INIT; 744 745 while (strbuf_getline(&sb, stdin) != EOF) { 746 strbuf_addch(&sb, '\n'); 747 name_rev_line(sb.buf, &cmd); 748 } 749 strbuf_release(&sb); 750 } else if (all) { 751 int i, max; 752 753 max = get_max_object_index(the_repository); 754 for (i = 0; i < max; i++) { 755 struct object *obj = get_indexed_object(the_repository, i); 756 if (!obj || obj->type != OBJ_COMMIT) 757 continue; 758 show_name(obj, NULL, 759 always, allow_undefined, data.name_only); 760 } 761 } else { 762 int i; 763 for (i = 0; i < revs.nr; i++) 764 show_name(revs.objects[i].item, revs.objects[i].name, 765 always, allow_undefined, data.name_only); 766 } 767 768 string_list_clear(&data.ref_filters, 0); 769 string_list_clear(&data.exclude_filters, 0); 770 mem_pool_discard(&string_pool, 0); 771 object_array_clear(&revs); 772 return 0; 773 } 774 775 struct format_nul_data { 776 bool nul_input; 777 bool nul_output; 778 }; 779 780 static int format_nul_cb(const struct option *option, 781 const char *arg, 782 int unset) 783 { 784 struct format_nul_data *data = option->value; 785 data->nul_input = 1; 786 data->nul_output = 1; 787 BUG_ON_OPT_NEG(unset); 788 BUG_ON_OPT_ARG(arg); 789 return 0; 790 } 791 792 static enum stdin_mode parse_stdin_mode(const char *stdin_mode) 793 { 794 if (!strcmp(stdin_mode, "text")) 795 return TEXT; 796 else if (!strcmp(stdin_mode, "revs") || 797 !strcmp(stdin_mode, "rev")) 798 return REVS; 799 else 800 die(_("'%s' needs to be either text, revs, or rev"), 801 "--stdin-mode"); 802 } 803 804 static char const *const format_rev_usage[] = { 805 N_("(EXPERIMENTAL!) git format-rev --stdin-mode= " 806 "--format= [--[no-]notes=] " 807 "[-z] [--[no-]null-output] [--[no-]null-input]"), 808 NULL 809 }; 810 811 int cmd_format_rev(int argc, 812 const char **argv, 813 const char *prefix, 814 struct repository *repo UNUSED) 815 { 816 const char *format = NULL; 817 enum stdin_mode stdin_mode; 818 const char *stdin_mode_arg = NULL; 819 struct format_nul_data nul_data = { 0, 0 }; 820 char output_terminator; 821 strbuf_getline_fn getline_fn; 822 struct display_notes_opt format_notes_opt; 823 struct rev_info format_rev = REV_INFO_INIT; 824 struct pretty_format format_pp = { 0 }; 825 struct string_list notes = STRING_LIST_INIT_NODUP; 826 struct strbuf scratch_buf = STRBUF_INIT; 827 struct command cmd; 828 struct option opts[] = { 829 OPT_STRING(0, "format", &format, N_("format"), 830 N_("pretty format to use")), 831 OPT_STRING(0, "stdin-mode", &stdin_mode_arg, N_("stdin-mode"), 832 N_("how revs are processed")), 833 OPT_STRING_LIST(0, "notes", ¬es, N_("notes"), 834 N_("display notes for pretty format")), 835 OPT_CALLBACK_F('z', "null", &nul_data, N_("z"), 836 N_("Use NUL for input and output termination"), 837 PARSE_OPT_NOARG | PARSE_OPT_NONEG, format_nul_cb), 838 OPT_BOOL(0, "null-input", &nul_data.nul_input, 839 N_("Use NUL for input termination")), 840 OPT_BOOL(0, "null-output", &nul_data.nul_output, 841 N_("Use NUL for output termination")), 842 OPT_END(), 843 }; 844 845 argc = parse_options(argc, argv, prefix, opts, format_rev_usage, 0); 846 847 if (argc > 0) { 848 error(_("too many arguments")); 849 usage_with_options(format_rev_usage, opts); 850 } 851 852 if (!format) 853 die(_("'%s' is required"), "--format"); 854 if (!stdin_mode_arg) 855 die(_("'%s' is required"), "--stdin-mode"); 856 857 getline_fn = nul_data.nul_input ? strbuf_getline_nul : strbuf_getline_lf; 858 output_terminator = nul_data.nul_output ? '\0' : '\n'; 859 860 init_display_notes(&format_notes_opt); 861 stdin_mode = parse_stdin_mode(stdin_mode_arg); 862 863 get_commit_format(format, &format_rev); 864 format_pp.ctx.rev = &format_rev; 865 format_pp.ctx.fmt = format_rev.commit_format; 866 format_pp.ctx.abbrev = format_rev.abbrev; 867 format_pp.ctx.date_mode_explicit = format_rev.date_mode_explicit; 868 format_pp.ctx.date_mode = format_rev.date_mode; 869 format_pp.ctx.color = GIT_COLOR_AUTO; 870 871 userformat_find_requirements(format, 872 &format_pp.want); 873 if (format_pp.want.notes) { 874 int ignore_show_notes = 0; 875 struct string_list_item *n; 876 877 for_each_string_list_item(n, ¬es) 878 enable_ref_display_notes(&format_notes_opt, 879 &ignore_show_notes, 880 n->string); 881 load_display_notes(&format_notes_opt); 882 } 883 884 init_format_rev_command(&cmd, &format_pp); 885 886 switch (stdin_mode) { 887 case TEXT: 888 while (getline_fn(&scratch_buf, stdin) != EOF) { 889 name_rev_line(scratch_buf.buf, &cmd); 890 /* 891 * We do not pass on the terminator to name_rev_line, 892 * unlike name-rev. 893 */ 894 printf("%c", output_terminator); 895 maybe_flush_or_die(stdout, "stdout"); 896 } 897 break; 898 case REVS: 899 while (getline_fn(&scratch_buf, stdin) != EOF) { 900 struct object_id oid; 901 struct object *object; 902 struct object *peeled; 903 904 if (repo_get_oid(the_repository, scratch_buf.buf, &oid)) { 905 fprintf(stderr, "Could not get object name for %s. Skipping.\n", 906 scratch_buf.buf); 907 continue; 908 } 909 910 object = parse_object(the_repository, &oid); 911 if (!object) { 912 fprintf(stderr, "Could not get object for %s. Skipping.\n", 913 scratch_buf.buf); 914 continue; 915 } 916 917 peeled = deref_tag(the_repository, object, scratch_buf.buf, 0); 918 if (!peeled || peeled->type != OBJ_COMMIT) { 919 fprintf(stderr, 920 "Could not get commit for %s. Skipping.\n", 921 scratch_buf.buf); 922 continue; 923 } 924 925 get_format_rev((struct commit *)peeled, 926 &format_pp, &scratch_buf); 927 printf("%s%c", scratch_buf.buf, output_terminator); 928 maybe_flush_or_die(stdout, "stdout"); 929 strbuf_release(&scratch_buf); 930 } 931 break; 932 default: 933 BUG("uncovered case: %d", stdin_mode); 934 } 935 936 strbuf_release(&scratch_buf); 937 string_list_clear(¬es, 0); 938 release_display_notes(&format_notes_opt); 939 return 0; 940 }