]> git.ipfire.org Git - thirdparty/git.git/blame - reset.c
reftable: generic interface to tables
[thirdparty/git.git] / reset.c
CommitLineData
b309a971
DL
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"
10
11int 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)
15{
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;
9a863b33 24 struct unpack_trees_options unpack_tree_opts = { 0 };
b309a971
DL
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;
32
33 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
34 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
35
36 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
37 ret = -1;
38 goto leave_reset_head;
39 }
40
41 if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
42 ret = error(_("could not determine HEAD revision"));
43 goto leave_reset_head;
44 }
45
46 if (!oid)
47 oid = &head_oid;
48
49 if (refs_only)
50 goto reset_head_refs;
51
b309a971
DL
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;
bf102008 59 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
b309a971
DL
60 if (!detach_head)
61 unpack_tree_opts.reset = 1;
62
63 if (repo_read_index_unmerged(r) < 0) {
64 ret = error(_("could not read index"));
65 goto leave_reset_head;
66 }
67
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;
72 }
73
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;
77 }
78
79 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
80 ret = -1;
81 goto leave_reset_head;
82 }
83
84 tree = parse_tree_indirect(oid);
85 prime_cache_tree(r, r->index, tree);
86
87 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
88 ret = error(_("could not write index"));
89 goto leave_reset_head;
90 }
91
92reset_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;
96
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;
105 }
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);
110 }
111
112 if (!reflog_head) {
113 strbuf_setlen(&msg, prefix_len);
114 strbuf_addstr(&msg, "updating HEAD");
115 reflog_head = msg.buf;
116 }
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);
127 }
128 if (run_hook)
129 run_hook_le(NULL, "post-checkout",
14228447 130 oid_to_hex(orig ? orig : null_oid()),
b309a971
DL
131 oid_to_hex(oid), "1", NULL);
132
133leave_reset_head:
134 strbuf_release(&msg);
135 rollback_lock_file(&lock);
9a863b33 136 clear_unpack_trees_porcelain(&unpack_tree_opts);
b309a971
DL
137 while (nr)
138 free((void *)desc[--nr].buffer);
139 return ret;
140
141}