blob: 5fa1789d0c2b90ef9ee83d091b67292f2ff0db26 [file] [log] [blame]
Carlos Rica0e5a7fa2007-09-11 05:19:34 +02001/*
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"
Daniel Barkalowc369e7b2008-02-07 11:40:16 -050019#include "branch.h"
Carlos Rica5eee6b22008-03-04 23:11:34 +010020#include "parse-options.h"
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020021
Carlos Rica5eee6b22008-03-04 23:11:34 +010022static const char * const git_reset_usage[] = {
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080023 "git reset [--mixed | --soft | --hard | --merge] [-q] [<commit>]",
Stephan Beyer1b1dd232008-07-13 15:36:15 +020024 "git reset [--mixed] <commit> [--] <paths>...",
Carlos Rica5eee6b22008-03-04 23:11:34 +010025 NULL
26};
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020027
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080028enum reset_type { MIXED, SOFT, HARD, MERGE, NONE };
29static const char *reset_type_names[] = { "mixed", "soft", "hard", "merge", NULL };
30
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020031static char *args_to_str(const char **argv)
32{
33 char *buf = NULL;
34 unsigned long len, space = 0, nr = 0;
35
36 for (; *argv; argv++) {
37 len = strlen(*argv);
38 ALLOC_GROW(buf, nr + 1 + len, space);
39 if (nr)
40 buf[nr++] = ' ';
41 memcpy(buf + nr, *argv, len);
42 nr += len;
43 }
44 ALLOC_GROW(buf, nr + 1, space);
45 buf[nr] = '\0';
46
47 return buf;
48}
49
50static inline int is_merge(void)
51{
52 return !access(git_path("MERGE_HEAD"), F_OK);
53}
54
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080055static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020056{
57 int i = 0;
58 const char *args[6];
59
60 args[i++] = "read-tree";
Jamis Buck5aa965a2008-05-31 18:10:58 -070061 if (!quiet)
62 args[i++] = "-v";
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080063 switch (reset_type) {
64 case MERGE:
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020065 args[i++] = "-u";
Linus Torvalds9e8ecea2008-12-01 09:30:31 -080066 args[i++] = "-m";
67 break;
68 case HARD:
69 args[i++] = "-u";
70 /* fallthrough */
71 default:
72 args[i++] = "--reset";
73 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020074 args[i++] = sha1_to_hex(sha1);
75 args[i] = NULL;
76
77 return run_command_v_opt(args, RUN_GIT_CMD);
78}
79
80static void print_new_head_line(struct commit *commit)
81{
Junio C Hamano2efb3b02008-03-01 23:43:32 -080082 const char *hex, *body;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020083
84 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
Junio C Hamano2efb3b02008-03-01 23:43:32 -080085 printf("HEAD is now at %s", hex);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +020086 body = strstr(commit->buffer, "\n\n");
87 if (body) {
88 const char *eol;
89 size_t len;
90 body += 2;
91 eol = strchr(body, '\n');
92 len = eol ? eol - body : strlen(body);
93 printf(" %.*s\n", (int) len, body);
94 }
95 else
96 printf("\n");
97}
98
Stephan Beyerb0320ea2008-07-25 22:49:08 +020099static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200100{
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000101 int result;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200102
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000103 if (!index_lock) {
104 index_lock = xcalloc(1, sizeof(struct lock_file));
105 fd = hold_locked_index(index_lock, 1);
106 }
107
108 if (read_cache() < 0)
109 return error("Could not read index");
Stephan Beyerb0320ea2008-07-25 22:49:08 +0200110
111 result = refresh_cache(flags) ? 1 : 0;
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000112 if (write_cache(fd, active_cache, active_nr) ||
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000113 commit_locked_index(index_lock))
114 return error ("Could not refresh index");
115 return result;
116}
Johannes Schindelin2e7a9782007-11-03 13:12:17 +0000117
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200118static void update_index_from_diff(struct diff_queue_struct *q,
119 struct diff_options *opt, void *data)
120{
121 int i;
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000122 int *discard_flag = data;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200123
124 /* do_diff_cache() mangled the index */
125 discard_cache();
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000126 *discard_flag = 1;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200127 read_cache();
128
129 for (i = 0; i < q->nr; i++) {
130 struct diff_filespec *one = q->queue[i]->one;
131 if (one->mode) {
132 struct cache_entry *ce;
133 ce = make_cache_entry(one->mode, one->sha1, one->path,
134 0, 0);
Dmitry Potapovd09e2cd2008-10-05 06:14:40 +0400135 if (!ce)
136 die("make_cache_entry failed for path '%s'",
137 one->path);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200138 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
139 ADD_CACHE_OK_TO_REPLACE);
140 } else
141 remove_file_from_cache(one->path);
142 }
143}
144
145static int read_from_tree(const char *prefix, const char **argv,
Stephan Beyerb0320ea2008-07-25 22:49:08 +0200146 unsigned char *tree_sha1, int refresh_flags)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200147{
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000148 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
149 int index_fd, index_was_discarded = 0;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200150 struct diff_options opt;
151
152 memset(&opt, 0, sizeof(opt));
153 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
154 opt.output_format = DIFF_FORMAT_CALLBACK;
155 opt.format_callback = update_index_from_diff;
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000156 opt.format_callback_data = &index_was_discarded;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200157
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000158 index_fd = hold_locked_index(lock, 1);
159 index_was_discarded = 0;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200160 read_cache();
161 if (do_diff_cache(tree_sha1, &opt))
162 return 1;
163 diffcore_std(&opt);
164 diff_flush(&opt);
Mike Hommey03b69c72007-12-11 22:59:55 +0100165 diff_tree_release_paths(&opt);
Johannes Schindelin2e7a9782007-11-03 13:12:17 +0000166
Johannes Schindelin620a6cd2007-11-03 15:21:21 +0000167 if (!index_was_discarded)
168 /* The index is still clobbered from do_diff_cache() */
169 discard_cache();
Stephan Beyerb0320ea2008-07-25 22:49:08 +0200170 return update_index_refresh(index_fd, lock, refresh_flags);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200171}
172
173static void prepend_reflog_action(const char *action, char *buf, size_t size)
174{
175 const char *sep = ": ";
176 const char *rla = getenv("GIT_REFLOG_ACTION");
177 if (!rla)
178 rla = sep = "";
179 if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
180 warning("Reflog action message too long: %.*s...", 50, buf);
181}
182
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200183int cmd_reset(int argc, const char **argv, const char *prefix)
184{
Carlos Rica5eee6b22008-03-04 23:11:34 +0100185 int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200186 const char *rev = "HEAD";
187 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
188 *old_orig = NULL, sha1_old_orig[20];
189 struct commit *commit;
190 char *reflog_action, msg[1024];
Carlos Rica5eee6b22008-03-04 23:11:34 +0100191 const struct option options[] = {
192 OPT_SET_INT(0, "mixed", &reset_type,
193 "reset HEAD and index", MIXED),
194 OPT_SET_INT(0, "soft", &reset_type, "reset only HEAD", SOFT),
195 OPT_SET_INT(0, "hard", &reset_type,
196 "reset HEAD, index and working tree", HARD),
Linus Torvalds9e8ecea2008-12-01 09:30:31 -0800197 OPT_SET_INT(0, "merge", &reset_type,
198 "reset HEAD, index and working tree", MERGE),
Carlos Rica5eee6b22008-03-04 23:11:34 +0100199 OPT_BOOLEAN('q', NULL, &quiet,
Jamis Buck5aa965a2008-05-31 18:10:58 -0700200 "disable showing new HEAD in hard reset and progress message"),
Carlos Rica5eee6b22008-03-04 23:11:34 +0100201 OPT_END()
202 };
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200203
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100204 git_config(git_default_config, NULL);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200205
Stephen Boyd37782922009-05-23 11:53:12 -0700206 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
Carlos Rica5eee6b22008-03-04 23:11:34 +0100207 PARSE_OPT_KEEP_DASHDASH);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200208 reflog_action = args_to_str(argv);
209 setenv("GIT_REFLOG_ACTION", reflog_action, 0);
210
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700211 /*
212 * Possible arguments are:
213 *
214 * git reset [-opts] <rev> <paths>...
215 * git reset [-opts] <rev> -- <paths>...
216 * git reset [-opts] -- <paths>...
217 * git reset [-opts] <paths>...
218 *
219 * At this point, argv[i] points immediately after [-opts].
220 */
221
222 if (i < argc) {
223 if (!strcmp(argv[i], "--")) {
224 i++; /* reset to HEAD, possibly with paths */
225 } else if (i + 1 < argc && !strcmp(argv[i+1], "--")) {
226 rev = argv[i];
227 i += 2;
228 }
229 /*
230 * Otherwise, argv[i] could be either <rev> or <paths> and
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100231 * has to be unambiguous.
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700232 */
233 else if (!get_sha1(argv[i], sha1)) {
234 /*
235 * Ok, argv[i] looks like a rev; it should not
236 * be a filename.
237 */
238 verify_non_filename(prefix, argv[i]);
239 rev = argv[i++];
240 } else {
241 /* Otherwise we treat this as a filename */
242 verify_filename(prefix, argv[i]);
243 }
244 }
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200245
246 if (get_sha1(rev, sha1))
247 die("Failed to resolve '%s' as a valid ref.", rev);
248
249 commit = lookup_commit_reference(sha1);
250 if (!commit)
251 die("Could not parse object '%s'.", rev);
252 hashcpy(sha1, commit->object.sha1);
253
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200254 /* git reset tree [--] paths... can be used to
255 * load chosen paths from the tree into the index without
256 * affecting the working tree nor HEAD. */
257 if (i < argc) {
258 if (reset_type == MIXED)
259 warning("--mixed option is deprecated with paths.");
260 else if (reset_type != NONE)
261 die("Cannot do %s reset with paths.",
262 reset_type_names[reset_type]);
Stephan Beyerb0320ea2008-07-25 22:49:08 +0200263 return read_from_tree(prefix, argv + i, sha1,
264 quiet ? REFRESH_QUIET : REFRESH_SAY_CHANGED);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200265 }
266 if (reset_type == NONE)
267 reset_type = MIXED; /* by default */
268
Jeff King49b93622007-12-31 02:13:52 -0500269 if (reset_type == HARD && is_bare_repository())
270 die("hard reset makes no sense in a bare repository");
271
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200272 /* Soft reset does not touch the index file nor the working tree
273 * at all, but requires them in a good order. Other resets reset
274 * the index file to the tree object we are switching to. */
275 if (reset_type == SOFT) {
Daniel Barkalow94a57282008-02-07 11:40:13 -0500276 if (is_merge() || read_cache() < 0 || unmerged_cache())
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200277 die("Cannot do a soft reset in the middle of a merge.");
278 }
Linus Torvalds9e8ecea2008-12-01 09:30:31 -0800279 else if (reset_index_file(sha1, reset_type, quiet))
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200280 die("Could not reset index file to revision '%s'.", rev);
281
282 /* Any resets update HEAD to the head being switched to,
283 * saving the previous head in ORIG_HEAD before. */
284 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
285 old_orig = sha1_old_orig;
286 if (!get_sha1("HEAD", sha1_orig)) {
287 orig = sha1_orig;
288 prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
289 update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
290 }
291 else if (old_orig)
Miklos Vajnaeca35a22008-10-26 03:33:56 +0100292 delete_ref("ORIG_HEAD", old_orig, 0);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200293 prepend_reflog_action("updating HEAD", msg, sizeof(msg));
294 update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
295
296 switch (reset_type) {
297 case HARD:
Gerrit Pape521b53e2007-11-04 09:37:20 +0000298 if (!update_ref_status && !quiet)
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200299 print_new_head_line(commit);
300 break;
301 case SOFT: /* Nothing else to do. */
302 break;
303 case MIXED: /* Report what has not been updated. */
Stephan Beyerb0320ea2008-07-25 22:49:08 +0200304 update_index_refresh(0, NULL,
305 quiet ? REFRESH_QUIET : REFRESH_SAY_CHANGED);
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200306 break;
307 }
308
Daniel Barkalowc369e7b2008-02-07 11:40:16 -0500309 remove_branch_state();
Carlos Rica0e5a7fa2007-09-11 05:19:34 +0200310
311 free(reflog_action);
312
313 return update_ref_status;
314}