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
14 #include "run-command.h"
20 #include "parse-options.h"
21 #include "unpack-trees.h"
22 #include "cache-tree.h"
24 static const char * const git_reset_usage
[] = {
25 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
26 N_("git reset [-q] <tree-ish> [--] <paths>..."),
27 N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
31 enum reset_type
{ MIXED
, SOFT
, HARD
, MERGE
, KEEP
, NONE
};
32 static const char *reset_type_names
[] = {
33 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
36 static inline int is_merge(void)
38 return !access(git_path("MERGE_HEAD"), F_OK
);
41 static int reset_index(const unsigned char *sha1
, int reset_type
, int quiet
)
44 struct tree_desc desc
[2];
46 struct unpack_trees_options opts
;
48 memset(&opts
, 0, sizeof(opts
));
50 opts
.src_index
= &the_index
;
51 opts
.dst_index
= &the_index
;
52 opts
.fn
= oneway_merge
;
55 opts
.verbose_update
= 1;
68 read_cache_unmerged();
70 if (reset_type
== KEEP
) {
71 unsigned char head_sha1
[20];
72 if (get_sha1("HEAD", head_sha1
))
73 return error(_("You do not have a valid HEAD."));
74 if (!fill_tree_descriptor(desc
, head_sha1
))
75 return error(_("Failed to find tree of HEAD."));
77 opts
.fn
= twoway_merge
;
80 if (!fill_tree_descriptor(desc
+ nr
- 1, sha1
))
81 return error(_("Failed to find tree of %s."), sha1_to_hex(sha1
));
82 if (unpack_trees(nr
, desc
, &opts
))
85 if (reset_type
== MIXED
|| reset_type
== HARD
) {
86 tree
= parse_tree_indirect(sha1
);
87 prime_cache_tree(&active_cache_tree
, tree
);
93 static void print_new_head_line(struct commit
*commit
)
95 const char *hex
, *body
;
98 hex
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
99 printf(_("HEAD is now at %s"), hex
);
100 msg
= logmsg_reencode(commit
, NULL
, get_log_output_encoding());
101 body
= strstr(msg
, "\n\n");
106 eol
= strchr(body
, '\n');
107 len
= eol
? eol
- body
: strlen(body
);
108 printf(" %.*s\n", (int) len
, body
);
112 logmsg_free(msg
, commit
);
115 static void update_index_from_diff(struct diff_queue_struct
*q
,
116 struct diff_options
*opt
, void *data
)
119 int intent_to_add
= *(int *)data
;
121 for (i
= 0; i
< q
->nr
; i
++) {
122 struct diff_filespec
*one
= q
->queue
[i
]->one
;
123 int is_missing
= !(one
->mode
&& !is_null_sha1(one
->sha1
));
124 struct cache_entry
*ce
;
126 if (is_missing
&& !intent_to_add
) {
127 remove_file_from_cache(one
->path
);
131 ce
= make_cache_entry(one
->mode
, one
->sha1
, one
->path
,
134 die(_("make_cache_entry failed for path '%s'"),
137 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
138 set_object_name_for_intent_to_add_entry(ce
);
140 add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
);
144 static int read_from_tree(const struct pathspec
*pathspec
,
145 unsigned char *tree_sha1
,
148 struct diff_options opt
;
150 memset(&opt
, 0, sizeof(opt
));
151 copy_pathspec(&opt
.pathspec
, pathspec
);
152 opt
.output_format
= DIFF_FORMAT_CALLBACK
;
153 opt
.format_callback
= update_index_from_diff
;
154 opt
.format_callback_data
= &intent_to_add
;
156 if (do_diff_cache(tree_sha1
, &opt
))
160 free_pathspec(&opt
.pathspec
);
165 static void set_reflog_message(struct strbuf
*sb
, const char *action
,
168 const char *rla
= getenv("GIT_REFLOG_ACTION");
172 strbuf_addf(sb
, "%s: %s", rla
, action
);
174 strbuf_addf(sb
, "reset: moving to %s", rev
);
176 strbuf_addf(sb
, "reset: %s", action
);
179 static void die_if_unmerged_cache(int reset_type
)
181 if (is_merge() || unmerged_cache())
182 die(_("Cannot do a %s reset in the middle of a merge."),
183 _(reset_type_names
[reset_type
]));
187 static void parse_args(struct pathspec
*pathspec
,
188 const char **argv
, const char *prefix
,
190 const char **rev_ret
)
192 const char *rev
= "HEAD";
193 unsigned char unused
[20];
195 * Possible arguments are:
197 * git reset [-opts] [<rev>]
198 * git reset [-opts] <tree> [<paths>...]
199 * git reset [-opts] <tree> -- [<paths>...]
200 * git reset [-opts] -- [<paths>...]
201 * git reset [-opts] <paths>...
203 * At this point, argv points immediately after [-opts].
207 if (!strcmp(argv
[0], "--")) {
208 argv
++; /* reset to HEAD, possibly with paths */
209 } else if (argv
[1] && !strcmp(argv
[1], "--")) {
214 * Otherwise, argv[0] could be either <rev> or <paths> and
215 * has to be unambiguous. If there is a single argument, it
218 else if ((!argv
[1] && !get_sha1_committish(argv
[0], unused
)) ||
219 (argv
[1] && !get_sha1_treeish(argv
[0], unused
))) {
221 * Ok, argv[0] looks like a commit/tree; it should not
224 verify_non_filename(prefix
, argv
[0]);
227 /* Otherwise we treat this as a filename */
228 verify_filename(prefix
, argv
[0], 1);
233 if (read_cache() < 0)
234 die(_("index file corrupt"));
236 parse_pathspec(pathspec
, 0,
237 PATHSPEC_PREFER_FULL
|
238 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
|
239 (patch_mode
? PATHSPEC_PREFIX_ORIGIN
: 0),
243 static int reset_refs(const char *rev
, const unsigned char *sha1
)
245 int update_ref_status
;
246 struct strbuf msg
= STRBUF_INIT
;
247 unsigned char *orig
= NULL
, sha1_orig
[20],
248 *old_orig
= NULL
, sha1_old_orig
[20];
250 if (!get_sha1("ORIG_HEAD", sha1_old_orig
))
251 old_orig
= sha1_old_orig
;
252 if (!get_sha1("HEAD", sha1_orig
)) {
254 set_reflog_message(&msg
, "updating ORIG_HEAD", NULL
);
255 update_ref(msg
.buf
, "ORIG_HEAD", orig
, old_orig
, 0,
256 UPDATE_REFS_MSG_ON_ERR
);
258 delete_ref("ORIG_HEAD", old_orig
, 0);
259 set_reflog_message(&msg
, "updating HEAD", rev
);
260 update_ref_status
= update_ref(msg
.buf
, "HEAD", sha1
, orig
, 0,
261 UPDATE_REFS_MSG_ON_ERR
);
262 strbuf_release(&msg
);
263 return update_ref_status
;
266 int cmd_reset(int argc
, const char **argv
, const char *prefix
)
268 int reset_type
= NONE
, update_ref_status
= 0, quiet
= 0;
269 int patch_mode
= 0, unborn
;
271 unsigned char sha1
[20];
272 struct pathspec pathspec
;
273 int intent_to_add
= 0;
274 const struct option options
[] = {
275 OPT__QUIET(&quiet
, N_("be quiet, only report errors")),
276 OPT_SET_INT(0, "mixed", &reset_type
,
277 N_("reset HEAD and index"), MIXED
),
278 OPT_SET_INT(0, "soft", &reset_type
, N_("reset only HEAD"), SOFT
),
279 OPT_SET_INT(0, "hard", &reset_type
,
280 N_("reset HEAD, index and working tree"), HARD
),
281 OPT_SET_INT(0, "merge", &reset_type
,
282 N_("reset HEAD, index and working tree"), MERGE
),
283 OPT_SET_INT(0, "keep", &reset_type
,
284 N_("reset HEAD but keep local changes"), KEEP
),
285 OPT_BOOL('p', "patch", &patch_mode
, N_("select hunks interactively")),
286 OPT_BOOL('N', "intent-to-add", &intent_to_add
,
287 N_("record only the fact that removed paths will be added later")),
291 git_config(git_default_config
, NULL
);
293 argc
= parse_options(argc
, argv
, prefix
, options
, git_reset_usage
,
294 PARSE_OPT_KEEP_DASHDASH
);
295 parse_args(&pathspec
, argv
, prefix
, patch_mode
, &rev
);
297 unborn
= !strcmp(rev
, "HEAD") && get_sha1("HEAD", sha1
);
299 /* reset on unborn branch: treat as reset to empty tree */
300 hashcpy(sha1
, EMPTY_TREE_SHA1_BIN
);
301 } else if (!pathspec
.nr
) {
302 struct commit
*commit
;
303 if (get_sha1_committish(rev
, sha1
))
304 die(_("Failed to resolve '%s' as a valid revision."), rev
);
305 commit
= lookup_commit_reference(sha1
);
307 die(_("Could not parse object '%s'."), rev
);
308 hashcpy(sha1
, commit
->object
.sha1
);
311 if (get_sha1_treeish(rev
, sha1
))
312 die(_("Failed to resolve '%s' as a valid tree."), rev
);
313 tree
= parse_tree_indirect(sha1
);
315 die(_("Could not parse object '%s'."), rev
);
316 hashcpy(sha1
, tree
->object
.sha1
);
320 if (reset_type
!= NONE
)
321 die(_("--patch is incompatible with --{hard,mixed,soft}"));
322 return run_add_interactive(rev
, "--patch=reset", &pathspec
);
325 /* git reset tree [--] paths... can be used to
326 * load chosen paths from the tree into the index without
327 * affecting the working tree nor HEAD. */
329 if (reset_type
== MIXED
)
330 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
331 else if (reset_type
!= NONE
)
332 die(_("Cannot do %s reset with paths."),
333 _(reset_type_names
[reset_type
]));
335 if (reset_type
== NONE
)
336 reset_type
= MIXED
; /* by default */
338 if (reset_type
!= SOFT
&& (reset_type
!= MIXED
|| get_git_work_tree()))
341 if (reset_type
== MIXED
&& is_bare_repository())
342 die(_("%s reset is not allowed in a bare repository"),
343 _(reset_type_names
[reset_type
]));
345 if (intent_to_add
&& reset_type
!= MIXED
)
346 die(_("-N can only be used with --mixed"));
348 /* Soft reset does not touch the index file nor the working tree
349 * at all, but requires them in a good order. Other resets reset
350 * the index file to the tree object we are switching to. */
351 if (reset_type
== SOFT
|| reset_type
== KEEP
)
352 die_if_unmerged_cache(reset_type
);
354 if (reset_type
!= SOFT
) {
355 struct lock_file
*lock
= xcalloc(1, sizeof(*lock
));
356 int newfd
= hold_locked_index(lock
, 1);
357 if (reset_type
== MIXED
) {
358 int flags
= quiet
? REFRESH_QUIET
: REFRESH_IN_PORCELAIN
;
359 if (read_from_tree(&pathspec
, sha1
, intent_to_add
))
361 if (get_git_work_tree())
362 refresh_index(&the_index
, flags
, NULL
, NULL
,
363 _("Unstaged changes after reset:"));
365 int err
= reset_index(sha1
, reset_type
, quiet
);
366 if (reset_type
== KEEP
&& !err
)
367 err
= reset_index(sha1
, MIXED
, quiet
);
369 die(_("Could not reset index file to revision '%s'."), rev
);
372 if (write_cache(newfd
, active_cache
, active_nr
) ||
373 commit_locked_index(lock
))
374 die(_("Could not write new index file."));
377 if (!pathspec
.nr
&& !unborn
) {
378 /* Any resets without paths update HEAD to the head being
379 * switched to, saving the previous head in ORIG_HEAD before. */
380 update_ref_status
= reset_refs(rev
, sha1
);
382 if (reset_type
== HARD
&& !update_ref_status
&& !quiet
)
383 print_new_head_line(lookup_commit_reference(sha1
));
386 remove_branch_state();
388 return update_ref_status
;