2020-04-07 17:28:00 +03:00
|
|
|
#include "git-compat-util.h"
|
|
|
|
#include "cache-tree.h"
|
|
|
|
#include "lockfile.h"
|
|
|
|
#include "refs.h"
|
|
|
|
#include "reset.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
#include "tree-walk.h"
|
|
|
|
#include "tree.h"
|
|
|
|
#include "unpack-trees.h"
|
2021-12-22 06:59:35 +03:00
|
|
|
#include "hook.h"
|
2020-04-07 17:28:00 +03:00
|
|
|
|
2022-01-26 16:05:46 +03:00
|
|
|
static int update_refs(const struct reset_head_opts *opts,
|
|
|
|
const struct object_id *oid,
|
|
|
|
const struct object_id *head)
|
2020-04-07 17:28:00 +03:00
|
|
|
{
|
2022-01-26 16:05:46 +03:00
|
|
|
unsigned detach_head = opts->flags & RESET_HEAD_DETACH;
|
|
|
|
unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
|
|
|
|
unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
|
2022-01-26 16:05:48 +03:00
|
|
|
const struct object_id *orig_head = opts->orig_head;
|
2022-01-26 16:05:46 +03:00
|
|
|
const char *switch_to_branch = opts->branch;
|
2022-01-26 16:05:47 +03:00
|
|
|
const char *reflog_branch = opts->branch_msg;
|
2022-01-26 16:05:46 +03:00
|
|
|
const char *reflog_head = opts->head_msg;
|
|
|
|
const char *reflog_orig_head = opts->orig_head_msg;
|
|
|
|
const char *default_reflog_action = opts->default_reflog_action;
|
2022-01-26 16:05:42 +03:00
|
|
|
struct object_id *old_orig = NULL, oid_old_orig;
|
|
|
|
struct strbuf msg = STRBUF_INIT;
|
|
|
|
const char *reflog_action;
|
|
|
|
size_t prefix_len;
|
|
|
|
int ret;
|
|
|
|
|
2022-01-26 16:05:43 +03:00
|
|
|
if ((update_orig_head && !reflog_orig_head) || !reflog_head) {
|
|
|
|
if (!default_reflog_action)
|
|
|
|
BUG("default_reflog_action must be given when reflog messages are omitted");
|
|
|
|
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
|
|
|
|
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action :
|
|
|
|
default_reflog_action);
|
|
|
|
}
|
2022-01-26 16:05:42 +03:00
|
|
|
prefix_len = msg.len;
|
|
|
|
|
|
|
|
if (update_orig_head) {
|
|
|
|
if (!get_oid("ORIG_HEAD", &oid_old_orig))
|
|
|
|
old_orig = &oid_old_orig;
|
|
|
|
if (head) {
|
|
|
|
if (!reflog_orig_head) {
|
|
|
|
strbuf_addstr(&msg, "updating ORIG_HEAD");
|
|
|
|
reflog_orig_head = msg.buf;
|
|
|
|
}
|
2022-01-26 16:05:48 +03:00
|
|
|
update_ref(reflog_orig_head, "ORIG_HEAD",
|
|
|
|
orig_head ? orig_head : head,
|
2022-01-26 16:05:42 +03:00
|
|
|
old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
|
|
|
|
} else if (old_orig)
|
|
|
|
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reflog_head) {
|
|
|
|
strbuf_setlen(&msg, prefix_len);
|
|
|
|
strbuf_addstr(&msg, "updating HEAD");
|
|
|
|
reflog_head = msg.buf;
|
|
|
|
}
|
|
|
|
if (!switch_to_branch)
|
|
|
|
ret = update_ref(reflog_head, "HEAD", oid, head,
|
|
|
|
detach_head ? REF_NO_DEREF : 0,
|
|
|
|
UPDATE_REFS_MSG_ON_ERR);
|
|
|
|
else {
|
2022-01-26 16:05:47 +03:00
|
|
|
ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
|
|
|
|
switch_to_branch, oid, NULL, 0,
|
|
|
|
UPDATE_REFS_MSG_ON_ERR);
|
2022-01-26 16:05:42 +03:00
|
|
|
if (!ret)
|
|
|
|
ret = create_symref("HEAD", switch_to_branch,
|
|
|
|
reflog_head);
|
|
|
|
}
|
|
|
|
if (!ret && run_hook)
|
2022-02-19 00:53:27 +03:00
|
|
|
run_hooks_l("post-checkout",
|
2022-01-26 16:05:42 +03:00
|
|
|
oid_to_hex(head ? head : null_oid()),
|
|
|
|
oid_to_hex(oid), "1", NULL);
|
|
|
|
strbuf_release(&msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:05:46 +03:00
|
|
|
int reset_head(struct repository *r, const struct reset_head_opts *opts)
|
2020-04-07 17:28:00 +03:00
|
|
|
{
|
2022-01-26 16:05:46 +03:00
|
|
|
const struct object_id *oid = opts->oid;
|
|
|
|
const char *switch_to_branch = opts->branch;
|
|
|
|
unsigned reset_hard = opts->flags & RESET_HEAD_HARD;
|
|
|
|
unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;
|
|
|
|
unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
|
2022-01-26 16:05:38 +03:00
|
|
|
struct object_id *head = NULL, head_oid;
|
2020-04-07 17:28:00 +03:00
|
|
|
struct tree_desc desc[2] = { { NULL }, { NULL } };
|
|
|
|
struct lock_file lock = LOCK_INIT;
|
2021-07-25 16:08:30 +03:00
|
|
|
struct unpack_trees_options unpack_tree_opts = { 0 };
|
2020-04-07 17:28:00 +03:00
|
|
|
struct tree *tree;
|
2022-01-26 16:05:42 +03:00
|
|
|
const char *action;
|
2020-04-07 17:28:00 +03:00
|
|
|
int ret = 0, nr = 0;
|
|
|
|
|
|
|
|
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
|
|
|
|
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
|
|
|
|
|
2022-01-26 16:05:47 +03:00
|
|
|
if (opts->orig_head_msg && !update_orig_head)
|
|
|
|
BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
|
|
|
|
|
|
|
|
if (opts->branch_msg && !opts->branch)
|
|
|
|
BUG("branch reflog message given without a branch");
|
|
|
|
|
2020-04-07 17:28:00 +03:00
|
|
|
if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto leave_reset_head;
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:05:38 +03:00
|
|
|
if (!get_oid("HEAD", &head_oid)) {
|
|
|
|
head = &head_oid;
|
|
|
|
} else if (!oid || !reset_hard) {
|
2020-04-07 17:28:00 +03:00
|
|
|
ret = error(_("could not determine HEAD revision"));
|
|
|
|
goto leave_reset_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!oid)
|
|
|
|
oid = &head_oid;
|
|
|
|
|
|
|
|
if (refs_only)
|
2022-01-26 16:05:46 +03:00
|
|
|
return update_refs(opts, oid, head);
|
2020-04-07 17:28:00 +03:00
|
|
|
|
2022-01-26 16:05:41 +03:00
|
|
|
action = reset_hard ? "reset" : "checkout";
|
2020-04-07 17:28:00 +03:00
|
|
|
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
|
|
|
|
unpack_tree_opts.head_idx = 1;
|
|
|
|
unpack_tree_opts.src_index = r->index;
|
|
|
|
unpack_tree_opts.dst_index = r->index;
|
|
|
|
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
|
|
|
|
unpack_tree_opts.update = 1;
|
|
|
|
unpack_tree_opts.merge = 1;
|
2021-09-27 19:33:43 +03:00
|
|
|
unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
|
2020-04-30 02:15:27 +03:00
|
|
|
init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
|
2022-01-26 16:05:39 +03:00
|
|
|
if (reset_hard)
|
2021-09-27 19:33:44 +03:00
|
|
|
unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
|
2020-04-07 17:28:00 +03:00
|
|
|
|
|
|
|
if (repo_read_index_unmerged(r) < 0) {
|
|
|
|
ret = error(_("could not read index"));
|
|
|
|
goto leave_reset_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
|
|
|
|
ret = error(_("failed to find tree of %s"),
|
|
|
|
oid_to_hex(&head_oid));
|
|
|
|
goto leave_reset_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
|
|
|
|
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
|
|
|
|
goto leave_reset_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unpack_trees(nr, desc, &unpack_tree_opts)) {
|
|
|
|
ret = -1;
|
|
|
|
goto leave_reset_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
tree = parse_tree_indirect(oid);
|
|
|
|
prime_cache_tree(r, r->index, tree);
|
|
|
|
|
|
|
|
if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
|
|
|
|
ret = error(_("could not write index"));
|
|
|
|
goto leave_reset_head;
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:05:43 +03:00
|
|
|
if (oid != &head_oid || update_orig_head || switch_to_branch)
|
2022-01-26 16:05:46 +03:00
|
|
|
ret = update_refs(opts, oid, head);
|
2020-04-07 17:28:00 +03:00
|
|
|
|
|
|
|
leave_reset_head:
|
|
|
|
rollback_lock_file(&lock);
|
2021-07-25 16:08:30 +03:00
|
|
|
clear_unpack_trees_porcelain(&unpack_tree_opts);
|
2020-04-07 17:28:00 +03:00
|
|
|
while (nr)
|
|
|
|
free((void *)desc[--nr].buffer);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|