]>
Commit | Line | Data |
---|---|---|
0e5a7faa CR |
1 | /* |
2 | * "git reset" builtin command | |
3 | * | |
4 | * Copyright (c) 2007 Carlos Rica | |
5 | * | |
6 | * Based on git-reset.sh, which is | |
7 | * | |
8 | * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano | |
9 | */ | |
07047d68 | 10 | #define USE_THE_INDEX_VARIABLE |
c2e86add | 11 | #include "builtin.h" |
b2141fc1 | 12 | #include "config.h" |
32a8f510 | 13 | #include "environment.h" |
f394e093 | 14 | #include "gettext.h" |
41771fa4 | 15 | #include "hex.h" |
697cc8ef | 16 | #include "lockfile.h" |
0e5a7faa CR |
17 | #include "tag.h" |
18 | #include "object.h" | |
cf394719 | 19 | #include "pretty.h" |
0e5a7faa CR |
20 | #include "run-command.h" |
21 | #include "refs.h" | |
22 | #include "diff.h" | |
23 | #include "diffcore.h" | |
24 | #include "tree.h" | |
c369e7b8 | 25 | #include "branch.h" |
5eee6b28 | 26 | #include "parse-options.h" |
d0f379c2 SB |
27 | #include "unpack-trees.h" |
28 | #include "cache-tree.h" | |
e38da487 | 29 | #include "setup.h" |
35b96d1d SB |
30 | #include "submodule.h" |
31 | #include "submodule-config.h" | |
74ea5c95 EN |
32 | #include "trace.h" |
33 | #include "trace2.h" | |
71471b2a | 34 | #include "dir.h" |
d21878f0 | 35 | #include "add-interactive.h" |
35b96d1d | 36 | |
649bf3a4 BP |
37 | #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000) |
38 | ||
5eee6b28 | 39 | static const char * const git_reset_usage[] = { |
c1e9c2a7 | 40 | N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"), |
d137b507 | 41 | N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."), |
64bac8df | 42 | N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"), |
d137b507 | 43 | N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"), |
5eee6b28 CR |
44 | NULL |
45 | }; | |
0e5a7faa | 46 | |
9bc454df CC |
47 | enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE }; |
48 | static const char *reset_type_names[] = { | |
8b2a57b6 | 49 | N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL |
9bc454df | 50 | }; |
9e8eceab | 51 | |
0e5a7faa CR |
52 | static inline int is_merge(void) |
53 | { | |
102de880 | 54 | return !access(git_path_merge_head(the_repository), F_OK); |
0e5a7faa CR |
55 | } |
56 | ||
4cf76f6b | 57 | static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet) |
0e5a7faa | 58 | { |
afbb8838 | 59 | int i, nr = 0; |
d0f379c2 | 60 | struct tree_desc desc[2]; |
6c52ec8a | 61 | struct tree *tree; |
d0f379c2 | 62 | struct unpack_trees_options opts; |
afbb8838 | 63 | int ret = -1; |
0e5a7faa | 64 | |
d0f379c2 SB |
65 | memset(&opts, 0, sizeof(opts)); |
66 | opts.head_idx = 1; | |
67 | opts.src_index = &the_index; | |
68 | opts.dst_index = &the_index; | |
69 | opts.fn = oneway_merge; | |
70 | opts.merge = 1; | |
4cf76f6b | 71 | init_checkout_metadata(&opts.meta, ref, oid, NULL); |
5aa965a0 | 72 | if (!quiet) |
d0f379c2 | 73 | opts.verbose_update = 1; |
9e8eceab | 74 | switch (reset_type) { |
9bc454df | 75 | case KEEP: |
9e8eceab | 76 | case MERGE: |
d0f379c2 | 77 | opts.update = 1; |
1b5f3733 | 78 | opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
9e8eceab LT |
79 | break; |
80 | case HARD: | |
d0f379c2 | 81 | opts.update = 1; |
480d3d6b | 82 | opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED; |
0e47bca0 | 83 | opts.skip_cache_tree_update = 1; |
480d3d6b EN |
84 | break; |
85 | case MIXED: | |
86 | opts.reset = UNPACK_RESET_PROTECT_UNTRACKED; | |
0e47bca0 | 87 | opts.skip_cache_tree_update = 1; |
480d3d6b EN |
88 | /* but opts.update=0, so working tree not updated */ |
89 | break; | |
9e8eceab | 90 | default: |
480d3d6b | 91 | BUG("invalid reset_type passed to reset_index"); |
9e8eceab | 92 | } |
0e5a7faa | 93 | |
fbc1ed62 | 94 | repo_read_index_unmerged(the_repository); |
d0f379c2 | 95 | |
9bc454df | 96 | if (reset_type == KEEP) { |
3a5d7c55 | 97 | struct object_id head_oid; |
d850b7a5 | 98 | if (repo_get_oid(the_repository, "HEAD", &head_oid)) |
b50a64e8 | 99 | return error(_("You do not have a valid HEAD.")); |
5e575807 | 100 | if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid)) |
b50a64e8 | 101 | return error(_("Failed to find tree of HEAD.")); |
9bc454df CC |
102 | nr++; |
103 | opts.fn = twoway_merge; | |
104 | } | |
105 | ||
5e575807 | 106 | if (!fill_tree_descriptor(the_repository, desc + nr, oid)) { |
afbb8838 JK |
107 | error(_("Failed to find tree of %s."), oid_to_hex(oid)); |
108 | goto out; | |
109 | } | |
e9ce897b JK |
110 | nr++; |
111 | ||
d0f379c2 | 112 | if (unpack_trees(nr, desc, &opts)) |
afbb8838 | 113 | goto out; |
6c52ec8a TR |
114 | |
115 | if (reset_type == MIXED || reset_type == HARD) { | |
a9dbc179 | 116 | tree = parse_tree_indirect(oid); |
c207e9e1 | 117 | prime_cache_tree(the_repository, the_repository->index, tree); |
6c52ec8a TR |
118 | } |
119 | ||
afbb8838 JK |
120 | ret = 0; |
121 | ||
122 | out: | |
123 | for (i = 0; i < nr; i++) | |
124 | free((void *)desc[i].buffer); | |
125 | return ret; | |
0e5a7faa CR |
126 | } |
127 | ||
128 | static void print_new_head_line(struct commit *commit) | |
129 | { | |
1cf823fb | 130 | struct strbuf buf = STRBUF_INIT; |
0e5a7faa | 131 | |
1cf823fb | 132 | printf(_("HEAD is now at %s"), |
d850b7a5 | 133 | repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV)); |
1cf823fb TG |
134 | |
135 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf); | |
136 | if (buf.len > 0) | |
137 | printf(" %s", buf.buf); | |
138 | putchar('\n'); | |
139 | strbuf_release(&buf); | |
0e5a7faa CR |
140 | } |
141 | ||
0e5a7faa | 142 | static void update_index_from_diff(struct diff_queue_struct *q, |
61bdc7c5 JK |
143 | struct diff_options *opt UNUSED, |
144 | void *data) | |
0e5a7faa CR |
145 | { |
146 | int i; | |
b4b313f9 | 147 | int intent_to_add = *(int *)data; |
0e5a7faa CR |
148 | |
149 | for (i = 0; i < q->nr; i++) { | |
71471b2a | 150 | int pos; |
0e5a7faa | 151 | struct diff_filespec *one = q->queue[i]->one; |
1f86b7cb | 152 | int is_in_reset_tree = one->mode && !is_null_oid(&one->oid); |
b4b313f9 NTND |
153 | struct cache_entry *ce; |
154 | ||
1f86b7cb | 155 | if (!is_in_reset_tree && !intent_to_add) { |
031b2033 | 156 | remove_file_from_index(&the_index, one->path); |
b4b313f9 NTND |
157 | continue; |
158 | } | |
159 | ||
a849735b | 160 | ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path, |
b4b313f9 | 161 | 0, 0); |
71471b2a VD |
162 | |
163 | /* | |
164 | * If the file 1) corresponds to an existing index entry with | |
165 | * skip-worktree set, or 2) does not exist in the index but is | |
166 | * outside the sparse checkout definition, add a skip-worktree bit | |
4d1cfc13 VD |
167 | * to the new index entry. Note that a sparse index will be expanded |
168 | * if this entry is outside the sparse cone - this is necessary | |
169 | * to properly construct the reset sparse directory. | |
71471b2a | 170 | */ |
07047d68 | 171 | pos = index_name_pos(&the_index, one->path, strlen(one->path)); |
dc594180 | 172 | if ((pos >= 0 && ce_skip_worktree(the_index.cache[pos])) || |
71471b2a VD |
173 | (pos < 0 && !path_in_sparse_checkout(one->path, &the_index))) |
174 | ce->ce_flags |= CE_SKIP_WORKTREE; | |
175 | ||
b4b313f9 NTND |
176 | if (!ce) |
177 | die(_("make_cache_entry failed for path '%s'"), | |
178 | one->path); | |
1f86b7cb | 179 | if (!is_in_reset_tree) { |
b4b313f9 NTND |
180 | ce->ce_flags |= CE_INTENT_TO_ADD; |
181 | set_object_name_for_intent_to_add_entry(ce); | |
182 | } | |
031b2033 ÆAB |
183 | add_index_entry(&the_index, ce, |
184 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
0e5a7faa CR |
185 | } |
186 | } | |
187 | ||
bd1928df | 188 | static int read_from_tree(const struct pathspec *pathspec, |
3a5d7c55 | 189 | struct object_id *tree_oid, |
b4b313f9 | 190 | int intent_to_add) |
0e5a7faa | 191 | { |
0e5a7faa CR |
192 | struct diff_options opt; |
193 | ||
194 | memset(&opt, 0, sizeof(opt)); | |
bd1928df | 195 | copy_pathspec(&opt.pathspec, pathspec); |
0e5a7faa CR |
196 | opt.output_format = DIFF_FORMAT_CALLBACK; |
197 | opt.format_callback = update_index_from_diff; | |
b4b313f9 | 198 | opt.format_callback_data = &intent_to_add; |
0d1e0e78 | 199 | opt.flags.override_submodule_config = 1; |
4d1cfc13 | 200 | opt.flags.recursive = 1; |
b78ea5fc | 201 | opt.repo = the_repository; |
4d1cfc13 VD |
202 | opt.change = diff_change; |
203 | opt.add_remove = diff_addremove; | |
204 | ||
b29ad383 | 205 | if (pathspec->nr && pathspec_needs_expanded_index(&the_index, pathspec)) |
4d1cfc13 | 206 | ensure_full_index(&the_index); |
0e5a7faa | 207 | |
944cffbd | 208 | if (do_diff_cache(tree_oid, &opt)) |
0e5a7faa CR |
209 | return 1; |
210 | diffcore_std(&opt); | |
211 | diff_flush(&opt); | |
2e7a9785 | 212 | |
bf883f30 | 213 | return 0; |
0e5a7faa CR |
214 | } |
215 | ||
d04520e3 JK |
216 | static void set_reflog_message(struct strbuf *sb, const char *action, |
217 | const char *rev) | |
0e5a7faa | 218 | { |
0e5a7faa | 219 | const char *rla = getenv("GIT_REFLOG_ACTION"); |
d04520e3 JK |
220 | |
221 | strbuf_reset(sb); | |
222 | if (rla) | |
223 | strbuf_addf(sb, "%s: %s", rla, action); | |
224 | else if (rev) | |
225 | strbuf_addf(sb, "reset: moving to %s", rev); | |
226 | else | |
227 | strbuf_addf(sb, "reset: %s", action); | |
0e5a7faa CR |
228 | } |
229 | ||
812d2a3d CC |
230 | static void die_if_unmerged_cache(int reset_type) |
231 | { | |
fbc1ed62 | 232 | if (is_merge() || unmerged_index(&the_index)) |
8b2a57b6 ÆAB |
233 | die(_("Cannot do a %s reset in the middle of a merge."), |
234 | _(reset_type_names[reset_type])); | |
812d2a3d CC |
235 | |
236 | } | |
237 | ||
f8144c9f NTND |
238 | static void parse_args(struct pathspec *pathspec, |
239 | const char **argv, const char *prefix, | |
240 | int patch_mode, | |
241 | const char **rev_ret) | |
0e5a7faa | 242 | { |
0e5a7faa | 243 | const char *rev = "HEAD"; |
3a5d7c55 | 244 | struct object_id unused; |
dfc8f39e JH |
245 | /* |
246 | * Possible arguments are: | |
247 | * | |
2f328c3d MZ |
248 | * git reset [-opts] [<rev>] |
249 | * git reset [-opts] <tree> [<paths>...] | |
250 | * git reset [-opts] <tree> -- [<paths>...] | |
251 | * git reset [-opts] -- [<paths>...] | |
dfc8f39e JH |
252 | * git reset [-opts] <paths>... |
253 | * | |
dca48cf5 | 254 | * At this point, argv points immediately after [-opts]. |
dfc8f39e JH |
255 | */ |
256 | ||
dca48cf5 MZ |
257 | if (argv[0]) { |
258 | if (!strcmp(argv[0], "--")) { | |
259 | argv++; /* reset to HEAD, possibly with paths */ | |
260 | } else if (argv[1] && !strcmp(argv[1], "--")) { | |
261 | rev = argv[0]; | |
262 | argv += 2; | |
dfc8f39e JH |
263 | } |
264 | /* | |
dca48cf5 | 265 | * Otherwise, argv[0] could be either <rev> or <paths> and |
2f328c3d MZ |
266 | * has to be unambiguous. If there is a single argument, it |
267 | * can not be a tree | |
dfc8f39e | 268 | */ |
d850b7a5 ÆAB |
269 | else if ((!argv[1] && !repo_get_oid_committish(the_repository, argv[0], &unused)) || |
270 | (argv[1] && !repo_get_oid_treeish(the_repository, argv[0], &unused))) { | |
dfc8f39e | 271 | /* |
2f328c3d | 272 | * Ok, argv[0] looks like a commit/tree; it should not |
dfc8f39e JH |
273 | * be a filename. |
274 | */ | |
dca48cf5 MZ |
275 | verify_non_filename(prefix, argv[0]); |
276 | rev = *argv++; | |
dfc8f39e JH |
277 | } else { |
278 | /* Otherwise we treat this as a filename */ | |
dca48cf5 | 279 | verify_filename(prefix, argv[0], 1); |
dfc8f39e JH |
280 | } |
281 | } | |
39ea722d | 282 | *rev_ret = rev; |
2c63d6eb | 283 | |
480ca644 NTND |
284 | parse_pathspec(pathspec, 0, |
285 | PATHSPEC_PREFER_FULL | | |
286 | (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0), | |
f8144c9f | 287 | prefix, argv); |
39ea722d MZ |
288 | } |
289 | ||
3a5d7c55 | 290 | static int reset_refs(const char *rev, const struct object_id *oid) |
7bca0e45 MZ |
291 | { |
292 | int update_ref_status; | |
293 | struct strbuf msg = STRBUF_INIT; | |
3a5d7c55 | 294 | struct object_id *orig = NULL, oid_orig, |
295 | *old_orig = NULL, oid_old_orig; | |
7bca0e45 | 296 | |
d850b7a5 | 297 | if (!repo_get_oid(the_repository, "ORIG_HEAD", &oid_old_orig)) |
3a5d7c55 | 298 | old_orig = &oid_old_orig; |
d850b7a5 | 299 | if (!repo_get_oid(the_repository, "HEAD", &oid_orig)) { |
3a5d7c55 | 300 | orig = &oid_orig; |
7bca0e45 | 301 | set_reflog_message(&msg, "updating ORIG_HEAD", NULL); |
ae077771 | 302 | update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, |
f4124112 | 303 | UPDATE_REFS_MSG_ON_ERR); |
7bca0e45 | 304 | } else if (old_orig) |
2616a5e5 | 305 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); |
7bca0e45 | 306 | set_reflog_message(&msg, "updating HEAD", rev); |
ae077771 | 307 | update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0, |
f4124112 | 308 | UPDATE_REFS_MSG_ON_ERR); |
7bca0e45 MZ |
309 | strbuf_release(&msg); |
310 | return update_ref_status; | |
311 | } | |
312 | ||
046b4823 SB |
313 | static int git_reset_config(const char *var, const char *value, void *cb) |
314 | { | |
315 | if (!strcmp(var, "submodule.recurse")) | |
316 | return git_default_submodule_config(var, value, cb); | |
317 | ||
318 | return git_default_config(var, value, cb); | |
319 | } | |
320 | ||
39ea722d MZ |
321 | int cmd_reset(int argc, const char **argv, const char *prefix) |
322 | { | |
323 | int reset_type = NONE, update_ref_status = 0, quiet = 0; | |
5891c76c | 324 | int no_refresh = 0; |
64bac8df | 325 | int patch_mode = 0, pathspec_file_nul = 0, unborn; |
7ce4088a JK |
326 | const char *rev; |
327 | char *pathspec_from_file = NULL; | |
f2fd0760 | 328 | struct object_id oid; |
f8144c9f | 329 | struct pathspec pathspec; |
b4b313f9 | 330 | int intent_to_add = 0; |
39ea722d MZ |
331 | const struct option options[] = { |
332 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
5891c76c | 333 | OPT_BOOL(0, "no-refresh", &no_refresh, |
fd56fba9 | 334 | N_("skip refreshing the index after reset")), |
39ea722d MZ |
335 | OPT_SET_INT(0, "mixed", &reset_type, |
336 | N_("reset HEAD and index"), MIXED), | |
337 | OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT), | |
338 | OPT_SET_INT(0, "hard", &reset_type, | |
339 | N_("reset HEAD, index and working tree"), HARD), | |
340 | OPT_SET_INT(0, "merge", &reset_type, | |
341 | N_("reset HEAD, index and working tree"), MERGE), | |
342 | OPT_SET_INT(0, "keep", &reset_type, | |
343 | N_("reset HEAD but keep local changes"), KEEP), | |
203c8533 | 344 | OPT_CALLBACK_F(0, "recurse-submodules", NULL, |
35b96d1d | 345 | "reset", "control recursive updating of submodules", |
203c8533 | 346 | PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater), |
d5d09d47 | 347 | OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")), |
b4b313f9 NTND |
348 | OPT_BOOL('N', "intent-to-add", &intent_to_add, |
349 | N_("record only the fact that removed paths will be added later")), | |
64bac8df AM |
350 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
351 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), | |
39ea722d MZ |
352 | OPT_END() |
353 | }; | |
354 | ||
046b4823 | 355 | git_config(git_reset_config, NULL); |
39ea722d MZ |
356 | |
357 | argc = parse_options(argc, argv, prefix, options, git_reset_usage, | |
358 | PARSE_OPT_KEEP_DASHDASH); | |
f8144c9f | 359 | parse_args(&pathspec, argv, prefix, patch_mode, &rev); |
0e5a7faa | 360 | |
64bac8df AM |
361 | if (pathspec_from_file) { |
362 | if (patch_mode) | |
12909b6b | 363 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch"); |
64bac8df AM |
364 | |
365 | if (pathspec.nr) | |
246cac85 | 366 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
64bac8df AM |
367 | |
368 | parse_pathspec_file(&pathspec, 0, | |
369 | PATHSPEC_PREFER_FULL, | |
370 | prefix, pathspec_from_file, pathspec_file_nul); | |
371 | } else if (pathspec_file_nul) { | |
6fa00ee8 | 372 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
64bac8df AM |
373 | } |
374 | ||
d850b7a5 ÆAB |
375 | unborn = !strcmp(rev, "HEAD") && repo_get_oid(the_repository, "HEAD", |
376 | &oid); | |
166ec2e9 MZ |
377 | if (unborn) { |
378 | /* reset on unborn branch: treat as reset to empty tree */ | |
d8448522 | 379 | oidcpy(&oid, the_hash_algo->empty_tree); |
0a8e3036 | 380 | } else if (!pathspec.nr && !patch_mode) { |
2f328c3d | 381 | struct commit *commit; |
d850b7a5 | 382 | if (repo_get_oid_committish(the_repository, rev, &oid)) |
2f328c3d | 383 | die(_("Failed to resolve '%s' as a valid revision."), rev); |
2122f675 | 384 | commit = lookup_commit_reference(the_repository, &oid); |
2f328c3d MZ |
385 | if (!commit) |
386 | die(_("Could not parse object '%s'."), rev); | |
f2fd0760 | 387 | oidcpy(&oid, &commit->object.oid); |
2f328c3d MZ |
388 | } else { |
389 | struct tree *tree; | |
d850b7a5 | 390 | if (repo_get_oid_treeish(the_repository, rev, &oid)) |
2f328c3d | 391 | die(_("Failed to resolve '%s' as a valid tree."), rev); |
a9dbc179 | 392 | tree = parse_tree_indirect(&oid); |
2f328c3d MZ |
393 | if (!tree) |
394 | die(_("Could not parse object '%s'."), rev); | |
f2fd0760 | 395 | oidcpy(&oid, &tree->object.oid); |
2f328c3d | 396 | } |
0e5a7faa | 397 | |
d002ef4d TR |
398 | if (patch_mode) { |
399 | if (reset_type != NONE) | |
12909b6b | 400 | die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}"); |
c18b6c1a | 401 | trace2_cmd_mode("patch-interactive"); |
72972ea0 | 402 | update_ref_status = !!run_add_p(the_repository, ADD_P_RESET, rev, |
d21878f0 | 403 | &pathspec); |
7615cf94 | 404 | goto cleanup; |
d002ef4d TR |
405 | } |
406 | ||
0e5a7faa CR |
407 | /* git reset tree [--] paths... can be used to |
408 | * load chosen paths from the tree into the index without | |
409 | * affecting the working tree nor HEAD. */ | |
f8144c9f | 410 | if (pathspec.nr) { |
0e5a7faa | 411 | if (reset_type == MIXED) |
b50a64e8 | 412 | warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead.")); |
0e5a7faa | 413 | else if (reset_type != NONE) |
8b2a57b6 ÆAB |
414 | die(_("Cannot do %s reset with paths."), |
415 | _(reset_type_names[reset_type])); | |
0e5a7faa CR |
416 | } |
417 | if (reset_type == NONE) | |
418 | reset_type = MIXED; /* by default */ | |
419 | ||
c18b6c1a JH |
420 | if (pathspec.nr) |
421 | trace2_cmd_mode("path"); | |
422 | else | |
423 | trace2_cmd_mode(reset_type_names[reset_type]); | |
424 | ||
b7756d41 | 425 | if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree())) |
cd0f0f68 | 426 | setup_work_tree(); |
49b9362f | 427 | |
2b06b0a0 | 428 | if (reset_type == MIXED && is_bare_repository()) |
8b2a57b6 ÆAB |
429 | die(_("%s reset is not allowed in a bare repository"), |
430 | _(reset_type_names[reset_type])); | |
2b06b0a0 | 431 | |
b4b313f9 | 432 | if (intent_to_add && reset_type != MIXED) |
6fa00ee8 | 433 | die(_("the option '%s' requires '%s'"), "-N", "--mixed"); |
b4b313f9 | 434 | |
c01b1cbd VD |
435 | prepare_repo_settings(the_repository); |
436 | the_repository->settings.command_requires_full_index = 0; | |
437 | ||
07047d68 | 438 | if (repo_read_index(the_repository) < 0) |
c01b1cbd VD |
439 | die(_("index file corrupt")); |
440 | ||
0e5a7faa CR |
441 | /* Soft reset does not touch the index file nor the working tree |
442 | * at all, but requires them in a good order. Other resets reset | |
443 | * the index file to the tree object we are switching to. */ | |
352f58a5 | 444 | if (reset_type == SOFT || reset_type == KEEP) |
812d2a3d | 445 | die_if_unmerged_cache(reset_type); |
352f58a5 MZ |
446 | |
447 | if (reset_type != SOFT) { | |
bfffb48c | 448 | struct lock_file lock = LOCK_INIT; |
07047d68 ÆAB |
449 | repo_hold_locked_index(the_repository, &lock, |
450 | LOCK_DIE_ON_ERROR); | |
3fde386a | 451 | if (reset_type == MIXED) { |
f38798f4 | 452 | int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN; |
7615cf94 ÆAB |
453 | if (read_from_tree(&pathspec, &oid, intent_to_add)) { |
454 | update_ref_status = 1; | |
455 | goto cleanup; | |
456 | } | |
1956ecd0 | 457 | the_index.updated_skipworktree = 1; |
5891c76c | 458 | if (!no_refresh && get_git_work_tree()) { |
649bf3a4 BP |
459 | uint64_t t_begin, t_delta_in_ms; |
460 | ||
461 | t_begin = getnanotime(); | |
b7756d41 NTND |
462 | refresh_index(&the_index, flags, NULL, NULL, |
463 | _("Unstaged changes after reset:")); | |
649bf3a4 | 464 | t_delta_in_ms = (getnanotime() - t_begin) / 1000000; |
d492abb0 | 465 | if (!quiet && advice_enabled(ADVICE_RESET_NO_REFRESH_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) { |
9396251b | 466 | advise(_("It took %.2f seconds to refresh the index after reset. You can use\n" |
7cff6765 | 467 | "'--no-refresh' to avoid this."), t_delta_in_ms / 1000.0); |
649bf3a4 BP |
468 | } |
469 | } | |
3bbf2f20 | 470 | } else { |
4cf76f6b | 471 | struct object_id dummy; |
472 | char *ref = NULL; | |
473 | int err; | |
474 | ||
12cb1c10 ÆAB |
475 | repo_dwim_ref(the_repository, rev, strlen(rev), |
476 | &dummy, &ref, 0); | |
4cf76f6b | 477 | if (ref && !starts_with(ref, "refs/")) |
e901de68 | 478 | FREE_AND_NULL(ref); |
4cf76f6b | 479 | |
480 | err = reset_index(ref, &oid, reset_type, quiet); | |
3bbf2f20 | 481 | if (reset_type == KEEP && !err) |
4cf76f6b | 482 | err = reset_index(ref, &oid, MIXED, quiet); |
3bbf2f20 MZ |
483 | if (err) |
484 | die(_("Could not reset index file to revision '%s'."), rev); | |
4cf76f6b | 485 | free(ref); |
3bbf2f20 | 486 | } |
bc41bf42 | 487 | |
bfffb48c | 488 | if (write_locked_index(&the_index, &lock, COMMIT_LOCK)) |
1ca38f85 | 489 | die(_("Could not write new index file.")); |
0e5a7faa | 490 | } |
0e5a7faa | 491 | |
f8144c9f | 492 | if (!pathspec.nr && !unborn) { |
3bbf2f20 MZ |
493 | /* Any resets without paths update HEAD to the head being |
494 | * switched to, saving the previous head in ORIG_HEAD before. */ | |
3a5d7c55 | 495 | update_ref_status = reset_refs(rev, &oid); |
0e5a7faa | 496 | |
3bbf2f20 | 497 | if (reset_type == HARD && !update_ref_status && !quiet) |
2122f675 | 498 | print_new_head_line(lookup_commit_reference(the_repository, &oid)); |
3bbf2f20 | 499 | } |
f8144c9f | 500 | if (!pathspec.nr) |
f4a4b9ac | 501 | remove_branch_state(the_repository, 0); |
0e5a7faa | 502 | |
ab2cf371 ÆAB |
503 | discard_index(&the_index); |
504 | ||
7615cf94 ÆAB |
505 | cleanup: |
506 | clear_pathspec(&pathspec); | |
7ce4088a | 507 | free(pathspec_from_file); |
0e5a7faa CR |
508 | return update_ref_status; |
509 | } |