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