]> git.ipfire.org Git - thirdparty/git.git/blame - reset.c
rebase: pass correct arguments to post-checkout hook
[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;
69f4c230 21 struct object_id *head = NULL, head_oid;
b309a971
DL
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;
69f4c230 29 struct object_id *old_orig = NULL, oid_old_orig;
b309a971
DL
30 int ret = 0, nr = 0;
31
32 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
33 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
34
35 if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
36 ret = -1;
37 goto leave_reset_head;
38 }
39
69f4c230
PW
40 if (!get_oid("HEAD", &head_oid)) {
41 head = &head_oid;
42 } else if (!oid || !reset_hard) {
b309a971
DL
43 ret = error(_("could not determine HEAD revision"));
44 goto leave_reset_head;
45 }
46
47 if (!oid)
48 oid = &head_oid;
49
50 if (refs_only)
51 goto reset_head_refs;
52
b309a971
DL
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;
1b5f3733 60 unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
bf102008 61 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
b309a971 62 if (!detach_head)
480d3d6b 63 unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
b309a971
DL
64
65 if (repo_read_index_unmerged(r) < 0) {
66 ret = error(_("could not read index"));
67 goto leave_reset_head;
68 }
69
70 if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
71 ret = error(_("failed to find tree of %s"),
72 oid_to_hex(&head_oid));
73 goto leave_reset_head;
74 }
75
76 if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
77 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
78 goto leave_reset_head;
79 }
80
81 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
82 ret = -1;
83 goto leave_reset_head;
84 }
85
86 tree = parse_tree_indirect(oid);
87 prime_cache_tree(r, r->index, tree);
88
89 if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
90 ret = error(_("could not write index"));
91 goto leave_reset_head;
92 }
93
94reset_head_refs:
95 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
96 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action);
97 prefix_len = msg.len;
98
99 if (update_orig_head) {
100 if (!get_oid("ORIG_HEAD", &oid_old_orig))
101 old_orig = &oid_old_orig;
69f4c230 102 if (head) {
b309a971
DL
103 if (!reflog_orig_head) {
104 strbuf_addstr(&msg, "updating ORIG_HEAD");
105 reflog_orig_head = msg.buf;
106 }
69f4c230 107 update_ref(reflog_orig_head, "ORIG_HEAD", head,
b309a971
DL
108 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
109 } else if (old_orig)
110 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
111 }
112
113 if (!reflog_head) {
114 strbuf_setlen(&msg, prefix_len);
115 strbuf_addstr(&msg, "updating HEAD");
116 reflog_head = msg.buf;
117 }
118 if (!switch_to_branch)
69f4c230 119 ret = update_ref(reflog_head, "HEAD", oid, head,
b309a971
DL
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);
128 }
129 if (run_hook)
130 run_hook_le(NULL, "post-checkout",
69f4c230 131 oid_to_hex(head ? head : null_oid()),
b309a971
DL
132 oid_to_hex(oid), "1", NULL);
133
134leave_reset_head:
135 strbuf_release(&msg);
136 rollback_lock_file(&lock);
9a863b33 137 clear_unpack_trees_porcelain(&unpack_tree_opts);
b309a971
DL
138 while (nr)
139 free((void *)desc[--nr].buffer);
140 return ret;
141
142}