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