Merge branch 'backport/jk/range-diff-fixes'
[git.git] / reset.c
blob2f4fbd07c54b59f71c92391a601d1ec9fe05ce7d
1 #include "git-compat-util.h"
2 #include "cache-tree.h"
3 #include "lockfile.h"
4 #include "refs.h"
5 #include "reset.h"
6 #include "run-command.h"
7 #include "tree-walk.h"
8 #include "tree.h"
9 #include "unpack-trees.h"
11 int reset_head(struct repository *r, struct object_id *oid, const char *action,
12 const char *switch_to_branch, unsigned flags,
13 const char *reflog_orig_head, const char *reflog_head,
14 const char *default_reflog_action)
16 unsigned detach_head = flags & RESET_HEAD_DETACH;
17 unsigned reset_hard = flags & RESET_HEAD_HARD;
18 unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
19 unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
20 unsigned update_orig_head = flags & RESET_ORIG_HEAD;
21 struct object_id head_oid;
22 struct tree_desc desc[2] = { { NULL }, { NULL } };
23 struct lock_file lock = LOCK_INIT;
24 struct unpack_trees_options unpack_tree_opts;
25 struct tree *tree;
26 const char *reflog_action;
27 struct strbuf msg = STRBUF_INIT;
28 size_t prefix_len;
29 struct object_id *orig = NULL, oid_orig,
30 *old_orig = NULL, oid_old_orig;
31 int ret = 0, nr = 0;
33 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
34 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
36 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
37 ret = -1;
38 goto leave_reset_head;
41 if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
42 ret = error(_("could not determine HEAD revision"));
43 goto leave_reset_head;
46 if (!oid)
47 oid = &head_oid;
49 if (refs_only)
50 goto reset_head_refs;
52 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
53 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
54 unpack_tree_opts.head_idx = 1;
55 unpack_tree_opts.src_index = r->index;
56 unpack_tree_opts.dst_index = r->index;
57 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
58 unpack_tree_opts.update = 1;
59 unpack_tree_opts.merge = 1;
60 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
61 if (!detach_head)
62 unpack_tree_opts.reset = 1;
64 if (repo_read_index_unmerged(r) < 0) {
65 ret = error(_("could not read index"));
66 goto leave_reset_head;
69 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
70 ret = error(_("failed to find tree of %s"),
71 oid_to_hex(&head_oid));
72 goto leave_reset_head;
75 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
76 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
77 goto leave_reset_head;
80 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
81 ret = -1;
82 goto leave_reset_head;
85 tree = parse_tree_indirect(oid);
86 prime_cache_tree(r, r->index, tree);
88 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
89 ret = error(_("could not write index"));
90 goto leave_reset_head;
93 reset_head_refs:
94 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
95 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action);
96 prefix_len = msg.len;
98 if (update_orig_head) {
99 if (!get_oid("ORIG_HEAD", &oid_old_orig))
100 old_orig = &oid_old_orig;
101 if (!get_oid("HEAD", &oid_orig)) {
102 orig = &oid_orig;
103 if (!reflog_orig_head) {
104 strbuf_addstr(&msg, "updating ORIG_HEAD");
105 reflog_orig_head = msg.buf;
107 update_ref(reflog_orig_head, "ORIG_HEAD", orig,
108 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
109 } else if (old_orig)
110 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
113 if (!reflog_head) {
114 strbuf_setlen(&msg, prefix_len);
115 strbuf_addstr(&msg, "updating HEAD");
116 reflog_head = msg.buf;
118 if (!switch_to_branch)
119 ret = update_ref(reflog_head, "HEAD", oid, orig,
120 detach_head ? REF_NO_DEREF : 0,
121 UPDATE_REFS_MSG_ON_ERR);
122 else {
123 ret = update_ref(reflog_head, switch_to_branch, oid,
124 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
125 if (!ret)
126 ret = create_symref("HEAD", switch_to_branch,
127 reflog_head);
129 if (run_hook)
130 run_hook_le(NULL, "post-checkout",
131 oid_to_hex(orig ? orig : &null_oid),
132 oid_to_hex(oid), "1", NULL);
134 leave_reset_head:
135 strbuf_release(&msg);
136 rollback_lock_file(&lock);
137 while (nr)
138 free((void *)desc[--nr].buffer);
139 return ret;