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
15 #include "run-command.h"
21 #include "parse-options.h"
22 #include "unpack-trees.h"
23 #include "cache-tree.h"
24 #include "submodule.h"
25 #include "submodule-config.h"
27 static const char * const git_reset_usage
[] = {
28 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
29 N_("git reset [-q] [<tree-ish>] [--] <paths>..."),
30 N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
34 enum reset_type
{ MIXED
, SOFT
, HARD
, MERGE
, KEEP
, NONE
};
35 static const char *reset_type_names
[] = {
36 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
39 static inline int is_merge(void)
41 return !access(git_path_merge_head(), F_OK
);
44 static int reset_index(const struct object_id
*oid
, int reset_type
, int quiet
)
47 struct tree_desc desc
[2];
49 struct unpack_trees_options opts
;
51 memset(&opts
, 0, sizeof(opts
));
53 opts
.src_index
= &the_index
;
54 opts
.dst_index
= &the_index
;
55 opts
.fn
= oneway_merge
;
58 opts
.verbose_update
= 1;
71 read_cache_unmerged();
73 if (reset_type
== KEEP
) {
74 struct object_id head_oid
;
75 if (get_oid("HEAD", &head_oid
))
76 return error(_("You do not have a valid HEAD."));
77 if (!fill_tree_descriptor(desc
, head_oid
.hash
))
78 return error(_("Failed to find tree of HEAD."));
80 opts
.fn
= twoway_merge
;
83 if (!fill_tree_descriptor(desc
+ nr
- 1, oid
->hash
))
84 return error(_("Failed to find tree of %s."), oid_to_hex(oid
));
85 if (unpack_trees(nr
, desc
, &opts
))
88 if (reset_type
== MIXED
|| reset_type
== HARD
) {
89 tree
= parse_tree_indirect(oid
->hash
);
90 prime_cache_tree(&the_index
, tree
);
96 static void print_new_head_line(struct commit
*commit
)
98 const char *hex
, *body
;
101 hex
= find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
102 printf(_("HEAD is now at %s"), hex
);
103 msg
= logmsg_reencode(commit
, NULL
, get_log_output_encoding());
104 body
= strstr(msg
, "\n\n");
108 body
= skip_blank_lines(body
+ 2);
109 eol
= strchr(body
, '\n');
110 len
= eol
? eol
- body
: strlen(body
);
111 printf(" %.*s\n", (int) len
, body
);
115 unuse_commit_buffer(commit
, msg
);
118 static void update_index_from_diff(struct diff_queue_struct
*q
,
119 struct diff_options
*opt
, void *data
)
122 int intent_to_add
= *(int *)data
;
124 for (i
= 0; i
< q
->nr
; i
++) {
125 struct diff_filespec
*one
= q
->queue
[i
]->one
;
126 int is_missing
= !(one
->mode
&& !is_null_oid(&one
->oid
));
127 struct cache_entry
*ce
;
129 if (is_missing
&& !intent_to_add
) {
130 remove_file_from_cache(one
->path
);
134 ce
= make_cache_entry(one
->mode
, one
->oid
.hash
, one
->path
,
137 die(_("make_cache_entry failed for path '%s'"),
140 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
141 set_object_name_for_intent_to_add_entry(ce
);
143 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
147 static int read_from_tree(const struct pathspec
*pathspec
,
148 struct object_id
*tree_oid
,
151 struct diff_options opt
;
153 memset(&opt
, 0, sizeof(opt
));
154 copy_pathspec(&opt
.pathspec
, pathspec
);
155 opt
.output_format
= DIFF_FORMAT_CALLBACK
;
156 opt
.format_callback
= update_index_from_diff
;
157 opt
.format_callback_data
= &intent_to_add
;
159 if (do_diff_cache(tree_oid
->hash
, &opt
))
163 clear_pathspec(&opt
.pathspec
);
168 static void set_reflog_message(struct strbuf
*sb
, const char *action
,
171 const char *rla
= getenv("GIT_REFLOG_ACTION");
175 strbuf_addf(sb
, "%s: %s", rla
, action
);
177 strbuf_addf(sb
, "reset: moving to %s", rev
);
179 strbuf_addf(sb
, "reset: %s", action
);
182 static void die_if_unmerged_cache(int reset_type
)
184 if (is_merge() || unmerged_cache())
185 die(_("Cannot do a %s reset in the middle of a merge."),
186 _(reset_type_names
[reset_type
]));
190 static void parse_args(struct pathspec
*pathspec
,
191 const char **argv
, const char *prefix
,
193 const char **rev_ret
)
195 const char *rev
= "HEAD";
196 struct object_id unused
;
198 * Possible arguments are:
200 * git reset [-opts] [<rev>]
201 * git reset [-opts] <tree> [<paths>...]
202 * git reset [-opts] <tree> -- [<paths>...]
203 * git reset [-opts] -- [<paths>...]
204 * git reset [-opts] <paths>...
206 * At this point, argv points immediately after [-opts].
210 if (!strcmp(argv
[0], "--")) {
211 argv
++; /* reset to HEAD, possibly with paths */
212 } else if (argv
[1] && !strcmp(argv
[1], "--")) {
217 * Otherwise, argv[0] could be either <rev> or <paths> and
218 * has to be unambiguous. If there is a single argument, it
221 else if ((!argv
[1] && !get_sha1_committish(argv
[0], unused
.hash
)) ||
222 (argv
[1] && !get_sha1_treeish(argv
[0], unused
.hash
))) {
224 * Ok, argv[0] looks like a commit/tree; it should not
227 verify_non_filename(prefix
, argv
[0]);
230 /* Otherwise we treat this as a filename */
231 verify_filename(prefix
, argv
[0], 1);
236 if (read_cache() < 0)
237 die(_("index file corrupt"));
239 parse_pathspec(pathspec
, 0,
240 PATHSPEC_PREFER_FULL
|
241 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
|
242 (patch_mode
? PATHSPEC_PREFIX_ORIGIN
: 0),
246 static int reset_refs(const char *rev
, const struct object_id
*oid
)
248 int update_ref_status
;
249 struct strbuf msg
= STRBUF_INIT
;
250 struct object_id
*orig
= NULL
, oid_orig
,
251 *old_orig
= NULL
, oid_old_orig
;
253 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
254 old_orig
= &oid_old_orig
;
255 if (!get_oid("HEAD", &oid_orig
)) {
257 set_reflog_message(&msg
, "updating ORIG_HEAD", NULL
);
258 update_ref_oid(msg
.buf
, "ORIG_HEAD", orig
, old_orig
, 0,
259 UPDATE_REFS_MSG_ON_ERR
);
261 delete_ref(NULL
, "ORIG_HEAD", old_orig
->hash
, 0);
262 set_reflog_message(&msg
, "updating HEAD", rev
);
263 update_ref_status
= update_ref_oid(msg
.buf
, "HEAD", oid
, orig
, 0,
264 UPDATE_REFS_MSG_ON_ERR
);
265 strbuf_release(&msg
);
266 return update_ref_status
;
269 int cmd_reset(int argc
, const char **argv
, const char *prefix
)
271 int reset_type
= NONE
, update_ref_status
= 0, quiet
= 0;
272 int patch_mode
= 0, unborn
;
274 struct object_id oid
;
275 struct pathspec pathspec
;
276 int intent_to_add
= 0;
277 const struct option options
[] = {
278 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
279 OPT_SET_INT(0, "mixed", &reset_type
,
280 N_("reset HEAD and index"), MIXED
),
281 OPT_SET_INT(0, "soft", &reset_type
, N_("reset only HEAD"), SOFT
),
282 OPT_SET_INT(0, "hard", &reset_type
,
283 N_("reset HEAD, index and working tree"), HARD
),
284 OPT_SET_INT(0, "merge", &reset_type
,
285 N_("reset HEAD, index and working tree"), MERGE
),
286 OPT_SET_INT(0, "keep", &reset_type
,
287 N_("reset HEAD but keep local changes"), KEEP
),
288 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
,
289 "reset", "control recursive updating of submodules",
290 PARSE_OPT_OPTARG
, option_parse_recurse_submodules_worktree_updater
},
291 OPT_BOOL('p', "patch", &patch_mode
, N_("select hunks interactively")),
292 OPT_BOOL('N', "intent-to-add", &intent_to_add
,
293 N_("record only the fact that removed paths will be added later")),
297 git_config(git_default_config
, NULL
);
299 argc
= parse_options(argc
, argv
, prefix
, options
, git_reset_usage
,
300 PARSE_OPT_KEEP_DASHDASH
);
301 parse_args(&pathspec
, argv
, prefix
, patch_mode
, &rev
);
303 load_submodule_cache();
305 unborn
= !strcmp(rev
, "HEAD") && get_sha1("HEAD", oid
.hash
);
307 /* reset on unborn branch: treat as reset to empty tree */
308 hashcpy(oid
.hash
, EMPTY_TREE_SHA1_BIN
);
309 } else if (!pathspec
.nr
) {
310 struct commit
*commit
;
311 if (get_sha1_committish(rev
, oid
.hash
))
312 die(_("Failed to resolve '%s' as a valid revision."), rev
);
313 commit
= lookup_commit_reference(oid
.hash
);
315 die(_("Could not parse object '%s'."), rev
);
316 oidcpy(&oid
, &commit
->object
.oid
);
319 if (get_sha1_treeish(rev
, oid
.hash
))
320 die(_("Failed to resolve '%s' as a valid tree."), rev
);
321 tree
= parse_tree_indirect(oid
.hash
);
323 die(_("Could not parse object '%s'."), rev
);
324 oidcpy(&oid
, &tree
->object
.oid
);
328 if (reset_type
!= NONE
)
329 die(_("--patch is incompatible with --{hard,mixed,soft}"));
330 return run_add_interactive(rev
, "--patch=reset", &pathspec
);
333 /* git reset tree [--] paths... can be used to
334 * load chosen paths from the tree into the index without
335 * affecting the working tree nor HEAD. */
337 if (reset_type
== MIXED
)
338 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
339 else if (reset_type
!= NONE
)
340 die(_("Cannot do %s reset with paths."),
341 _(reset_type_names
[reset_type
]));
343 if (reset_type
== NONE
)
344 reset_type
= MIXED
; /* by default */
346 if (reset_type
!= SOFT
&& (reset_type
!= MIXED
|| get_git_work_tree()))
349 if (reset_type
== MIXED
&& is_bare_repository())
350 die(_("%s reset is not allowed in a bare repository"),
351 _(reset_type_names
[reset_type
]));
353 if (intent_to_add
&& reset_type
!= MIXED
)
354 die(_("-N can only be used with --mixed"));
356 /* Soft reset does not touch the index file nor the working tree
357 * at all, but requires them in a good order. Other resets reset
358 * the index file to the tree object we are switching to. */
359 if (reset_type
== SOFT
|| reset_type
== KEEP
)
360 die_if_unmerged_cache(reset_type
);
362 if (reset_type
!= SOFT
) {
363 struct lock_file
*lock
= xcalloc(1, sizeof(*lock
));
364 hold_locked_index(lock
, LOCK_DIE_ON_ERROR
);
365 if (reset_type
== MIXED
) {
366 int flags
= quiet
? REFRESH_QUIET
: REFRESH_IN_PORCELAIN
;
367 if (read_from_tree(&pathspec
, &oid
, intent_to_add
))
369 if (get_git_work_tree())
370 refresh_index(&the_index
, flags
, NULL
, NULL
,
371 _("Unstaged changes after reset:"));
373 int err
= reset_index(&oid
, reset_type
, quiet
);
374 if (reset_type
== KEEP
&& !err
)
375 err
= reset_index(&oid
, MIXED
, quiet
);
377 die(_("Could not reset index file to revision '%s'."), rev
);
380 if (write_locked_index(&the_index
, lock
, COMMIT_LOCK
))
381 die(_("Could not write new index file."));
384 if (!pathspec
.nr
&& !unborn
) {
385 /* Any resets without paths update HEAD to the head being
386 * switched to, saving the previous head in ORIG_HEAD before. */
387 update_ref_status
= reset_refs(rev
, &oid
);
389 if (reset_type
== HARD
&& !update_ref_status
&& !quiet
)
390 print_new_head_line(lookup_commit_reference(oid
.hash
));
393 remove_branch_state();
395 return update_ref_status
;