Git fork
at reftables-rust 2435 lines 65 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2 3#include "builtin.h" 4#include "abspath.h" 5#include "config.h" 6#include "environment.h" 7#include "gettext.h" 8#include "hash.h" 9#include "hex.h" 10#include "object-name.h" 11#include "parse-options.h" 12#include "refs.h" 13#include "lockfile.h" 14#include "cache-tree.h" 15#include "unpack-trees.h" 16#include "merge-ort-wrappers.h" 17#include "strvec.h" 18#include "run-command.h" 19#include "dir.h" 20#include "entry.h" 21#include "preload-index.h" 22#include "read-cache.h" 23#include "repository.h" 24#include "rerere.h" 25#include "revision.h" 26#include "setup.h" 27#include "sparse-index.h" 28#include "log-tree.h" 29#include "diffcore.h" 30#include "reflog.h" 31#include "reflog-walk.h" 32#include "add-interactive.h" 33#include "oid-array.h" 34#include "commit.h" 35 36#define INCLUDE_ALL_FILES 2 37 38#define BUILTIN_STASH_LIST_USAGE \ 39 N_("git stash list [<log-options>]") 40#define BUILTIN_STASH_SHOW_USAGE \ 41 N_("git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]") 42#define BUILTIN_STASH_DROP_USAGE \ 43 N_("git stash drop [-q | --quiet] [<stash>]") 44#define BUILTIN_STASH_POP_USAGE \ 45 N_("git stash pop [--index] [-q | --quiet] [<stash>]") 46#define BUILTIN_STASH_APPLY_USAGE \ 47 N_("git stash apply [--index] [-q | --quiet] [<stash>]") 48#define BUILTIN_STASH_BRANCH_USAGE \ 49 N_("git stash branch <branchname> [<stash>]") 50#define BUILTIN_STASH_STORE_USAGE \ 51 N_("git stash store [(-m | --message) <message>] [-q | --quiet] <commit>") 52#define BUILTIN_STASH_PUSH_USAGE \ 53 N_("git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \ 54 " [-u | --include-untracked] [-a | --all] [(-m | --message) <message>]\n" \ 55 " [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" \ 56 " [--] [<pathspec>...]]") 57#define BUILTIN_STASH_SAVE_USAGE \ 58 N_("git stash save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]\n" \ 59 " [-u | --include-untracked] [-a | --all] [<message>]") 60#define BUILTIN_STASH_CREATE_USAGE \ 61 N_("git stash create [<message>]") 62#define BUILTIN_STASH_EXPORT_USAGE \ 63 N_("git stash export (--print | --to-ref <ref>) [<stash>...]") 64#define BUILTIN_STASH_IMPORT_USAGE \ 65 N_("git stash import <commit>") 66#define BUILTIN_STASH_CLEAR_USAGE \ 67 "git stash clear" 68 69static const char * const git_stash_usage[] = { 70 BUILTIN_STASH_LIST_USAGE, 71 BUILTIN_STASH_SHOW_USAGE, 72 BUILTIN_STASH_DROP_USAGE, 73 BUILTIN_STASH_POP_USAGE, 74 BUILTIN_STASH_APPLY_USAGE, 75 BUILTIN_STASH_BRANCH_USAGE, 76 BUILTIN_STASH_PUSH_USAGE, 77 BUILTIN_STASH_SAVE_USAGE, 78 BUILTIN_STASH_CLEAR_USAGE, 79 BUILTIN_STASH_CREATE_USAGE, 80 BUILTIN_STASH_STORE_USAGE, 81 BUILTIN_STASH_EXPORT_USAGE, 82 BUILTIN_STASH_IMPORT_USAGE, 83 NULL 84}; 85 86static const char * const git_stash_list_usage[] = { 87 BUILTIN_STASH_LIST_USAGE, 88 NULL 89}; 90 91static const char * const git_stash_show_usage[] = { 92 BUILTIN_STASH_SHOW_USAGE, 93 NULL 94}; 95 96static const char * const git_stash_drop_usage[] = { 97 BUILTIN_STASH_DROP_USAGE, 98 NULL 99}; 100 101static const char * const git_stash_pop_usage[] = { 102 BUILTIN_STASH_POP_USAGE, 103 NULL 104}; 105 106static const char * const git_stash_apply_usage[] = { 107 BUILTIN_STASH_APPLY_USAGE, 108 NULL 109}; 110 111static const char * const git_stash_branch_usage[] = { 112 BUILTIN_STASH_BRANCH_USAGE, 113 NULL 114}; 115 116static const char * const git_stash_clear_usage[] = { 117 BUILTIN_STASH_CLEAR_USAGE, 118 NULL 119}; 120 121static const char * const git_stash_store_usage[] = { 122 BUILTIN_STASH_STORE_USAGE, 123 NULL 124}; 125 126static const char * const git_stash_push_usage[] = { 127 BUILTIN_STASH_PUSH_USAGE, 128 NULL 129}; 130 131static const char * const git_stash_save_usage[] = { 132 BUILTIN_STASH_SAVE_USAGE, 133 NULL 134}; 135 136static const char * const git_stash_export_usage[] = { 137 BUILTIN_STASH_EXPORT_USAGE, 138 NULL 139}; 140 141static const char * const git_stash_import_usage[] = { 142 BUILTIN_STASH_IMPORT_USAGE, 143 NULL 144}; 145 146static const char ref_stash[] = "refs/stash"; 147static struct strbuf stash_index_path = STRBUF_INIT; 148 149static int show_stat = 1; 150static int show_patch; 151static int show_include_untracked; 152static int use_index; 153 154/* 155 * w_commit is set to the commit containing the working tree 156 * b_commit is set to the base commit 157 * i_commit is set to the commit containing the index tree 158 * u_commit is set to the commit containing the untracked files tree 159 * c_commit is set to the first parent (chain commit) when importing and is otherwise unset 160 * w_tree is set to the working tree 161 * b_tree is set to the base tree 162 * i_tree is set to the index tree 163 * u_tree is set to the untracked files tree 164 */ 165struct stash_info { 166 struct object_id w_commit; 167 struct object_id b_commit; 168 struct object_id i_commit; 169 struct object_id u_commit; 170 struct object_id c_commit; 171 struct object_id w_tree; 172 struct object_id b_tree; 173 struct object_id i_tree; 174 struct object_id u_tree; 175 struct strbuf revision; 176 int is_stash_ref; 177 int has_u; 178}; 179 180#define STASH_INFO_INIT { \ 181 .revision = STRBUF_INIT, \ 182} 183 184static void free_stash_info(struct stash_info *info) 185{ 186 strbuf_release(&info->revision); 187} 188 189static int check_stash_topology(struct repository *r, struct commit *stash) 190{ 191 struct commit *p1, *p2, *p3 = NULL; 192 193 /* stash must have two or three parents */ 194 if (!stash->parents || !stash->parents->next || 195 (stash->parents->next->next && stash->parents->next->next->next)) 196 return -1; 197 p1 = stash->parents->item; 198 p2 = stash->parents->next->item; 199 if (stash->parents->next->next) 200 p3 = stash->parents->next->next->item; 201 if (repo_parse_commit(r, p1) || repo_parse_commit(r, p2) || 202 (p3 && repo_parse_commit(r, p3))) 203 return -1; 204 /* p2 must have a single parent, p3 must have no parents */ 205 if (!p2->parents || p2->parents->next || (p3 && p3->parents)) 206 return -1; 207 if (repo_parse_commit(r, p2->parents->item)) 208 return -1; 209 /* p2^1 must equal p1 */ 210 if (!oideq(&p1->object.oid, &p2->parents->item->object.oid)) 211 return -1; 212 213 return 0; 214} 215 216static void assert_stash_like(struct stash_info *info, const char *revision) 217{ 218 if (get_oidf(&info->b_commit, "%s^1", revision) || 219 get_oidf(&info->w_tree, "%s:", revision) || 220 get_oidf(&info->b_tree, "%s^1:", revision) || 221 get_oidf(&info->i_tree, "%s^2:", revision)) 222 die(_("'%s' is not a stash-like commit"), revision); 223} 224 225static int parse_stash_revision(struct strbuf *revision, const char *commit, int quiet) 226{ 227 strbuf_reset(revision); 228 if (!commit) { 229 if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) { 230 if (!quiet) 231 fprintf_ln(stderr, _("No stash entries found.")); 232 return -1; 233 } 234 235 strbuf_addf(revision, "%s@{0}", ref_stash); 236 } else if (strspn(commit, "0123456789") == strlen(commit)) { 237 strbuf_addf(revision, "%s@{%s}", ref_stash, commit); 238 } else { 239 strbuf_addstr(revision, commit); 240 } 241 return 0; 242} 243 244static int get_stash_info(struct stash_info *info, int argc, const char **argv) 245{ 246 int ret; 247 char *end_of_rev; 248 char *expanded_ref; 249 const char *revision; 250 const char *commit = NULL; 251 struct object_id dummy; 252 struct strbuf symbolic = STRBUF_INIT; 253 254 if (argc > 1) { 255 int i; 256 struct strbuf refs_msg = STRBUF_INIT; 257 258 for (i = 0; i < argc; i++) 259 strbuf_addf(&refs_msg, " '%s'", argv[i]); 260 261 fprintf_ln(stderr, _("Too many revisions specified:%s"), 262 refs_msg.buf); 263 strbuf_release(&refs_msg); 264 265 return -1; 266 } 267 268 if (argc == 1) 269 commit = argv[0]; 270 271 strbuf_init(&info->revision, 0); 272 if (parse_stash_revision(&info->revision, commit, 0)) { 273 return -1; 274 } 275 276 revision = info->revision.buf; 277 278 if (repo_get_oid(the_repository, revision, &info->w_commit)) 279 return error(_("%s is not a valid reference"), revision); 280 281 assert_stash_like(info, revision); 282 283 info->has_u = !get_oidf(&info->u_tree, "%s^3:", revision); 284 285 end_of_rev = strchrnul(revision, '@'); 286 strbuf_add(&symbolic, revision, end_of_rev - revision); 287 288 ret = repo_dwim_ref(the_repository, symbolic.buf, symbolic.len, 289 &dummy, &expanded_ref, 0); 290 strbuf_release(&symbolic); 291 switch (ret) { 292 case 0: /* Not found, but valid ref */ 293 info->is_stash_ref = 0; 294 break; 295 case 1: 296 info->is_stash_ref = !strcmp(expanded_ref, ref_stash); 297 break; 298 default: /* Invalid or ambiguous */ 299 break; 300 } 301 302 free(expanded_ref); 303 return !(ret == 0 || ret == 1); 304} 305 306static int do_clear_stash(void) 307{ 308 struct object_id obj; 309 if (repo_get_oid(the_repository, ref_stash, &obj)) 310 return 0; 311 312 return refs_delete_ref(get_main_ref_store(the_repository), NULL, 313 ref_stash, &obj, 0); 314} 315 316static int clear_stash(int argc, const char **argv, const char *prefix, 317 struct repository *repo UNUSED) 318{ 319 struct option options[] = { 320 OPT_END() 321 }; 322 323 argc = parse_options(argc, argv, prefix, options, 324 git_stash_clear_usage, 325 PARSE_OPT_STOP_AT_NON_OPTION); 326 327 if (argc) 328 return error(_("git stash clear with arguments is " 329 "unimplemented")); 330 331 return do_clear_stash(); 332} 333 334static int reset_tree(struct object_id *i_tree, int update, int reset) 335{ 336 int nr_trees = 1; 337 struct unpack_trees_options opts; 338 struct tree_desc t[MAX_UNPACK_TREES]; 339 struct tree *tree; 340 struct lock_file lock_file = LOCK_INIT; 341 342 repo_read_index_preload(the_repository, NULL, 0); 343 if (refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL)) 344 return -1; 345 346 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); 347 348 memset(&opts, 0, sizeof(opts)); 349 350 tree = parse_tree_indirect(i_tree); 351 if (parse_tree(tree)) 352 return -1; 353 354 init_tree_desc(t, &tree->object.oid, tree->buffer, tree->size); 355 356 opts.head_idx = 1; 357 opts.src_index = the_repository->index; 358 opts.dst_index = the_repository->index; 359 opts.merge = 1; 360 opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0; 361 opts.update = update; 362 if (update) 363 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ 364 opts.fn = oneway_merge; 365 366 if (unpack_trees(nr_trees, t, &opts)) 367 return -1; 368 369 if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK)) 370 return error(_("unable to write new index file")); 371 372 return 0; 373} 374 375static int diff_tree_binary(struct strbuf *out, struct object_id *w_commit) 376{ 377 struct child_process cp = CHILD_PROCESS_INIT; 378 const char *w_commit_hex = oid_to_hex(w_commit); 379 380 /* 381 * Diff-tree would not be very hard to replace with a native function, 382 * however it should be done together with apply_cached. 383 */ 384 cp.git_cmd = 1; 385 strvec_pushl(&cp.args, "diff-tree", "--binary", "--no-color", NULL); 386 strvec_pushf(&cp.args, "%s^2^..%s^2", w_commit_hex, w_commit_hex); 387 388 return pipe_command(&cp, NULL, 0, out, 0, NULL, 0); 389} 390 391static int apply_cached(struct strbuf *out) 392{ 393 struct child_process cp = CHILD_PROCESS_INIT; 394 395 /* 396 * Apply currently only reads either from stdin or a file, thus 397 * apply_all_patches would have to be updated to optionally take a 398 * buffer. 399 */ 400 cp.git_cmd = 1; 401 strvec_pushl(&cp.args, "apply", "--cached", NULL); 402 return pipe_command(&cp, out->buf, out->len, NULL, 0, NULL, 0); 403} 404 405static int reset_head(void) 406{ 407 struct child_process cp = CHILD_PROCESS_INIT; 408 409 /* 410 * Reset is overall quite simple, however there is no current public 411 * API for resetting. 412 */ 413 cp.git_cmd = 1; 414 strvec_pushl(&cp.args, "reset", "--quiet", "--refresh", NULL); 415 416 return run_command(&cp); 417} 418 419static int is_path_a_directory(const char *path) 420{ 421 /* 422 * This function differs from abspath.c:is_directory() in that 423 * here we use lstat() instead of stat(); we do not want to 424 * follow symbolic links here. 425 */ 426 struct stat st; 427 return (!lstat(path, &st) && S_ISDIR(st.st_mode)); 428} 429 430static void add_diff_to_buf(struct diff_queue_struct *q, 431 struct diff_options *options UNUSED, 432 void *data) 433{ 434 int i; 435 436 for (i = 0; i < q->nr; i++) { 437 if (is_path_a_directory(q->queue[i]->one->path)) 438 continue; 439 440 strbuf_addstr(data, q->queue[i]->one->path); 441 442 /* NUL-terminate: will be fed to update-index -z */ 443 strbuf_addch(data, '\0'); 444 } 445} 446 447static int restore_untracked(struct object_id *u_tree) 448{ 449 int res; 450 struct child_process cp = CHILD_PROCESS_INIT; 451 452 /* 453 * We need to run restore files from a given index, but without 454 * affecting the current index, so we use GIT_INDEX_FILE with 455 * run_command to fork processes that will not interfere. 456 */ 457 cp.git_cmd = 1; 458 strvec_push(&cp.args, "read-tree"); 459 strvec_push(&cp.args, oid_to_hex(u_tree)); 460 strvec_pushf(&cp.env, "GIT_INDEX_FILE=%s", 461 stash_index_path.buf); 462 if (run_command(&cp)) { 463 remove_path(stash_index_path.buf); 464 return -1; 465 } 466 467 child_process_init(&cp); 468 cp.git_cmd = 1; 469 strvec_pushl(&cp.args, "checkout-index", "--all", NULL); 470 strvec_pushf(&cp.env, "GIT_INDEX_FILE=%s", 471 stash_index_path.buf); 472 473 res = run_command(&cp); 474 remove_path(stash_index_path.buf); 475 return res; 476} 477 478static void unstage_changes_unless_new(struct object_id *orig_tree) 479{ 480 /* 481 * When we enter this function, there has been a clean merge of 482 * relevant trees, and the merge logic always stages whatever merges 483 * cleanly. We want to unstage those changes, unless it corresponds 484 * to a file that didn't exist as of orig_tree. 485 * 486 * However, if any SKIP_WORKTREE path is modified relative to 487 * orig_tree, then we want to clear the SKIP_WORKTREE bit and write 488 * it to the worktree before unstaging. 489 */ 490 491 struct checkout state = CHECKOUT_INIT; 492 struct diff_options diff_opts; 493 struct lock_file lock = LOCK_INIT; 494 int i; 495 496 /* If any entries have skip_worktree set, we'll have to check 'em out */ 497 state.force = 1; 498 state.quiet = 1; 499 state.refresh_cache = 1; 500 state.istate = the_repository->index; 501 502 /* 503 * Step 1: get a difference between orig_tree (which corresponding 504 * to the index before a merge was run) and the current index 505 * (reflecting the changes brought in by the merge). 506 */ 507 repo_diff_setup(the_repository, &diff_opts); 508 diff_opts.flags.recursive = 1; 509 diff_opts.detect_rename = 0; 510 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; 511 diff_setup_done(&diff_opts); 512 513 do_diff_cache(orig_tree, &diff_opts); 514 diffcore_std(&diff_opts); 515 516 /* Iterate over the paths that changed due to the merge... */ 517 for (i = 0; i < diff_queued_diff.nr; i++) { 518 struct diff_filepair *p; 519 struct cache_entry *ce; 520 int pos; 521 522 /* Look up the path's position in the current index. */ 523 p = diff_queued_diff.queue[i]; 524 pos = index_name_pos(the_repository->index, p->two->path, 525 strlen(p->two->path)); 526 527 /* 528 * Step 2: Place changes in the working tree 529 * 530 * Stash is about restoring changes *to the working tree*. 531 * So if the merge successfully got a new version of some 532 * path, but left it out of the working tree, then clear the 533 * SKIP_WORKTREE bit and write it to the working tree. 534 */ 535 if (pos >= 0 && ce_skip_worktree(the_repository->index->cache[pos])) { 536 struct stat st; 537 538 ce = the_repository->index->cache[pos]; 539 if (!lstat(ce->name, &st)) { 540 /* Conflicting path present; relocate it */ 541 struct strbuf new_path = STRBUF_INIT; 542 int fd; 543 544 strbuf_addf(&new_path, 545 "%s.stash.XXXXXX", ce->name); 546 fd = xmkstemp(new_path.buf); 547 close(fd); 548 printf(_("WARNING: Untracked file in way of " 549 "tracked file! Renaming\n " 550 " %s -> %s\n" 551 " to make room.\n"), 552 ce->name, new_path.buf); 553 if (rename(ce->name, new_path.buf)) 554 die("Failed to move %s to %s", 555 ce->name, new_path.buf); 556 strbuf_release(&new_path); 557 } 558 checkout_entry(ce, &state, NULL, NULL); 559 ce->ce_flags &= ~CE_SKIP_WORKTREE; 560 } 561 562 /* 563 * Step 3: "unstage" changes, as long as they are still tracked 564 */ 565 if (p->one->oid_valid) { 566 /* 567 * Path existed in orig_tree; restore index entry 568 * from that tree in order to "unstage" the changes. 569 */ 570 int option = ADD_CACHE_OK_TO_REPLACE; 571 if (pos < 0) 572 option = ADD_CACHE_OK_TO_ADD; 573 574 ce = make_cache_entry(the_repository->index, 575 p->one->mode, 576 &p->one->oid, 577 p->one->path, 578 0, 0); 579 add_index_entry(the_repository->index, ce, option); 580 } 581 } 582 diff_flush(&diff_opts); 583 584 /* 585 * Step 4: write the new index to disk 586 */ 587 repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR); 588 if (write_locked_index(the_repository->index, &lock, 589 COMMIT_LOCK | SKIP_IF_UNCHANGED)) 590 die(_("could not write index")); 591} 592 593static int do_apply_stash(const char *prefix, struct stash_info *info, 594 int index, int quiet) 595{ 596 int clean, ret; 597 int has_index = index; 598 struct merge_options o; 599 struct object_id c_tree; 600 struct object_id index_tree; 601 struct tree *head, *merge, *merge_base; 602 struct lock_file lock = LOCK_INIT; 603 604 repo_read_index_preload(the_repository, NULL, 0); 605 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0, 606 NULL, NULL, NULL)) 607 return error(_("could not write index")); 608 609 if (write_index_as_tree(&c_tree, the_repository->index, 610 repo_get_index_file(the_repository), 0, NULL)) 611 return error(_("cannot apply a stash in the middle of a merge")); 612 613 if (index) { 614 if (oideq(&info->b_tree, &info->i_tree) || 615 oideq(&c_tree, &info->i_tree)) { 616 has_index = 0; 617 } else { 618 struct strbuf out = STRBUF_INIT; 619 620 if (diff_tree_binary(&out, &info->w_commit)) { 621 strbuf_release(&out); 622 return error(_("could not generate diff %s^!."), 623 oid_to_hex(&info->w_commit)); 624 } 625 626 ret = apply_cached(&out); 627 strbuf_release(&out); 628 if (ret) 629 return error(_("conflicts in index. " 630 "Try without --index.")); 631 632 discard_index(the_repository->index); 633 repo_read_index(the_repository); 634 if (write_index_as_tree(&index_tree, the_repository->index, 635 repo_get_index_file(the_repository), 0, NULL)) 636 return error(_("could not save index tree")); 637 638 reset_head(); 639 discard_index(the_repository->index); 640 repo_read_index(the_repository); 641 } 642 } 643 644 init_ui_merge_options(&o, the_repository); 645 646 o.branch1 = "Updated upstream"; 647 o.branch2 = "Stashed changes"; 648 o.ancestor = "Stash base"; 649 650 if (oideq(&info->b_tree, &c_tree)) 651 o.branch1 = "Version stash was based on"; 652 653 if (quiet) 654 o.verbosity = 0; 655 656 if (o.verbosity >= 3) 657 printf_ln(_("Merging %s with %s"), o.branch1, o.branch2); 658 659 head = lookup_tree(o.repo, &c_tree); 660 merge = lookup_tree(o.repo, &info->w_tree); 661 merge_base = lookup_tree(o.repo, &info->b_tree); 662 663 repo_hold_locked_index(o.repo, &lock, LOCK_DIE_ON_ERROR); 664 clean = merge_ort_nonrecursive(&o, head, merge, merge_base); 665 666 /* 667 * If 'clean' >= 0, reverse the value for 'ret' so 'ret' is 0 when the 668 * merge was clean, and nonzero if the merge was unclean or encountered 669 * an error. 670 */ 671 ret = clean >= 0 ? !clean : clean; 672 673 if (ret < 0) 674 rollback_lock_file(&lock); 675 else if (write_locked_index(o.repo->index, &lock, 676 COMMIT_LOCK | SKIP_IF_UNCHANGED)) 677 ret = error(_("could not write index")); 678 679 if (ret) { 680 repo_rerere(the_repository, 0); 681 682 if (index) 683 fprintf_ln(stderr, _("Index was not unstashed.")); 684 685 goto restore_untracked; 686 } 687 688 if (has_index) { 689 if (reset_tree(&index_tree, 0, 0)) 690 ret = -1; 691 } else { 692 unstage_changes_unless_new(&c_tree); 693 } 694 695restore_untracked: 696 if (info->has_u && restore_untracked(&info->u_tree)) 697 ret = error(_("could not restore untracked files from stash")); 698 699 if (!quiet) { 700 struct child_process cp = CHILD_PROCESS_INIT; 701 702 /* 703 * Status is quite simple and could be replaced with calls to 704 * wt_status in the future, but it adds complexities which may 705 * require more tests. 706 */ 707 cp.git_cmd = 1; 708 cp.dir = prefix; 709 strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s", 710 absolute_path(repo_get_work_tree(the_repository))); 711 strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s", 712 absolute_path(repo_get_git_dir(the_repository))); 713 strvec_push(&cp.args, "status"); 714 run_command(&cp); 715 } 716 717 return ret; 718} 719 720static int apply_stash(int argc, const char **argv, const char *prefix, 721 struct repository *repo UNUSED) 722{ 723 int ret = -1; 724 int quiet = 0; 725 int index = use_index; 726 struct stash_info info = STASH_INFO_INIT; 727 struct option options[] = { 728 OPT__QUIET(&quiet, N_("be quiet, only report errors")), 729 OPT_BOOL(0, "index", &index, 730 N_("attempt to recreate the index")), 731 OPT_END() 732 }; 733 734 argc = parse_options(argc, argv, prefix, options, 735 git_stash_apply_usage, 0); 736 737 if (get_stash_info(&info, argc, argv)) 738 goto cleanup; 739 740 ret = do_apply_stash(prefix, &info, index, quiet); 741cleanup: 742 free_stash_info(&info); 743 return ret; 744} 745 746static int reject_reflog_ent(const char *refname UNUSED, 747 struct object_id *ooid UNUSED, 748 struct object_id *noid UNUSED, 749 const char *email UNUSED, 750 timestamp_t timestamp UNUSED, 751 int tz UNUSED, const char *message UNUSED, 752 void *cb_data UNUSED) 753{ 754 return 1; 755} 756 757static int reflog_is_empty(const char *refname) 758{ 759 return !refs_for_each_reflog_ent(get_main_ref_store(the_repository), 760 refname, reject_reflog_ent, NULL); 761} 762 763static int do_drop_stash(struct stash_info *info, int quiet) 764{ 765 if (!reflog_delete(info->revision.buf, 766 EXPIRE_REFLOGS_REWRITE | EXPIRE_REFLOGS_UPDATE_REF, 767 0)) { 768 if (!quiet) 769 printf_ln(_("Dropped %s (%s)"), info->revision.buf, 770 oid_to_hex(&info->w_commit)); 771 } else { 772 return error(_("%s: Could not drop stash entry"), 773 info->revision.buf); 774 } 775 776 if (reflog_is_empty(ref_stash)) 777 do_clear_stash(); 778 779 return 0; 780} 781 782static int get_stash_info_assert(struct stash_info *info, int argc, 783 const char **argv) 784{ 785 int ret = get_stash_info(info, argc, argv); 786 787 if (ret < 0) 788 return ret; 789 790 if (!info->is_stash_ref) 791 return error(_("'%s' is not a stash reference"), info->revision.buf); 792 793 return 0; 794} 795 796static int drop_stash(int argc, const char **argv, const char *prefix, 797 struct repository *repo UNUSED) 798{ 799 int ret = -1; 800 int quiet = 0; 801 struct stash_info info = STASH_INFO_INIT; 802 struct option options[] = { 803 OPT__QUIET(&quiet, N_("be quiet, only report errors")), 804 OPT_END() 805 }; 806 807 argc = parse_options(argc, argv, prefix, options, 808 git_stash_drop_usage, 0); 809 810 if (get_stash_info_assert(&info, argc, argv)) 811 goto cleanup; 812 813 ret = do_drop_stash(&info, quiet); 814cleanup: 815 free_stash_info(&info); 816 return ret; 817} 818 819static int pop_stash(int argc, const char **argv, const char *prefix, 820 struct repository *repo UNUSED) 821{ 822 int ret = -1; 823 int index = use_index; 824 int quiet = 0; 825 struct stash_info info = STASH_INFO_INIT; 826 struct option options[] = { 827 OPT__QUIET(&quiet, N_("be quiet, only report errors")), 828 OPT_BOOL(0, "index", &index, 829 N_("attempt to recreate the index")), 830 OPT_END() 831 }; 832 833 argc = parse_options(argc, argv, prefix, options, 834 git_stash_pop_usage, 0); 835 836 if (get_stash_info_assert(&info, argc, argv)) 837 goto cleanup; 838 839 if ((ret = do_apply_stash(prefix, &info, index, quiet))) 840 printf_ln(_("The stash entry is kept in case " 841 "you need it again.")); 842 else 843 ret = do_drop_stash(&info, quiet); 844 845cleanup: 846 free_stash_info(&info); 847 return ret; 848} 849 850static int branch_stash(int argc, const char **argv, const char *prefix, 851 struct repository *repo UNUSED) 852{ 853 int ret = -1; 854 const char *branch = NULL; 855 struct stash_info info = STASH_INFO_INIT; 856 struct child_process cp = CHILD_PROCESS_INIT; 857 struct option options[] = { 858 OPT_END() 859 }; 860 861 argc = parse_options(argc, argv, prefix, options, 862 git_stash_branch_usage, 0); 863 864 if (!argc) { 865 fprintf_ln(stderr, _("No branch name specified")); 866 return -1; 867 } 868 869 branch = argv[0]; 870 871 if (get_stash_info(&info, argc - 1, argv + 1)) 872 goto cleanup; 873 874 cp.git_cmd = 1; 875 strvec_pushl(&cp.args, "checkout", "-b", NULL); 876 strvec_push(&cp.args, branch); 877 strvec_push(&cp.args, oid_to_hex(&info.b_commit)); 878 ret = run_command(&cp); 879 if (!ret) 880 ret = do_apply_stash(prefix, &info, 1, 0); 881 if (!ret && info.is_stash_ref) 882 ret = do_drop_stash(&info, 0); 883 884cleanup: 885 free_stash_info(&info); 886 return ret; 887} 888 889static int list_stash(int argc, const char **argv, const char *prefix, 890 struct repository *repo UNUSED) 891{ 892 struct child_process cp = CHILD_PROCESS_INIT; 893 struct option options[] = { 894 OPT_END() 895 }; 896 897 argc = parse_options(argc, argv, prefix, options, 898 git_stash_list_usage, 899 PARSE_OPT_KEEP_UNKNOWN_OPT); 900 901 if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) 902 return 0; 903 904 cp.git_cmd = 1; 905 strvec_pushl(&cp.args, "log", "--format=%gd: %gs", "-g", 906 "--first-parent", NULL); 907 strvec_pushv(&cp.args, argv); 908 strvec_push(&cp.args, ref_stash); 909 strvec_push(&cp.args, "--"); 910 return run_command(&cp); 911} 912 913static int git_stash_config(const char *var, const char *value, 914 const struct config_context *ctx, void *cb) 915{ 916 if (!strcmp(var, "stash.showstat")) { 917 show_stat = git_config_bool(var, value); 918 return 0; 919 } 920 if (!strcmp(var, "stash.showpatch")) { 921 show_patch = git_config_bool(var, value); 922 return 0; 923 } 924 if (!strcmp(var, "stash.showincludeuntracked")) { 925 show_include_untracked = git_config_bool(var, value); 926 return 0; 927 } 928 if (!strcmp(var, "stash.index")) { 929 use_index = git_config_bool(var, value); 930 return 0; 931 } 932 return git_diff_basic_config(var, value, ctx, cb); 933} 934 935static void diff_include_untracked(const struct stash_info *info, struct diff_options *diff_opt) 936{ 937 const struct object_id *oid[] = { &info->w_commit, &info->u_tree }; 938 struct tree *tree[ARRAY_SIZE(oid)]; 939 struct tree_desc tree_desc[ARRAY_SIZE(oid)]; 940 struct unpack_trees_options unpack_tree_opt = { 0 }; 941 942 for (size_t i = 0; i < ARRAY_SIZE(oid); i++) { 943 tree[i] = parse_tree_indirect(oid[i]); 944 if (parse_tree(tree[i]) < 0) 945 die(_("failed to parse tree")); 946 init_tree_desc(&tree_desc[i], &tree[i]->object.oid, 947 tree[i]->buffer, tree[i]->size); 948 } 949 950 unpack_tree_opt.head_idx = -1; 951 unpack_tree_opt.src_index = the_repository->index; 952 unpack_tree_opt.dst_index = the_repository->index; 953 unpack_tree_opt.merge = 1; 954 unpack_tree_opt.fn = stash_worktree_untracked_merge; 955 956 if (unpack_trees(ARRAY_SIZE(tree_desc), tree_desc, &unpack_tree_opt)) 957 die(_("failed to unpack trees")); 958 959 do_diff_cache(&info->b_commit, diff_opt); 960} 961 962static int show_stash(int argc, const char **argv, const char *prefix, 963 struct repository *repo UNUSED) 964{ 965 int i; 966 int ret = -1; 967 struct stash_info info = STASH_INFO_INIT; 968 struct rev_info rev; 969 struct strvec stash_args = STRVEC_INIT; 970 struct strvec revision_args = STRVEC_INIT; 971 enum { 972 UNTRACKED_NONE, 973 UNTRACKED_INCLUDE, 974 UNTRACKED_ONLY 975 } show_untracked = show_include_untracked ? UNTRACKED_INCLUDE : UNTRACKED_NONE; 976 struct option options[] = { 977 OPT_SET_INT('u', "include-untracked", &show_untracked, 978 N_("include untracked files in the stash"), 979 UNTRACKED_INCLUDE), 980 OPT_SET_INT_F(0, "only-untracked", &show_untracked, 981 N_("only show untracked files in the stash"), 982 UNTRACKED_ONLY, PARSE_OPT_NONEG), 983 OPT_END() 984 }; 985 int do_usage = 0; 986 987 init_diff_ui_defaults(); 988 repo_config(the_repository, git_diff_ui_config, NULL); 989 repo_init_revisions(the_repository, &rev, prefix); 990 991 argc = parse_options(argc, argv, prefix, options, git_stash_show_usage, 992 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT | 993 PARSE_OPT_KEEP_DASHDASH); 994 995 strvec_push(&revision_args, argv[0]); 996 for (i = 1; i < argc; i++) { 997 if (argv[i][0] != '-') 998 strvec_push(&stash_args, argv[i]); 999 else 1000 strvec_push(&revision_args, argv[i]); 1001 } 1002 1003 if (get_stash_info(&info, stash_args.nr, stash_args.v)) 1004 goto cleanup; 1005 1006 /* 1007 * The config settings are applied only if there are not passed 1008 * any options. 1009 */ 1010 if (revision_args.nr == 1) { 1011 if (show_stat) 1012 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT; 1013 1014 if (show_patch) 1015 rev.diffopt.output_format |= DIFF_FORMAT_PATCH; 1016 1017 if (!show_stat && !show_patch) { 1018 ret = 0; 1019 goto cleanup; 1020 } 1021 } 1022 1023 setup_revisions_from_strvec(&revision_args, &rev, NULL); 1024 if (revision_args.nr > 1) 1025 goto usage; 1026 if (!rev.diffopt.output_format) { 1027 rev.diffopt.output_format = DIFF_FORMAT_PATCH; 1028 diff_setup_done(&rev.diffopt); 1029 } 1030 1031 rev.diffopt.flags.recursive = 1; 1032 setup_diff_pager(&rev.diffopt); 1033 switch (show_untracked) { 1034 case UNTRACKED_NONE: 1035 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt); 1036 break; 1037 case UNTRACKED_ONLY: 1038 if (info.has_u) 1039 diff_root_tree_oid(&info.u_tree, "", &rev.diffopt); 1040 break; 1041 case UNTRACKED_INCLUDE: 1042 if (info.has_u) 1043 diff_include_untracked(&info, &rev.diffopt); 1044 else 1045 diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt); 1046 break; 1047 } 1048 log_tree_diff_flush(&rev); 1049 1050 ret = diff_result_code(&rev); 1051 1052cleanup: 1053 strvec_clear(&revision_args); 1054 strvec_clear(&stash_args); 1055 free_stash_info(&info); 1056 release_revisions(&rev); 1057 if (do_usage) 1058 usage_with_options(git_stash_show_usage, options); 1059 return ret; 1060usage: 1061 do_usage = 1; 1062 goto cleanup; 1063} 1064 1065static int do_store_stash(const struct object_id *w_commit, const char *stash_msg, 1066 int quiet) 1067{ 1068 struct stash_info info; 1069 char revision[GIT_MAX_HEXSZ]; 1070 1071 oid_to_hex_r(revision, w_commit); 1072 assert_stash_like(&info, revision); 1073 1074 if (!stash_msg) 1075 stash_msg = "Created via \"git stash store\"."; 1076 1077 if (refs_update_ref(get_main_ref_store(the_repository), stash_msg, ref_stash, w_commit, NULL, 1078 REF_FORCE_CREATE_REFLOG, 1079 quiet ? UPDATE_REFS_QUIET_ON_ERR : 1080 UPDATE_REFS_MSG_ON_ERR)) { 1081 if (!quiet) { 1082 fprintf_ln(stderr, _("Cannot update %s with %s"), 1083 ref_stash, oid_to_hex(w_commit)); 1084 } 1085 return -1; 1086 } 1087 1088 return 0; 1089} 1090 1091static int store_stash(int argc, const char **argv, const char *prefix, 1092 struct repository *repo UNUSED) 1093{ 1094 int quiet = 0; 1095 const char *stash_msg = NULL; 1096 struct object_id obj; 1097 struct option options[] = { 1098 OPT__QUIET(&quiet, N_("be quiet")), 1099 OPT_STRING('m', "message", &stash_msg, "message", 1100 N_("stash message")), 1101 OPT_END() 1102 }; 1103 int ret; 1104 1105 argc = parse_options(argc, argv, prefix, options, 1106 git_stash_store_usage, 1107 PARSE_OPT_KEEP_UNKNOWN_OPT); 1108 1109 if (argc != 1) { 1110 if (!quiet) 1111 fprintf_ln(stderr, _("\"git stash store\" requires one " 1112 "<commit> argument")); 1113 return -1; 1114 } 1115 1116 if (repo_get_oid_with_flags(the_repository, argv[0], &obj, 1117 quiet ? GET_OID_QUIETLY : 0)) { 1118 if (!quiet) 1119 fprintf_ln(stderr, _("Cannot update %s with %s"), 1120 ref_stash, argv[0]); 1121 ret = -1; 1122 goto out; 1123 } 1124 1125 ret = do_store_stash(&obj, stash_msg, quiet); 1126 1127out: 1128 return ret; 1129} 1130 1131static void add_pathspecs(struct strvec *args, 1132 const struct pathspec *ps) { 1133 int i; 1134 1135 for (i = 0; i < ps->nr; i++) 1136 strvec_push(args, ps->items[i].original); 1137} 1138 1139/* 1140 * `untracked_files` will be filled with the names of untracked files. 1141 * The return value is: 1142 * 1143 * = 0 if there are not any untracked files 1144 * > 0 if there are untracked files 1145 */ 1146static int get_untracked_files(const struct pathspec *ps, int include_untracked, 1147 struct strbuf *untracked_files) 1148{ 1149 int i; 1150 int found = 0; 1151 struct dir_struct dir = DIR_INIT; 1152 1153 if (include_untracked != INCLUDE_ALL_FILES) 1154 setup_standard_excludes(&dir); 1155 1156 fill_directory(&dir, the_repository->index, ps); 1157 for (i = 0; i < dir.nr; i++) { 1158 struct dir_entry *ent = dir.entries[i]; 1159 found++; 1160 strbuf_addstr(untracked_files, ent->name); 1161 /* NUL-terminate: will be fed to update-index -z */ 1162 strbuf_addch(untracked_files, '\0'); 1163 } 1164 1165 dir_clear(&dir); 1166 return found; 1167} 1168 1169/* 1170 * The return value of `check_changes_tracked_files()` can be: 1171 * 1172 * < 0 if there was an error 1173 * = 0 if there are no changes. 1174 * > 0 if there are changes. 1175 */ 1176static int check_changes_tracked_files(const struct pathspec *ps) 1177{ 1178 struct rev_info rev; 1179 struct object_id dummy; 1180 int ret = 0; 1181 1182 /* No initial commit. */ 1183 if (repo_get_oid(the_repository, "HEAD", &dummy)) 1184 return -1; 1185 1186 if (repo_read_index(the_repository) < 0) 1187 return -1; 1188 1189 repo_init_revisions(the_repository, &rev, NULL); 1190 copy_pathspec(&rev.prune_data, ps); 1191 1192 rev.diffopt.flags.quick = 1; 1193 rev.diffopt.flags.ignore_submodules = 1; 1194 rev.abbrev = 0; 1195 1196 add_head_to_pending(&rev); 1197 diff_setup_done(&rev.diffopt); 1198 1199 run_diff_index(&rev, DIFF_INDEX_CACHED); 1200 if (diff_result_code(&rev)) { 1201 ret = 1; 1202 goto done; 1203 } 1204 1205 run_diff_files(&rev, 0); 1206 if (diff_result_code(&rev)) { 1207 ret = 1; 1208 goto done; 1209 } 1210 1211done: 1212 release_revisions(&rev); 1213 return ret; 1214} 1215 1216/* 1217 * The function will fill `untracked_files` with the names of untracked files 1218 * It will return 1 if there were any changes and 0 if there were not. 1219 */ 1220static int check_changes(const struct pathspec *ps, int include_untracked, 1221 struct strbuf *untracked_files) 1222{ 1223 int ret = 0; 1224 if (check_changes_tracked_files(ps)) 1225 ret = 1; 1226 1227 if (include_untracked && get_untracked_files(ps, include_untracked, 1228 untracked_files)) 1229 ret = 1; 1230 1231 return ret; 1232} 1233 1234static int save_untracked_files(struct stash_info *info, struct strbuf *msg, 1235 struct strbuf files) 1236{ 1237 int ret = 0; 1238 struct strbuf untracked_msg = STRBUF_INIT; 1239 struct child_process cp_upd_index = CHILD_PROCESS_INIT; 1240 struct index_state istate = INDEX_STATE_INIT(the_repository); 1241 1242 cp_upd_index.git_cmd = 1; 1243 strvec_pushl(&cp_upd_index.args, "update-index", "-z", "--add", 1244 "--remove", "--stdin", NULL); 1245 strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s", 1246 stash_index_path.buf); 1247 1248 strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf); 1249 if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0, 1250 NULL, 0)) { 1251 ret = -1; 1252 goto done; 1253 } 1254 1255 if (write_index_as_tree(&info->u_tree, &istate, stash_index_path.buf, 0, 1256 NULL)) { 1257 ret = -1; 1258 goto done; 1259 } 1260 1261 if (commit_tree(untracked_msg.buf, untracked_msg.len, 1262 &info->u_tree, NULL, &info->u_commit, NULL, NULL)) { 1263 ret = -1; 1264 goto done; 1265 } 1266 1267done: 1268 release_index(&istate); 1269 strbuf_release(&untracked_msg); 1270 remove_path(stash_index_path.buf); 1271 return ret; 1272} 1273 1274static int stash_staged(struct stash_info *info, struct strbuf *out_patch, 1275 int quiet) 1276{ 1277 int ret = 0; 1278 struct child_process cp_diff_tree = CHILD_PROCESS_INIT; 1279 struct index_state istate = INDEX_STATE_INIT(the_repository); 1280 1281 if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file, 1282 0, NULL)) { 1283 ret = -1; 1284 goto done; 1285 } 1286 1287 cp_diff_tree.git_cmd = 1; 1288 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "--binary", 1289 "--no-color", 1290 "-U1", "HEAD", oid_to_hex(&info->w_tree), "--", NULL); 1291 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) { 1292 ret = -1; 1293 goto done; 1294 } 1295 1296 if (!out_patch->len) { 1297 if (!quiet) 1298 fprintf_ln(stderr, _("No staged changes")); 1299 ret = 1; 1300 } 1301 1302done: 1303 release_index(&istate); 1304 return ret; 1305} 1306 1307static int stash_patch(struct stash_info *info, const struct pathspec *ps, 1308 struct strbuf *out_patch, int quiet, 1309 struct add_p_opt *add_p_opt) 1310{ 1311 int ret = 0; 1312 struct child_process cp_read_tree = CHILD_PROCESS_INIT; 1313 struct child_process cp_diff_tree = CHILD_PROCESS_INIT; 1314 struct index_state istate = INDEX_STATE_INIT(the_repository); 1315 char *old_index_env = NULL, *old_repo_index_file; 1316 1317 remove_path(stash_index_path.buf); 1318 1319 cp_read_tree.git_cmd = 1; 1320 strvec_pushl(&cp_read_tree.args, "read-tree", "HEAD", NULL); 1321 strvec_pushf(&cp_read_tree.env, "GIT_INDEX_FILE=%s", 1322 stash_index_path.buf); 1323 if (run_command(&cp_read_tree)) { 1324 ret = -1; 1325 goto done; 1326 } 1327 1328 /* Find out what the user wants. */ 1329 old_repo_index_file = the_repository->index_file; 1330 the_repository->index_file = stash_index_path.buf; 1331 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT)); 1332 setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1); 1333 1334 ret = !!run_add_p(the_repository, ADD_P_STASH, add_p_opt, NULL, ps); 1335 1336 the_repository->index_file = old_repo_index_file; 1337 if (old_index_env && *old_index_env) 1338 setenv(INDEX_ENVIRONMENT, old_index_env, 1); 1339 else 1340 unsetenv(INDEX_ENVIRONMENT); 1341 FREE_AND_NULL(old_index_env); 1342 1343 /* State of the working tree. */ 1344 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, 1345 NULL)) { 1346 ret = -1; 1347 goto done; 1348 } 1349 1350 cp_diff_tree.git_cmd = 1; 1351 strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD", 1352 "--no-color", 1353 oid_to_hex(&info->w_tree), "--", NULL); 1354 if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) { 1355 ret = -1; 1356 goto done; 1357 } 1358 1359 if (!out_patch->len) { 1360 if (!quiet) 1361 fprintf_ln(stderr, _("No changes selected")); 1362 ret = 1; 1363 } 1364 1365done: 1366 release_index(&istate); 1367 remove_path(stash_index_path.buf); 1368 return ret; 1369} 1370 1371static int stash_working_tree(struct stash_info *info, const struct pathspec *ps) 1372{ 1373 int ret = 0; 1374 struct rev_info rev; 1375 struct child_process cp_upd_index = CHILD_PROCESS_INIT; 1376 struct strbuf diff_output = STRBUF_INIT; 1377 struct index_state istate = INDEX_STATE_INIT(the_repository); 1378 1379 repo_init_revisions(the_repository, &rev, NULL); 1380 copy_pathspec(&rev.prune_data, ps); 1381 1382 set_alternate_index_output(stash_index_path.buf); 1383 if (reset_tree(&info->i_tree, 0, 0)) { 1384 ret = -1; 1385 goto done; 1386 } 1387 set_alternate_index_output(NULL); 1388 1389 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; 1390 rev.diffopt.format_callback = add_diff_to_buf; 1391 rev.diffopt.format_callback_data = &diff_output; 1392 1393 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) { 1394 ret = -1; 1395 goto done; 1396 } 1397 1398 add_pending_object(&rev, parse_object(the_repository, &info->b_commit), 1399 ""); 1400 run_diff_index(&rev, 0); 1401 1402 cp_upd_index.git_cmd = 1; 1403 strvec_pushl(&cp_upd_index.args, "update-index", 1404 "--ignore-skip-worktree-entries", 1405 "-z", "--add", "--remove", "--stdin", NULL); 1406 strvec_pushf(&cp_upd_index.env, "GIT_INDEX_FILE=%s", 1407 stash_index_path.buf); 1408 1409 if (pipe_command(&cp_upd_index, diff_output.buf, diff_output.len, 1410 NULL, 0, NULL, 0)) { 1411 ret = -1; 1412 goto done; 1413 } 1414 1415 if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0, 1416 NULL)) { 1417 ret = -1; 1418 goto done; 1419 } 1420 1421done: 1422 release_index(&istate); 1423 release_revisions(&rev); 1424 strbuf_release(&diff_output); 1425 remove_path(stash_index_path.buf); 1426 return ret; 1427} 1428 1429static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf, 1430 int include_untracked, int patch_mode, struct add_p_opt *add_p_opt, 1431 int only_staged, struct stash_info *info, struct strbuf *patch, 1432 int quiet) 1433{ 1434 int ret = 0; 1435 int flags = 0; 1436 int untracked_commit_option = 0; 1437 const char *head_short_sha1 = NULL; 1438 const char *branch_ref = NULL; 1439 const char *branch_name = "(no branch)"; 1440 char *branch_name_buf = NULL; 1441 struct commit *head_commit = NULL; 1442 struct commit_list *parents = NULL; 1443 struct strbuf msg = STRBUF_INIT; 1444 struct strbuf commit_tree_label = STRBUF_INIT; 1445 struct strbuf untracked_files = STRBUF_INIT; 1446 1447 prepare_fallback_ident("git stash", "git@stash"); 1448 1449 repo_read_index_preload(the_repository, NULL, 0); 1450 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0, 1451 NULL, NULL, NULL) < 0) { 1452 ret = error(_("could not write index")); 1453 goto done; 1454 } 1455 1456 if (repo_get_oid(the_repository, "HEAD", &info->b_commit)) { 1457 if (!quiet) 1458 fprintf_ln(stderr, _("You do not have " 1459 "the initial commit yet")); 1460 ret = -1; 1461 goto done; 1462 } else { 1463 head_commit = lookup_commit(the_repository, &info->b_commit); 1464 } 1465 1466 if (!check_changes(ps, include_untracked, &untracked_files)) { 1467 ret = 1; 1468 goto done; 1469 } 1470 1471 branch_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 1472 "HEAD", 0, NULL, &flags); 1473 1474 if (flags & REF_ISSYMREF) { 1475 if (skip_prefix(branch_ref, "refs/heads/", &branch_name)) 1476 branch_name = branch_name_buf = xstrdup(branch_name); 1477 } 1478 1479 head_short_sha1 = repo_find_unique_abbrev(the_repository, 1480 &head_commit->object.oid, 1481 DEFAULT_ABBREV); 1482 strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1); 1483 pp_commit_easy(CMIT_FMT_ONELINE, head_commit, &msg); 1484 1485 strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf); 1486 commit_list_insert(head_commit, &parents); 1487 if (write_index_as_tree(&info->i_tree, the_repository->index, 1488 repo_get_index_file(the_repository), 0, NULL) || 1489 commit_tree(commit_tree_label.buf, commit_tree_label.len, 1490 &info->i_tree, parents, &info->i_commit, NULL, NULL)) { 1491 if (!quiet) 1492 fprintf_ln(stderr, _("Cannot save the current " 1493 "index state")); 1494 ret = -1; 1495 goto done; 1496 } 1497 1498 free_commit_list(parents); 1499 parents = NULL; 1500 1501 if (include_untracked) { 1502 if (save_untracked_files(info, &msg, untracked_files)) { 1503 if (!quiet) 1504 fprintf_ln(stderr, _("Cannot save " 1505 "the untracked files")); 1506 ret = -1; 1507 goto done; 1508 } 1509 untracked_commit_option = 1; 1510 } 1511 if (patch_mode) { 1512 ret = stash_patch(info, ps, patch, quiet, add_p_opt); 1513 if (ret < 0) { 1514 if (!quiet) 1515 fprintf_ln(stderr, _("Cannot save the current " 1516 "worktree state")); 1517 goto done; 1518 } else if (ret > 0) { 1519 goto done; 1520 } 1521 } else if (only_staged) { 1522 ret = stash_staged(info, patch, quiet); 1523 if (ret < 0) { 1524 if (!quiet) 1525 fprintf_ln(stderr, _("Cannot save the current " 1526 "staged state")); 1527 goto done; 1528 } else if (ret > 0) { 1529 goto done; 1530 } 1531 } else { 1532 if (stash_working_tree(info, ps)) { 1533 if (!quiet) 1534 fprintf_ln(stderr, _("Cannot save the current " 1535 "worktree state")); 1536 ret = -1; 1537 goto done; 1538 } 1539 } 1540 1541 if (!stash_msg_buf->len) 1542 strbuf_addf(stash_msg_buf, "WIP on %s", msg.buf); 1543 else 1544 strbuf_insertf(stash_msg_buf, 0, "On %s: ", branch_name); 1545 1546 if (untracked_commit_option) 1547 commit_list_insert(lookup_commit(the_repository, 1548 &info->u_commit), 1549 &parents); 1550 commit_list_insert(lookup_commit(the_repository, &info->i_commit), 1551 &parents); 1552 commit_list_insert(head_commit, &parents); 1553 1554 if (commit_tree(stash_msg_buf->buf, stash_msg_buf->len, &info->w_tree, 1555 parents, &info->w_commit, NULL, NULL)) { 1556 if (!quiet) 1557 fprintf_ln(stderr, _("Cannot record " 1558 "working tree state")); 1559 ret = -1; 1560 goto done; 1561 } 1562 1563done: 1564 strbuf_release(&commit_tree_label); 1565 strbuf_release(&msg); 1566 strbuf_release(&untracked_files); 1567 free_commit_list(parents); 1568 free(branch_name_buf); 1569 return ret; 1570} 1571 1572static int create_stash(int argc, const char **argv, const char *prefix UNUSED, 1573 struct repository *repo UNUSED) 1574{ 1575 int ret; 1576 struct strbuf stash_msg_buf = STRBUF_INIT; 1577 struct stash_info info = STASH_INFO_INIT; 1578 struct pathspec ps; 1579 1580 /* Starting with argv[1], since argv[0] is "create" */ 1581 strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' '); 1582 1583 memset(&ps, 0, sizeof(ps)); 1584 if (!check_changes_tracked_files(&ps)) 1585 return 0; 1586 1587 ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info, 1588 NULL, 0); 1589 if (!ret) 1590 printf_ln("%s", oid_to_hex(&info.w_commit)); 1591 1592 free_stash_info(&info); 1593 strbuf_release(&stash_msg_buf); 1594 return ret; 1595} 1596 1597static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet, 1598 int keep_index, int patch_mode, struct add_p_opt *add_p_opt, 1599 int include_untracked, int only_staged) 1600{ 1601 int ret = 0; 1602 struct stash_info info = STASH_INFO_INIT; 1603 struct strbuf patch = STRBUF_INIT; 1604 struct strbuf stash_msg_buf = STRBUF_INIT; 1605 struct strbuf untracked_files = STRBUF_INIT; 1606 struct strbuf out = STRBUF_INIT; 1607 1608 if (patch_mode && keep_index == -1) 1609 keep_index = 1; 1610 1611 if (patch_mode && include_untracked) { 1612 fprintf_ln(stderr, _("Can't use --patch and --include-untracked" 1613 " or --all at the same time")); 1614 ret = -1; 1615 goto done; 1616 } 1617 1618 /* --patch overrides --staged */ 1619 if (patch_mode) 1620 only_staged = 0; 1621 1622 if (only_staged && include_untracked) { 1623 fprintf_ln(stderr, _("Can't use --staged and --include-untracked" 1624 " or --all at the same time")); 1625 ret = -1; 1626 goto done; 1627 } 1628 1629 repo_read_index_preload(the_repository, NULL, 0); 1630 if (!include_untracked && ps->nr) { 1631 char *ps_matched = xcalloc(ps->nr, 1); 1632 1633 /* TODO: audit for interaction with sparse-index. */ 1634 ensure_full_index(the_repository->index); 1635 for (size_t i = 0; i < the_repository->index->cache_nr; i++) 1636 ce_path_match(the_repository->index, the_repository->index->cache[i], ps, 1637 ps_matched); 1638 1639 if (report_path_error(ps_matched, ps)) { 1640 fprintf_ln(stderr, _("Did you forget to 'git add'?")); 1641 ret = -1; 1642 free(ps_matched); 1643 goto done; 1644 } 1645 free(ps_matched); 1646 } 1647 1648 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0, 1649 NULL, NULL, NULL)) { 1650 ret = error(_("could not write index")); 1651 goto done; 1652 } 1653 1654 if (!check_changes(ps, include_untracked, &untracked_files)) { 1655 if (!quiet) 1656 printf_ln(_("No local changes to save")); 1657 goto done; 1658 } 1659 1660 if (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) { 1661 ret = -1; 1662 if (!quiet) 1663 fprintf_ln(stderr, _("Cannot initialize stash")); 1664 goto done; 1665 } 1666 1667 if (stash_msg) 1668 strbuf_addstr(&stash_msg_buf, stash_msg); 1669 if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode, 1670 add_p_opt, only_staged, &info, &patch, quiet)) { 1671 ret = -1; 1672 goto done; 1673 } 1674 1675 if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) { 1676 ret = -1; 1677 if (!quiet) 1678 fprintf_ln(stderr, _("Cannot save the current status")); 1679 goto done; 1680 } 1681 1682 if (!quiet) 1683 printf_ln(_("Saved working directory and index state %s"), 1684 stash_msg_buf.buf); 1685 1686 if (!(patch_mode || only_staged)) { 1687 if (include_untracked && !ps->nr) { 1688 struct child_process cp = CHILD_PROCESS_INIT; 1689 1690 cp.git_cmd = 1; 1691 if (startup_info->original_cwd) { 1692 cp.dir = startup_info->original_cwd; 1693 strvec_pushf(&cp.env, "%s=%s", 1694 GIT_WORK_TREE_ENVIRONMENT, 1695 the_repository->worktree); 1696 } 1697 strvec_pushl(&cp.args, "clean", "--force", 1698 "--quiet", "-d", ":/", NULL); 1699 if (include_untracked == INCLUDE_ALL_FILES) 1700 strvec_push(&cp.args, "-x"); 1701 if (run_command(&cp)) { 1702 ret = -1; 1703 goto done; 1704 } 1705 } 1706 discard_index(the_repository->index); 1707 if (ps->nr) { 1708 struct child_process cp_add = CHILD_PROCESS_INIT; 1709 struct child_process cp_diff = CHILD_PROCESS_INIT; 1710 struct child_process cp_apply = CHILD_PROCESS_INIT; 1711 1712 cp_add.git_cmd = 1; 1713 strvec_push(&cp_add.args, "add"); 1714 if (!include_untracked) 1715 strvec_push(&cp_add.args, "-u"); 1716 if (include_untracked == INCLUDE_ALL_FILES) 1717 strvec_push(&cp_add.args, "--force"); 1718 strvec_push(&cp_add.args, "--"); 1719 add_pathspecs(&cp_add.args, ps); 1720 if (run_command(&cp_add)) { 1721 ret = -1; 1722 goto done; 1723 } 1724 1725 cp_diff.git_cmd = 1; 1726 strvec_pushl(&cp_diff.args, "diff-index", "-p", 1727 "--no-color", 1728 "--cached", "--binary", "HEAD", "--", 1729 NULL); 1730 add_pathspecs(&cp_diff.args, ps); 1731 if (pipe_command(&cp_diff, NULL, 0, &out, 0, NULL, 0)) { 1732 ret = -1; 1733 goto done; 1734 } 1735 1736 cp_apply.git_cmd = 1; 1737 strvec_pushl(&cp_apply.args, "apply", "--index", 1738 "-R", NULL); 1739 if (pipe_command(&cp_apply, out.buf, out.len, NULL, 0, 1740 NULL, 0)) { 1741 ret = -1; 1742 goto done; 1743 } 1744 } else { 1745 struct child_process cp = CHILD_PROCESS_INIT; 1746 cp.git_cmd = 1; 1747 /* BUG: this nukes untracked files in the way */ 1748 strvec_pushl(&cp.args, "reset", "--hard", "-q", 1749 "--no-recurse-submodules", NULL); 1750 if (run_command(&cp)) { 1751 ret = -1; 1752 goto done; 1753 } 1754 } 1755 1756 /* 1757 * When keeping staged entries, we need to reset the working 1758 * directory to match the state of our index. This can be 1759 * skipped when the index is the empty tree, because there is 1760 * nothing to reset in that case: 1761 * 1762 * - When the index has any file, regardless of whether 1763 * staged or not, the tree cannot be empty by definition 1764 * and thus we enter the condition. 1765 * 1766 * - When the index has no files, the only thing we need to 1767 * care about is untracked files when `--include-untracked` 1768 * is given. But as we already execute git-clean(1) further 1769 * up to delete such untracked files we don't have to do 1770 * anything here, either. 1771 * 1772 * We thus skip calling git-checkout(1) in this case, also 1773 * because running it on an empty tree will cause it to fail 1774 * due to the pathspec not matching anything. 1775 */ 1776 if (keep_index == 1 && !is_null_oid(&info.i_tree) && 1777 !is_empty_tree_oid(&info.i_tree, the_repository->hash_algo)) { 1778 struct child_process cp = CHILD_PROCESS_INIT; 1779 1780 cp.git_cmd = 1; 1781 strvec_pushl(&cp.args, "checkout", "--no-overlay", 1782 oid_to_hex(&info.i_tree), "--", NULL); 1783 if (!ps->nr) 1784 strvec_push(&cp.args, ":/"); 1785 else 1786 add_pathspecs(&cp.args, ps); 1787 if (run_command(&cp)) { 1788 ret = -1; 1789 goto done; 1790 } 1791 } 1792 goto done; 1793 } else { 1794 struct child_process cp = CHILD_PROCESS_INIT; 1795 1796 cp.git_cmd = 1; 1797 strvec_pushl(&cp.args, "apply", "-R", NULL); 1798 1799 if (pipe_command(&cp, patch.buf, patch.len, NULL, 0, NULL, 0)) { 1800 if (!quiet) 1801 fprintf_ln(stderr, _("Cannot remove " 1802 "worktree changes")); 1803 ret = -1; 1804 goto done; 1805 } 1806 1807 if (keep_index < 1) { 1808 struct child_process cp = CHILD_PROCESS_INIT; 1809 1810 cp.git_cmd = 1; 1811 strvec_pushl(&cp.args, "reset", "-q", "--refresh", "--", 1812 NULL); 1813 add_pathspecs(&cp.args, ps); 1814 if (run_command(&cp)) { 1815 ret = -1; 1816 goto done; 1817 } 1818 } 1819 goto done; 1820 } 1821 1822done: 1823 strbuf_release(&patch); 1824 strbuf_release(&out); 1825 free_stash_info(&info); 1826 strbuf_release(&stash_msg_buf); 1827 strbuf_release(&untracked_files); 1828 return ret; 1829} 1830 1831static int push_stash(int argc, const char **argv, const char *prefix, 1832 int push_assumed) 1833{ 1834 int force_assume = 0; 1835 int keep_index = -1; 1836 int only_staged = 0; 1837 int patch_mode = 0; 1838 int include_untracked = 0; 1839 int quiet = 0; 1840 int pathspec_file_nul = 0; 1841 const char *stash_msg = NULL; 1842 char *pathspec_from_file = NULL; 1843 struct pathspec ps; 1844 struct add_p_opt add_p_opt = ADD_P_OPT_INIT; 1845 struct option options[] = { 1846 OPT_BOOL('k', "keep-index", &keep_index, 1847 N_("keep index")), 1848 OPT_BOOL('S', "staged", &only_staged, 1849 N_("stash staged changes only")), 1850 OPT_BOOL('p', "patch", &patch_mode, 1851 N_("stash in patch mode")), 1852 OPT_DIFF_UNIFIED(&add_p_opt.context), 1853 OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext), 1854 OPT__QUIET(&quiet, N_("quiet mode")), 1855 OPT_BOOL('u', "include-untracked", &include_untracked, 1856 N_("include untracked files in stash")), 1857 OPT_SET_INT('a', "all", &include_untracked, 1858 N_("include ignore files"), 2), 1859 OPT_STRING('m', "message", &stash_msg, N_("message"), 1860 N_("stash message")), 1861 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), 1862 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), 1863 OPT_END() 1864 }; 1865 int ret; 1866 1867 if (argc) { 1868 int flags = PARSE_OPT_KEEP_DASHDASH; 1869 1870 if (push_assumed) 1871 flags |= PARSE_OPT_STOP_AT_NON_OPTION; 1872 1873 argc = parse_options(argc, argv, prefix, options, 1874 push_assumed ? git_stash_usage : 1875 git_stash_push_usage, flags); 1876 force_assume |= patch_mode; 1877 } 1878 1879 if (argc) { 1880 if (!strcmp(argv[0], "--")) { 1881 argc--; 1882 argv++; 1883 } else if (push_assumed && !force_assume) { 1884 die("subcommand wasn't specified; 'push' can't be assumed due to unexpected token '%s'", 1885 argv[0]); 1886 } 1887 } 1888 1889 parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN, 1890 prefix, argv); 1891 1892 if (pathspec_from_file) { 1893 if (patch_mode) 1894 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch"); 1895 1896 if (only_staged) 1897 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--staged"); 1898 1899 if (ps.nr) 1900 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); 1901 1902 parse_pathspec_file(&ps, 0, 1903 PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN, 1904 prefix, pathspec_from_file, pathspec_file_nul); 1905 } else if (pathspec_file_nul) { 1906 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); 1907 } 1908 1909 if (!patch_mode) { 1910 if (add_p_opt.context != -1) 1911 die(_("the option '%s' requires '%s'"), "--unified", "--patch"); 1912 if (add_p_opt.interhunkcontext != -1) 1913 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch"); 1914 } 1915 1916 if (add_p_opt.context < -1) 1917 die(_("'%s' cannot be negative"), "--unified"); 1918 if (add_p_opt.interhunkcontext < -1) 1919 die(_("'%s' cannot be negative"), "--inter-hunk-context"); 1920 1921 ret = do_push_stash(&ps, stash_msg, quiet, keep_index, patch_mode, 1922 &add_p_opt, include_untracked, only_staged); 1923 1924 clear_pathspec(&ps); 1925 free(pathspec_from_file); 1926 return ret; 1927} 1928 1929static int push_stash_unassumed(int argc, const char **argv, const char *prefix, 1930 struct repository *repo UNUSED) 1931{ 1932 return push_stash(argc, argv, prefix, 0); 1933} 1934 1935static int save_stash(int argc, const char **argv, const char *prefix, 1936 struct repository *repo UNUSED) 1937{ 1938 int keep_index = -1; 1939 int only_staged = 0; 1940 int patch_mode = 0; 1941 int include_untracked = 0; 1942 int quiet = 0; 1943 int ret = 0; 1944 const char *stash_msg = NULL; 1945 struct pathspec ps; 1946 struct strbuf stash_msg_buf = STRBUF_INIT; 1947 struct add_p_opt add_p_opt = ADD_P_OPT_INIT; 1948 struct option options[] = { 1949 OPT_BOOL('k', "keep-index", &keep_index, 1950 N_("keep index")), 1951 OPT_BOOL('S', "staged", &only_staged, 1952 N_("stash staged changes only")), 1953 OPT_BOOL('p', "patch", &patch_mode, 1954 N_("stash in patch mode")), 1955 OPT_DIFF_UNIFIED(&add_p_opt.context), 1956 OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext), 1957 OPT__QUIET(&quiet, N_("quiet mode")), 1958 OPT_BOOL('u', "include-untracked", &include_untracked, 1959 N_("include untracked files in stash")), 1960 OPT_SET_INT('a', "all", &include_untracked, 1961 N_("include ignore files"), 2), 1962 OPT_STRING('m', "message", &stash_msg, "message", 1963 N_("stash message")), 1964 OPT_END() 1965 }; 1966 1967 argc = parse_options(argc, argv, prefix, options, 1968 git_stash_save_usage, 1969 PARSE_OPT_KEEP_DASHDASH); 1970 1971 if (argc) 1972 stash_msg = strbuf_join_argv(&stash_msg_buf, argc, argv, ' '); 1973 1974 memset(&ps, 0, sizeof(ps)); 1975 1976 if (add_p_opt.context < -1) 1977 die(_("'%s' cannot be negative"), "--unified"); 1978 if (add_p_opt.interhunkcontext < -1) 1979 die(_("'%s' cannot be negative"), "--inter-hunk-context"); 1980 1981 if (!patch_mode) { 1982 if (add_p_opt.context != -1) 1983 die(_("the option '%s' requires '%s'"), "--unified", "--patch"); 1984 if (add_p_opt.interhunkcontext != -1) 1985 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch"); 1986 } 1987 1988 ret = do_push_stash(&ps, stash_msg, quiet, keep_index, 1989 patch_mode, &add_p_opt, include_untracked, 1990 only_staged); 1991 1992 strbuf_release(&stash_msg_buf); 1993 return ret; 1994} 1995 1996static int write_commit_with_parents(struct repository *r, 1997 struct object_id *out, 1998 const struct object_id *oid, 1999 struct commit_list *parents) 2000{ 2001 size_t author_len, committer_len; 2002 struct commit *this; 2003 const char *orig_author, *orig_committer; 2004 char *author = NULL, *committer = NULL; 2005 const char *buffer; 2006 unsigned long bufsize; 2007 const char *p; 2008 struct strbuf msg = STRBUF_INIT; 2009 int ret = 0; 2010 struct ident_split id; 2011 2012 this = lookup_commit_reference(r, oid); 2013 buffer = repo_get_commit_buffer(r, this, &bufsize); 2014 orig_author = find_commit_header(buffer, "author", &author_len); 2015 orig_committer = find_commit_header(buffer, "committer", &committer_len); 2016 2017 if (!orig_author || !orig_committer) { 2018 ret = error(_("cannot parse commit %s"), oid_to_hex(oid)); 2019 goto out; 2020 } 2021 2022 if (split_ident_line(&id, orig_author, author_len) < 0 || 2023 split_ident_line(&id, orig_committer, committer_len) < 0) { 2024 ret = error(_("invalid author or committer for %s"), oid_to_hex(oid)); 2025 goto out; 2026 } 2027 2028 p = strstr(buffer, "\n\n"); 2029 strbuf_addstr(&msg, "git stash: "); 2030 2031 if (p) 2032 strbuf_add(&msg, p + 2, bufsize - (p + 2 - buffer)); 2033 strbuf_complete_line(&msg); 2034 2035 author = xmemdupz(orig_author, author_len); 2036 committer = xmemdupz(orig_committer, committer_len); 2037 2038 if (commit_tree_extended(msg.buf, msg.len, 2039 r->hash_algo->empty_tree, parents, 2040 out, author, committer, 2041 NULL, NULL)) { 2042 ret = error(_("could not write commit")); 2043 goto out; 2044 } 2045out: 2046 strbuf_release(&msg); 2047 repo_unuse_commit_buffer(r, this, buffer); 2048 free(author); 2049 free(committer); 2050 return ret; 2051} 2052 2053static int do_import_stash(struct repository *r, const char *rev) 2054{ 2055 struct object_id chain; 2056 int res = 0; 2057 const char *buffer = NULL; 2058 unsigned long bufsize; 2059 struct commit *this = NULL; 2060 struct commit_list *items = NULL, *cur; 2061 char *msg = NULL; 2062 2063 if (repo_get_oid(r, rev, &chain)) 2064 return error(_("not a valid revision: %s"), rev); 2065 2066 this = lookup_commit_reference(r, &chain); 2067 if (!this) 2068 return error(_("not a commit: %s"), rev); 2069 2070 /* 2071 * Walk the commit history, finding each stash entry, and load data into 2072 * the array. 2073 */ 2074 for (;;) { 2075 const char *author, *committer; 2076 size_t author_len, committer_len; 2077 const char *p; 2078 const char *expected = "git stash <git@stash> 1000684800 +0000"; 2079 const char *prefix = "git stash: "; 2080 struct commit *stash; 2081 struct tree *tree = repo_get_commit_tree(r, this); 2082 2083 if (!tree || 2084 !oideq(&tree->object.oid, r->hash_algo->empty_tree) || 2085 (this->parents && 2086 (!this->parents->next || this->parents->next->next))) { 2087 res = error(_("%s is not a valid exported stash commit"), 2088 oid_to_hex(&this->object.oid)); 2089 goto out; 2090 } 2091 2092 buffer = repo_get_commit_buffer(r, this, &bufsize); 2093 2094 if (!this->parents) { 2095 /* 2096 * We don't have any parents. Make sure this is our 2097 * root commit. 2098 */ 2099 author = find_commit_header(buffer, "author", &author_len); 2100 committer = find_commit_header(buffer, "committer", &committer_len); 2101 2102 if (!author || !committer) { 2103 error(_("cannot parse commit %s"), oid_to_hex(&this->object.oid)); 2104 goto out; 2105 } 2106 2107 if (author_len != strlen(expected) || 2108 committer_len != strlen(expected) || 2109 memcmp(author, expected, author_len) || 2110 memcmp(committer, expected, committer_len)) { 2111 res = error(_("found root commit %s with invalid data"), oid_to_hex(&this->object.oid)); 2112 goto out; 2113 } 2114 break; 2115 } 2116 2117 p = strstr(buffer, "\n\n"); 2118 if (!p) { 2119 res = error(_("cannot parse commit %s"), oid_to_hex(&this->object.oid)); 2120 goto out; 2121 } 2122 2123 p += 2; 2124 if (((size_t)(bufsize - (p - buffer)) < strlen(prefix)) || 2125 memcmp(prefix, p, strlen(prefix))) { 2126 res = error(_("found stash commit %s without expected prefix"), oid_to_hex(&this->object.oid)); 2127 goto out; 2128 } 2129 2130 stash = this->parents->next->item; 2131 2132 if (repo_parse_commit(r, this->parents->item) || 2133 repo_parse_commit(r, stash)) { 2134 res = error(_("cannot parse parents of commit: %s"), 2135 oid_to_hex(&this->object.oid)); 2136 goto out; 2137 } 2138 2139 if (check_stash_topology(r, stash)) { 2140 res = error(_("%s does not look like a stash commit"), 2141 oid_to_hex(&stash->object.oid)); 2142 goto out; 2143 } 2144 2145 repo_unuse_commit_buffer(r, this, buffer); 2146 buffer = NULL; 2147 items = commit_list_insert(stash, &items); 2148 this = this->parents->item; 2149 } 2150 2151 /* 2152 * Now, walk each entry, adding it to the stash as a normal stash 2153 * commit. 2154 */ 2155 for (cur = items; cur; cur = cur->next) { 2156 const char *p; 2157 struct object_id *oid; 2158 2159 this = cur->item; 2160 oid = &this->object.oid; 2161 buffer = repo_get_commit_buffer(r, this, &bufsize); 2162 if (!buffer) { 2163 res = error(_("cannot read commit buffer for %s"), oid_to_hex(oid)); 2164 goto out; 2165 } 2166 2167 p = strstr(buffer, "\n\n"); 2168 if (!p) { 2169 res = error(_("cannot parse commit %s"), oid_to_hex(oid)); 2170 goto out; 2171 } 2172 2173 p += 2; 2174 msg = xmemdupz(p, bufsize - (p - buffer)); 2175 repo_unuse_commit_buffer(r, this, buffer); 2176 buffer = NULL; 2177 2178 if (do_store_stash(oid, msg, 1)) { 2179 res = error(_("cannot save the stash for %s"), oid_to_hex(oid)); 2180 goto out; 2181 } 2182 FREE_AND_NULL(msg); 2183 } 2184out: 2185 if (this && buffer) 2186 repo_unuse_commit_buffer(r, this, buffer); 2187 free_commit_list(items); 2188 free(msg); 2189 2190 return res; 2191} 2192 2193static int import_stash(int argc, const char **argv, const char *prefix, 2194 struct repository *repo) 2195{ 2196 struct option options[] = { 2197 OPT_END() 2198 }; 2199 2200 argc = parse_options(argc, argv, prefix, options, 2201 git_stash_import_usage, 2202 PARSE_OPT_KEEP_DASHDASH); 2203 2204 if (argc != 1) 2205 usage_msg_opt("a revision is required", git_stash_import_usage, options); 2206 2207 return do_import_stash(repo, argv[0]); 2208} 2209 2210struct stash_entry_data { 2211 struct repository *r; 2212 struct commit_list **items; 2213 size_t count; 2214}; 2215 2216static int collect_stash_entries(const char *refname UNUSED, 2217 struct object_id *old_oid UNUSED, 2218 struct object_id *new_oid, 2219 const char *committer UNUSED, 2220 timestamp_t timestamp UNUSED, 2221 int tz UNUSED, const char *msg UNUSED, 2222 void *cb_data) 2223{ 2224 struct stash_entry_data *data = cb_data; 2225 struct commit *stash; 2226 2227 data->count++; 2228 stash = lookup_commit_reference(data->r, new_oid); 2229 if (!stash || check_stash_topology(data->r, stash)) { 2230 return error(_("%s does not look like a stash commit"), 2231 oid_to_hex(new_oid)); 2232 } 2233 data->items = commit_list_append(stash, data->items); 2234 return 0; 2235} 2236 2237static int do_export_stash(struct repository *r, 2238 const char *ref, 2239 int argc, 2240 const char **argv) 2241{ 2242 struct object_id base; 2243 struct commit *prev; 2244 struct commit_list *items = NULL, **iter = &items, *cur; 2245 int res = 0; 2246 int i; 2247 struct strbuf revision = STRBUF_INIT; 2248 const char *author, *committer; 2249 2250 /* 2251 * This is an arbitrary, fixed date, specifically the one used by git 2252 * format-patch. The goal is merely to produce reproducible output. 2253 */ 2254 prepare_fallback_ident("git stash", "git@stash"); 2255 author = fmt_ident("git stash", "git@stash", WANT_BLANK_IDENT, 2256 "2001-09-17T00:00:00Z", 0); 2257 committer = fmt_ident("git stash", "git@stash", WANT_BLANK_IDENT, 2258 "2001-09-17T00:00:00Z", 0); 2259 2260 /* First, we create a single empty commit. */ 2261 if (commit_tree_extended("", 0, r->hash_algo->empty_tree, NULL, 2262 &base, author, committer, NULL, NULL)) 2263 return error(_("unable to write base commit")); 2264 2265 prev = lookup_commit_reference(r, &base); 2266 2267 if (argc) { 2268 /* 2269 * Find each specified stash, and load data into the array. 2270 */ 2271 for (i = 0; i < argc; i++) { 2272 struct object_id oid; 2273 struct commit *stash; 2274 2275 if (parse_stash_revision(&revision, argv[i], 1) || 2276 repo_get_oid_with_flags(r, revision.buf, &oid, 2277 GET_OID_QUIETLY | 2278 GET_OID_GENTLY)) { 2279 res = error(_("unable to find stash entry %s"), argv[i]); 2280 goto out; 2281 } 2282 2283 stash = lookup_commit_reference(r, &oid); 2284 if (!stash || check_stash_topology(r, stash)) { 2285 res = error(_("%s does not look like a stash commit"), 2286 revision.buf); 2287 goto out; 2288 } 2289 iter = commit_list_append(stash, iter); 2290 } 2291 } else { 2292 /* 2293 * Walk the reflog, finding each stash entry, and load data into the 2294 * array. 2295 */ 2296 struct stash_entry_data cb_data = { 2297 .r = r, .items = iter, 2298 }; 2299 if (refs_for_each_reflog_ent_reverse(get_main_ref_store(r), 2300 "refs/stash", 2301 collect_stash_entries, 2302 &cb_data) && cb_data.count) 2303 goto out; 2304 } 2305 2306 /* 2307 * Now, create a set of commits identical to the regular stash commits, 2308 * but where their first parents form a chain to our original empty 2309 * base commit. 2310 */ 2311 items = reverse_commit_list(items); 2312 for (cur = items; cur; cur = cur->next) { 2313 struct commit_list *parents = NULL; 2314 struct commit_list **next = &parents; 2315 struct object_id out; 2316 struct commit *stash = cur->item; 2317 2318 next = commit_list_append(prev, next); 2319 next = commit_list_append(stash, next); 2320 res = write_commit_with_parents(r, &out, &stash->object.oid, parents); 2321 free_commit_list(parents); 2322 if (res) 2323 goto out; 2324 prev = lookup_commit_reference(r, &out); 2325 } 2326 if (ref) 2327 refs_update_ref(get_main_ref_store(r), NULL, ref, 2328 &prev->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR); 2329 else 2330 puts(oid_to_hex(&prev->object.oid)); 2331out: 2332 strbuf_release(&revision); 2333 free_commit_list(items); 2334 2335 return res; 2336} 2337 2338enum export_action { 2339 ACTION_NONE, 2340 ACTION_PRINT, 2341 ACTION_TO_REF, 2342}; 2343 2344static int export_stash(int argc, 2345 const char **argv, 2346 const char *prefix, 2347 struct repository *repo) 2348{ 2349 const char *ref = NULL; 2350 enum export_action action = ACTION_NONE; 2351 struct option options[] = { 2352 OPT_CMDMODE(0, "print", &action, 2353 N_("print the object ID instead of writing it to a ref"), 2354 ACTION_PRINT), 2355 OPT_STRING(0, "to-ref", &ref, "ref", 2356 N_("save the data to the given ref")), 2357 OPT_END() 2358 }; 2359 2360 argc = parse_options(argc, argv, prefix, options, 2361 git_stash_export_usage, 2362 PARSE_OPT_KEEP_DASHDASH); 2363 2364 if (ref && action == ACTION_NONE) 2365 action = ACTION_TO_REF; 2366 2367 if (action == ACTION_NONE || (ref && action == ACTION_PRINT)) 2368 return error(_("exactly one of --print and --to-ref is required")); 2369 2370 return do_export_stash(repo, ref, argc, argv); 2371} 2372 2373int cmd_stash(int argc, 2374 const char **argv, 2375 const char *prefix, 2376 struct repository *repo) 2377{ 2378 pid_t pid = getpid(); 2379 const char *index_file; 2380 struct strvec args = STRVEC_INIT; 2381 parse_opt_subcommand_fn *fn = NULL; 2382 struct option options[] = { 2383 OPT_SUBCOMMAND("apply", &fn, apply_stash), 2384 OPT_SUBCOMMAND("clear", &fn, clear_stash), 2385 OPT_SUBCOMMAND("drop", &fn, drop_stash), 2386 OPT_SUBCOMMAND("pop", &fn, pop_stash), 2387 OPT_SUBCOMMAND("branch", &fn, branch_stash), 2388 OPT_SUBCOMMAND("list", &fn, list_stash), 2389 OPT_SUBCOMMAND("show", &fn, show_stash), 2390 OPT_SUBCOMMAND("store", &fn, store_stash), 2391 OPT_SUBCOMMAND("create", &fn, create_stash), 2392 OPT_SUBCOMMAND("push", &fn, push_stash_unassumed), 2393 OPT_SUBCOMMAND("export", &fn, export_stash), 2394 OPT_SUBCOMMAND("import", &fn, import_stash), 2395 OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE), 2396 OPT_END() 2397 }; 2398 const char **args_copy; 2399 int ret; 2400 2401 repo_config(the_repository, git_stash_config, NULL); 2402 2403 argc = parse_options(argc, argv, prefix, options, git_stash_usage, 2404 PARSE_OPT_SUBCOMMAND_OPTIONAL | 2405 PARSE_OPT_KEEP_UNKNOWN_OPT | 2406 PARSE_OPT_KEEP_DASHDASH); 2407 2408 prepare_repo_settings(the_repository); 2409 the_repository->settings.command_requires_full_index = 0; 2410 2411 index_file = repo_get_index_file(the_repository); 2412 strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, 2413 (uintmax_t)pid); 2414 2415 if (fn) 2416 return !!fn(argc, argv, prefix, repo); 2417 else if (!argc) 2418 return !!push_stash_unassumed(0, NULL, prefix, repo); 2419 2420 /* Assume 'stash push' */ 2421 strvec_push(&args, "push"); 2422 strvec_pushv(&args, argv); 2423 2424 /* 2425 * `push_stash()` ends up modifying the array, which causes memory 2426 * leaks if we didn't copy the array here. 2427 */ 2428 DUP_ARRAY(args_copy, args.v, args.nr); 2429 2430 ret = !!push_stash(args.nr, args_copy, prefix, 1); 2431 2432 strvec_clear(&args); 2433 free(args_copy); 2434 return ret; 2435}