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