Add test for git rebase --abort
[git/dscho.git] / builtin-reset.c
blobaf0037ec6e3456ccd688da6375c76db4a59f099a
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"
21 static const char builtin_reset_usage[] =
22 "git-reset [--mixed | --soft | --hard] [-q] [<commit-ish>] [ [--] <paths>...]";
24 static char *args_to_str(const char **argv)
26 char *buf = NULL;
27 unsigned long len, space = 0, nr = 0;
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;
37 ALLOC_GROW(buf, nr + 1, space);
38 buf[nr] = '\0';
40 return buf;
43 static inline int is_merge(void)
45 return !access(git_path("MERGE_HEAD"), F_OK);
48 static int reset_index_file(const unsigned char *sha1, int is_hard_reset)
50 int i = 0;
51 const char *args[6];
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;
61 return run_command_v_opt(args, RUN_GIT_CMD);
64 static void print_new_head_line(struct commit *commit)
66 const char *hex, *dots = "...", *body;
68 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
69 if (!hex) {
70 hex = sha1_to_hex(commit->object.sha1);
71 dots = "";
73 printf("HEAD is now at %s%s", hex, dots);
74 body = strstr(commit->buffer, "\n\n");
75 if (body) {
76 const char *eol;
77 size_t len;
78 body += 2;
79 eol = strchr(body, '\n');
80 len = eol ? eol - body : strlen(body);
81 printf(" %.*s\n", (int) len, body);
83 else
84 printf("\n");
87 static int update_index_refresh(int fd, struct lock_file *index_lock)
89 int result;
91 if (!index_lock) {
92 index_lock = xcalloc(1, sizeof(struct lock_file));
93 fd = hold_locked_index(index_lock, 1);
96 if (read_cache() < 0)
97 return error("Could not read index");
98 result = refresh_cache(0) ? 1 : 0;
99 if (write_cache(fd, active_cache, active_nr) ||
100 commit_locked_index(index_lock))
101 return error ("Could not refresh index");
102 return result;
105 static void update_index_from_diff(struct diff_queue_struct *q,
106 struct diff_options *opt, void *data)
108 int i;
109 int *discard_flag = data;
111 /* do_diff_cache() mangled the index */
112 discard_cache();
113 *discard_flag = 1;
114 read_cache();
116 for (i = 0; i < q->nr; i++) {
117 struct diff_filespec *one = q->queue[i]->one;
118 if (one->mode) {
119 struct cache_entry *ce;
120 ce = make_cache_entry(one->mode, one->sha1, one->path,
121 0, 0);
122 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
123 ADD_CACHE_OK_TO_REPLACE);
124 } else
125 remove_file_from_cache(one->path);
129 static int read_from_tree(const char *prefix, const char **argv,
130 unsigned char *tree_sha1)
132 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
133 int index_fd, index_was_discarded = 0;
134 struct diff_options opt;
136 memset(&opt, 0, sizeof(opt));
137 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
138 opt.output_format = DIFF_FORMAT_CALLBACK;
139 opt.format_callback = update_index_from_diff;
140 opt.format_callback_data = &index_was_discarded;
142 index_fd = hold_locked_index(lock, 1);
143 index_was_discarded = 0;
144 read_cache();
145 if (do_diff_cache(tree_sha1, &opt))
146 return 1;
147 diffcore_std(&opt);
148 diff_flush(&opt);
149 diff_tree_release_paths(&opt);
151 if (!index_was_discarded)
152 /* The index is still clobbered from do_diff_cache() */
153 discard_cache();
154 return update_index_refresh(index_fd, lock);
157 static void prepend_reflog_action(const char *action, char *buf, size_t size)
159 const char *sep = ": ";
160 const char *rla = getenv("GIT_REFLOG_ACTION");
161 if (!rla)
162 rla = sep = "";
163 if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
164 warning("Reflog action message too long: %.*s...", 50, buf);
167 enum reset_type { MIXED, SOFT, HARD, NONE };
168 static const char *reset_type_names[] = { "mixed", "soft", "hard", NULL };
170 int cmd_reset(int argc, const char **argv, const char *prefix)
172 int i = 1, reset_type = NONE, update_ref_status = 0, quiet = 0;
173 const char *rev = "HEAD";
174 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
175 *old_orig = NULL, sha1_old_orig[20];
176 struct commit *commit;
177 char *reflog_action, msg[1024];
179 git_config(git_default_config);
181 reflog_action = args_to_str(argv);
182 setenv("GIT_REFLOG_ACTION", reflog_action, 0);
184 while (i < argc) {
185 if (!strcmp(argv[i], "--mixed")) {
186 reset_type = MIXED;
187 i++;
189 else if (!strcmp(argv[i], "--soft")) {
190 reset_type = SOFT;
191 i++;
193 else if (!strcmp(argv[i], "--hard")) {
194 reset_type = HARD;
195 i++;
197 else if (!strcmp(argv[i], "-q")) {
198 quiet = 1;
199 i++;
201 else
202 break;
205 if (i < argc && argv[i][0] != '-')
206 rev = argv[i++];
208 if (get_sha1(rev, sha1))
209 die("Failed to resolve '%s' as a valid ref.", rev);
211 commit = lookup_commit_reference(sha1);
212 if (!commit)
213 die("Could not parse object '%s'.", rev);
214 hashcpy(sha1, commit->object.sha1);
216 if (i < argc && !strcmp(argv[i], "--"))
217 i++;
218 else if (i < argc && argv[i][0] == '-')
219 usage(builtin_reset_usage);
221 /* git reset tree [--] paths... can be used to
222 * load chosen paths from the tree into the index without
223 * affecting the working tree nor HEAD. */
224 if (i < argc) {
225 if (reset_type == MIXED)
226 warning("--mixed option is deprecated with paths.");
227 else if (reset_type != NONE)
228 die("Cannot do %s reset with paths.",
229 reset_type_names[reset_type]);
230 return read_from_tree(prefix, argv + i, sha1);
232 if (reset_type == NONE)
233 reset_type = MIXED; /* by default */
235 if (reset_type == HARD && is_bare_repository())
236 die("hard reset makes no sense in a bare repository");
238 /* Soft reset does not touch the index file nor the working tree
239 * at all, but requires them in a good order. Other resets reset
240 * the index file to the tree object we are switching to. */
241 if (reset_type == SOFT) {
242 if (is_merge() || read_cache() < 0 || unmerged_cache())
243 die("Cannot do a soft reset in the middle of a merge.");
245 else if (reset_index_file(sha1, (reset_type == HARD)))
246 die("Could not reset index file to revision '%s'.", rev);
248 /* Any resets update HEAD to the head being switched to,
249 * saving the previous head in ORIG_HEAD before. */
250 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
251 old_orig = sha1_old_orig;
252 if (!get_sha1("HEAD", sha1_orig)) {
253 orig = sha1_orig;
254 prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
255 update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
257 else if (old_orig)
258 delete_ref("ORIG_HEAD", old_orig);
259 prepend_reflog_action("updating HEAD", msg, sizeof(msg));
260 update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
262 switch (reset_type) {
263 case HARD:
264 if (!update_ref_status && !quiet)
265 print_new_head_line(commit);
266 break;
267 case SOFT: /* Nothing else to do. */
268 break;
269 case MIXED: /* Report what has not been updated. */
270 update_index_refresh(0, NULL);
271 break;
274 remove_branch_state();
276 free(reflog_action);
278 return update_ref_status;