| 4656 | } |
| 4657 | |
| 4658 | static void create_autostash_internal(struct repository *r, |
| 4659 | const char *path, |
| 4660 | const char *refname, |
| 4661 | const char *message, |
| 4662 | bool silent) |
| 4663 | { |
| 4664 | struct strbuf buf = STRBUF_INIT; |
| 4665 | struct lock_file lock_file = LOCK_INIT; |
| 4666 | int fd; |
| 4667 | |
| 4668 | if (path && refname) |
| 4669 | BUG("can only pass path or refname"); |
| 4670 | |
| 4671 | fd = repo_hold_locked_index(r, &lock_file, 0); |
| 4672 | refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL); |
| 4673 | if (0 <= fd) |
| 4674 | repo_update_index_if_able(r, &lock_file); |
| 4675 | rollback_lock_file(&lock_file); |
| 4676 | |
| 4677 | if (has_unstaged_changes(r, 1) || |
| 4678 | has_uncommitted_changes(r, 1)) { |
| 4679 | struct child_process stash = CHILD_PROCESS_INIT; |
| 4680 | struct reset_head_opts ropts = { .flags = RESET_HEAD_HARD }; |
| 4681 | struct object_id oid; |
| 4682 | |
| 4683 | strvec_pushl(&stash.args, |
| 4684 | "stash", "create", |
| 4685 | message ? message : "autostash", NULL); |
| 4686 | stash.git_cmd = 1; |
| 4687 | stash.no_stdin = 1; |
| 4688 | strbuf_reset(&buf); |
| 4689 | if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) |
| 4690 | die(_("Cannot autostash")); |
| 4691 | strbuf_trim_trailing_newline(&buf); |
| 4692 | if (repo_get_oid(r, buf.buf, &oid)) |
| 4693 | die(_("Unexpected stash response: '%s'"), |
| 4694 | buf.buf); |
| 4695 | strbuf_reset(&buf); |
| 4696 | strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); |
| 4697 | |
| 4698 | if (path) { |
| 4699 | if (safe_create_leading_directories_const(the_repository, path)) |
| 4700 | die(_("Could not create directory for '%s'"), |
| 4701 | path); |
| 4702 | write_file(path, "%s", oid_to_hex(&oid)); |
| 4703 | } else { |
| 4704 | refs_update_ref(get_main_ref_store(r), "", refname, |
| 4705 | &oid, null_oid(the_hash_algo), 0, UPDATE_REFS_DIE_ON_ERR); |
| 4706 | } |
| 4707 | |
| 4708 | if (!silent) |
| 4709 | printf(_("Created autostash: %s\n"), buf.buf); |
| 4710 | if (reset_head(r, &ropts) < 0) |
| 4711 | die(_("could not reset --hard")); |
| 4712 | discard_index(r->index); |
| 4713 | if (repo_read_index(r) < 0) |
| 4714 | die(_("could not read index")); |
| 4715 | } |
no test coverage detected