]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/reset.c
Change unpack_trees' 'reset' flag into an enum
[thirdparty/git.git] / builtin / reset.c
1 /*
2 * "git reset" builtin command
3 *
4 * Copyright (c) 2007 Carlos Rica
5 *
6 * Based on git-reset.sh, which is
7 *
8 * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
9 */
10 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "builtin.h"
12 #include "config.h"
13 #include "lockfile.h"
14 #include "tag.h"
15 #include "object.h"
16 #include "pretty.h"
17 #include "run-command.h"
18 #include "refs.h"
19 #include "diff.h"
20 #include "diffcore.h"
21 #include "tree.h"
22 #include "branch.h"
23 #include "parse-options.h"
24 #include "unpack-trees.h"
25 #include "cache-tree.h"
26 #include "submodule.h"
27 #include "submodule-config.h"
28
29 #define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
30
31 static const char * const git_reset_usage[] = {
32 N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
33 N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
34 N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
35 N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
36 NULL
37 };
38
39 enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
40 static const char *reset_type_names[] = {
41 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
42 };
43
44 static inline int is_merge(void)
45 {
46 return !access(git_path_merge_head(the_repository), F_OK);
47 }
48
49 static int reset_index(const char *ref, const struct object_id *oid, int reset_type, int quiet)
50 {
51 int i, nr = 0;
52 struct tree_desc desc[2];
53 struct tree *tree;
54 struct unpack_trees_options opts;
55 int ret = -1;
56
57 memset(&opts, 0, sizeof(opts));
58 opts.head_idx = 1;
59 opts.src_index = &the_index;
60 opts.dst_index = &the_index;
61 opts.fn = oneway_merge;
62 opts.merge = 1;
63 init_checkout_metadata(&opts.meta, ref, oid, NULL);
64 if (!quiet)
65 opts.verbose_update = 1;
66 switch (reset_type) {
67 case KEEP:
68 case MERGE:
69 opts.update = 1;
70 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
71 break;
72 case HARD:
73 opts.update = 1;
74 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
75 break;
76 case MIXED:
77 opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
78 /* but opts.update=0, so working tree not updated */
79 break;
80 default:
81 BUG("invalid reset_type passed to reset_index");
82 }
83
84 read_cache_unmerged();
85
86 if (reset_type == KEEP) {
87 struct object_id head_oid;
88 if (get_oid("HEAD", &head_oid))
89 return error(_("You do not have a valid HEAD."));
90 if (!fill_tree_descriptor(the_repository, desc + nr, &head_oid))
91 return error(_("Failed to find tree of HEAD."));
92 nr++;
93 opts.fn = twoway_merge;
94 }
95
96 if (!fill_tree_descriptor(the_repository, desc + nr, oid)) {
97 error(_("Failed to find tree of %s."), oid_to_hex(oid));
98 goto out;
99 }
100 nr++;
101
102 if (unpack_trees(nr, desc, &opts))
103 goto out;
104
105 if (reset_type == MIXED || reset_type == HARD) {
106 tree = parse_tree_indirect(oid);
107 prime_cache_tree(the_repository, the_repository->index, tree);
108 }
109
110 ret = 0;
111
112 out:
113 for (i = 0; i < nr; i++)
114 free((void *)desc[i].buffer);
115 return ret;
116 }
117
118 static void print_new_head_line(struct commit *commit)
119 {
120 struct strbuf buf = STRBUF_INIT;
121
122 printf(_("HEAD is now at %s"),
123 find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV));
124
125 pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
126 if (buf.len > 0)
127 printf(" %s", buf.buf);
128 putchar('\n');
129 strbuf_release(&buf);
130 }
131
132 static void update_index_from_diff(struct diff_queue_struct *q,
133 struct diff_options *opt, void *data)
134 {
135 int i;
136 int intent_to_add = *(int *)data;
137
138 for (i = 0; i < q->nr; i++) {
139 struct diff_filespec *one = q->queue[i]->one;
140 int is_missing = !(one->mode && !is_null_oid(&one->oid));
141 struct cache_entry *ce;
142
143 if (is_missing && !intent_to_add) {
144 remove_file_from_cache(one->path);
145 continue;
146 }
147
148 ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
149 0, 0);
150 if (!ce)
151 die(_("make_cache_entry failed for path '%s'"),
152 one->path);
153 if (is_missing) {
154 ce->ce_flags |= CE_INTENT_TO_ADD;
155 set_object_name_for_intent_to_add_entry(ce);
156 }
157 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
158 }
159 }
160
161 static int read_from_tree(const struct pathspec *pathspec,
162 struct object_id *tree_oid,
163 int intent_to_add)
164 {
165 struct diff_options opt;
166
167 memset(&opt, 0, sizeof(opt));
168 copy_pathspec(&opt.pathspec, pathspec);
169 opt.output_format = DIFF_FORMAT_CALLBACK;
170 opt.format_callback = update_index_from_diff;
171 opt.format_callback_data = &intent_to_add;
172 opt.flags.override_submodule_config = 1;
173 opt.repo = the_repository;
174
175 if (do_diff_cache(tree_oid, &opt))
176 return 1;
177 diffcore_std(&opt);
178 diff_flush(&opt);
179 clear_pathspec(&opt.pathspec);
180
181 return 0;
182 }
183
184 static void set_reflog_message(struct strbuf *sb, const char *action,
185 const char *rev)
186 {
187 const char *rla = getenv("GIT_REFLOG_ACTION");
188
189 strbuf_reset(sb);
190 if (rla)
191 strbuf_addf(sb, "%s: %s", rla, action);
192 else if (rev)
193 strbuf_addf(sb, "reset: moving to %s", rev);
194 else
195 strbuf_addf(sb, "reset: %s", action);
196 }
197
198 static void die_if_unmerged_cache(int reset_type)
199 {
200 if (is_merge() || unmerged_cache())
201 die(_("Cannot do a %s reset in the middle of a merge."),
202 _(reset_type_names[reset_type]));
203
204 }
205
206 static void parse_args(struct pathspec *pathspec,
207 const char **argv, const char *prefix,
208 int patch_mode,
209 const char **rev_ret)
210 {
211 const char *rev = "HEAD";
212 struct object_id unused;
213 /*
214 * Possible arguments are:
215 *
216 * git reset [-opts] [<rev>]
217 * git reset [-opts] <tree> [<paths>...]
218 * git reset [-opts] <tree> -- [<paths>...]
219 * git reset [-opts] -- [<paths>...]
220 * git reset [-opts] <paths>...
221 *
222 * At this point, argv points immediately after [-opts].
223 */
224
225 if (argv[0]) {
226 if (!strcmp(argv[0], "--")) {
227 argv++; /* reset to HEAD, possibly with paths */
228 } else if (argv[1] && !strcmp(argv[1], "--")) {
229 rev = argv[0];
230 argv += 2;
231 }
232 /*
233 * Otherwise, argv[0] could be either <rev> or <paths> and
234 * has to be unambiguous. If there is a single argument, it
235 * can not be a tree
236 */
237 else if ((!argv[1] && !get_oid_committish(argv[0], &unused)) ||
238 (argv[1] && !get_oid_treeish(argv[0], &unused))) {
239 /*
240 * Ok, argv[0] looks like a commit/tree; it should not
241 * be a filename.
242 */
243 verify_non_filename(prefix, argv[0]);
244 rev = *argv++;
245 } else {
246 /* Otherwise we treat this as a filename */
247 verify_filename(prefix, argv[0], 1);
248 }
249 }
250 *rev_ret = rev;
251
252 if (read_cache() < 0)
253 die(_("index file corrupt"));
254
255 parse_pathspec(pathspec, 0,
256 PATHSPEC_PREFER_FULL |
257 (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
258 prefix, argv);
259 }
260
261 static int reset_refs(const char *rev, const struct object_id *oid)
262 {
263 int update_ref_status;
264 struct strbuf msg = STRBUF_INIT;
265 struct object_id *orig = NULL, oid_orig,
266 *old_orig = NULL, oid_old_orig;
267
268 if (!get_oid("ORIG_HEAD", &oid_old_orig))
269 old_orig = &oid_old_orig;
270 if (!get_oid("HEAD", &oid_orig)) {
271 orig = &oid_orig;
272 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
273 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
274 UPDATE_REFS_MSG_ON_ERR);
275 } else if (old_orig)
276 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
277 set_reflog_message(&msg, "updating HEAD", rev);
278 update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0,
279 UPDATE_REFS_MSG_ON_ERR);
280 strbuf_release(&msg);
281 return update_ref_status;
282 }
283
284 static int git_reset_config(const char *var, const char *value, void *cb)
285 {
286 if (!strcmp(var, "submodule.recurse"))
287 return git_default_submodule_config(var, value, cb);
288
289 return git_default_config(var, value, cb);
290 }
291
292 int cmd_reset(int argc, const char **argv, const char *prefix)
293 {
294 int reset_type = NONE, update_ref_status = 0, quiet = 0;
295 int patch_mode = 0, pathspec_file_nul = 0, unborn;
296 const char *rev, *pathspec_from_file = NULL;
297 struct object_id oid;
298 struct pathspec pathspec;
299 int intent_to_add = 0;
300 const struct option options[] = {
301 OPT__QUIET(&quiet, N_("be quiet, only report errors")),
302 OPT_SET_INT(0, "mixed", &reset_type,
303 N_("reset HEAD and index"), MIXED),
304 OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT),
305 OPT_SET_INT(0, "hard", &reset_type,
306 N_("reset HEAD, index and working tree"), HARD),
307 OPT_SET_INT(0, "merge", &reset_type,
308 N_("reset HEAD, index and working tree"), MERGE),
309 OPT_SET_INT(0, "keep", &reset_type,
310 N_("reset HEAD but keep local changes"), KEEP),
311 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
312 "reset", "control recursive updating of submodules",
313 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
314 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
315 OPT_BOOL('N', "intent-to-add", &intent_to_add,
316 N_("record only the fact that removed paths will be added later")),
317 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
318 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
319 OPT_END()
320 };
321
322 git_config(git_reset_config, NULL);
323 git_config_get_bool("reset.quiet", &quiet);
324
325 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
326 PARSE_OPT_KEEP_DASHDASH);
327 parse_args(&pathspec, argv, prefix, patch_mode, &rev);
328
329 if (pathspec_from_file) {
330 if (patch_mode)
331 die(_("--pathspec-from-file is incompatible with --patch"));
332
333 if (pathspec.nr)
334 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
335
336 parse_pathspec_file(&pathspec, 0,
337 PATHSPEC_PREFER_FULL,
338 prefix, pathspec_from_file, pathspec_file_nul);
339 } else if (pathspec_file_nul) {
340 die(_("--pathspec-file-nul requires --pathspec-from-file"));
341 }
342
343 unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
344 if (unborn) {
345 /* reset on unborn branch: treat as reset to empty tree */
346 oidcpy(&oid, the_hash_algo->empty_tree);
347 } else if (!pathspec.nr && !patch_mode) {
348 struct commit *commit;
349 if (get_oid_committish(rev, &oid))
350 die(_("Failed to resolve '%s' as a valid revision."), rev);
351 commit = lookup_commit_reference(the_repository, &oid);
352 if (!commit)
353 die(_("Could not parse object '%s'."), rev);
354 oidcpy(&oid, &commit->object.oid);
355 } else {
356 struct tree *tree;
357 if (get_oid_treeish(rev, &oid))
358 die(_("Failed to resolve '%s' as a valid tree."), rev);
359 tree = parse_tree_indirect(&oid);
360 if (!tree)
361 die(_("Could not parse object '%s'."), rev);
362 oidcpy(&oid, &tree->object.oid);
363 }
364
365 if (patch_mode) {
366 if (reset_type != NONE)
367 die(_("--patch is incompatible with --{hard,mixed,soft}"));
368 trace2_cmd_mode("patch-interactive");
369 return run_add_interactive(rev, "--patch=reset", &pathspec);
370 }
371
372 /* git reset tree [--] paths... can be used to
373 * load chosen paths from the tree into the index without
374 * affecting the working tree nor HEAD. */
375 if (pathspec.nr) {
376 if (reset_type == MIXED)
377 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
378 else if (reset_type != NONE)
379 die(_("Cannot do %s reset with paths."),
380 _(reset_type_names[reset_type]));
381 }
382 if (reset_type == NONE)
383 reset_type = MIXED; /* by default */
384
385 if (pathspec.nr)
386 trace2_cmd_mode("path");
387 else
388 trace2_cmd_mode(reset_type_names[reset_type]);
389
390 if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
391 setup_work_tree();
392
393 if (reset_type == MIXED && is_bare_repository())
394 die(_("%s reset is not allowed in a bare repository"),
395 _(reset_type_names[reset_type]));
396
397 if (intent_to_add && reset_type != MIXED)
398 die(_("-N can only be used with --mixed"));
399
400 /* Soft reset does not touch the index file nor the working tree
401 * at all, but requires them in a good order. Other resets reset
402 * the index file to the tree object we are switching to. */
403 if (reset_type == SOFT || reset_type == KEEP)
404 die_if_unmerged_cache(reset_type);
405
406 if (reset_type != SOFT) {
407 struct lock_file lock = LOCK_INIT;
408 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
409 if (reset_type == MIXED) {
410 int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN;
411 if (read_from_tree(&pathspec, &oid, intent_to_add))
412 return 1;
413 the_index.updated_skipworktree = 1;
414 if (!quiet && get_git_work_tree()) {
415 uint64_t t_begin, t_delta_in_ms;
416
417 t_begin = getnanotime();
418 refresh_index(&the_index, flags, NULL, NULL,
419 _("Unstaged changes after reset:"));
420 t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
421 if (advice_enabled(ADVICE_RESET_QUIET_WARNING) && t_delta_in_ms > REFRESH_INDEX_DELAY_WARNING_IN_MS) {
422 printf(_("\nIt took %.2f seconds to enumerate unstaged changes after reset. You can\n"
423 "use '--quiet' to avoid this. Set the config setting reset.quiet to true\n"
424 "to make this the default.\n"), t_delta_in_ms / 1000.0);
425 }
426 }
427 } else {
428 struct object_id dummy;
429 char *ref = NULL;
430 int err;
431
432 dwim_ref(rev, strlen(rev), &dummy, &ref, 0);
433 if (ref && !starts_with(ref, "refs/"))
434 FREE_AND_NULL(ref);
435
436 err = reset_index(ref, &oid, reset_type, quiet);
437 if (reset_type == KEEP && !err)
438 err = reset_index(ref, &oid, MIXED, quiet);
439 if (err)
440 die(_("Could not reset index file to revision '%s'."), rev);
441 free(ref);
442 }
443
444 if (write_locked_index(&the_index, &lock, COMMIT_LOCK))
445 die(_("Could not write new index file."));
446 }
447
448 if (!pathspec.nr && !unborn) {
449 /* Any resets without paths update HEAD to the head being
450 * switched to, saving the previous head in ORIG_HEAD before. */
451 update_ref_status = reset_refs(rev, &oid);
452
453 if (reset_type == HARD && !update_ref_status && !quiet)
454 print_new_head_line(lookup_commit_reference(the_repository, &oid));
455 }
456 if (!pathspec.nr)
457 remove_branch_state(the_repository, 0);
458
459 return update_ref_status;
460 }