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