Code

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