add 'git credential' plumbing command
[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"
0e5a7faa
CR
11#include "tag.h"
12#include "object.h"
13#include "commit.h"
14#include "run-command.h"
15#include "refs.h"
16#include "diff.h"
17#include "diffcore.h"
18#include "tree.h"
c369e7b8 19#include "branch.h"
5eee6b28 20#include "parse-options.h"
d0f379c2
SB
21#include "unpack-trees.h"
22#include "cache-tree.h"
0e5a7faa 23
5eee6b28 24static const char * const git_reset_usage[] = {
9bc454df 25 "git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]",
e4762865
JS
26 "git reset [-q] <commit> [--] <paths>...",
27 "git reset --patch [<commit>] [--] [<paths>...]",
5eee6b28
CR
28 NULL
29};
0e5a7faa 30
9bc454df
CC
31enum reset_type { MIXED, SOFT, HARD, MERGE, KEEP, NONE };
32static const char *reset_type_names[] = {
8b2a57b6 33 N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
9bc454df 34};
9e8eceab 35
0e5a7faa
CR
36static inline int is_merge(void)
37{
38 return !access(git_path("MERGE_HEAD"), F_OK);
39}
40
9e8eceab 41static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet)
0e5a7faa 42{
d0f379c2
SB
43 int nr = 1;
44 int newfd;
45 struct tree_desc desc[2];
6c52ec8a 46 struct tree *tree;
d0f379c2
SB
47 struct unpack_trees_options opts;
48 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
0e5a7faa 49
d0f379c2
SB
50 memset(&opts, 0, sizeof(opts));
51 opts.head_idx = 1;
52 opts.src_index = &the_index;
53 opts.dst_index = &the_index;
54 opts.fn = oneway_merge;
55 opts.merge = 1;
5aa965a0 56 if (!quiet)
d0f379c2 57 opts.verbose_update = 1;
9e8eceab 58 switch (reset_type) {
9bc454df 59 case KEEP:
9e8eceab 60 case MERGE:
d0f379c2 61 opts.update = 1;
9e8eceab
LT
62 break;
63 case HARD:
d0f379c2 64 opts.update = 1;
9e8eceab
LT
65 /* fallthrough */
66 default:
d0f379c2 67 opts.reset = 1;
9e8eceab 68 }
0e5a7faa 69
d0f379c2
SB
70 newfd = hold_locked_index(lock, 1);
71
72 read_cache_unmerged();
73
9bc454df
CC
74 if (reset_type == KEEP) {
75 unsigned char head_sha1[20];
76 if (get_sha1("HEAD", head_sha1))
b50a64e8 77 return error(_("You do not have a valid HEAD."));
9bc454df 78 if (!fill_tree_descriptor(desc, head_sha1))
b50a64e8 79 return error(_("Failed to find tree of HEAD."));
9bc454df
CC
80 nr++;
81 opts.fn = twoway_merge;
82 }
83
d0f379c2 84 if (!fill_tree_descriptor(desc + nr - 1, sha1))
b50a64e8 85 return error(_("Failed to find tree of %s."), sha1_to_hex(sha1));
d0f379c2
SB
86 if (unpack_trees(nr, desc, &opts))
87 return -1;
6c52ec8a
TR
88
89 if (reset_type == MIXED || reset_type == HARD) {
90 tree = parse_tree_indirect(sha1);
91 prime_cache_tree(&active_cache_tree, tree);
92 }
93
d0f379c2
SB
94 if (write_cache(newfd, active_cache, active_nr) ||
95 commit_locked_index(lock))
b50a64e8 96 return error(_("Could not write new index file."));
d0f379c2
SB
97
98 return 0;
0e5a7faa
CR
99}
100
101static void print_new_head_line(struct commit *commit)
102{
2efb3b06 103 const char *hex, *body;
0e5a7faa
CR
104
105 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
b50a64e8 106 printf(_("HEAD is now at %s"), hex);
0e5a7faa
CR
107 body = strstr(commit->buffer, "\n\n");
108 if (body) {
109 const char *eol;
110 size_t len;
111 body += 2;
112 eol = strchr(body, '\n');
113 len = eol ? eol - body : strlen(body);
114 printf(" %.*s\n", (int) len, body);
115 }
116 else
117 printf("\n");
118}
119
b0320eaf 120static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
0e5a7faa 121{
620a6cd4 122 int result;
0e5a7faa 123
620a6cd4
JS
124 if (!index_lock) {
125 index_lock = xcalloc(1, sizeof(struct lock_file));
126 fd = hold_locked_index(index_lock, 1);
127 }
0e5a7faa 128
620a6cd4 129 if (read_cache() < 0)
b50a64e8 130 return error(_("Could not read index"));
b0320eaf 131
3deffc52 132 result = refresh_index(&the_index, (flags), NULL, NULL,
4d3cd970 133 _("Unstaged changes after reset:")) ? 1 : 0;
620a6cd4 134 if (write_cache(fd, active_cache, active_nr) ||
620a6cd4
JS
135 commit_locked_index(index_lock))
136 return error ("Could not refresh index");
137 return result;
138}
2e7a9785 139
0e5a7faa
CR
140static void update_index_from_diff(struct diff_queue_struct *q,
141 struct diff_options *opt, void *data)
142{
143 int i;
620a6cd4 144 int *discard_flag = data;
0e5a7faa
CR
145
146 /* do_diff_cache() mangled the index */
147 discard_cache();
620a6cd4 148 *discard_flag = 1;
0e5a7faa
CR
149 read_cache();
150
151 for (i = 0; i < q->nr; i++) {
152 struct diff_filespec *one = q->queue[i]->one;
ff00b682 153 if (one->mode && !is_null_sha1(one->sha1)) {
0e5a7faa
CR
154 struct cache_entry *ce;
155 ce = make_cache_entry(one->mode, one->sha1, one->path,
156 0, 0);
d09e2cd5 157 if (!ce)
b50a64e8 158 die(_("make_cache_entry failed for path '%s'"),
d09e2cd5 159 one->path);
0e5a7faa
CR
160 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
161 ADD_CACHE_OK_TO_REPLACE);
162 } else
163 remove_file_from_cache(one->path);
164 }
165}
166
d002ef4d
TR
167static int interactive_reset(const char *revision, const char **argv,
168 const char *prefix)
169{
170 const char **pathspec = NULL;
171
172 if (*argv)
173 pathspec = get_pathspec(prefix, argv);
174
175 return run_add_interactive(revision, "--patch=reset", pathspec);
176}
177
0e5a7faa 178static int read_from_tree(const char *prefix, const char **argv,
b0320eaf 179 unsigned char *tree_sha1, int refresh_flags)
0e5a7faa 180{
620a6cd4
JS
181 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
182 int index_fd, index_was_discarded = 0;
0e5a7faa
CR
183 struct diff_options opt;
184
185 memset(&opt, 0, sizeof(opt));
186 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
187 opt.output_format = DIFF_FORMAT_CALLBACK;
188 opt.format_callback = update_index_from_diff;
620a6cd4 189 opt.format_callback_data = &index_was_discarded;
0e5a7faa 190
620a6cd4
JS
191 index_fd = hold_locked_index(lock, 1);
192 index_was_discarded = 0;
0e5a7faa
CR
193 read_cache();
194 if (do_diff_cache(tree_sha1, &opt))
195 return 1;
196 diffcore_std(&opt);
197 diff_flush(&opt);
03b69c76 198 diff_tree_release_paths(&opt);
2e7a9785 199
620a6cd4
JS
200 if (!index_was_discarded)
201 /* The index is still clobbered from do_diff_cache() */
202 discard_cache();
b0320eaf 203 return update_index_refresh(index_fd, lock, refresh_flags);
0e5a7faa
CR
204}
205
d04520e3
JK
206static void set_reflog_message(struct strbuf *sb, const char *action,
207 const char *rev)
0e5a7faa 208{
0e5a7faa 209 const char *rla = getenv("GIT_REFLOG_ACTION");
d04520e3
JK
210
211 strbuf_reset(sb);
212 if (rla)
213 strbuf_addf(sb, "%s: %s", rla, action);
214 else if (rev)
215 strbuf_addf(sb, "reset: moving to %s", rev);
216 else
217 strbuf_addf(sb, "reset: %s", action);
0e5a7faa
CR
218}
219
812d2a3d
CC
220static void die_if_unmerged_cache(int reset_type)
221{
222 if (is_merge() || read_cache() < 0 || unmerged_cache())
8b2a57b6
ÆAB
223 die(_("Cannot do a %s reset in the middle of a merge."),
224 _(reset_type_names[reset_type]));
812d2a3d
CC
225
226}
227
0e5a7faa
CR
228int cmd_reset(int argc, const char **argv, const char *prefix)
229{
5eee6b28 230 int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
d002ef4d 231 int patch_mode = 0;
0e5a7faa
CR
232 const char *rev = "HEAD";
233 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
234 *old_orig = NULL, sha1_old_orig[20];
235 struct commit *commit;
d04520e3 236 struct strbuf msg = STRBUF_INIT;
5eee6b28 237 const struct option options[] = {
8c839683 238 OPT__QUIET(&quiet, "be quiet, only report errors"),
5eee6b28
CR
239 OPT_SET_INT(0, "mixed", &reset_type,
240 "reset HEAD and index", MIXED),
241 OPT_SET_INT(0, "soft", &reset_type, "reset only HEAD", SOFT),
242 OPT_SET_INT(0, "hard", &reset_type,
243 "reset HEAD, index and working tree", HARD),
9e8eceab
LT
244 OPT_SET_INT(0, "merge", &reset_type,
245 "reset HEAD, index and working tree", MERGE),
9bc454df
CC
246 OPT_SET_INT(0, "keep", &reset_type,
247 "reset HEAD but keep local changes", KEEP),
d002ef4d 248 OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"),
5eee6b28
CR
249 OPT_END()
250 };
0e5a7faa 251
ef90d6d4 252 git_config(git_default_config, NULL);
0e5a7faa 253
37782920 254 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
5eee6b28 255 PARSE_OPT_KEEP_DASHDASH);
0e5a7faa 256
dfc8f39e
JH
257 /*
258 * Possible arguments are:
259 *
260 * git reset [-opts] <rev> <paths>...
261 * git reset [-opts] <rev> -- <paths>...
262 * git reset [-opts] -- <paths>...
263 * git reset [-opts] <paths>...
264 *
265 * At this point, argv[i] points immediately after [-opts].
266 */
267
268 if (i < argc) {
269 if (!strcmp(argv[i], "--")) {
270 i++; /* reset to HEAD, possibly with paths */
271 } else if (i + 1 < argc && !strcmp(argv[i+1], "--")) {
272 rev = argv[i];
273 i += 2;
274 }
275 /*
276 * Otherwise, argv[i] could be either <rev> or <paths> and
3ea3c215 277 * has to be unambiguous.
dfc8f39e
JH
278 */
279 else if (!get_sha1(argv[i], sha1)) {
280 /*
281 * Ok, argv[i] looks like a rev; it should not
282 * be a filename.
283 */
284 verify_non_filename(prefix, argv[i]);
285 rev = argv[i++];
286 } else {
287 /* Otherwise we treat this as a filename */
288 verify_filename(prefix, argv[i]);
289 }
290 }
0e5a7faa
CR
291
292 if (get_sha1(rev, sha1))
b50a64e8 293 die(_("Failed to resolve '%s' as a valid ref."), rev);
0e5a7faa
CR
294
295 commit = lookup_commit_reference(sha1);
296 if (!commit)
b50a64e8 297 die(_("Could not parse object '%s'."), rev);
0e5a7faa
CR
298 hashcpy(sha1, commit->object.sha1);
299
d002ef4d
TR
300 if (patch_mode) {
301 if (reset_type != NONE)
b50a64e8 302 die(_("--patch is incompatible with --{hard,mixed,soft}"));
d002ef4d
TR
303 return interactive_reset(rev, argv + i, prefix);
304 }
305
0e5a7faa
CR
306 /* git reset tree [--] paths... can be used to
307 * load chosen paths from the tree into the index without
308 * affecting the working tree nor HEAD. */
309 if (i < argc) {
310 if (reset_type == MIXED)
b50a64e8 311 warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead."));
0e5a7faa 312 else if (reset_type != NONE)
8b2a57b6
ÆAB
313 die(_("Cannot do %s reset with paths."),
314 _(reset_type_names[reset_type]));
b0320eaf 315 return read_from_tree(prefix, argv + i, sha1,
43673fdd 316 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
0e5a7faa
CR
317 }
318 if (reset_type == NONE)
319 reset_type = MIXED; /* by default */
320
ab892a19 321 if (reset_type != SOFT && reset_type != MIXED)
cd0f0f68 322 setup_work_tree();
49b9362f 323
2b06b0a0 324 if (reset_type == MIXED && is_bare_repository())
8b2a57b6
ÆAB
325 die(_("%s reset is not allowed in a bare repository"),
326 _(reset_type_names[reset_type]));
2b06b0a0 327
0e5a7faa
CR
328 /* Soft reset does not touch the index file nor the working tree
329 * at all, but requires them in a good order. Other resets reset
330 * the index file to the tree object we are switching to. */
812d2a3d
CC
331 if (reset_type == SOFT)
332 die_if_unmerged_cache(reset_type);
333 else {
334 int err;
335 if (reset_type == KEEP)
336 die_if_unmerged_cache(reset_type);
337 err = reset_index_file(sha1, reset_type, quiet);
9bc454df
CC
338 if (reset_type == KEEP)
339 err = err || reset_index_file(sha1, MIXED, quiet);
340 if (err)
b50a64e8 341 die(_("Could not reset index file to revision '%s'."), rev);
0e5a7faa 342 }
0e5a7faa
CR
343
344 /* Any resets update HEAD to the head being switched to,
345 * saving the previous head in ORIG_HEAD before. */
346 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
347 old_orig = sha1_old_orig;
348 if (!get_sha1("HEAD", sha1_orig)) {
349 orig = sha1_orig;
d04520e3
JK
350 set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
351 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
0e5a7faa
CR
352 }
353 else if (old_orig)
eca35a25 354 delete_ref("ORIG_HEAD", old_orig, 0);
d04520e3
JK
355 set_reflog_message(&msg, "updating HEAD", rev);
356 update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
0e5a7faa
CR
357
358 switch (reset_type) {
359 case HARD:
521b53e5 360 if (!update_ref_status && !quiet)
0e5a7faa
CR
361 print_new_head_line(commit);
362 break;
363 case SOFT: /* Nothing else to do. */
364 break;
365 case MIXED: /* Report what has not been updated. */
b0320eaf 366 update_index_refresh(0, NULL,
43673fdd 367 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
0e5a7faa
CR
368 break;
369 }
370
c369e7b8 371 remove_branch_state();
0e5a7faa 372
d04520e3 373 strbuf_release(&msg);
0e5a7faa
CR
374
375 return update_ref_status;
376}