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
16 #include "run-command.h"
22 #include "parse-options.h"
23 #include "unpack-trees.h"
24 #include "cache-tree.h"
25 #include "submodule.h"
26 #include "submodule-config.h"
28 static const char * const git_reset_usage
[] = {
29 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
30 N_("git reset [-q] [<tree-ish>] [--] <paths>..."),
31 N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
35 enum reset_type
{ MIXED
, SOFT
, HARD
, MERGE
, KEEP
, NONE
};
36 static const char *reset_type_names
[] = {
37 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
40 static inline int is_merge(void)
42 return !access(git_path_merge_head(), F_OK
);
45 static int reset_index(const struct object_id
*oid
, int reset_type
, int quiet
)
48 struct tree_desc desc
[2];
50 struct unpack_trees_options opts
;
52 memset(&opts
, 0, sizeof(opts
));
54 opts
.src_index
= &the_index
;
55 opts
.dst_index
= &the_index
;
56 opts
.fn
= oneway_merge
;
59 opts
.verbose_update
= 1;
72 read_cache_unmerged();
74 if (reset_type
== KEEP
) {
75 struct object_id head_oid
;
76 if (get_oid("HEAD", &head_oid
))
77 return error(_("You do not have a valid HEAD."));
78 if (!fill_tree_descriptor(desc
, &head_oid
))
79 return error(_("Failed to find tree of HEAD."));
81 opts
.fn
= twoway_merge
;
84 if (!fill_tree_descriptor(desc
+ nr
- 1, oid
))
85 return error(_("Failed to find tree of %s."), oid_to_hex(oid
));
86 if (unpack_trees(nr
, desc
, &opts
))
89 if (reset_type
== MIXED
|| reset_type
== HARD
) {
90 tree
= parse_tree_indirect(oid
);
91 prime_cache_tree(&the_index
, tree
);
97 static void print_new_head_line(struct commit
*commit
)
99 const char *hex
, *body
;
102 hex
= find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
103 printf(_("HEAD is now at %s"), hex
);
104 msg
= logmsg_reencode(commit
, NULL
, get_log_output_encoding());
105 body
= strstr(msg
, "\n\n");
109 body
= skip_blank_lines(body
+ 2);
110 eol
= strchr(body
, '\n');
111 len
= eol
? eol
- body
: strlen(body
);
112 printf(" %.*s\n", (int) len
, body
);
116 unuse_commit_buffer(commit
, msg
);
119 static void update_index_from_diff(struct diff_queue_struct
*q
,
120 struct diff_options
*opt
, void *data
)
123 int intent_to_add
= *(int *)data
;
125 for (i
= 0; i
< q
->nr
; i
++) {
126 struct diff_filespec
*one
= q
->queue
[i
]->one
;
127 int is_missing
= !(one
->mode
&& !is_null_oid(&one
->oid
));
128 struct cache_entry
*ce
;
130 if (is_missing
&& !intent_to_add
) {
131 remove_file_from_cache(one
->path
);
135 ce
= make_cache_entry(one
->mode
, one
->oid
.hash
, one
->path
,
138 die(_("make_cache_entry failed for path '%s'"),
141 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
142 set_object_name_for_intent_to_add_entry(ce
);
144 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
148 static int read_from_tree(const struct pathspec
*pathspec
,
149 struct object_id
*tree_oid
,
152 struct diff_options opt
;
154 memset(&opt
, 0, sizeof(opt
));
155 copy_pathspec(&opt
.pathspec
, pathspec
);
156 opt
.output_format
= DIFF_FORMAT_CALLBACK
;
157 opt
.format_callback
= update_index_from_diff
;
158 opt
.format_callback_data
= &intent_to_add
;
159 opt
.flags
|= DIFF_OPT_OVERRIDE_SUBMODULE_CONFIG
;
161 if (do_diff_cache(tree_oid
, &opt
))
165 clear_pathspec(&opt
.pathspec
);
170 static void set_reflog_message(struct strbuf
*sb
, const char *action
,
173 const char *rla
= getenv("GIT_REFLOG_ACTION");
177 strbuf_addf(sb
, "%s: %s", rla
, action
);
179 strbuf_addf(sb
, "reset: moving to %s", rev
);
181 strbuf_addf(sb
, "reset: %s", action
);
184 static void die_if_unmerged_cache(int reset_type
)
186 if (is_merge() || unmerged_cache())
187 die(_("Cannot do a %s reset in the middle of a merge."),
188 _(reset_type_names
[reset_type
]));
192 static void parse_args(struct pathspec
*pathspec
,
193 const char **argv
, const char *prefix
,
195 const char **rev_ret
)
197 const char *rev
= "HEAD";
198 struct object_id unused
;
200 * Possible arguments are:
202 * git reset [-opts] [<rev>]
203 * git reset [-opts] <tree> [<paths>...]
204 * git reset [-opts] <tree> -- [<paths>...]
205 * git reset [-opts] -- [<paths>...]
206 * git reset [-opts] <paths>...
208 * At this point, argv points immediately after [-opts].
212 if (!strcmp(argv
[0], "--")) {
213 argv
++; /* reset to HEAD, possibly with paths */
214 } else if (argv
[1] && !strcmp(argv
[1], "--")) {
219 * Otherwise, argv[0] could be either <rev> or <paths> and
220 * has to be unambiguous. If there is a single argument, it
223 else if ((!argv
[1] && !get_oid_committish(argv
[0], &unused
)) ||
224 (argv
[1] && !get_oid_treeish(argv
[0], &unused
))) {
226 * Ok, argv[0] looks like a commit/tree; it should not
229 verify_non_filename(prefix
, argv
[0]);
232 /* Otherwise we treat this as a filename */
233 verify_filename(prefix
, argv
[0], 1);
238 if (read_cache() < 0)
239 die(_("index file corrupt"));
241 parse_pathspec(pathspec
, 0,
242 PATHSPEC_PREFER_FULL
|
243 (patch_mode
? PATHSPEC_PREFIX_ORIGIN
: 0),
247 static int reset_refs(const char *rev
, const struct object_id
*oid
)
249 int update_ref_status
;
250 struct strbuf msg
= STRBUF_INIT
;
251 struct object_id
*orig
= NULL
, oid_orig
,
252 *old_orig
= NULL
, oid_old_orig
;
254 if (!get_oid("ORIG_HEAD", &oid_old_orig
))
255 old_orig
= &oid_old_orig
;
256 if (!get_oid("HEAD", &oid_orig
)) {
258 set_reflog_message(&msg
, "updating ORIG_HEAD", NULL
);
259 update_ref_oid(msg
.buf
, "ORIG_HEAD", orig
, old_orig
, 0,
260 UPDATE_REFS_MSG_ON_ERR
);
262 delete_ref(NULL
, "ORIG_HEAD", old_orig
->hash
, 0);
263 set_reflog_message(&msg
, "updating HEAD", rev
);
264 update_ref_status
= update_ref_oid(msg
.buf
, "HEAD", oid
, orig
, 0,
265 UPDATE_REFS_MSG_ON_ERR
);
266 strbuf_release(&msg
);
267 return update_ref_status
;
270 static int git_reset_config(const char *var
, const char *value
, void *cb
)
272 if (!strcmp(var
, "submodule.recurse"))
273 return git_default_submodule_config(var
, value
, cb
);
275 return git_default_config(var
, value
, cb
);
278 int cmd_reset(int argc
, const char **argv
, const char *prefix
)
280 int reset_type
= NONE
, update_ref_status
= 0, quiet
= 0;
281 int patch_mode
= 0, unborn
;
283 struct object_id oid
;
284 struct pathspec pathspec
;
285 int intent_to_add
= 0;
286 const struct option options
[] = {
287 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
288 OPT_SET_INT(0, "mixed", &reset_type
,
289 N_("reset HEAD and index"), MIXED
),
290 OPT_SET_INT(0, "soft", &reset_type
, N_("reset only HEAD"), SOFT
),
291 OPT_SET_INT(0, "hard", &reset_type
,
292 N_("reset HEAD, index and working tree"), HARD
),
293 OPT_SET_INT(0, "merge", &reset_type
,
294 N_("reset HEAD, index and working tree"), MERGE
),
295 OPT_SET_INT(0, "keep", &reset_type
,
296 N_("reset HEAD but keep local changes"), KEEP
),
297 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
,
298 "reset", "control recursive updating of submodules",
299 PARSE_OPT_OPTARG
, option_parse_recurse_submodules_worktree_updater
},
300 OPT_BOOL('p', "patch", &patch_mode
, N_("select hunks interactively")),
301 OPT_BOOL('N', "intent-to-add", &intent_to_add
,
302 N_("record only the fact that removed paths will be added later")),
306 git_config(git_reset_config
, NULL
);
308 argc
= parse_options(argc
, argv
, prefix
, options
, git_reset_usage
,
309 PARSE_OPT_KEEP_DASHDASH
);
310 parse_args(&pathspec
, argv
, prefix
, patch_mode
, &rev
);
312 unborn
= !strcmp(rev
, "HEAD") && get_oid("HEAD", &oid
);
314 /* reset on unborn branch: treat as reset to empty tree */
315 hashcpy(oid
.hash
, EMPTY_TREE_SHA1_BIN
);
316 } else if (!pathspec
.nr
) {
317 struct commit
*commit
;
318 if (get_oid_committish(rev
, &oid
))
319 die(_("Failed to resolve '%s' as a valid revision."), rev
);
320 commit
= lookup_commit_reference(&oid
);
322 die(_("Could not parse object '%s'."), rev
);
323 oidcpy(&oid
, &commit
->object
.oid
);
326 if (get_oid_treeish(rev
, &oid
))
327 die(_("Failed to resolve '%s' as a valid tree."), rev
);
328 tree
= parse_tree_indirect(&oid
);
330 die(_("Could not parse object '%s'."), rev
);
331 oidcpy(&oid
, &tree
->object
.oid
);
335 if (reset_type
!= NONE
)
336 die(_("--patch is incompatible with --{hard,mixed,soft}"));
337 return run_add_interactive(rev
, "--patch=reset", &pathspec
);
340 /* git reset tree [--] paths... can be used to
341 * load chosen paths from the tree into the index without
342 * affecting the working tree nor HEAD. */
344 if (reset_type
== MIXED
)
345 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
346 else if (reset_type
!= NONE
)
347 die(_("Cannot do %s reset with paths."),
348 _(reset_type_names
[reset_type
]));
350 if (reset_type
== NONE
)
351 reset_type
= MIXED
; /* by default */
353 if (reset_type
!= SOFT
&& (reset_type
!= MIXED
|| get_git_work_tree()))
356 if (reset_type
== MIXED
&& is_bare_repository())
357 die(_("%s reset is not allowed in a bare repository"),
358 _(reset_type_names
[reset_type
]));
360 if (intent_to_add
&& reset_type
!= MIXED
)
361 die(_("-N can only be used with --mixed"));
363 /* Soft reset does not touch the index file nor the working tree
364 * at all, but requires them in a good order. Other resets reset
365 * the index file to the tree object we are switching to. */
366 if (reset_type
== SOFT
|| reset_type
== KEEP
)
367 die_if_unmerged_cache(reset_type
);
369 if (reset_type
!= SOFT
) {
370 struct lock_file
*lock
= xcalloc(1, sizeof(*lock
));
371 hold_locked_index(lock
, LOCK_DIE_ON_ERROR
);
372 if (reset_type
== MIXED
) {
373 int flags
= quiet
? REFRESH_QUIET
: REFRESH_IN_PORCELAIN
;
374 if (read_from_tree(&pathspec
, &oid
, intent_to_add
))
376 if (get_git_work_tree())
377 refresh_index(&the_index
, flags
, NULL
, NULL
,
378 _("Unstaged changes after reset:"));
380 int err
= reset_index(&oid
, reset_type
, quiet
);
381 if (reset_type
== KEEP
&& !err
)
382 err
= reset_index(&oid
, MIXED
, quiet
);
384 die(_("Could not reset index file to revision '%s'."), rev
);
387 if (write_locked_index(&the_index
, lock
, COMMIT_LOCK
))
388 die(_("Could not write new index file."));
391 if (!pathspec
.nr
&& !unborn
) {
392 /* Any resets without paths update HEAD to the head being
393 * switched to, saving the previous head in ORIG_HEAD before. */
394 update_ref_status
= reset_refs(rev
, &oid
);
396 if (reset_type
== HARD
&& !update_ref_status
&& !quiet
)
397 print_new_head_line(lookup_commit_reference(&oid
));
400 remove_branch_state();
402 return update_ref_status
;