2 * "git reset" builtin command
4 * Copyright (c) 2007 Carlos Rica
6 * Based on git-reset.sh, which is
8 * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
10 #define USE_THE_INDEX_VARIABLE
14 #include "environment.h"
22 #include "run-command.h"
28 #include "object-name.h"
29 #include "parse-options.h"
31 #include "unpack-trees.h"
32 #include "cache-tree.h"
34 #include "sparse-index.h"
35 #include "submodule.h"
36 #include "submodule-config.h"
40 #include "add-interactive.h"
42 #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
44 static const char * const git_reset_usage
[] = {
45 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
46 N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
47 N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
48 N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
52 enum reset_type
{ MIXED
, SOFT
, HARD
, MERGE
, KEEP
, NONE
};
53 static const char *reset_type_names
[] = {
54 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
57 static inline int is_merge(void)
59 return !access(git_path_merge_head(the_repository
), F_OK
);
62 static int reset_index(const char *ref
, const struct object_id
*oid
, int reset_type
, int quiet
)
65 struct tree_desc desc
[2];
67 struct unpack_trees_options opts
;
70 memset(&opts
, 0, sizeof(opts
));
72 opts
.src_index
= &the_index
;
73 opts
.dst_index
= &the_index
;
74 opts
.fn
= oneway_merge
;
76 init_checkout_metadata(&opts
.meta
, ref
, oid
, NULL
);
78 opts
.verbose_update
= 1;
83 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
87 opts
.reset
= UNPACK_RESET_OVERWRITE_UNTRACKED
;
88 opts
.skip_cache_tree_update
= 1;
91 opts
.reset
= UNPACK_RESET_PROTECT_UNTRACKED
;
92 opts
.skip_cache_tree_update
= 1;
93 /* but opts.update=0, so working tree not updated */
96 BUG("invalid reset_type passed to reset_index");
99 repo_read_index_unmerged(the_repository
);
101 if (reset_type
== KEEP
) {
102 struct object_id head_oid
;
103 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
104 return error(_("You do not have a valid HEAD."));
105 if (!fill_tree_descriptor(the_repository
, desc
+ nr
, &head_oid
))
106 return error(_("Failed to find tree of HEAD."));
108 opts
.fn
= twoway_merge
;
111 if (!fill_tree_descriptor(the_repository
, desc
+ nr
, oid
)) {
112 error(_("Failed to find tree of %s."), oid_to_hex(oid
));
117 if (unpack_trees(nr
, desc
, &opts
))
120 if (reset_type
== MIXED
|| reset_type
== HARD
) {
121 tree
= parse_tree_indirect(oid
);
122 prime_cache_tree(the_repository
, the_repository
->index
, tree
);
128 for (i
= 0; i
< nr
; i
++)
129 free((void *)desc
[i
].buffer
);
133 static void print_new_head_line(struct commit
*commit
)
135 struct strbuf buf
= STRBUF_INIT
;
137 printf(_("HEAD is now at %s"),
138 repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, DEFAULT_ABBREV
));
140 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &buf
);
142 printf(" %s", buf
.buf
);
144 strbuf_release(&buf
);
147 static void update_index_from_diff(struct diff_queue_struct
*q
,
148 struct diff_options
*opt UNUSED
,
152 int intent_to_add
= *(int *)data
;
154 for (i
= 0; i
< q
->nr
; i
++) {
156 struct diff_filespec
*one
= q
->queue
[i
]->one
;
157 int is_in_reset_tree
= one
->mode
&& !is_null_oid(&one
->oid
);
158 struct cache_entry
*ce
;
160 if (!is_in_reset_tree
&& !intent_to_add
) {
161 remove_file_from_index(&the_index
, one
->path
);
165 ce
= make_cache_entry(&the_index
, one
->mode
, &one
->oid
, one
->path
,
169 * If the file 1) corresponds to an existing index entry with
170 * skip-worktree set, or 2) does not exist in the index but is
171 * outside the sparse checkout definition, add a skip-worktree bit
172 * to the new index entry. Note that a sparse index will be expanded
173 * if this entry is outside the sparse cone - this is necessary
174 * to properly construct the reset sparse directory.
176 pos
= index_name_pos(&the_index
, one
->path
, strlen(one
->path
));
177 if ((pos
>= 0 && ce_skip_worktree(the_index
.cache
[pos
])) ||
178 (pos
< 0 && !path_in_sparse_checkout(one
->path
, &the_index
)))
179 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
182 die(_("make_cache_entry failed for path '%s'"),
184 if (!is_in_reset_tree
) {
185 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
186 set_object_name_for_intent_to_add_entry(ce
);
188 add_index_entry(&the_index
, ce
,
189 ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
193 static int read_from_tree(const struct pathspec
*pathspec
,
194 struct object_id
*tree_oid
,
197 struct diff_options opt
;
199 memset(&opt
, 0, sizeof(opt
));
200 copy_pathspec(&opt
.pathspec
, pathspec
);
201 opt
.output_format
= DIFF_FORMAT_CALLBACK
;
202 opt
.format_callback
= update_index_from_diff
;
203 opt
.format_callback_data
= &intent_to_add
;
204 opt
.flags
.override_submodule_config
= 1;
205 opt
.flags
.recursive
= 1;
206 opt
.repo
= the_repository
;
207 opt
.change
= diff_change
;
208 opt
.add_remove
= diff_addremove
;
210 if (pathspec
->nr
&& pathspec_needs_expanded_index(&the_index
, pathspec
))
211 ensure_full_index(&the_index
);
213 if (do_diff_cache(tree_oid
, &opt
))
221 static void set_reflog_message(struct strbuf
*sb
, const char *action
,
224 const char *rla
= getenv("GIT_REFLOG_ACTION");
228 strbuf_addf(sb
, "%s: %s", rla
, action
);
230 strbuf_addf(sb
, "reset: moving to %s", rev
);
232 strbuf_addf(sb
, "reset: %s", action
);
235 static void die_if_unmerged_cache(int reset_type
)
237 if (is_merge() || unmerged_index(&the_index
))
238 die(_("Cannot do a %s reset in the middle of a merge."),
239 _(reset_type_names
[reset_type
]));
243 static void parse_args(struct pathspec
*pathspec
,
244 const char **argv
, const char *prefix
,
246 const char **rev_ret
)
248 const char *rev
= "HEAD";
249 struct object_id unused
;
251 * Possible arguments are:
253 * git reset [-opts] [<rev>]
254 * git reset [-opts] <tree> [<paths>...]
255 * git reset [-opts] <tree> -- [<paths>...]
256 * git reset [-opts] -- [<paths>...]
257 * git reset [-opts] <paths>...
259 * At this point, argv points immediately after [-opts].
263 if (!strcmp(argv
[0], "--")) {
264 argv
++; /* reset to HEAD, possibly with paths */
265 } else if (argv
[1] && !strcmp(argv
[1], "--")) {
270 * Otherwise, argv[0] could be either <rev> or <paths> and
271 * has to be unambiguous. If there is a single argument, it
274 else if ((!argv
[1] && !repo_get_oid_committish(the_repository
, argv
[0], &unused
)) ||
275 (argv
[1] && !repo_get_oid_treeish(the_repository
, argv
[0], &unused
))) {
277 * Ok, argv[0] looks like a commit/tree; it should not
280 verify_non_filename(prefix
, argv
[0]);
283 /* Otherwise we treat this as a filename */
284 verify_filename(prefix
, argv
[0], 1);
289 parse_pathspec(pathspec
, 0,
290 PATHSPEC_PREFER_FULL
|
291 (patch_mode
? PATHSPEC_PREFIX_ORIGIN
: 0),
295 static int reset_refs(const char *rev
, const struct object_id
*oid
)
297 int update_ref_status
;
298 struct strbuf msg
= STRBUF_INIT
;
299 struct object_id
*orig
= NULL
, oid_orig
,
300 *old_orig
= NULL
, oid_old_orig
;
302 if (!repo_get_oid(the_repository
, "ORIG_HEAD", &oid_old_orig
))
303 old_orig
= &oid_old_orig
;
304 if (!repo_get_oid(the_repository
, "HEAD", &oid_orig
)) {
306 set_reflog_message(&msg
, "updating ORIG_HEAD", NULL
);
307 update_ref(msg
.buf
, "ORIG_HEAD", orig
, old_orig
, 0,
308 UPDATE_REFS_MSG_ON_ERR
);
310 delete_ref(NULL
, "ORIG_HEAD", old_orig
, 0);
311 set_reflog_message(&msg
, "updating HEAD", rev
);
312 update_ref_status
= update_ref(msg
.buf
, "HEAD", oid
, orig
, 0,
313 UPDATE_REFS_MSG_ON_ERR
);
314 strbuf_release(&msg
);
315 return update_ref_status
;
318 static int git_reset_config(const char *var
, const char *value
,
319 const struct config_context
*ctx
, void *cb
)
321 if (!strcmp(var
, "submodule.recurse"))
322 return git_default_submodule_config(var
, value
, cb
);
324 return git_default_config(var
, value
, ctx
, cb
);
327 int cmd_reset(int argc
, const char **argv
, const char *prefix
)
329 int reset_type
= NONE
, update_ref_status
= 0, quiet
= 0;
331 int patch_mode
= 0, pathspec_file_nul
= 0, unborn
;
333 char *pathspec_from_file
= NULL
;
334 struct object_id oid
;
335 struct pathspec pathspec
;
336 int intent_to_add
= 0;
337 const struct option options
[] = {
338 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
339 OPT_BOOL(0, "no-refresh", &no_refresh
,
340 N_("skip refreshing the index after reset")),
341 OPT_SET_INT_F(0, "mixed", &reset_type
,
342 N_("reset HEAD and index"),
343 MIXED
, PARSE_OPT_NONEG
),
344 OPT_SET_INT_F(0, "soft", &reset_type
,
345 N_("reset only HEAD"),
346 SOFT
, PARSE_OPT_NONEG
),
347 OPT_SET_INT_F(0, "hard", &reset_type
,
348 N_("reset HEAD, index and working tree"),
349 HARD
, PARSE_OPT_NONEG
),
350 OPT_SET_INT_F(0, "merge", &reset_type
,
351 N_("reset HEAD, index and working tree"),
352 MERGE
, PARSE_OPT_NONEG
),
353 OPT_SET_INT_F(0, "keep", &reset_type
,
354 N_("reset HEAD but keep local changes"),
355 KEEP
, PARSE_OPT_NONEG
),
356 OPT_CALLBACK_F(0, "recurse-submodules", NULL
,
357 "reset", "control recursive updating of submodules",
359 option_parse_recurse_submodules_worktree_updater
),
360 OPT_BOOL('p', "patch", &patch_mode
, N_("select hunks interactively")),
361 OPT_BOOL('N', "intent-to-add", &intent_to_add
,
362 N_("record only the fact that removed paths will be added later")),
363 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
364 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
368 git_config(git_reset_config
, NULL
);
370 argc
= parse_options(argc
, argv
, prefix
, options
, git_reset_usage
,
371 PARSE_OPT_KEEP_DASHDASH
);
372 parse_args(&pathspec
, argv
, prefix
, patch_mode
, &rev
);
374 if (pathspec_from_file
) {
376 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
379 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
381 parse_pathspec_file(&pathspec
, 0,
382 PATHSPEC_PREFER_FULL
,
383 prefix
, pathspec_from_file
, pathspec_file_nul
);
384 } else if (pathspec_file_nul
) {
385 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
388 unborn
= !strcmp(rev
, "HEAD") && repo_get_oid(the_repository
, "HEAD",
391 /* reset on unborn branch: treat as reset to empty tree */
392 oidcpy(&oid
, the_hash_algo
->empty_tree
);
393 } else if (!pathspec
.nr
&& !patch_mode
) {
394 struct commit
*commit
;
395 if (repo_get_oid_committish(the_repository
, rev
, &oid
))
396 die(_("Failed to resolve '%s' as a valid revision."), rev
);
397 commit
= lookup_commit_reference(the_repository
, &oid
);
399 die(_("Could not parse object '%s'."), rev
);
400 oidcpy(&oid
, &commit
->object
.oid
);
403 if (repo_get_oid_treeish(the_repository
, rev
, &oid
))
404 die(_("Failed to resolve '%s' as a valid tree."), rev
);
405 tree
= parse_tree_indirect(&oid
);
407 die(_("Could not parse object '%s'."), rev
);
408 oidcpy(&oid
, &tree
->object
.oid
);
412 if (reset_type
!= NONE
)
413 die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
414 trace2_cmd_mode("patch-interactive");
415 update_ref_status
= !!run_add_p(the_repository
, ADD_P_RESET
, rev
,
420 /* git reset tree [--] paths... can be used to
421 * load chosen paths from the tree into the index without
422 * affecting the working tree nor HEAD. */
424 if (reset_type
== MIXED
)
425 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
426 else if (reset_type
!= NONE
)
427 die(_("Cannot do %s reset with paths."),
428 _(reset_type_names
[reset_type
]));
430 if (reset_type
== NONE
)
431 reset_type
= MIXED
; /* by default */
434 trace2_cmd_mode("path");
436 trace2_cmd_mode(reset_type_names
[reset_type
]);
438 if (reset_type
!= SOFT
&& (reset_type
!= MIXED
|| get_git_work_tree()))
441 if (reset_type
== MIXED
&& is_bare_repository())
442 die(_("%s reset is not allowed in a bare repository"),
443 _(reset_type_names
[reset_type
]));
445 if (intent_to_add
&& reset_type
!= MIXED
)
446 die(_("the option '%s' requires '%s'"), "-N", "--mixed");
448 prepare_repo_settings(the_repository
);
449 the_repository
->settings
.command_requires_full_index
= 0;
451 if (repo_read_index(the_repository
) < 0)
452 die(_("index file corrupt"));
454 /* Soft reset does not touch the index file nor the working tree
455 * at all, but requires them in a good order. Other resets reset
456 * the index file to the tree object we are switching to. */
457 if (reset_type
== SOFT
|| reset_type
== KEEP
)
458 die_if_unmerged_cache(reset_type
);
460 if (reset_type
!= SOFT
) {
461 struct lock_file lock
= LOCK_INIT
;
462 repo_hold_locked_index(the_repository
, &lock
,
464 if (reset_type
== MIXED
) {
465 int flags
= quiet
? REFRESH_QUIET
: REFRESH_IN_PORCELAIN
;
466 if (read_from_tree(&pathspec
, &oid
, intent_to_add
)) {
467 update_ref_status
= 1;
470 the_index
.updated_skipworktree
= 1;
471 if (!no_refresh
&& get_git_work_tree()) {
472 uint64_t t_begin
, t_delta_in_ms
;
474 t_begin
= getnanotime();
475 refresh_index(&the_index
, flags
, NULL
, NULL
,
476 _("Unstaged changes after reset:"));
477 t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
478 if (!quiet
&& advice_enabled(ADVICE_RESET_NO_REFRESH_WARNING
) && t_delta_in_ms
> REFRESH_INDEX_DELAY_WARNING_IN_MS
) {
479 advise(_("It took %.2f seconds to refresh the index after reset. You can use\n"
480 "'--no-refresh' to avoid this."), t_delta_in_ms
/ 1000.0);
484 struct object_id dummy
;
488 repo_dwim_ref(the_repository
, rev
, strlen(rev
),
490 if (ref
&& !starts_with(ref
, "refs/"))
493 err
= reset_index(ref
, &oid
, reset_type
, quiet
);
494 if (reset_type
== KEEP
&& !err
)
495 err
= reset_index(ref
, &oid
, MIXED
, quiet
);
497 die(_("Could not reset index file to revision '%s'."), rev
);
501 if (write_locked_index(&the_index
, &lock
, COMMIT_LOCK
))
502 die(_("Could not write new index file."));
505 if (!pathspec
.nr
&& !unborn
) {
506 /* Any resets without paths update HEAD to the head being
507 * switched to, saving the previous head in ORIG_HEAD before. */
508 update_ref_status
= reset_refs(rev
, &oid
);
510 if (reset_type
== HARD
&& !update_ref_status
&& !quiet
)
511 print_new_head_line(lookup_commit_reference(the_repository
, &oid
));
514 remove_branch_state(the_repository
, 0);
516 discard_index(&the_index
);
519 clear_pathspec(&pathspec
);
520 free(pathspec_from_file
);
521 return update_ref_status
;