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