Merge branch 'maint'
[alt-git.git] / builtin-reset.c
blob0fc0b0743f946dcfbab0257f9c69963a779ed38d
1 /*
2 * "git reset" builtin command
4 * Copyright (c) 2007 Carlos Rica
6 * Based on git-reset.sh, which is
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"
19 #include "branch.h"
20 #include "parse-options.h"
22 static const char * const git_reset_usage[] = {
23 "git reset [--mixed | --soft | --hard | --merge] [-q] [<commit>]",
24 "git reset [--mixed] <commit> [--] <paths>...",
25 NULL
28 enum reset_type { MIXED, SOFT, HARD, MERGE, NONE };
29 static const char *reset_type_names[] = { "mixed", "soft", "hard", "merge", NULL };
31 static char *args_to_str(const char **argv)
33 char *buf = NULL;
34 unsigned long len, space = 0, nr = 0;
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;
44 ALLOC_GROW(buf, nr + 1, space);
45 buf[nr] = '\0';
47 return buf;
50 static inline int is_merge(void)
52 return !access(git_path("MERGE_HEAD"), F_OK);
55 static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet)
57 int i = 0;
58 const char *args[6];
60 args[i++] = "read-tree";
61 if (!quiet)
62 args[i++] = "-v";
63 switch (reset_type) {
64 case MERGE:
65 args[i++] = "-u";
66 args[i++] = "-m";
67 break;
68 case HARD:
69 args[i++] = "-u";
70 /* fallthrough */
71 default:
72 args[i++] = "--reset";
74 args[i++] = sha1_to_hex(sha1);
75 args[i] = NULL;
77 return run_command_v_opt(args, RUN_GIT_CMD);
80 static void print_new_head_line(struct commit *commit)
82 const char *hex, *body;
84 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
85 printf("HEAD is now at %s", hex);
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);
95 else
96 printf("\n");
99 static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
101 int result;
103 if (!index_lock) {
104 index_lock = xcalloc(1, sizeof(struct lock_file));
105 fd = hold_locked_index(index_lock, 1);
108 if (read_cache() < 0)
109 return error("Could not read index");
111 result = refresh_index(&the_index, (flags), NULL, NULL,
112 "Unstaged changes after reset:") ? 1 : 0;
113 if (write_cache(fd, active_cache, active_nr) ||
114 commit_locked_index(index_lock))
115 return error ("Could not refresh index");
116 return result;
119 static void update_index_from_diff(struct diff_queue_struct *q,
120 struct diff_options *opt, void *data)
122 int i;
123 int *discard_flag = data;
125 /* do_diff_cache() mangled the index */
126 discard_cache();
127 *discard_flag = 1;
128 read_cache();
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);
136 if (!ce)
137 die("make_cache_entry failed for path '%s'",
138 one->path);
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);
146 static int read_from_tree(const char *prefix, const char **argv,
147 unsigned char *tree_sha1, int refresh_flags)
149 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
150 int index_fd, index_was_discarded = 0;
151 struct diff_options opt;
153 memset(&opt, 0, sizeof(opt));
154 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
155 opt.output_format = DIFF_FORMAT_CALLBACK;
156 opt.format_callback = update_index_from_diff;
157 opt.format_callback_data = &index_was_discarded;
159 index_fd = hold_locked_index(lock, 1);
160 index_was_discarded = 0;
161 read_cache();
162 if (do_diff_cache(tree_sha1, &opt))
163 return 1;
164 diffcore_std(&opt);
165 diff_flush(&opt);
166 diff_tree_release_paths(&opt);
168 if (!index_was_discarded)
169 /* The index is still clobbered from do_diff_cache() */
170 discard_cache();
171 return update_index_refresh(index_fd, lock, refresh_flags);
174 static void prepend_reflog_action(const char *action, char *buf, size_t size)
176 const char *sep = ": ";
177 const char *rla = getenv("GIT_REFLOG_ACTION");
178 if (!rla)
179 rla = sep = "";
180 if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
181 warning("Reflog action message too long: %.*s...", 50, buf);
184 int cmd_reset(int argc, const char **argv, const char *prefix)
186 int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
187 const char *rev = "HEAD";
188 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
189 *old_orig = NULL, sha1_old_orig[20];
190 struct commit *commit;
191 char *reflog_action, msg[1024];
192 const struct option options[] = {
193 OPT_SET_INT(0, "mixed", &reset_type,
194 "reset HEAD and index", MIXED),
195 OPT_SET_INT(0, "soft", &reset_type, "reset only HEAD", SOFT),
196 OPT_SET_INT(0, "hard", &reset_type,
197 "reset HEAD, index and working tree", HARD),
198 OPT_SET_INT(0, "merge", &reset_type,
199 "reset HEAD, index and working tree", MERGE),
200 OPT_BOOLEAN('q', NULL, &quiet,
201 "disable showing new HEAD in hard reset and progress message"),
202 OPT_END()
205 git_config(git_default_config, NULL);
207 argc = parse_options(argc, argv, prefix, options, git_reset_usage,
208 PARSE_OPT_KEEP_DASHDASH);
209 reflog_action = args_to_str(argv);
210 setenv("GIT_REFLOG_ACTION", reflog_action, 0);
213 * Possible arguments are:
215 * git reset [-opts] <rev> <paths>...
216 * git reset [-opts] <rev> -- <paths>...
217 * git reset [-opts] -- <paths>...
218 * git reset [-opts] <paths>...
220 * At this point, argv[i] points immediately after [-opts].
223 if (i < argc) {
224 if (!strcmp(argv[i], "--")) {
225 i++; /* reset to HEAD, possibly with paths */
226 } else if (i + 1 < argc && !strcmp(argv[i+1], "--")) {
227 rev = argv[i];
228 i += 2;
231 * Otherwise, argv[i] could be either <rev> or <paths> and
232 * has to be unambiguous.
234 else if (!get_sha1(argv[i], sha1)) {
236 * Ok, argv[i] looks like a rev; it should not
237 * be a filename.
239 verify_non_filename(prefix, argv[i]);
240 rev = argv[i++];
241 } else {
242 /* Otherwise we treat this as a filename */
243 verify_filename(prefix, argv[i]);
247 if (get_sha1(rev, sha1))
248 die("Failed to resolve '%s' as a valid ref.", rev);
250 commit = lookup_commit_reference(sha1);
251 if (!commit)
252 die("Could not parse object '%s'.", rev);
253 hashcpy(sha1, commit->object.sha1);
255 /* git reset tree [--] paths... can be used to
256 * load chosen paths from the tree into the index without
257 * affecting the working tree nor HEAD. */
258 if (i < argc) {
259 if (reset_type == MIXED)
260 warning("--mixed option is deprecated with paths.");
261 else if (reset_type != NONE)
262 die("Cannot do %s reset with paths.",
263 reset_type_names[reset_type]);
264 return read_from_tree(prefix, argv + i, sha1,
265 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
267 if (reset_type == NONE)
268 reset_type = MIXED; /* by default */
270 if (reset_type == HARD && is_bare_repository())
271 die("hard reset makes no sense in a bare repository");
273 /* Soft reset does not touch the index file nor the working tree
274 * at all, but requires them in a good order. Other resets reset
275 * the index file to the tree object we are switching to. */
276 if (reset_type == SOFT) {
277 if (is_merge() || read_cache() < 0 || unmerged_cache())
278 die("Cannot do a soft reset in the middle of a merge.");
280 else if (reset_index_file(sha1, reset_type, quiet))
281 die("Could not reset index file to revision '%s'.", rev);
283 /* Any resets update HEAD to the head being switched to,
284 * saving the previous head in ORIG_HEAD before. */
285 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
286 old_orig = sha1_old_orig;
287 if (!get_sha1("HEAD", sha1_orig)) {
288 orig = sha1_orig;
289 prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
290 update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
292 else if (old_orig)
293 delete_ref("ORIG_HEAD", old_orig, 0);
294 prepend_reflog_action("updating HEAD", msg, sizeof(msg));
295 update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
297 switch (reset_type) {
298 case HARD:
299 if (!update_ref_status && !quiet)
300 print_new_head_line(commit);
301 break;
302 case SOFT: /* Nothing else to do. */
303 break;
304 case MIXED: /* Report what has not been updated. */
305 update_index_refresh(0, NULL,
306 quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
307 break;
310 remove_branch_state();
312 free(reflog_action);
314 return update_ref_status;