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