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