]>
Commit | Line | Data |
---|---|---|
b309a971 DL |
1 | #include "git-compat-util.h" |
2 | #include "cache-tree.h" | |
f394e093 | 3 | #include "gettext.h" |
41771fa4 | 4 | #include "hex.h" |
b309a971 | 5 | #include "lockfile.h" |
dabab1d6 | 6 | #include "object-name.h" |
b309a971 DL |
7 | #include "refs.h" |
8 | #include "reset.h" | |
b309a971 DL |
9 | #include "tree-walk.h" |
10 | #include "tree.h" | |
11 | #include "unpack-trees.h" | |
72ddf34d | 12 | #include "hook.h" |
b309a971 | 13 | |
6ae80861 PW |
14 | static int update_refs(const struct reset_head_opts *opts, |
15 | const struct object_id *oid, | |
16 | const struct object_id *head) | |
b309a971 | 17 | { |
6ae80861 PW |
18 | unsigned detach_head = opts->flags & RESET_HEAD_DETACH; |
19 | unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; | |
20 | unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; | |
cd1528ef | 21 | const struct object_id *orig_head = opts->orig_head; |
6ae80861 | 22 | const char *switch_to_branch = opts->branch; |
7700ab08 | 23 | const char *reflog_branch = opts->branch_msg; |
6ae80861 PW |
24 | const char *reflog_head = opts->head_msg; |
25 | const char *reflog_orig_head = opts->orig_head_msg; | |
26 | const char *default_reflog_action = opts->default_reflog_action; | |
d6a9f5ea PW |
27 | struct object_id *old_orig = NULL, oid_old_orig; |
28 | struct strbuf msg = STRBUF_INIT; | |
29 | const char *reflog_action; | |
30 | size_t prefix_len; | |
31 | int ret; | |
32 | ||
1526d0fc PW |
33 | if ((update_orig_head && !reflog_orig_head) || !reflog_head) { |
34 | if (!default_reflog_action) | |
35 | BUG("default_reflog_action must be given when reflog messages are omitted"); | |
36 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); | |
37 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : | |
38 | default_reflog_action); | |
39 | } | |
d6a9f5ea PW |
40 | prefix_len = msg.len; |
41 | ||
42 | if (update_orig_head) { | |
d850b7a5 | 43 | if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig)) |
d6a9f5ea PW |
44 | old_orig = &oid_old_orig; |
45 | if (head) { | |
46 | if (!reflog_orig_head) { | |
47 | strbuf_addstr(&msg, "updating ORIG_HEAD"); | |
48 | reflog_orig_head = msg.buf; | |
49 | } | |
cd1528ef PW |
50 | update_ref(reflog_orig_head, "ORIG_HEAD", |
51 | orig_head ? orig_head : head, | |
d6a9f5ea PW |
52 | old_orig, 0, UPDATE_REFS_MSG_ON_ERR); |
53 | } else if (old_orig) | |
54 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); | |
55 | } | |
56 | ||
57 | if (!reflog_head) { | |
58 | strbuf_setlen(&msg, prefix_len); | |
59 | strbuf_addstr(&msg, "updating HEAD"); | |
60 | reflog_head = msg.buf; | |
61 | } | |
62 | if (!switch_to_branch) | |
63 | ret = update_ref(reflog_head, "HEAD", oid, head, | |
64 | detach_head ? REF_NO_DEREF : 0, | |
65 | UPDATE_REFS_MSG_ON_ERR); | |
66 | else { | |
7700ab08 PW |
67 | ret = update_ref(reflog_branch ? reflog_branch : reflog_head, |
68 | switch_to_branch, oid, NULL, 0, | |
69 | UPDATE_REFS_MSG_ON_ERR); | |
d6a9f5ea PW |
70 | if (!ret) |
71 | ret = create_symref("HEAD", switch_to_branch, | |
72 | reflog_head); | |
73 | } | |
74 | if (!ret && run_hook) | |
bcd020f8 | 75 | run_hooks_l("post-checkout", |
d6a9f5ea PW |
76 | oid_to_hex(head ? head : null_oid()), |
77 | oid_to_hex(oid), "1", NULL); | |
78 | strbuf_release(&msg); | |
79 | return ret; | |
80 | } | |
81 | ||
6ae80861 | 82 | int reset_head(struct repository *r, const struct reset_head_opts *opts) |
b309a971 | 83 | { |
6ae80861 PW |
84 | const struct object_id *oid = opts->oid; |
85 | const char *switch_to_branch = opts->branch; | |
86 | unsigned reset_hard = opts->flags & RESET_HEAD_HARD; | |
87 | unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY; | |
88 | unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; | |
69f4c230 | 89 | struct object_id *head = NULL, head_oid; |
b309a971 DL |
90 | struct tree_desc desc[2] = { { NULL }, { NULL } }; |
91 | struct lock_file lock = LOCK_INIT; | |
9a863b33 | 92 | struct unpack_trees_options unpack_tree_opts = { 0 }; |
b309a971 | 93 | struct tree *tree; |
d6a9f5ea | 94 | const char *action; |
b309a971 DL |
95 | int ret = 0, nr = 0; |
96 | ||
97 | if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) | |
98 | BUG("Not a fully qualified branch: '%s'", switch_to_branch); | |
99 | ||
7700ab08 PW |
100 | if (opts->orig_head_msg && !update_orig_head) |
101 | BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD"); | |
102 | ||
103 | if (opts->branch_msg && !opts->branch) | |
104 | BUG("branch reflog message given without a branch"); | |
105 | ||
b309a971 DL |
106 | if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { |
107 | ret = -1; | |
108 | goto leave_reset_head; | |
109 | } | |
110 | ||
4a93b899 | 111 | if (!repo_get_oid(r, "HEAD", &head_oid)) { |
69f4c230 PW |
112 | head = &head_oid; |
113 | } else if (!oid || !reset_hard) { | |
b309a971 DL |
114 | ret = error(_("could not determine HEAD revision")); |
115 | goto leave_reset_head; | |
116 | } | |
117 | ||
118 | if (!oid) | |
119 | oid = &head_oid; | |
120 | ||
121 | if (refs_only) | |
6ae80861 | 122 | return update_refs(opts, oid, head); |
b309a971 | 123 | |
1946d458 | 124 | action = reset_hard ? "reset" : "checkout"; |
b309a971 DL |
125 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); |
126 | unpack_tree_opts.head_idx = 1; | |
127 | unpack_tree_opts.src_index = r->index; | |
128 | unpack_tree_opts.dst_index = r->index; | |
129 | unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; | |
130 | unpack_tree_opts.update = 1; | |
131 | unpack_tree_opts.merge = 1; | |
1b5f3733 | 132 | unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
652bd021 | 133 | unpack_tree_opts.skip_cache_tree_update = 1; |
bf102008 | 134 | init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL); |
ab2fba08 | 135 | if (reset_hard) |
480d3d6b | 136 | unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED; |
b309a971 DL |
137 | |
138 | if (repo_read_index_unmerged(r) < 0) { | |
139 | ret = error(_("could not read index")); | |
140 | goto leave_reset_head; | |
141 | } | |
142 | ||
143 | if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) { | |
144 | ret = error(_("failed to find tree of %s"), | |
145 | oid_to_hex(&head_oid)); | |
146 | goto leave_reset_head; | |
147 | } | |
148 | ||
149 | if (!fill_tree_descriptor(r, &desc[nr++], oid)) { | |
150 | ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); | |
151 | goto leave_reset_head; | |
152 | } | |
153 | ||
154 | if (unpack_trees(nr, desc, &unpack_tree_opts)) { | |
155 | ret = -1; | |
156 | goto leave_reset_head; | |
157 | } | |
158 | ||
159 | tree = parse_tree_indirect(oid); | |
160 | prime_cache_tree(r, r->index, tree); | |
161 | ||
162 | if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) { | |
163 | ret = error(_("could not write index")); | |
164 | goto leave_reset_head; | |
165 | } | |
166 | ||
1526d0fc | 167 | if (oid != &head_oid || update_orig_head || switch_to_branch) |
6ae80861 | 168 | ret = update_refs(opts, oid, head); |
b309a971 DL |
169 | |
170 | leave_reset_head: | |
b309a971 | 171 | rollback_lock_file(&lock); |
9a863b33 | 172 | clear_unpack_trees_porcelain(&unpack_tree_opts); |
b309a971 DL |
173 | while (nr) |
174 | free((void *)desc[--nr].buffer); | |
175 | return ret; | |
176 | ||
177 | } |