| 87 | } |
| 88 | |
| 89 | int reset_head(struct repository *r, const struct reset_head_opts *opts) |
| 90 | { |
| 91 | const struct object_id *oid = opts->oid; |
| 92 | const char *switch_to_branch = opts->branch; |
| 93 | unsigned reset_hard = opts->flags & RESET_HEAD_HARD; |
| 94 | unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY; |
| 95 | unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; |
| 96 | struct object_id *head = NULL, head_oid; |
| 97 | struct tree_desc desc[2] = { { NULL }, { NULL } }; |
| 98 | struct lock_file lock = LOCK_INIT; |
| 99 | struct unpack_trees_options unpack_tree_opts = { 0 }; |
| 100 | struct tree *tree; |
| 101 | const char *action; |
| 102 | int ret = 0, nr = 0; |
| 103 | |
| 104 | if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) |
| 105 | BUG("Not a fully qualified branch: '%s'", switch_to_branch); |
| 106 | |
| 107 | if (opts->orig_head_msg && !update_orig_head) |
| 108 | BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD"); |
| 109 | |
| 110 | if (opts->branch_msg && !opts->branch) |
| 111 | BUG("branch reflog message given without a branch"); |
| 112 | |
| 113 | if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
| 114 | ret = -1; |
| 115 | goto leave_reset_head; |
| 116 | } |
| 117 | |
| 118 | if (!repo_get_oid(r, "HEAD", &head_oid)) { |
| 119 | head = &head_oid; |
| 120 | } else if (!oid || !reset_hard) { |
| 121 | ret = error(_("could not determine HEAD revision")); |
| 122 | goto leave_reset_head; |
| 123 | } |
| 124 | |
| 125 | if (!oid) |
| 126 | oid = &head_oid; |
| 127 | |
| 128 | if (refs_only) |
| 129 | return update_refs(opts, oid, head); |
| 130 | |
| 131 | action = reset_hard ? "reset" : "checkout"; |
| 132 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); |
| 133 | unpack_tree_opts.head_idx = 1; |
| 134 | unpack_tree_opts.src_index = r->index; |
| 135 | unpack_tree_opts.dst_index = r->index; |
| 136 | unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; |
| 137 | unpack_tree_opts.update = 1; |
| 138 | unpack_tree_opts.merge = 1; |
| 139 | unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
| 140 | unpack_tree_opts.skip_cache_tree_update = 1; |
| 141 | init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL); |
| 142 | if (reset_hard) |
| 143 | unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED; |
| 144 | |
| 145 | if (repo_read_index_unmerged(r) < 0) { |
| 146 | ret = error(_("could not read index")); |
no test coverage detected