]>
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 NTND |
25 | N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"), |
26 | N_("git reset [-q] <commit> [--] <paths>..."), | |
27 | N_("git reset --patch [<commit>] [--] [<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 | ||
9e8eceab | 41 | static 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 | ||
101 | static 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 | 120 | static int update_index_refresh(int fd, struct lock_file *index_lock, int flags) |
0e5a7faa | 121 | { |
620a6cd4 JS |
122 | if (!index_lock) { |
123 | index_lock = xcalloc(1, sizeof(struct lock_file)); | |
124 | fd = hold_locked_index(index_lock, 1); | |
125 | } | |
0e5a7faa | 126 | |
d94c5e2f MZ |
127 | refresh_index(&the_index, (flags), NULL, NULL, |
128 | _("Unstaged changes after reset:")); | |
620a6cd4 | 129 | if (write_cache(fd, active_cache, active_nr) || |
620a6cd4 JS |
130 | commit_locked_index(index_lock)) |
131 | return error ("Could not refresh index"); | |
d94c5e2f | 132 | return 0; |
620a6cd4 | 133 | } |
2e7a9785 | 134 | |
0e5a7faa CR |
135 | static void update_index_from_diff(struct diff_queue_struct *q, |
136 | struct diff_options *opt, void *data) | |
137 | { | |
138 | int i; | |
0e5a7faa CR |
139 | |
140 | for (i = 0; i < q->nr; i++) { | |
141 | struct diff_filespec *one = q->queue[i]->one; | |
ff00b682 | 142 | if (one->mode && !is_null_sha1(one->sha1)) { |
0e5a7faa CR |
143 | struct cache_entry *ce; |
144 | ce = make_cache_entry(one->mode, one->sha1, one->path, | |
145 | 0, 0); | |
d09e2cd5 | 146 | if (!ce) |
b50a64e8 | 147 | die(_("make_cache_entry failed for path '%s'"), |
d09e2cd5 | 148 | one->path); |
0e5a7faa CR |
149 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | |
150 | ADD_CACHE_OK_TO_REPLACE); | |
151 | } else | |
152 | remove_file_from_cache(one->path); | |
153 | } | |
154 | } | |
155 | ||
18648e89 MZ |
156 | static int read_from_tree(const char **pathspec, unsigned char *tree_sha1, |
157 | int refresh_flags) | |
0e5a7faa | 158 | { |
620a6cd4 | 159 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
10746a36 | 160 | int index_fd; |
0e5a7faa CR |
161 | struct diff_options opt; |
162 | ||
163 | memset(&opt, 0, sizeof(opt)); | |
18648e89 | 164 | diff_tree_setup_paths(pathspec, &opt); |
0e5a7faa CR |
165 | opt.output_format = DIFF_FORMAT_CALLBACK; |
166 | opt.format_callback = update_index_from_diff; | |
167 | ||
620a6cd4 | 168 | index_fd = hold_locked_index(lock, 1); |
0e5a7faa CR |
169 | read_cache(); |
170 | if (do_diff_cache(tree_sha1, &opt)) | |
171 | return 1; | |
172 | diffcore_std(&opt); | |
173 | diff_flush(&opt); | |
03b69c76 | 174 | diff_tree_release_paths(&opt); |
2e7a9785 | 175 | |
b0320eaf | 176 | return update_index_refresh(index_fd, lock, refresh_flags); |
0e5a7faa CR |
177 | } |
178 | ||
d04520e3 JK |
179 | static void set_reflog_message(struct strbuf *sb, const char *action, |
180 | const char *rev) | |
0e5a7faa | 181 | { |
0e5a7faa | 182 | const char *rla = getenv("GIT_REFLOG_ACTION"); |
d04520e3 JK |
183 | |
184 | strbuf_reset(sb); | |
185 | if (rla) | |
186 | strbuf_addf(sb, "%s: %s", rla, action); | |
187 | else if (rev) | |
188 | strbuf_addf(sb, "reset: moving to %s", rev); | |
189 | else | |
190 | strbuf_addf(sb, "reset: %s", action); | |
0e5a7faa CR |
191 | } |
192 | ||
812d2a3d CC |
193 | static void die_if_unmerged_cache(int reset_type) |
194 | { | |
195 | if (is_merge() || read_cache() < 0 || unmerged_cache()) | |
8b2a57b6 ÆAB |
196 | die(_("Cannot do a %s reset in the middle of a merge."), |
197 | _(reset_type_names[reset_type])); | |
812d2a3d CC |
198 | |
199 | } | |
200 | ||
dca48cf5 | 201 | static const char **parse_args(const char **argv, const char *prefix, const char **rev_ret) |
0e5a7faa | 202 | { |
0e5a7faa | 203 | const char *rev = "HEAD"; |
39ea722d | 204 | unsigned char unused[20]; |
dfc8f39e JH |
205 | /* |
206 | * Possible arguments are: | |
207 | * | |
208 | * git reset [-opts] <rev> <paths>... | |
209 | * git reset [-opts] <rev> -- <paths>... | |
210 | * git reset [-opts] -- <paths>... | |
211 | * git reset [-opts] <paths>... | |
212 | * | |
dca48cf5 | 213 | * At this point, argv points immediately after [-opts]. |
dfc8f39e JH |
214 | */ |
215 | ||
dca48cf5 MZ |
216 | if (argv[0]) { |
217 | if (!strcmp(argv[0], "--")) { | |
218 | argv++; /* reset to HEAD, possibly with paths */ | |
219 | } else if (argv[1] && !strcmp(argv[1], "--")) { | |
220 | rev = argv[0]; | |
221 | argv += 2; | |
dfc8f39e JH |
222 | } |
223 | /* | |
dca48cf5 | 224 | * Otherwise, argv[0] could be either <rev> or <paths> and |
3ea3c215 | 225 | * has to be unambiguous. |
dfc8f39e | 226 | */ |
dca48cf5 | 227 | else if (!get_sha1_committish(argv[0], unused)) { |
dfc8f39e | 228 | /* |
dca48cf5 | 229 | * Ok, argv[0] looks like a rev; it should not |
dfc8f39e JH |
230 | * be a filename. |
231 | */ | |
dca48cf5 MZ |
232 | verify_non_filename(prefix, argv[0]); |
233 | rev = *argv++; | |
dfc8f39e JH |
234 | } else { |
235 | /* Otherwise we treat this as a filename */ | |
dca48cf5 | 236 | verify_filename(prefix, argv[0], 1); |
dfc8f39e JH |
237 | } |
238 | } | |
39ea722d | 239 | *rev_ret = rev; |
dca48cf5 | 240 | return argv[0] ? get_pathspec(prefix, argv) : NULL; |
39ea722d MZ |
241 | } |
242 | ||
243 | int cmd_reset(int argc, const char **argv, const char *prefix) | |
244 | { | |
245 | int reset_type = NONE, update_ref_status = 0, quiet = 0; | |
246 | int patch_mode = 0; | |
247 | const char *rev; | |
248 | unsigned char sha1[20], *orig = NULL, sha1_orig[20], | |
249 | *old_orig = NULL, sha1_old_orig[20]; | |
250 | const char **pathspec = NULL; | |
251 | struct commit *commit; | |
252 | struct strbuf msg = STRBUF_INIT; | |
253 | const struct option options[] = { | |
254 | OPT__QUIET(&quiet, N_("be quiet, only report errors")), | |
255 | OPT_SET_INT(0, "mixed", &reset_type, | |
256 | N_("reset HEAD and index"), MIXED), | |
257 | OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT), | |
258 | OPT_SET_INT(0, "hard", &reset_type, | |
259 | N_("reset HEAD, index and working tree"), HARD), | |
260 | OPT_SET_INT(0, "merge", &reset_type, | |
261 | N_("reset HEAD, index and working tree"), MERGE), | |
262 | OPT_SET_INT(0, "keep", &reset_type, | |
263 | N_("reset HEAD but keep local changes"), KEEP), | |
264 | OPT_BOOLEAN('p', "patch", &patch_mode, N_("select hunks interactively")), | |
265 | OPT_END() | |
266 | }; | |
267 | ||
268 | git_config(git_default_config, NULL); | |
269 | ||
270 | argc = parse_options(argc, argv, prefix, options, git_reset_usage, | |
271 | PARSE_OPT_KEEP_DASHDASH); | |
dca48cf5 | 272 | pathspec = parse_args(argv, prefix, &rev); |
0e5a7faa | 273 | |
13243c2c | 274 | if (get_sha1_committish(rev, sha1)) |
b50a64e8 | 275 | die(_("Failed to resolve '%s' as a valid ref."), rev); |
0e5a7faa | 276 | |
13243c2c JH |
277 | /* |
278 | * NOTE: As "git reset $treeish -- $path" should be usable on | |
279 | * any tree-ish, this is not strictly correct. We are not | |
280 | * moving the HEAD to any commit; we are merely resetting the | |
281 | * entries in the index to that of a treeish. | |
282 | */ | |
0e5a7faa CR |
283 | commit = lookup_commit_reference(sha1); |
284 | if (!commit) | |
b50a64e8 | 285 | die(_("Could not parse object '%s'."), rev); |
0e5a7faa CR |
286 | hashcpy(sha1, commit->object.sha1); |
287 | ||
d002ef4d TR |
288 | if (patch_mode) { |
289 | if (reset_type != NONE) | |
b50a64e8 | 290 | die(_("--patch is incompatible with --{hard,mixed,soft}")); |
18648e89 | 291 | return run_add_interactive(rev, "--patch=reset", pathspec); |
d002ef4d TR |
292 | } |
293 | ||
0e5a7faa CR |
294 | /* git reset tree [--] paths... can be used to |
295 | * load chosen paths from the tree into the index without | |
296 | * affecting the working tree nor HEAD. */ | |
18648e89 | 297 | if (pathspec) { |
0e5a7faa | 298 | if (reset_type == MIXED) |
b50a64e8 | 299 | warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead.")); |
0e5a7faa | 300 | else if (reset_type != NONE) |
8b2a57b6 ÆAB |
301 | die(_("Cannot do %s reset with paths."), |
302 | _(reset_type_names[reset_type])); | |
0e5a7faa CR |
303 | } |
304 | if (reset_type == NONE) | |
305 | reset_type = MIXED; /* by default */ | |
306 | ||
ab892a19 | 307 | if (reset_type != SOFT && reset_type != MIXED) |
cd0f0f68 | 308 | setup_work_tree(); |
49b9362f | 309 | |
2b06b0a0 | 310 | if (reset_type == MIXED && is_bare_repository()) |
8b2a57b6 ÆAB |
311 | die(_("%s reset is not allowed in a bare repository"), |
312 | _(reset_type_names[reset_type])); | |
2b06b0a0 | 313 | |
4f4ad3d9 MZ |
314 | if (pathspec) |
315 | return read_from_tree(pathspec, sha1, | |
316 | quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); | |
317 | ||
0e5a7faa CR |
318 | /* Soft reset does not touch the index file nor the working tree |
319 | * at all, but requires them in a good order. Other resets reset | |
320 | * the index file to the tree object we are switching to. */ | |
812d2a3d CC |
321 | if (reset_type == SOFT) |
322 | die_if_unmerged_cache(reset_type); | |
323 | else { | |
324 | int err; | |
325 | if (reset_type == KEEP) | |
326 | die_if_unmerged_cache(reset_type); | |
327 | err = reset_index_file(sha1, reset_type, quiet); | |
9bc454df CC |
328 | if (reset_type == KEEP) |
329 | err = err || reset_index_file(sha1, MIXED, quiet); | |
330 | if (err) | |
b50a64e8 | 331 | die(_("Could not reset index file to revision '%s'."), rev); |
0e5a7faa | 332 | } |
0e5a7faa CR |
333 | |
334 | /* Any resets update HEAD to the head being switched to, | |
335 | * saving the previous head in ORIG_HEAD before. */ | |
336 | if (!get_sha1("ORIG_HEAD", sha1_old_orig)) | |
337 | old_orig = sha1_old_orig; | |
338 | if (!get_sha1("HEAD", sha1_orig)) { | |
339 | orig = sha1_orig; | |
d04520e3 JK |
340 | set_reflog_message(&msg, "updating ORIG_HEAD", NULL); |
341 | update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR); | |
0e5a7faa CR |
342 | } |
343 | else if (old_orig) | |
eca35a25 | 344 | delete_ref("ORIG_HEAD", old_orig, 0); |
d04520e3 JK |
345 | set_reflog_message(&msg, "updating HEAD", rev); |
346 | update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR); | |
0e5a7faa CR |
347 | |
348 | switch (reset_type) { | |
349 | case HARD: | |
521b53e5 | 350 | if (!update_ref_status && !quiet) |
0e5a7faa CR |
351 | print_new_head_line(commit); |
352 | break; | |
353 | case SOFT: /* Nothing else to do. */ | |
354 | break; | |
355 | case MIXED: /* Report what has not been updated. */ | |
b0320eaf | 356 | update_index_refresh(0, NULL, |
43673fdd | 357 | quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN); |
0e5a7faa CR |
358 | break; |
359 | } | |
360 | ||
c369e7b8 | 361 | remove_branch_state(); |
0e5a7faa | 362 | |
d04520e3 | 363 | strbuf_release(&msg); |
0e5a7faa CR |
364 | |
365 | return update_ref_status; | |
366 | } |