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