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