]>
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 CR |
22 | static const char * const git_reset_usage[] = { |
23 | "git-reset [--mixed | --soft | --hard] [-q] [<commit>]", | |
24 | "git-reset [--mixed] <commit> [--] <paths>...", | |
25 | NULL | |
26 | }; | |
0e5a7faa CR |
27 | |
28 | static char *args_to_str(const char **argv) | |
29 | { | |
30 | char *buf = NULL; | |
31 | unsigned long len, space = 0, nr = 0; | |
32 | ||
33 | for (; *argv; argv++) { | |
34 | len = strlen(*argv); | |
35 | ALLOC_GROW(buf, nr + 1 + len, space); | |
36 | if (nr) | |
37 | buf[nr++] = ' '; | |
38 | memcpy(buf + nr, *argv, len); | |
39 | nr += len; | |
40 | } | |
41 | ALLOC_GROW(buf, nr + 1, space); | |
42 | buf[nr] = '\0'; | |
43 | ||
44 | return buf; | |
45 | } | |
46 | ||
47 | static inline int is_merge(void) | |
48 | { | |
49 | return !access(git_path("MERGE_HEAD"), F_OK); | |
50 | } | |
51 | ||
5aa965a0 | 52 | static int reset_index_file(const unsigned char *sha1, int is_hard_reset, int quiet) |
0e5a7faa CR |
53 | { |
54 | int i = 0; | |
55 | const char *args[6]; | |
56 | ||
57 | args[i++] = "read-tree"; | |
5aa965a0 JB |
58 | if (!quiet) |
59 | args[i++] = "-v"; | |
0e5a7faa CR |
60 | args[i++] = "--reset"; |
61 | if (is_hard_reset) | |
62 | args[i++] = "-u"; | |
63 | args[i++] = sha1_to_hex(sha1); | |
64 | args[i] = NULL; | |
65 | ||
66 | return run_command_v_opt(args, RUN_GIT_CMD); | |
67 | } | |
68 | ||
69 | static void print_new_head_line(struct commit *commit) | |
70 | { | |
2efb3b06 | 71 | const char *hex, *body; |
0e5a7faa CR |
72 | |
73 | hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); | |
2efb3b06 | 74 | printf("HEAD is now at %s", hex); |
0e5a7faa CR |
75 | body = strstr(commit->buffer, "\n\n"); |
76 | if (body) { | |
77 | const char *eol; | |
78 | size_t len; | |
79 | body += 2; | |
80 | eol = strchr(body, '\n'); | |
81 | len = eol ? eol - body : strlen(body); | |
82 | printf(" %.*s\n", (int) len, body); | |
83 | } | |
84 | else | |
85 | printf("\n"); | |
86 | } | |
87 | ||
620a6cd4 | 88 | static int update_index_refresh(int fd, struct lock_file *index_lock) |
0e5a7faa | 89 | { |
620a6cd4 | 90 | int result; |
0e5a7faa | 91 | |
620a6cd4 JS |
92 | if (!index_lock) { |
93 | index_lock = xcalloc(1, sizeof(struct lock_file)); | |
94 | fd = hold_locked_index(index_lock, 1); | |
95 | } | |
0e5a7faa | 96 | |
620a6cd4 JS |
97 | if (read_cache() < 0) |
98 | return error("Could not read index"); | |
99 | result = refresh_cache(0) ? 1 : 0; | |
100 | if (write_cache(fd, active_cache, active_nr) || | |
620a6cd4 JS |
101 | commit_locked_index(index_lock)) |
102 | return error ("Could not refresh index"); | |
103 | return result; | |
104 | } | |
2e7a9785 | 105 | |
0e5a7faa CR |
106 | static void update_index_from_diff(struct diff_queue_struct *q, |
107 | struct diff_options *opt, void *data) | |
108 | { | |
109 | int i; | |
620a6cd4 | 110 | int *discard_flag = data; |
0e5a7faa CR |
111 | |
112 | /* do_diff_cache() mangled the index */ | |
113 | discard_cache(); | |
620a6cd4 | 114 | *discard_flag = 1; |
0e5a7faa CR |
115 | read_cache(); |
116 | ||
117 | for (i = 0; i < q->nr; i++) { | |
118 | struct diff_filespec *one = q->queue[i]->one; | |
119 | if (one->mode) { | |
120 | struct cache_entry *ce; | |
121 | ce = make_cache_entry(one->mode, one->sha1, one->path, | |
122 | 0, 0); | |
123 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | | |
124 | ADD_CACHE_OK_TO_REPLACE); | |
125 | } else | |
126 | remove_file_from_cache(one->path); | |
127 | } | |
128 | } | |
129 | ||
130 | static int read_from_tree(const char *prefix, const char **argv, | |
131 | unsigned char *tree_sha1) | |
132 | { | |
620a6cd4 JS |
133 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
134 | int index_fd, index_was_discarded = 0; | |
0e5a7faa CR |
135 | struct diff_options opt; |
136 | ||
137 | memset(&opt, 0, sizeof(opt)); | |
138 | diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt); | |
139 | opt.output_format = DIFF_FORMAT_CALLBACK; | |
140 | opt.format_callback = update_index_from_diff; | |
620a6cd4 | 141 | opt.format_callback_data = &index_was_discarded; |
0e5a7faa | 142 | |
620a6cd4 JS |
143 | index_fd = hold_locked_index(lock, 1); |
144 | index_was_discarded = 0; | |
0e5a7faa CR |
145 | read_cache(); |
146 | if (do_diff_cache(tree_sha1, &opt)) | |
147 | return 1; | |
148 | diffcore_std(&opt); | |
149 | diff_flush(&opt); | |
03b69c76 | 150 | diff_tree_release_paths(&opt); |
2e7a9785 | 151 | |
620a6cd4 JS |
152 | if (!index_was_discarded) |
153 | /* The index is still clobbered from do_diff_cache() */ | |
154 | discard_cache(); | |
155 | return update_index_refresh(index_fd, lock); | |
0e5a7faa CR |
156 | } |
157 | ||
158 | static void prepend_reflog_action(const char *action, char *buf, size_t size) | |
159 | { | |
160 | const char *sep = ": "; | |
161 | const char *rla = getenv("GIT_REFLOG_ACTION"); | |
162 | if (!rla) | |
163 | rla = sep = ""; | |
164 | if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size) | |
165 | warning("Reflog action message too long: %.*s...", 50, buf); | |
166 | } | |
167 | ||
168 | enum reset_type { MIXED, SOFT, HARD, NONE }; | |
538dfe73 | 169 | static const char *reset_type_names[] = { "mixed", "soft", "hard", NULL }; |
0e5a7faa CR |
170 | |
171 | int cmd_reset(int argc, const char **argv, const char *prefix) | |
172 | { | |
5eee6b28 | 173 | int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0; |
0e5a7faa CR |
174 | const char *rev = "HEAD"; |
175 | unsigned char sha1[20], *orig = NULL, sha1_orig[20], | |
176 | *old_orig = NULL, sha1_old_orig[20]; | |
177 | struct commit *commit; | |
178 | char *reflog_action, msg[1024]; | |
5eee6b28 CR |
179 | const struct option options[] = { |
180 | OPT_SET_INT(0, "mixed", &reset_type, | |
181 | "reset HEAD and index", MIXED), | |
182 | OPT_SET_INT(0, "soft", &reset_type, "reset only HEAD", SOFT), | |
183 | OPT_SET_INT(0, "hard", &reset_type, | |
184 | "reset HEAD, index and working tree", HARD), | |
185 | OPT_BOOLEAN('q', NULL, &quiet, | |
5aa965a0 | 186 | "disable showing new HEAD in hard reset and progress message"), |
5eee6b28 CR |
187 | OPT_END() |
188 | }; | |
0e5a7faa | 189 | |
ef90d6d4 | 190 | git_config(git_default_config, NULL); |
0e5a7faa | 191 | |
5eee6b28 CR |
192 | argc = parse_options(argc, argv, options, git_reset_usage, |
193 | PARSE_OPT_KEEP_DASHDASH); | |
0e5a7faa CR |
194 | reflog_action = args_to_str(argv); |
195 | setenv("GIT_REFLOG_ACTION", reflog_action, 0); | |
196 | ||
5eee6b28 | 197 | if (i < argc && strcmp(argv[i], "--")) |
0e5a7faa CR |
198 | rev = argv[i++]; |
199 | ||
200 | if (get_sha1(rev, sha1)) | |
201 | die("Failed to resolve '%s' as a valid ref.", rev); | |
202 | ||
203 | commit = lookup_commit_reference(sha1); | |
204 | if (!commit) | |
205 | die("Could not parse object '%s'.", rev); | |
206 | hashcpy(sha1, commit->object.sha1); | |
207 | ||
208 | if (i < argc && !strcmp(argv[i], "--")) | |
209 | i++; | |
0e5a7faa CR |
210 | |
211 | /* git reset tree [--] paths... can be used to | |
212 | * load chosen paths from the tree into the index without | |
213 | * affecting the working tree nor HEAD. */ | |
214 | if (i < argc) { | |
215 | if (reset_type == MIXED) | |
216 | warning("--mixed option is deprecated with paths."); | |
217 | else if (reset_type != NONE) | |
218 | die("Cannot do %s reset with paths.", | |
219 | reset_type_names[reset_type]); | |
620a6cd4 | 220 | return read_from_tree(prefix, argv + i, sha1); |
0e5a7faa CR |
221 | } |
222 | if (reset_type == NONE) | |
223 | reset_type = MIXED; /* by default */ | |
224 | ||
49b9362f JK |
225 | if (reset_type == HARD && is_bare_repository()) |
226 | die("hard reset makes no sense in a bare repository"); | |
227 | ||
0e5a7faa CR |
228 | /* Soft reset does not touch the index file nor the working tree |
229 | * at all, but requires them in a good order. Other resets reset | |
230 | * the index file to the tree object we are switching to. */ | |
231 | if (reset_type == SOFT) { | |
94a5728c | 232 | if (is_merge() || read_cache() < 0 || unmerged_cache()) |
0e5a7faa CR |
233 | die("Cannot do a soft reset in the middle of a merge."); |
234 | } | |
5aa965a0 | 235 | else if (reset_index_file(sha1, (reset_type == HARD), quiet)) |
0e5a7faa CR |
236 | die("Could not reset index file to revision '%s'.", rev); |
237 | ||
238 | /* Any resets update HEAD to the head being switched to, | |
239 | * saving the previous head in ORIG_HEAD before. */ | |
240 | if (!get_sha1("ORIG_HEAD", sha1_old_orig)) | |
241 | old_orig = sha1_old_orig; | |
242 | if (!get_sha1("HEAD", sha1_orig)) { | |
243 | orig = sha1_orig; | |
244 | prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg)); | |
245 | update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR); | |
246 | } | |
247 | else if (old_orig) | |
248 | delete_ref("ORIG_HEAD", old_orig); | |
249 | prepend_reflog_action("updating HEAD", msg, sizeof(msg)); | |
250 | update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR); | |
251 | ||
252 | switch (reset_type) { | |
253 | case HARD: | |
521b53e5 | 254 | if (!update_ref_status && !quiet) |
0e5a7faa CR |
255 | print_new_head_line(commit); |
256 | break; | |
257 | case SOFT: /* Nothing else to do. */ | |
258 | break; | |
259 | case MIXED: /* Report what has not been updated. */ | |
620a6cd4 | 260 | update_index_refresh(0, NULL); |
0e5a7faa CR |
261 | break; |
262 | } | |
263 | ||
c369e7b8 | 264 | remove_branch_state(); |
0e5a7faa CR |
265 | |
266 | free(reflog_action); | |
267 | ||
268 | return update_ref_status; | |
269 | } |