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