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