Merge branch 'ab/send-email-config-fix'
[git/debian.git] / reset.c
blob79310ae071b7e897e71d6778926b6e7d21869647
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 = { 0 };
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 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
53 unpack_tree_opts.head_idx = 1;
54 unpack_tree_opts.src_index = r->index;
55 unpack_tree_opts.dst_index = r->index;
56 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
57 unpack_tree_opts.update = 1;
58 unpack_tree_opts.merge = 1;
59 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
60 if (!detach_head)
61 unpack_tree_opts.reset = 1;
63 if (repo_read_index_unmerged(r) < 0) {
64 ret = error(_("could not read index"));
65 goto leave_reset_head;
68 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
69 ret = error(_("failed to find tree of %s"),
70 oid_to_hex(&head_oid));
71 goto leave_reset_head;
74 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
75 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
76 goto leave_reset_head;
79 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
80 ret = -1;
81 goto leave_reset_head;
84 tree = parse_tree_indirect(oid);
85 prime_cache_tree(r, r->index, tree);
87 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
88 ret = error(_("could not write index"));
89 goto leave_reset_head;
92 reset_head_refs:
93 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
94 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action);
95 prefix_len = msg.len;
97 if (update_orig_head) {
98 if (!get_oid("ORIG_HEAD", &oid_old_orig))
99 old_orig = &oid_old_orig;
100 if (!get_oid("HEAD", &oid_orig)) {
101 orig = &oid_orig;
102 if (!reflog_orig_head) {
103 strbuf_addstr(&msg, "updating ORIG_HEAD");
104 reflog_orig_head = msg.buf;
106 update_ref(reflog_orig_head, "ORIG_HEAD", orig,
107 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
108 } else if (old_orig)
109 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
112 if (!reflog_head) {
113 strbuf_setlen(&msg, prefix_len);
114 strbuf_addstr(&msg, "updating HEAD");
115 reflog_head = msg.buf;
117 if (!switch_to_branch)
118 ret = update_ref(reflog_head, "HEAD", oid, orig,
119 detach_head ? REF_NO_DEREF : 0,
120 UPDATE_REFS_MSG_ON_ERR);
121 else {
122 ret = update_ref(reflog_head, switch_to_branch, oid,
123 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
124 if (!ret)
125 ret = create_symref("HEAD", switch_to_branch,
126 reflog_head);
128 if (run_hook)
129 run_hook_le(NULL, "post-checkout",
130 oid_to_hex(orig ? orig : null_oid()),
131 oid_to_hex(oid), "1", NULL);
133 leave_reset_head:
134 strbuf_release(&msg);
135 rollback_lock_file(&lock);
136 clear_unpack_trees_porcelain(&unpack_tree_opts);
137 while (nr)
138 free((void *)desc[--nr].buffer);
139 return ret;