Merge branch 'jk/bundle-use-dash-for-stdfiles'
[git/debian.git] / builtin / reset.c
blob24b04aeecb9222750fe9dd7dcff1e1ab97cb467f
1 /*
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
9 */
10 #define USE_THE_INDEX_VARIABLE
11 #include "builtin.h"
12 #include "config.h"
13 #include "hex.h"
14 #include "lockfile.h"
15 #include "tag.h"
16 #include "object.h"
17 #include "pretty.h"
18 #include "run-command.h"
19 #include "refs.h"
20 #include "diff.h"
21 #include "diffcore.h"
22 #include "tree.h"
23 #include "branch.h"
24 #include "parse-options.h"
25 #include "unpack-trees.h"
26 #include "cache-tree.h"
27 #include "submodule.h"
28 #include "submodule-config.h"
29 #include "dir.h"
30 #include "add-interactive.h"
32 #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
34 static const char * const git_reset_usage[] = {
35 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
36 N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
37 N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
38 N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
39 NULL
42 enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
43 static const char *reset_type_names[] = {
44 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
47 static inline int is_merge(void)
49 return !access(git_path_merge_head(the_repository), F_OK);
52 static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet)
54 int i, nr = 0;
55 struct tree_desc desc[2];
56 struct tree *tree;
57 struct unpack_trees_options opts;
58 int ret = -1;
60 memset(&opts, 0, sizeof(opts));
61 opts.head_idx = 1;
62 opts.src_index = &the_index;
63 opts.dst_index = &the_index;
64 opts.fn = oneway_merge;
65 opts.merge = 1;
66 init_checkout_metadata(&opts.meta, ref, oid, NULL);
67 if (!quiet)
68 opts.verbose_update = 1;
69 switch (reset_type) {
70 case KEEP:
71 case MERGE:
72 opts.update = 1;
73 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
74 break;
75 case HARD:
76 opts.update = 1;
77 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
78 opts.skip_cache_tree_update = 1;
79 break;
80 case MIXED:
81 opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
82 opts.skip_cache_tree_update = 1;
83 /* but opts.update=0, so working tree not updated */
84 break;
85 default:
86 BUG("invalid reset_type passed to reset_index");
89 repo_read_index_unmerged(the_repository);
91 if (reset_type == KEEP) {
92 struct object_id head_oid;
93 if (get_oid("HEAD", &head_oid))
94 return error(_("You do not have a valid HEAD."));
95 if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid))
96 return error(_("Failed to find tree of HEAD."));
97 nr++;
98 opts.fn = twoway_merge;
101 if (!fill_tree_descriptor(the_repository, desc + nr, oid)) {
102 error(_("Failed to find tree of %s."), oid_to_hex(oid));
103 goto out;
105 nr++;
107 if (unpack_trees(nr, desc, &opts))
108 goto out;
110 if (reset_type == MIXED || reset_type == HARD) {
111 tree = parse_tree_indirect(oid);
112 prime_cache_tree(the_repository, the_repository->index, tree);
115 ret = 0;
117 out:
118 for (i = 0; i < nr; i++)
119 free((void *)desc[i].buffer);
120 return ret;
123 static void print_new_head_line(struct commit *commit)
125 struct strbuf buf = STRBUF_INIT;
127 printf(_("HEAD is now at %s"),
128 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
130 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
131 if (buf.len > 0)
132 printf(" %s", buf.buf);
133 putchar('\n');
134 strbuf_release(&buf);
137 static void update_index_from_diff(struct diff_queue_struct *q,
138 struct diff_options *opt UNUSED,
139 void *data)
141 int i;
142 int intent_to_add = *(int *)data;
144 for (i = 0; i < q->nr; i++) {
145 int pos;
146 struct diff_filespec *one = q->queue[i]->one;
147 int is_in_reset_tree = one->mode && !is_null_oid(&one->oid);
148 struct cache_entry *ce;
150 if (!is_in_reset_tree && !intent_to_add) {
151 remove_file_from_index(&the_index, one->path);
152 continue;
155 ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
156 0, 0);
159 * If the file 1) corresponds to an existing index entry with
160 * skip-worktree set, or 2) does not exist in the index but is
161 * outside the sparse checkout definition, add a skip-worktree bit
162 * to the new index entry. Note that a sparse index will be expanded
163 * if this entry is outside the sparse cone - this is necessary
164 * to properly construct the reset sparse directory.
166 pos = index_name_pos(&the_index, one->path, strlen(one->path));
167 if ((pos >= 0 && ce_skip_worktree(the_index.cache[pos])) ||
168 (pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
169 ce->ce_flags |= CE_SKIP_WORKTREE;
171 if (!ce)
172 die(_("make_cache_entry failed for path '%s'"),
173 one->path);
174 if (!is_in_reset_tree) {
175 ce->ce_flags |= CE_INTENT_TO_ADD;
176 set_object_name_for_intent_to_add_entry(ce);
178 add_index_entry(&the_index, ce,
179 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
183 static int read_from_tree(const struct pathspec *pathspec,
184 struct object_id *tree_oid,
185 int intent_to_add)
187 struct diff_options opt;
189 memset(&opt, 0, sizeof(opt));
190 copy_pathspec(&opt.pathspec, pathspec);
191 opt.output_format = DIFF_FORMAT_CALLBACK;
192 opt.format_callback = update_index_from_diff;
193 opt.format_callback_data = &intent_to_add;
194 opt.flags.override_submodule_config = 1;
195 opt.flags.recursive = 1;
196 opt.repo = the_repository;
197 opt.change = diff_change;
198 opt.add_remove = diff_addremove;
200 if (pathspec->nr && pathspec_needs_expanded_index(&the_index, pathspec))
201 ensure_full_index(&the_index);
203 if (do_diff_cache(tree_oid, &opt))
204 return 1;
205 diffcore_std(&opt);
206 diff_flush(&opt);
208 return 0;
211 static void set_reflog_message(struct strbuf *sb, const char *action,
212 const char *rev)
214 const char *rla = getenv("GIT_REFLOG_ACTION");
216 strbuf_reset(sb);
217 if (rla)
218 strbuf_addf(sb, "%s: %s", rla, action);
219 else if (rev)
220 strbuf_addf(sb, "reset: moving to %s", rev);
221 else
222 strbuf_addf(sb, "reset: %s", action);
225 static void die_if_unmerged_cache(int reset_type)
227 if (is_merge() || unmerged_index(&the_index))
228 die(_("Cannot do a %s reset in the middle of a merge."),
229 _(reset_type_names[reset_type]));
233 static void parse_args(struct pathspec *pathspec,
234 const char **argv, const char *prefix,
235 int patch_mode,
236 const char **rev_ret)
238 const char *rev = "HEAD";
239 struct object_id unused;
241 * Possible arguments are:
243 * git reset [-opts] [<rev>]
244 * git reset [-opts] <tree> [<paths>...]
245 * git reset [-opts] <tree> -- [<paths>...]
246 * git reset [-opts] -- [<paths>...]
247 * git reset [-opts] <paths>...
249 * At this point, argv points immediately after [-opts].
252 if (argv[0]) {
253 if (!strcmp(argv[0], "--")) {
254 argv++; /* reset to HEAD, possibly with paths */
255 } else if (argv[1] && !strcmp(argv[1], "--")) {
256 rev = argv[0];
257 argv += 2;
260 * Otherwise, argv[0] could be either <rev> or <paths> and
261 * has to be unambiguous. If there is a single argument, it
262 * can not be a tree
264 else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
265 (argv[1] && !get_oid_treeish(argv[0], &unused))) {
267 * Ok, argv[0] looks like a commit/tree; it should not
268 * be a filename.
270 verify_non_filename(prefix, argv[0]);
271 rev = *argv++;
272 } else {
273 /* Otherwise we treat this as a filename */
274 verify_filename(prefix, argv[0], 1);
277 *rev_ret = rev;
279 parse_pathspec(pathspec, 0,
280 PATHSPEC_PREFER_FULL |
281 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
282 prefix, argv);
285 static int reset_refs(const char *rev, const struct object_id *oid)
287 int update_ref_status;
288 struct strbuf msg = STRBUF_INIT;
289 struct object_id *orig = NULL, oid_orig,
290 *old_orig = NULL, oid_old_orig;
292 if (!get_oid("ORIG_HEAD", &oid_old_orig))
293 old_orig = &oid_old_orig;
294 if (!get_oid("HEAD", &oid_orig)) {
295 orig = &oid_orig;
296 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
297 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
298 UPDATE_REFS_MSG_ON_ERR);
299 } else if (old_orig)
300 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
301 set_reflog_message(&msg, "updating HEAD", rev);
302 update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
303 UPDATE_REFS_MSG_ON_ERR);
304 strbuf_release(&msg);
305 return update_ref_status;
308 static int git_reset_config(const char *var, const char *value, void *cb)
310 if (!strcmp(var, "submodule.recurse"))
311 return git_default_submodule_config(var, value, cb);
313 return git_default_config(var, value, cb);
316 int cmd_reset(int argc, const char **argv, const char *prefix)
318 int reset_type = NONE, update_ref_status = 0, quiet = 0;
319 int no_refresh = 0;
320 int patch_mode = 0, pathspec_file_nul = 0, unborn;
321 const char *rev;
322 char *pathspec_from_file = NULL;
323 struct object_id oid;
324 struct pathspec pathspec;
325 int intent_to_add = 0;
326 const struct option options[] = {
327 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
328 OPT_BOOL(0, "no-refresh", &no_refresh,
329 N_("skip refreshing the index after reset")),
330 OPT_SET_INT(0, "mixed", &reset_type,
331 N_("reset HEAD and index"), MIXED),
332 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
333 OPT_SET_INT(0, "hard", &reset_type,
334 N_("reset HEAD, index and working tree"), HARD),
335 OPT_SET_INT(0, "merge", &reset_type,
336 N_("reset HEAD, index and working tree"), MERGE),
337 OPT_SET_INT(0, "keep", &reset_type,
338 N_("reset HEAD but keep local changes"), KEEP),
339 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
340 "reset", "control recursive updating of submodules",
341 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
342 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
343 OPT_BOOL('N', "intent-to-add", &intent_to_add,
344 N_("record only the fact that removed paths will be added later")),
345 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
346 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
347 OPT_END()
350 git_config(git_reset_config, NULL);
352 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
353 PARSE_OPT_KEEP_DASHDASH);
354 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
356 if (pathspec_from_file) {
357 if (patch_mode)
358 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
360 if (pathspec.nr)
361 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
363 parse_pathspec_file(&pathspec, 0,
364 PATHSPEC_PREFER_FULL,
365 prefix, pathspec_from_file, pathspec_file_nul);
366 } else if (pathspec_file_nul) {
367 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
370 unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
371 if (unborn) {
372 /* reset on unborn branch: treat as reset to empty tree */
373 oidcpy(&oid, the_hash_algo->empty_tree);
374 } else if (!pathspec.nr && !patch_mode) {
375 struct commit *commit;
376 if (get_oid_committish(rev, &oid))
377 die(_("Failed to resolve '%s' as a valid revision."), rev);
378 commit = lookup_commit_reference(the_repository, &oid);
379 if (!commit)
380 die(_("Could not parse object '%s'."), rev);
381 oidcpy(&oid, &commit->object.oid);
382 } else {
383 struct tree *tree;
384 if (get_oid_treeish(rev, &oid))
385 die(_("Failed to resolve '%s' as a valid tree."), rev);
386 tree = parse_tree_indirect(&oid);
387 if (!tree)
388 die(_("Could not parse object '%s'."), rev);
389 oidcpy(&oid, &tree->object.oid);
392 if (patch_mode) {
393 if (reset_type != NONE)
394 die(_("options '%s' and '%s' cannot be used together"), "--patch", "--{hard,mixed,soft}");
395 trace2_cmd_mode("patch-interactive");
396 update_ref_status = !!run_add_p(the_repository, ADD_P_RESET, rev,
397 &pathspec);
398 goto cleanup;
401 /* git reset tree [--] paths... can be used to
402 * load chosen paths from the tree into the index without
403 * affecting the working tree nor HEAD. */
404 if (pathspec.nr) {
405 if (reset_type == MIXED)
406 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
407 else if (reset_type != NONE)
408 die(_("Cannot do %s reset with paths."),
409 _(reset_type_names[reset_type]));
411 if (reset_type == NONE)
412 reset_type = MIXED; /* by default */
414 if (pathspec.nr)
415 trace2_cmd_mode("path");
416 else
417 trace2_cmd_mode(reset_type_names[reset_type]);
419 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
420 setup_work_tree();
422 if (reset_type == MIXED && is_bare_repository())
423 die(_("%s reset is not allowed in a bare repository"),
424 _(reset_type_names[reset_type]));
426 if (intent_to_add && reset_type != MIXED)
427 die(_("the option '%s' requires '%s'"), "-N", "--mixed");
429 prepare_repo_settings(the_repository);
430 the_repository->settings.command_requires_full_index = 0;
432 if (repo_read_index(the_repository) < 0)
433 die(_("index file corrupt"));
435 /* Soft reset does not touch the index file nor the working tree
436 * at all, but requires them in a good order. Other resets reset
437 * the index file to the tree object we are switching to. */
438 if (reset_type == SOFT || reset_type == KEEP)
439 die_if_unmerged_cache(reset_type);
441 if (reset_type != SOFT) {
442 struct lock_file lock = LOCK_INIT;
443 repo_hold_locked_index(the_repository, &lock,
444 LOCK_DIE_ON_ERROR);
445 if (reset_type == MIXED) {
446 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
447 if (read_from_tree(&pathspec, &oid, intent_to_add)) {
448 update_ref_status = 1;
449 goto cleanup;
451 the_index.updated_skipworktree = 1;
452 if (!no_refresh && get_git_work_tree()) {
453 uint64_t t_begin, t_delta_in_ms;
455 t_begin = getnanotime();
456 refresh_index(&the_index, flags, NULL, NULL,
457 _("Unstaged changes after reset:"));
458 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
459 if (!quiet && advice_enabled(ADVICE_RESET_NO_REFRESH_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
460 advise(_("It took %.2f seconds to refresh the index after reset. You can use\n"
461 "'--no-refresh' to avoid this."), t_delta_in_ms / 1000.0);
464 } else {
465 struct object_id dummy;
466 char *ref = NULL;
467 int err;
469 dwim_ref(rev, strlen(rev), &dummy, &ref, 0);
470 if (ref && !starts_with(ref, "refs/"))
471 FREE_AND_NULL(ref);
473 err = reset_index(ref, &oid, reset_type, quiet);
474 if (reset_type == KEEP && !err)
475 err = reset_index(ref, &oid, MIXED, quiet);
476 if (err)
477 die(_("Could not reset index file to revision '%s'."), rev);
478 free(ref);
481 if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
482 die(_("Could not write new index file."));
485 if (!pathspec.nr && !unborn) {
486 /* Any resets without paths update HEAD to the head being
487 * switched to, saving the previous head in ORIG_HEAD before. */
488 update_ref_status = reset_refs(rev, &oid);
490 if (reset_type == HARD && !update_ref_status && !quiet)
491 print_new_head_line(lookup_commit_reference(the_repository, &oid));
493 if (!pathspec.nr)
494 remove_branch_state(the_repository, 0);
496 discard_index(&the_index);
498 cleanup:
499 clear_pathspec(&pathspec);
500 free(pathspec_from_file);
501 return update_ref_status;