git
/
reset.c
189 строк · 5.7 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "cache-tree.h"5#include "gettext.h"6#include "hex.h"7#include "lockfile.h"8#include "object-name.h"9#include "refs.h"10#include "reset.h"11#include "tree-walk.h"12#include "tree.h"13#include "unpack-trees.h"14#include "hook.h"15
16static int update_refs(const struct reset_head_opts *opts,17const struct object_id *oid,18const struct object_id *head)19{
20unsigned detach_head = opts->flags & RESET_HEAD_DETACH;21unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;22unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;23const struct object_id *orig_head = opts->orig_head;24const char *switch_to_branch = opts->branch;25const char *reflog_branch = opts->branch_msg;26const char *reflog_head = opts->head_msg;27const char *reflog_orig_head = opts->orig_head_msg;28const char *default_reflog_action = opts->default_reflog_action;29struct object_id *old_orig = NULL, oid_old_orig;30struct strbuf msg = STRBUF_INIT;31const char *reflog_action;32size_t prefix_len;33int ret;34
35if ((update_orig_head && !reflog_orig_head) || !reflog_head) {36if (!default_reflog_action)37BUG("default_reflog_action must be given when reflog messages are omitted");38reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);39strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :40default_reflog_action);41}42prefix_len = msg.len;43
44if (update_orig_head) {45if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig))46old_orig = &oid_old_orig;47if (head) {48if (!reflog_orig_head) {49strbuf_addstr(&msg, "updating ORIG_HEAD");50reflog_orig_head = msg.buf;51}52refs_update_ref(get_main_ref_store(the_repository),53reflog_orig_head, "ORIG_HEAD",54orig_head ? orig_head : head,55old_orig, 0, UPDATE_REFS_MSG_ON_ERR);56} else if (old_orig)57refs_delete_ref(get_main_ref_store(the_repository),58NULL, "ORIG_HEAD", old_orig, 0);59}60
61if (!reflog_head) {62strbuf_setlen(&msg, prefix_len);63strbuf_addstr(&msg, "updating HEAD");64reflog_head = msg.buf;65}66if (!switch_to_branch)67ret = refs_update_ref(get_main_ref_store(the_repository),68reflog_head, "HEAD", oid, head,69detach_head ? REF_NO_DEREF : 0,70UPDATE_REFS_MSG_ON_ERR);71else {72ret = refs_update_ref(get_main_ref_store(the_repository),73reflog_branch ? reflog_branch : reflog_head,74switch_to_branch, oid, NULL, 0,75UPDATE_REFS_MSG_ON_ERR);76if (!ret)77ret = refs_update_symref(get_main_ref_store(the_repository),78"HEAD", switch_to_branch,79reflog_head);80}81if (!ret && run_hook)82run_hooks_l(the_repository, "post-checkout",83oid_to_hex(head ? head : null_oid()),84oid_to_hex(oid), "1", NULL);85strbuf_release(&msg);86return ret;87}
88
89int reset_head(struct repository *r, const struct reset_head_opts *opts)90{
91const struct object_id *oid = opts->oid;92const char *switch_to_branch = opts->branch;93unsigned reset_hard = opts->flags & RESET_HEAD_HARD;94unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;95unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;96struct object_id *head = NULL, head_oid;97struct tree_desc desc[2] = { { NULL }, { NULL } };98struct lock_file lock = LOCK_INIT;99struct unpack_trees_options unpack_tree_opts = { 0 };100struct tree *tree;101const char *action;102int ret = 0, nr = 0;103
104if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))105BUG("Not a fully qualified branch: '%s'", switch_to_branch);106
107if (opts->orig_head_msg && !update_orig_head)108BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");109
110if (opts->branch_msg && !opts->branch)111BUG("branch reflog message given without a branch");112
113if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {114ret = -1;115goto leave_reset_head;116}117
118if (!repo_get_oid(r, "HEAD", &head_oid)) {119head = &head_oid;120} else if (!oid || !reset_hard) {121ret = error(_("could not determine HEAD revision"));122goto leave_reset_head;123}124
125if (!oid)126oid = &head_oid;127
128if (refs_only)129return update_refs(opts, oid, head);130
131action = reset_hard ? "reset" : "checkout";132setup_unpack_trees_porcelain(&unpack_tree_opts, action);133unpack_tree_opts.head_idx = 1;134unpack_tree_opts.src_index = r->index;135unpack_tree_opts.dst_index = r->index;136unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;137unpack_tree_opts.update = 1;138unpack_tree_opts.merge = 1;139unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */140unpack_tree_opts.skip_cache_tree_update = 1;141init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);142if (reset_hard)143unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;144
145if (repo_read_index_unmerged(r) < 0) {146ret = error(_("could not read index"));147goto leave_reset_head;148}149
150if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {151ret = error(_("failed to find tree of %s"),152oid_to_hex(&head_oid));153goto leave_reset_head;154}155
156if (!fill_tree_descriptor(r, &desc[nr++], oid)) {157ret = error(_("failed to find tree of %s"), oid_to_hex(oid));158goto leave_reset_head;159}160
161if (unpack_trees(nr, desc, &unpack_tree_opts)) {162ret = -1;163goto leave_reset_head;164}165
166tree = parse_tree_indirect(oid);167if (!tree) {168ret = error(_("unable to read tree (%s)"), oid_to_hex(oid));169goto leave_reset_head;170}171
172prime_cache_tree(r, r->index, tree);173
174if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {175ret = error(_("could not write index"));176goto leave_reset_head;177}178
179if (oid != &head_oid || update_orig_head || switch_to_branch)180ret = update_refs(opts, oid, head);181
182leave_reset_head:183rollback_lock_file(&lock);184clear_unpack_trees_porcelain(&unpack_tree_opts);185while (nr)186free((void *)desc[--nr].buffer);187return ret;188
189}
190