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