git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "builtin.h" |
| 5 | #include "config.h" |
| 6 | #include "commit.h" |
| 7 | #include "diff.h" |
| 8 | #include "environment.h" |
| 9 | #include "gettext.h" |
| 10 | #include "hex.h" |
| 11 | #include "revision.h" |
| 12 | #include "list-objects.h" |
| 13 | #include "list-objects-filter-options.h" |
| 14 | #include "object.h" |
| 15 | #include "object-name.h" |
| 16 | #include "object-file.h" |
| 17 | #include "odb.h" |
| 18 | #include "pack-bitmap.h" |
| 19 | #include "parse-options.h" |
| 20 | #include "log-tree.h" |
| 21 | #include "graph.h" |
| 22 | #include "bisect.h" |
| 23 | #include "progress.h" |
| 24 | #include "reflog-walk.h" |
| 25 | #include "oidset.h" |
| 26 | #include "oidmap.h" |
| 27 | #include "packfile.h" |
| 28 | #include "commit-reach.h" |
| 29 | #include "quote.h" |
| 30 | #include "strbuf.h" |
| 31 | #include "tree-verify.h" |
| 32 | #include "parse.h" |
| 33 | |
| 34 | #define INCREMENTAL_MAX_COMMITS 1000 |
| 35 | |
| 36 | enum connectivity_check_mode { |
| 37 | CONNECTIVITY_CHECK_FULL, |
| 38 | CONNECTIVITY_CHECK_INCREMENTAL, |
| 39 | CONNECTIVITY_CHECK_AUTO, |
| 40 | }; |
| 41 | |
| 42 | struct connectivity_check { |
| 43 | enum connectivity_check_mode mode; |
| 44 | unsigned long max_commits; |
| 45 | }; |
| 46 | |
| 47 | static void parse_connectivity_check(const char *arg, |
| 48 | struct connectivity_check *check) |
| 49 | { |
| 50 | const char *cutoff; |
| 51 | |
| 52 | if (!strcmp(arg, "full")) |
| 53 | check->mode = CONNECTIVITY_CHECK_FULL; |
| 54 | else if (!strcmp(arg, "incremental")) |
| 55 | check->mode = CONNECTIVITY_CHECK_INCREMENTAL; |
| 56 | else if (!strcmp(arg, "auto")) |
| 57 | check->mode = CONNECTIVITY_CHECK_AUTO; |
| 58 | else if (skip_prefix(arg, "auto=", &cutoff) && |
| 59 | git_parse_ulong(cutoff, &check->max_commits)) |
| 60 | check->mode = CONNECTIVITY_CHECK_AUTO; |
| 61 | else |
| 62 | die(_("unknown --connectivity-check mode '%s'"), arg); |
| 63 | } |
| 64 | |
| 65 | struct rev_list_info { |
| 66 | struct rev_info *revs; |
| 67 | int flags; |
| 68 | int show_timestamp; |
| 69 | int hdr_termination; |
| 70 | const char *header_prefix; |
| 71 | }; |
| 72 | |
| 73 | static const char rev_list_usage[] = |
| 74 | "git rev-list [<options>] <commit>... [--] [<path>...]\n" |
| 75 | "\n" |
| 76 | " limiting output:\n" |
| 77 | " --max-count=<n>\n" |
| 78 | " --max-age=<epoch>\n" |
| 79 | " --min-age=<epoch>\n" |
| 80 | " --sparse\n" |
| 81 | " --no-merges\n" |
| 82 | " --min-parents=<n>\n" |
| 83 | " --no-min-parents\n" |
| 84 | " --max-parents=<n>\n" |
| 85 | " --no-max-parents\n" |
| 86 | " --remove-empty\n" |
| 87 | " --all\n" |
| 88 | " --branches\n" |
| 89 | " --tags\n" |
| 90 | " --remotes\n" |
| 91 | " --stdin\n" |
| 92 | " --exclude-hidden=[fetch|receive|uploadpack]\n" |
| 93 | " --quiet\n" |
| 94 | " ordering output:\n" |
| 95 | " --topo-order\n" |
| 96 | " --date-order\n" |
| 97 | " --reverse\n" |
| 98 | " formatting output:\n" |
| 99 | " --parents\n" |
| 100 | " --children\n" |
| 101 | " --objects | --objects-edge\n" |
| 102 | " --disk-usage[=human]\n" |
| 103 | " --unpacked\n" |
| 104 | " --header | --pretty\n" |
| 105 | " --[no-]object-names\n" |
| 106 | " --abbrev=<n> | --no-abbrev\n" |
| 107 | " --abbrev-commit\n" |
| 108 | " --left-right\n" |
| 109 | " --count\n" |
| 110 | " -z\n" |
| 111 | " special purpose:\n" |
| 112 | " --bisect\n" |
| 113 | " --bisect-vars\n" |
| 114 | " --bisect-all" |
| 115 | ; |
| 116 | |
| 117 | static struct progress *progress; |
| 118 | static unsigned progress_counter; |
| 119 | |
| 120 | static struct oidset omitted_objects; |
| 121 | static int arg_print_omitted; /* print objects omitted by filter */ |
| 122 | |
| 123 | struct missing_objects_map_entry { |
| 124 | struct oidmap_entry entry; |
| 125 | char *path; |
| 126 | unsigned type; |
| 127 | }; |
| 128 | |
| 129 | static void missing_objects_map_entry_free(void *e) |
| 130 | { |
| 131 | struct missing_objects_map_entry *entry = |
| 132 | container_of(e, struct missing_objects_map_entry, entry); |
| 133 | |
| 134 | free(entry->path); |
| 135 | free(entry); |
| 136 | } |
| 137 | |
| 138 | static struct oidmap missing_objects; |
| 139 | enum missing_action { |
| 140 | MA_ERROR = 0, /* fail if any missing objects are encountered */ |
| 141 | MA_ALLOW_ANY, /* silently allow ALL missing objects */ |
| 142 | MA_PRINT, /* print ALL missing objects in special section */ |
| 143 | MA_PRINT_INFO, /* same as MA_PRINT but also prints missing object info */ |
| 144 | MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */ |
| 145 | }; |
| 146 | static enum missing_action arg_missing_action; |
| 147 | static int arg_missing_only; |
| 148 | |
| 149 | static inline int should_collect_missing(void) |
| 150 | { |
| 151 | return arg_missing_action == MA_PRINT || |
| 152 | arg_missing_action == MA_PRINT_INFO; |
| 153 | } |
| 154 | |
| 155 | /* display only the oid of each object encountered */ |
| 156 | static int arg_show_object_names = 1; |
| 157 | |
| 158 | #define DEFAULT_OIDSET_SIZE (16*1024) |
| 159 | |
| 160 | static char line_term = '\n'; |
| 161 | static char info_term = ' '; |
| 162 | |
| 163 | static int show_disk_usage; |
| 164 | static off_t total_disk_usage; |
| 165 | static int human_readable; |
| 166 | |
| 167 | static off_t get_object_disk_usage(struct object *obj) |
| 168 | { |
| 169 | off_t size; |
| 170 | struct object_info oi = OBJECT_INFO_INIT; |
| 171 | oi.disk_sizep = &size; |
| 172 | if (odb_read_object_info_extended(the_repository->objects, |
| 173 | &obj->oid, &oi, 0) < 0) |
| 174 | die(_("unable to get disk usage of %s"), oid_to_hex(&obj->oid)); |
| 175 | return size; |
| 176 | } |
| 177 | |
| 178 | static void add_missing_object_entry(struct object_id *oid, const char *path, |
| 179 | unsigned type) |
| 180 | { |
| 181 | struct missing_objects_map_entry *entry; |
| 182 | |
| 183 | if (oidmap_get(&missing_objects, oid)) |
| 184 | return; |
| 185 | |
| 186 | CALLOC_ARRAY(entry, 1); |
| 187 | entry->entry.oid = *oid; |
| 188 | entry->type = type; |
| 189 | if (path) |
| 190 | entry->path = xstrdup(path); |
| 191 | oidmap_put(&missing_objects, entry); |
| 192 | } |
| 193 | |
| 194 | static void print_missing_object(struct missing_objects_map_entry *entry, |
| 195 | int print_missing_info) |
| 196 | { |
| 197 | struct strbuf sb = STRBUF_INIT; |
| 198 | |
| 199 | /* |
| 200 | * --missing-only filters present objects out of the walk output. |
| 201 | * It still uses the selected --missing= format for missing ones, |
| 202 | * except the human "?" prefix is omitted (script-friendly OIDs). |
| 203 | */ |
| 204 | if (arg_missing_only && line_term) |
| 205 | printf("%s", oid_to_hex(&entry->entry.oid)); |
| 206 | else if (line_term) |
| 207 | printf("?%s", oid_to_hex(&entry->entry.oid)); |
| 208 | else |
| 209 | printf("%s%cmissing=yes", oid_to_hex(&entry->entry.oid), |
| 210 | info_term); |
| 211 | |
| 212 | if (!print_missing_info) { |
| 213 | putchar(line_term); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (entry->path && *entry->path) { |
| 218 | strbuf_addf(&sb, "%cpath=", info_term); |
| 219 | |
| 220 | if (line_term) { |
| 221 | struct strbuf path = STRBUF_INIT; |
| 222 | |
| 223 | quote_path(entry->path, NULL, &path, QUOTE_PATH_QUOTE_SP); |
| 224 | strbuf_addbuf(&sb, &path); |
| 225 | |
| 226 | strbuf_release(&path); |
| 227 | } else { |
| 228 | strbuf_addstr(&sb, entry->path); |
| 229 | } |
| 230 | } |
| 231 | if (entry->type) |
| 232 | strbuf_addf(&sb, "%ctype=%s", info_term, type_name(entry->type)); |
| 233 | |
| 234 | fwrite(sb.buf, sizeof(char), sb.len, stdout); |
| 235 | putchar(line_term); |
| 236 | |
| 237 | strbuf_release(&sb); |
| 238 | } |
| 239 | |
| 240 | static inline void finish_object__ma(struct object *obj, const char *name) |
| 241 | { |
| 242 | /* |
| 243 | * Whether or not we try to dynamically fetch missing objects |
| 244 | * from the server, we currently DO NOT have the object. We |
| 245 | * can either print, allow (ignore), or conditionally allow |
| 246 | * (ignore) them. |
| 247 | */ |
| 248 | switch (arg_missing_action) { |
| 249 | case MA_ERROR: |
| 250 | die("missing %s object '%s'", |
| 251 | type_name(obj->type), oid_to_hex(&obj->oid)); |
| 252 | return; |
| 253 | |
| 254 | case MA_ALLOW_ANY: |
| 255 | return; |
| 256 | |
| 257 | case MA_PRINT: |
| 258 | case MA_PRINT_INFO: |
| 259 | add_missing_object_entry(&obj->oid, name, obj->type); |
| 260 | return; |
| 261 | |
| 262 | case MA_ALLOW_PROMISOR: |
| 263 | if (is_promisor_object(the_repository, &obj->oid)) |
| 264 | return; |
| 265 | die("unexpected missing %s object '%s'", |
| 266 | type_name(obj->type), oid_to_hex(&obj->oid)); |
| 267 | return; |
| 268 | |
| 269 | default: |
| 270 | BUG("unhandled missing_action"); |
| 271 | return; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void finish_commit(struct commit *commit) |
| 276 | { |
| 277 | commit_list_free(commit->parents); |
| 278 | commit->parents = NULL; |
| 279 | free_commit_buffer(the_repository->parsed_objects, |
| 280 | commit); |
| 281 | } |
| 282 | |
| 283 | static void show_commit(struct commit *commit, void *data) |
| 284 | { |
| 285 | struct rev_list_info *info = data; |
| 286 | struct rev_info *revs = info->revs; |
| 287 | |
| 288 | display_progress(progress, ++progress_counter); |
| 289 | |
| 290 | if (revs->do_not_die_on_missing_objects && |
| 291 | oidset_contains(&revs->missing_commits, &commit->object.oid)) { |
| 292 | finish_object__ma(&commit->object, NULL); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | if (arg_missing_only) { |
| 297 | finish_commit(commit); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | if (show_disk_usage) |
| 302 | total_disk_usage += get_object_disk_usage(&commit->object); |
| 303 | |
| 304 | if (info->flags & REV_LIST_QUIET) { |
| 305 | finish_commit(commit); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | graph_show_commit(revs->graph); |
| 310 | |
| 311 | if (revs->count) { |
| 312 | if (commit->object.flags & PATCHSAME) |
| 313 | revs->count_same++; |
| 314 | else if (commit->object.flags & SYMMETRIC_LEFT) |
| 315 | revs->count_left++; |
| 316 | else |
| 317 | revs->count_right++; |
| 318 | finish_commit(commit); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | if (info->show_timestamp) |
| 323 | printf("%"PRItime" ", commit->date); |
| 324 | if (info->header_prefix) |
| 325 | fputs(info->header_prefix, stdout); |
| 326 | |
| 327 | if (revs->include_header) { |
| 328 | if (!revs->graph && line_term) |
| 329 | fputs(get_revision_mark(revs, commit), stdout); |
| 330 | if (revs->abbrev_commit && revs->abbrev) |
| 331 | fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, revs->abbrev), |
| 332 | stdout); |
| 333 | else |
| 334 | fputs(oid_to_hex(&commit->object.oid), stdout); |
| 335 | |
| 336 | if (!line_term) { |
| 337 | if (commit->object.flags & BOUNDARY) |
| 338 | printf("%cboundary=yes", info_term); |
| 339 | } |
| 340 | } |
| 341 | if (revs->print_parents) { |
| 342 | struct commit_list *parents = commit->parents; |
| 343 | while (parents) { |
| 344 | printf(" %s", oid_to_hex(&parents->item->object.oid)); |
| 345 | parents = parents->next; |
| 346 | } |
| 347 | } |
| 348 | if (revs->children.name) { |
| 349 | struct commit_list *children; |
| 350 | |
| 351 | children = lookup_decoration(&revs->children, &commit->object); |
| 352 | while (children) { |
| 353 | printf(" %s", oid_to_hex(&children->item->object.oid)); |
| 354 | children = children->next; |
| 355 | } |
| 356 | } |
| 357 | show_decorations(revs, commit); |
| 358 | if (revs->commit_format == CMIT_FMT_ONELINE) |
| 359 | putchar(' '); |
| 360 | else if (revs->include_header) |
| 361 | putchar(line_term); |
| 362 | |
| 363 | if (revs->verbose_header) { |
| 364 | struct strbuf buf = STRBUF_INIT; |
| 365 | struct pretty_print_context ctx = {0}; |
| 366 | ctx.abbrev = revs->abbrev; |
| 367 | ctx.date_mode = revs->date_mode; |
| 368 | ctx.date_mode_explicit = revs->date_mode_explicit; |
| 369 | ctx.fmt = revs->commit_format; |
| 370 | ctx.output_encoding = get_log_output_encoding(); |
| 371 | ctx.color = revs->diffopt.use_color; |
| 372 | ctx.rev = revs; |
| 373 | pretty_print_commit(&ctx, commit, &buf); |
| 374 | if (buf.len) { |
| 375 | if (revs->commit_format != CMIT_FMT_ONELINE) |
| 376 | graph_show_oneline(revs->graph); |
| 377 | |
| 378 | graph_show_commit_msg(revs->graph, stdout, &buf); |
| 379 | |
| 380 | /* |
| 381 | * Add a newline after the commit message. |
| 382 | * |
| 383 | * Usually, this newline produces a blank |
| 384 | * padding line between entries, in which case |
| 385 | * we need to add graph padding on this line. |
| 386 | * |
| 387 | * However, the commit message may not end in a |
| 388 | * newline. In this case the newline simply |
| 389 | * ends the last line of the commit message, |
| 390 | * and we don't need any graph output. (This |
| 391 | * always happens with CMIT_FMT_ONELINE, and it |
| 392 | * happens with CMIT_FMT_USERFORMAT when the |
| 393 | * format doesn't explicitly end in a newline.) |
| 394 | */ |
| 395 | if (buf.len && buf.buf[buf.len - 1] == '\n') |
| 396 | graph_show_padding(revs->graph); |
| 397 | putchar(info->hdr_termination); |
| 398 | } else { |
| 399 | /* |
| 400 | * If the message buffer is empty, just show |
| 401 | * the rest of the graph output for this |
| 402 | * commit. |
| 403 | */ |
| 404 | if (graph_show_remainder(revs->graph)) |
| 405 | putchar('\n'); |
| 406 | if (revs->commit_format == CMIT_FMT_ONELINE) |
| 407 | putchar('\n'); |
| 408 | } |
| 409 | strbuf_release(&buf); |
| 410 | } else { |
| 411 | if (graph_show_remainder(revs->graph)) |
| 412 | putchar('\n'); |
| 413 | } |
| 414 | maybe_flush_or_die(stdout, "stdout"); |
| 415 | finish_commit(commit); |
| 416 | } |
| 417 | |
| 418 | static int finish_object(struct object *obj, const char *name, void *cb_data) |
| 419 | { |
| 420 | struct rev_list_info *info = cb_data; |
| 421 | if (odb_read_object_info_extended(the_repository->objects, |
| 422 | &obj->oid, NULL, 0) < 0) { |
| 423 | finish_object__ma(obj, name); |
| 424 | return 1; |
| 425 | } |
| 426 | if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT) |
| 427 | parse_object(the_repository, &obj->oid); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static void show_object(struct object *obj, const char *name, void *cb_data) |
| 432 | { |
| 433 | struct rev_list_info *info = cb_data; |
| 434 | struct rev_info *revs = info->revs; |
| 435 | |
| 436 | if (finish_object(obj, name, cb_data)) |
| 437 | return; |
| 438 | display_progress(progress, ++progress_counter); |
| 439 | if (arg_missing_only) |
| 440 | return; |
| 441 | if (show_disk_usage) |
| 442 | total_disk_usage += get_object_disk_usage(obj); |
| 443 | if (info->flags & REV_LIST_QUIET) |
| 444 | return; |
| 445 | |
| 446 | if (revs->count) { |
| 447 | /* |
| 448 | * The object count is always accumulated in the .count_right |
| 449 | * field for traversal that is not a left-right traversal, |
| 450 | * and cmd_rev_list() made sure that a .count request that |
| 451 | * wants to count non-commit objects, which is handled by |
| 452 | * the show_object() callback, does not ask for .left_right. |
| 453 | */ |
| 454 | revs->count_right++; |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | printf("%s", oid_to_hex(&obj->oid)); |
| 459 | |
| 460 | if (arg_show_object_names) { |
| 461 | if (line_term) { |
| 462 | putchar(info_term); |
| 463 | for (const char *p = name; *p && *p != '\n'; p++) |
| 464 | putchar(*p); |
| 465 | } else if (*name) { |
| 466 | printf("%cpath=%s", info_term, name); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | putchar(line_term); |
| 471 | } |
| 472 | |
| 473 | static void show_edge(struct commit *commit) |
| 474 | { |
| 475 | printf("-%s\n", oid_to_hex(&commit->object.oid)); |
| 476 | } |
| 477 | |
| 478 | static void print_var_str(const char *var, const char *val) |
| 479 | { |
| 480 | printf("%s='%s'\n", var, val); |
| 481 | } |
| 482 | |
| 483 | static void print_var_int(const char *var, int val) |
| 484 | { |
| 485 | printf("%s=%d\n", var, val); |
| 486 | } |
| 487 | |
| 488 | static int show_bisect_vars(struct rev_list_info *info, int reaches, int all) |
| 489 | { |
| 490 | int cnt, flags = info->flags; |
| 491 | char hex[GIT_MAX_HEXSZ + 1] = ""; |
| 492 | struct commit_list *tried; |
| 493 | struct rev_info *revs = info->revs; |
| 494 | |
| 495 | if (!revs->commits) |
| 496 | return 1; |
| 497 | |
| 498 | revs->commits = filter_skipped(revs->commits, &tried, |
| 499 | flags & BISECT_SHOW_ALL, |
| 500 | NULL, NULL); |
| 501 | |
| 502 | /* |
| 503 | * revs->commits can reach "reaches" commits among |
| 504 | * "all" commits. If it is good, then there are |
| 505 | * (all-reaches) commits left to be bisected. |
| 506 | * On the other hand, if it is bad, then the set |
| 507 | * to bisect is "reaches". |
| 508 | * A bisect set of size N has (N-1) commits further |
| 509 | * to test, as we already know one bad one. |
| 510 | */ |
| 511 | cnt = all - reaches; |
| 512 | if (cnt < reaches) |
| 513 | cnt = reaches; |
| 514 | |
| 515 | if (revs->commits) |
| 516 | oid_to_hex_r(hex, &revs->commits->item->object.oid); |
| 517 | |
| 518 | if (flags & BISECT_SHOW_ALL) { |
| 519 | traverse_commit_list(revs, show_commit, show_object, info); |
| 520 | printf("------\n"); |
| 521 | } |
| 522 | |
| 523 | print_var_str("bisect_rev", hex); |
| 524 | print_var_int("bisect_nr", cnt - 1); |
| 525 | print_var_int("bisect_good", all - reaches - 1); |
| 526 | print_var_int("bisect_bad", reaches - 1); |
| 527 | print_var_int("bisect_all", all); |
| 528 | print_var_int("bisect_steps", estimate_bisect_steps(all)); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static int show_object_fast( |
| 534 | const struct object_id *oid, |
| 535 | enum object_type type UNUSED, |
| 536 | int exclude UNUSED, |
| 537 | uint32_t name_hash UNUSED, |
| 538 | struct packed_git *found_pack UNUSED, |
| 539 | off_t found_offset UNUSED, |
| 540 | void *payload UNUSED) |
| 541 | { |
| 542 | fprintf(stdout, "%s\n", oid_to_hex(oid)); |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static void print_disk_usage(off_t size) |
| 547 | { |
| 548 | struct strbuf sb = STRBUF_INIT; |
| 549 | if (human_readable) |
| 550 | strbuf_humanise_bytes(&sb, size); |
| 551 | else |
| 552 | strbuf_addf(&sb, "%"PRIuMAX, (uintmax_t)size); |
| 553 | puts(sb.buf); |
| 554 | strbuf_release(&sb); |
| 555 | } |
| 556 | |
| 557 | static inline int parse_missing_action_value(struct repository *repo, |
| 558 | const char *value) |
| 559 | { |
| 560 | if (!strcmp(value, "error")) { |
| 561 | arg_missing_action = MA_ERROR; |
| 562 | return 1; |
| 563 | } |
| 564 | |
| 565 | if (!strcmp(value, "allow-any")) { |
| 566 | arg_missing_action = MA_ALLOW_ANY; |
| 567 | repo->fetch_if_missing = 0; |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | if (!strcmp(value, "print")) { |
| 572 | arg_missing_action = MA_PRINT; |
| 573 | repo->fetch_if_missing = 0; |
| 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | if (!strcmp(value, "print-info")) { |
| 578 | arg_missing_action = MA_PRINT_INFO; |
| 579 | repo->fetch_if_missing = 0; |
| 580 | return 1; |
| 581 | } |
| 582 | |
| 583 | if (!strcmp(value, "allow-promisor")) { |
| 584 | arg_missing_action = MA_ALLOW_PROMISOR; |
| 585 | repo->fetch_if_missing = 0; |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int try_bitmap_count(struct rev_info *revs, |
| 593 | int filter_provided_objects) |
| 594 | { |
| 595 | uint32_t commit_count = 0, |
| 596 | tag_count = 0, |
| 597 | tree_count = 0, |
| 598 | blob_count = 0; |
| 599 | int max_count; |
| 600 | struct bitmap_index *bitmap_git; |
| 601 | |
| 602 | /* This function only handles counting, not general traversal. */ |
| 603 | if (!revs->count) |
| 604 | return -1; |
| 605 | |
| 606 | /* |
| 607 | * A bitmap result can't know left/right, etc, because we don't |
| 608 | * actually traverse. |
| 609 | */ |
| 610 | if (revs->left_right || revs->cherry_mark) |
| 611 | return -1; |
| 612 | |
| 613 | /* |
| 614 | * If we're counting reachable objects, we can't handle a max count of |
| 615 | * commits to traverse, since we don't know which objects go with which |
| 616 | * commit. |
| 617 | */ |
| 618 | if (revs->max_count >= 0 && |
| 619 | (revs->tag_objects || revs->tree_objects || revs->blob_objects)) |
| 620 | return -1; |
| 621 | |
| 622 | /* |
| 623 | * This must be saved before doing any walking, since the revision |
| 624 | * machinery will count it down to zero while traversing. |
| 625 | */ |
| 626 | max_count = revs->max_count; |
| 627 | |
| 628 | bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects); |
| 629 | if (!bitmap_git) |
| 630 | return -1; |
| 631 | |
| 632 | count_bitmap_commit_list(bitmap_git, &commit_count, |
| 633 | revs->tree_objects ? &tree_count : NULL, |
| 634 | revs->blob_objects ? &blob_count : NULL, |
| 635 | revs->tag_objects ? &tag_count : NULL); |
| 636 | if (max_count >= 0 && max_count < commit_count) |
| 637 | commit_count = max_count; |
| 638 | |
| 639 | printf("%d\n", commit_count + tree_count + blob_count + tag_count); |
| 640 | free_bitmap_index(bitmap_git); |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int try_bitmap_traversal(struct rev_info *revs, |
| 645 | int filter_provided_objects) |
| 646 | { |
| 647 | struct bitmap_index *bitmap_git; |
| 648 | |
| 649 | /* |
| 650 | * We can't use a bitmap result with a traversal limit, since the set |
| 651 | * of commits we'd get would be essentially random. |
| 652 | */ |
| 653 | if (revs->max_count >= 0) |
| 654 | return -1; |
| 655 | |
| 656 | /* |
| 657 | * We can't know which commits were left/right in a single traversal, |
| 658 | * and we don't yet know how to traverse them separately. |
| 659 | */ |
| 660 | if (revs->left_right) |
| 661 | return -1; |
| 662 | |
| 663 | bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects); |
| 664 | if (!bitmap_git) |
| 665 | return -1; |
| 666 | |
| 667 | traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast); |
| 668 | free_bitmap_index(bitmap_git); |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static int try_bitmap_disk_usage(struct rev_info *revs, |
| 673 | int filter_provided_objects) |
| 674 | { |
| 675 | struct bitmap_index *bitmap_git; |
| 676 | off_t size_from_bitmap; |
| 677 | |
| 678 | if (!show_disk_usage) |
| 679 | return -1; |
| 680 | |
| 681 | bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects); |
| 682 | if (!bitmap_git) |
| 683 | return -1; |
| 684 | |
| 685 | size_from_bitmap = get_disk_usage_from_bitmap(bitmap_git, revs); |
| 686 | print_disk_usage(size_from_bitmap); |
| 687 | |
| 688 | free_bitmap_index(bitmap_git); |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * If revs->maximal_only is set and no other walk modifiers are provided, |
| 694 | * run a faster computation to filter the independent commits and prepare |
| 695 | * them for output. Set revs->no_walk to prevent later walking. |
| 696 | * |
| 697 | * If this algorithm doesn't apply, then no changes are made to revs. |
| 698 | */ |
| 699 | static void prepare_maximal_independent(struct rev_info *revs) |
| 700 | { |
| 701 | struct commit_list *c; |
| 702 | |
| 703 | if (!revs->maximal_only) |
| 704 | return; |
| 705 | |
| 706 | for (c = revs->commits; c; c = c->next) { |
| 707 | if (c->item->object.flags & UNINTERESTING) |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | if (revs->limited || |
| 712 | revs->topo_order || |
| 713 | revs->first_parent_only || |
| 714 | revs->reverse || |
| 715 | revs->max_count >= 0 || |
| 716 | revs->skip_count >= 0 || |
| 717 | revs->min_age != (timestamp_t)-1 || |
| 718 | revs->max_age != (timestamp_t)-1 || |
| 719 | revs->min_parents > 0 || |
| 720 | revs->max_parents >= 0 || |
| 721 | revs->prune_data.nr || |
| 722 | revs->count || |
| 723 | revs->left_right || |
| 724 | revs->boundary || |
| 725 | revs->tag_objects || |
| 726 | revs->tree_objects || |
| 727 | revs->blob_objects || |
| 728 | revs->filter.choice || |
| 729 | revs->reflog_info || |
| 730 | revs->diff || |
| 731 | revs->grep_filter.pattern_list || |
| 732 | revs->grep_filter.header_list || |
| 733 | revs->verbose_header || |
| 734 | revs->print_parents || |
| 735 | revs->edge_hint || |
| 736 | revs->unpacked || |
| 737 | revs->no_kept_objects || |
| 738 | revs->line_level_traverse) |
| 739 | return; |
| 740 | |
| 741 | reduce_heads_replace(&revs->commits); |
| 742 | |
| 743 | /* Modify 'revs' to only output this commit list. */ |
| 744 | revs->no_walk = 1; |
| 745 | } |
| 746 | |
| 747 | int cmd_rev_list(int argc, |
| 748 | const char **argv, |
| 749 | const char *prefix, |
| 750 | struct repository *repo) |
| 751 | { |
| 752 | struct rev_info revs; |
| 753 | struct rev_list_info info; |
| 754 | struct setup_revision_opt s_r_opt = { |
| 755 | .allow_exclude_promisor_objects = 1, |
| 756 | }; |
| 757 | int i; |
| 758 | int bisect_list = 0; |
| 759 | int bisect_show_vars = 0; |
| 760 | int bisect_find_all = 0; |
| 761 | int use_bitmap_index = 0; |
| 762 | int filter_provided_objects = 0; |
| 763 | struct connectivity_check connectivity_check = { |
| 764 | .max_commits = INCREMENTAL_MAX_COMMITS, |
| 765 | }; |
| 766 | const char *show_progress = NULL; |
| 767 | int ret = 0; |
| 768 | |
| 769 | show_usage_if_asked(argc, argv, rev_list_usage); |
| 770 | |
| 771 | repo_config(the_repository, git_default_config, NULL); |
| 772 | repo_init_revisions(the_repository, &revs, prefix); |
| 773 | revs.abbrev = DEFAULT_ABBREV; |
| 774 | revs.commit_format = CMIT_FMT_UNSPECIFIED; |
| 775 | revs.include_header = 1; |
| 776 | |
| 777 | /* |
| 778 | * Scan the argument list before invoking setup_revisions(), so that we |
| 779 | * know if fetch_if_missing needs to be set to 0. |
| 780 | * |
| 781 | * "--exclude-promisor-objects" acts as a pre-filter on missing objects |
| 782 | * by not crossing the boundary from realized objects to promisor |
| 783 | * objects. |
| 784 | * |
| 785 | * Let "--missing" to conditionally set fetch_if_missing. |
| 786 | */ |
| 787 | |
| 788 | /* |
| 789 | * NEEDSWORK: The next loop is utterly broken. It tries to |
| 790 | * notice an option is used, but without understanding if each |
| 791 | * option takes an argument, which fundamentally would not |
| 792 | * work. It would not know "--grep |
| 793 | * --exclude-promisor-objects" is not triggering |
| 794 | * "--exclude-promisor-objects" option, for example. |
| 795 | * |
| 796 | * We really need setup_revisions() to have a mechanism to |
| 797 | * allow and disallow some sets of options for different |
| 798 | * commands (like rev-list, replay, etc). Such a mechanism |
| 799 | * should do an early parsing of options and be able to manage |
| 800 | * the `--missing=...` and `--exclude-promisor-objects` |
| 801 | * options below. |
| 802 | */ |
| 803 | for (i = 1; i < argc; i++) { |
| 804 | const char *arg = argv[i]; |
| 805 | if (!strcmp(arg, "--exclude-promisor-objects")) { |
| 806 | repo->fetch_if_missing = 0; |
| 807 | revs.exclude_promisor_objects = 1; |
| 808 | } else if (skip_prefix(arg, "--connectivity-check=", &arg)) { |
| 809 | parse_connectivity_check(arg, &connectivity_check); |
| 810 | } else if (skip_prefix(arg, "--missing=", &arg)) { |
| 811 | parse_missing_action_value(repo, arg); |
| 812 | } else if (!strcmp(arg, "--missing-only")) { |
| 813 | arg_missing_only = 1; |
| 814 | } else if (!strcmp(arg, "-z")) { |
| 815 | line_term = '\0'; |
| 816 | info_term = '\0'; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | if (arg_missing_only && !should_collect_missing()) |
| 821 | die(_("--missing-only requires --missing=print or --missing=print-info")); |
| 822 | |
| 823 | die_for_incompatible_opt2(revs.exclude_promisor_objects, |
| 824 | "--exclude_promisor_objects", |
| 825 | arg_missing_action, "--missing"); |
| 826 | |
| 827 | if (arg_missing_action) |
| 828 | revs.do_not_die_on_missing_objects = 1; |
| 829 | |
| 830 | argc = setup_revisions(argc, argv, &revs, &s_r_opt); |
| 831 | |
| 832 | memset(&info, 0, sizeof(info)); |
| 833 | info.revs = &revs; |
| 834 | if (revs.bisect) |
| 835 | bisect_list = 1; |
| 836 | |
| 837 | if (revs.diffopt.flags.quick) |
| 838 | info.flags |= REV_LIST_QUIET; |
| 839 | for (i = 1 ; i < argc; i++) { |
| 840 | const char *arg = argv[i]; |
| 841 | |
| 842 | if (!strcmp(arg, "--header")) { |
| 843 | revs.verbose_header = 1; |
| 844 | continue; |
| 845 | } |
| 846 | if (!strcmp(arg, "--timestamp")) { |
| 847 | info.show_timestamp = 1; |
| 848 | continue; |
| 849 | } |
| 850 | if (!strcmp(arg, "--bisect")) { |
| 851 | bisect_list = 1; |
| 852 | continue; |
| 853 | } |
| 854 | if (!strcmp(arg, "--bisect-all")) { |
| 855 | bisect_list = 1; |
| 856 | bisect_find_all = 1; |
| 857 | info.flags |= BISECT_SHOW_ALL; |
| 858 | revs.show_decorations = 1; |
| 859 | continue; |
| 860 | } |
| 861 | if (!strcmp(arg, "--bisect-vars")) { |
| 862 | bisect_list = 1; |
| 863 | bisect_show_vars = 1; |
| 864 | continue; |
| 865 | } |
| 866 | if (!strcmp(arg, "--use-bitmap-index")) { |
| 867 | use_bitmap_index = 1; |
| 868 | continue; |
| 869 | } |
| 870 | if (!strcmp(arg, "--test-bitmap")) { |
| 871 | test_bitmap_walk(&revs); |
| 872 | goto cleanup; |
| 873 | } |
| 874 | if (skip_prefix(arg, "--progress=", &arg)) { |
| 875 | show_progress = arg; |
| 876 | continue; |
| 877 | } |
| 878 | if (!strcmp(arg, "--filter-provided-objects")) { |
| 879 | filter_provided_objects = 1; |
| 880 | continue; |
| 881 | } |
| 882 | if (!strcmp(arg, "--filter-print-omitted")) { |
| 883 | arg_print_omitted = 1; |
| 884 | continue; |
| 885 | } |
| 886 | |
| 887 | if (!strcmp(arg, "--exclude-promisor-objects")) |
| 888 | continue; /* already handled above */ |
| 889 | if (starts_with(arg, "--connectivity-check=")) |
| 890 | continue; /* already handled above */ |
| 891 | if (skip_prefix(arg, "--missing=", &arg)) |
| 892 | continue; /* already handled above */ |
| 893 | |
| 894 | if (!strcmp(arg, ("--no-object-names"))) { |
| 895 | arg_show_object_names = 0; |
| 896 | continue; |
| 897 | } |
| 898 | |
| 899 | if (!strcmp(arg, ("--object-names"))) { |
| 900 | arg_show_object_names = 1; |
| 901 | continue; |
| 902 | } |
| 903 | |
| 904 | if (!strcmp(arg, ("--commit-header"))) { |
| 905 | revs.include_header = 1; |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | if (!strcmp(arg, ("--no-commit-header"))) { |
| 910 | revs.include_header = 0; |
| 911 | continue; |
| 912 | } |
| 913 | |
| 914 | if (skip_prefix(arg, "--disk-usage", &arg)) { |
| 915 | if (*arg == '=') { |
| 916 | if (!strcmp(++arg, "human")) { |
| 917 | human_readable = 1; |
| 918 | } else |
| 919 | die(_("invalid value for '%s': '%s', the only allowed format is '%s'"), |
| 920 | "--disk-usage=<format>", arg, "human"); |
| 921 | } else if (*arg) { |
| 922 | /* |
| 923 | * Arguably should goto a label to continue chain of ifs? |
| 924 | * Doesn't matter unless we try to add --disk-usage-foo |
| 925 | * afterwards. |
| 926 | */ |
| 927 | usage(rev_list_usage); |
| 928 | } |
| 929 | show_disk_usage = 1; |
| 930 | info.flags |= REV_LIST_QUIET; |
| 931 | continue; |
| 932 | } |
| 933 | |
| 934 | if (!strcmp(arg, "--missing-only")) |
| 935 | continue; |
| 936 | |
| 937 | usage(rev_list_usage); |
| 938 | |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Reject options currently incompatible with -z. For some options, this |
| 943 | * is not an inherent limitation and support may be implemented in the |
| 944 | * future. |
| 945 | */ |
| 946 | if (!line_term) { |
| 947 | if (revs.graph || revs.verbose_header || show_disk_usage || |
| 948 | info.show_timestamp || info.header_prefix || bisect_list || |
| 949 | use_bitmap_index || revs.edge_hint || revs.left_right || |
| 950 | revs.cherry_mark) |
| 951 | die(_("-z option used with unsupported option")); |
| 952 | } |
| 953 | |
| 954 | if (revs.commit_format != CMIT_FMT_USERFORMAT) |
| 955 | revs.include_header = 1; |
| 956 | if (revs.commit_format != CMIT_FMT_UNSPECIFIED) { |
| 957 | /* The command line has a --pretty */ |
| 958 | info.hdr_termination = '\n'; |
| 959 | if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header) |
| 960 | info.header_prefix = ""; |
| 961 | else |
| 962 | info.header_prefix = "commit "; |
| 963 | } |
| 964 | else if (revs.verbose_header) |
| 965 | /* Only --header was specified */ |
| 966 | revs.commit_format = CMIT_FMT_RAW; |
| 967 | |
| 968 | if ((!revs.commits && reflog_walk_empty(revs.reflog_info) && |
| 969 | (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) && |
| 970 | !revs.pending.nr) && |
| 971 | !revs.rev_input_given && !revs.read_from_stdin) || |
| 972 | revs.diff) |
| 973 | usage(rev_list_usage); |
| 974 | |
| 975 | if (revs.show_notes) |
| 976 | die(_("rev-list does not support display of notes")); |
| 977 | |
| 978 | if (revs.count && |
| 979 | (revs.tag_objects || revs.tree_objects || revs.blob_objects) && |
| 980 | (revs.left_right || revs.cherry_mark)) |
| 981 | die(_("marked counting and '%s' cannot be used together"), "--objects"); |
| 982 | |
| 983 | die_for_incompatible_opt2(arg_missing_only, "--missing-only", |
| 984 | revs.count, "--count"); |
| 985 | die_for_incompatible_opt2(arg_missing_only, "--missing-only", |
| 986 | show_disk_usage, "--disk-usage"); |
| 987 | |
| 988 | save_commit_buffer = (revs.verbose_header || |
| 989 | revs.grep_filter.pattern_list || |
| 990 | revs.grep_filter.header_list); |
| 991 | if (bisect_list) |
| 992 | revs.limited = 1; |
| 993 | |
| 994 | if (show_progress) |
| 995 | progress = start_delayed_progress(the_repository, |
| 996 | show_progress, 0); |
| 997 | |
| 998 | if (use_bitmap_index) { |
| 999 | if (!try_bitmap_count(&revs, filter_provided_objects)) |
| 1000 | goto cleanup; |
| 1001 | if (!try_bitmap_disk_usage(&revs, filter_provided_objects)) |
| 1002 | goto cleanup; |
| 1003 | if (!try_bitmap_traversal(&revs, filter_provided_objects)) |
| 1004 | goto cleanup; |
| 1005 | } |
| 1006 | |
| 1007 | if (prepare_revision_walk(&revs)) |
| 1008 | die("revision walk setup failed"); |
| 1009 | |
| 1010 | prepare_maximal_independent(&revs); |
| 1011 | |
| 1012 | /* |
| 1013 | * Without a boundary of negative revisions, as in a clone or a |
| 1014 | * push into an empty repository, every incoming commit is new. |
| 1015 | * The incremental check would still work, but it would verify |
| 1016 | * the whole object graph like the full check, with extra |
| 1017 | * per-commit overhead on top, so use the full check. |
| 1018 | * |
| 1019 | * Otherwise the walk is limited, and revs.commits already holds |
| 1020 | * all incoming commits, so counting them for "auto" is cheap. |
| 1021 | */ |
| 1022 | if (!revs.limited) { |
| 1023 | connectivity_check.mode = CONNECTIVITY_CHECK_FULL; |
| 1024 | } else if (connectivity_check.mode == CONNECTIVITY_CHECK_AUTO) { |
| 1025 | if (commit_list_count(revs.commits) <= |
| 1026 | connectivity_check.max_commits) |
| 1027 | connectivity_check.mode = CONNECTIVITY_CHECK_INCREMENTAL; |
| 1028 | else |
| 1029 | connectivity_check.mode = CONNECTIVITY_CHECK_FULL; |
| 1030 | } |
| 1031 | |
| 1032 | if (connectivity_check.mode == CONNECTIVITY_CHECK_INCREMENTAL) { |
| 1033 | struct commit *commit; |
| 1034 | struct commit_list *new_commits = NULL; |
| 1035 | |
| 1036 | while ((commit = get_revision(&revs)) != NULL) |
| 1037 | commit_list_insert(commit, &new_commits); |
| 1038 | |
| 1039 | if (verify_commits_incremental(repo, &new_commits, |
| 1040 | revs.exclude_promisor_objects)) |
| 1041 | die(_("connectivity check failed")); |
| 1042 | commit_list_free(new_commits); |
| 1043 | } |
| 1044 | |
| 1045 | if (revs.tree_objects) |
| 1046 | mark_edges_uninteresting(&revs, show_edge, 0); |
| 1047 | |
| 1048 | if (bisect_list) { |
| 1049 | int reaches, all; |
| 1050 | unsigned bisect_flags = 0; |
| 1051 | |
| 1052 | if (bisect_find_all) |
| 1053 | bisect_flags |= FIND_BISECTION_ALL; |
| 1054 | |
| 1055 | if (revs.first_parent_only) |
| 1056 | bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY; |
| 1057 | |
| 1058 | find_bisection(&revs.commits, &reaches, &all, bisect_flags); |
| 1059 | |
| 1060 | if (bisect_show_vars) { |
| 1061 | ret = show_bisect_vars(&info, reaches, all); |
| 1062 | goto cleanup; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | if (filter_provided_objects) { |
| 1067 | struct commit_list *c; |
| 1068 | for (i = 0; i < revs.pending.nr; i++) { |
| 1069 | struct object_array_entry *pending = revs.pending.objects + i; |
| 1070 | pending->item->flags |= NOT_USER_GIVEN; |
| 1071 | } |
| 1072 | for (c = revs.commits; c; c = c->next) |
| 1073 | c->item->object.flags |= NOT_USER_GIVEN; |
| 1074 | } |
| 1075 | |
| 1076 | if (arg_print_omitted) |
| 1077 | oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE); |
| 1078 | if (should_collect_missing()) { |
| 1079 | struct oidset_iter iter; |
| 1080 | struct object_id *oid; |
| 1081 | |
| 1082 | oidmap_init(&missing_objects, DEFAULT_OIDSET_SIZE); |
| 1083 | oidset_iter_init(&revs.missing_commits, &iter); |
| 1084 | |
| 1085 | /* Add missing tips */ |
| 1086 | while ((oid = oidset_iter_next(&iter))) |
| 1087 | add_missing_object_entry(oid, NULL, 0); |
| 1088 | |
| 1089 | oidset_clear(&revs.missing_commits); |
| 1090 | } |
| 1091 | |
| 1092 | traverse_commit_list_filtered( |
| 1093 | &revs, show_commit, show_object, &info, |
| 1094 | (arg_print_omitted ? &omitted_objects : NULL)); |
| 1095 | |
| 1096 | if (arg_print_omitted) { |
| 1097 | struct oidset_iter iter; |
| 1098 | struct object_id *oid; |
| 1099 | oidset_iter_init(&omitted_objects, &iter); |
| 1100 | while ((oid = oidset_iter_next(&iter))) |
| 1101 | printf("~%s\n", oid_to_hex(oid)); |
| 1102 | oidset_clear(&omitted_objects); |
| 1103 | } |
| 1104 | if (should_collect_missing()) { |
| 1105 | struct missing_objects_map_entry *entry; |
| 1106 | struct oidmap_iter iter; |
| 1107 | |
| 1108 | oidmap_iter_init(&missing_objects, &iter); |
| 1109 | |
| 1110 | while ((entry = oidmap_iter_next(&iter))) { |
| 1111 | print_missing_object(entry, arg_missing_action == |
| 1112 | MA_PRINT_INFO); |
| 1113 | } |
| 1114 | |
| 1115 | oidmap_clear_with_free(&missing_objects, missing_objects_map_entry_free); |
| 1116 | } |
| 1117 | |
| 1118 | stop_progress(&progress); |
| 1119 | |
| 1120 | if (revs.count) { |
| 1121 | if (revs.left_right && revs.cherry_mark) |
| 1122 | printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same); |
| 1123 | else if (revs.left_right) |
| 1124 | printf("%d\t%d\n", revs.count_left, revs.count_right); |
| 1125 | else if (revs.cherry_mark) |
| 1126 | printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same); |
| 1127 | else |
| 1128 | printf("%d\n", revs.count_left + revs.count_right); |
| 1129 | } |
| 1130 | |
| 1131 | if (show_disk_usage) |
| 1132 | print_disk_usage(total_disk_usage); |
| 1133 | |
| 1134 | cleanup: |
| 1135 | release_revisions(&revs); |
| 1136 | return ret; |
| 1137 | } |