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