Merge commit 'refs/top-bases/girocco' into girocco
[git/repo.git] / builtin-reset.c
blob16e6bb20f1af6a7a684a747b3c0cc26ceabe8ce7
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] [-q] [<commit>]",
24 "git reset [--mixed] <commit> [--] <paths>...",
25 NULL
28 static char *args_to_str(const char **argv)
30 char *buf = NULL;
31 unsigned long len, space = 0, nr = 0;
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;
41 ALLOC_GROW(buf, nr + 1, space);
42 buf[nr] = '\0';
44 return buf;
47 static inline int is_merge(void)
49 return !access(git_path("MERGE_HEAD"), F_OK);
52 static int reset_index_file(const unsigned char *sha1, int is_hard_reset, int quiet)
54 int i = 0;
55 const char *args[6];
57 args[i++] = "read-tree";
58 if (!quiet)
59 args[i++] = "-v";
60 args[i++] = "--reset";
61 if (is_hard_reset)
62 args[i++] = "-u";
63 args[i++] = sha1_to_hex(sha1);
64 args[i] = NULL;
66 return run_command_v_opt(args, RUN_GIT_CMD);
69 static void print_new_head_line(struct commit *commit)
71 const char *hex, *body;
73 hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
74 printf("HEAD is now at %s", hex);
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);
84 else
85 printf("\n");
88 static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
90 int result;
92 if (!index_lock) {
93 index_lock = xcalloc(1, sizeof(struct lock_file));
94 fd = hold_locked_index(index_lock, 1);
97 if (read_cache() < 0)
98 return error("Could not read index");
100 result = refresh_cache(flags) ? 1 : 0;
101 if (write_cache(fd, active_cache, active_nr) ||
102 commit_locked_index(index_lock))
103 return error ("Could not refresh index");
104 return result;
107 static void update_index_from_diff(struct diff_queue_struct *q,
108 struct diff_options *opt, void *data)
110 int i;
111 int *discard_flag = data;
113 /* do_diff_cache() mangled the index */
114 discard_cache();
115 *discard_flag = 1;
116 read_cache();
118 for (i = 0; i < q->nr; i++) {
119 struct diff_filespec *one = q->queue[i]->one;
120 if (one->mode) {
121 struct cache_entry *ce;
122 ce = make_cache_entry(one->mode, one->sha1, one->path,
123 0, 0);
124 if (!ce)
125 die("make_cache_entry failed for path '%s'",
126 one->path);
127 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
128 ADD_CACHE_OK_TO_REPLACE);
129 } else
130 remove_file_from_cache(one->path);
134 static int read_from_tree(const char *prefix, const char **argv,
135 unsigned char *tree_sha1, int refresh_flags)
137 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
138 int index_fd, index_was_discarded = 0;
139 struct diff_options opt;
141 memset(&opt, 0, sizeof(opt));
142 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
143 opt.output_format = DIFF_FORMAT_CALLBACK;
144 opt.format_callback = update_index_from_diff;
145 opt.format_callback_data = &index_was_discarded;
147 index_fd = hold_locked_index(lock, 1);
148 index_was_discarded = 0;
149 read_cache();
150 if (do_diff_cache(tree_sha1, &opt))
151 return 1;
152 diffcore_std(&opt);
153 diff_flush(&opt);
154 diff_tree_release_paths(&opt);
156 if (!index_was_discarded)
157 /* The index is still clobbered from do_diff_cache() */
158 discard_cache();
159 return update_index_refresh(index_fd, lock, refresh_flags);
162 static void prepend_reflog_action(const char *action, char *buf, size_t size)
164 const char *sep = ": ";
165 const char *rla = getenv("GIT_REFLOG_ACTION");
166 if (!rla)
167 rla = sep = "";
168 if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
169 warning("Reflog action message too long: %.*s...", 50, buf);
172 enum reset_type { MIXED, SOFT, HARD, NONE };
173 static const char *reset_type_names[] = { "mixed", "soft", "hard", NULL };
175 int cmd_reset(int argc, const char **argv, const char *prefix)
177 int i = 0, reset_type = NONE, update_ref_status = 0, quiet = 0;
178 const char *rev = "HEAD";
179 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
180 *old_orig = NULL, sha1_old_orig[20];
181 struct commit *commit;
182 char *reflog_action, msg[1024];
183 const struct option options[] = {
184 OPT_SET_INT(0, "mixed", &reset_type,
185 "reset HEAD and index", MIXED),
186 OPT_SET_INT(0, "soft", &reset_type, "reset only HEAD", SOFT),
187 OPT_SET_INT(0, "hard", &reset_type,
188 "reset HEAD, index and working tree", HARD),
189 OPT_BOOLEAN('q', NULL, &quiet,
190 "disable showing new HEAD in hard reset and progress message"),
191 OPT_END()
194 git_config(git_default_config, NULL);
196 argc = parse_options(argc, argv, options, git_reset_usage,
197 PARSE_OPT_KEEP_DASHDASH);
198 reflog_action = args_to_str(argv);
199 setenv("GIT_REFLOG_ACTION", reflog_action, 0);
202 * Possible arguments are:
204 * git reset [-opts] <rev> <paths>...
205 * git reset [-opts] <rev> -- <paths>...
206 * git reset [-opts] -- <paths>...
207 * git reset [-opts] <paths>...
209 * At this point, argv[i] points immediately after [-opts].
212 if (i < argc) {
213 if (!strcmp(argv[i], "--")) {
214 i++; /* reset to HEAD, possibly with paths */
215 } else if (i + 1 < argc && !strcmp(argv[i+1], "--")) {
216 rev = argv[i];
217 i += 2;
220 * Otherwise, argv[i] could be either <rev> or <paths> and
221 * has to be unambigous.
223 else if (!get_sha1(argv[i], sha1)) {
225 * Ok, argv[i] looks like a rev; it should not
226 * be a filename.
228 verify_non_filename(prefix, argv[i]);
229 rev = argv[i++];
230 } else {
231 /* Otherwise we treat this as a filename */
232 verify_filename(prefix, argv[i]);
236 if (get_sha1(rev, sha1))
237 die("Failed to resolve '%s' as a valid ref.", rev);
239 commit = lookup_commit_reference(sha1);
240 if (!commit)
241 die("Could not parse object '%s'.", rev);
242 hashcpy(sha1, commit->object.sha1);
244 /* git reset tree [--] paths... can be used to
245 * load chosen paths from the tree into the index without
246 * affecting the working tree nor HEAD. */
247 if (i < argc) {
248 if (reset_type == MIXED)
249 warning("--mixed option is deprecated with paths.");
250 else if (reset_type != NONE)
251 die("Cannot do %s reset with paths.",
252 reset_type_names[reset_type]);
253 return read_from_tree(prefix, argv + i, sha1,
254 quiet ? REFRESH_QUIET : REFRESH_SAY_CHANGED);
256 if (reset_type == NONE)
257 reset_type = MIXED; /* by default */
259 if (reset_type == HARD && is_bare_repository())
260 die("hard reset makes no sense in a bare repository");
262 /* Soft reset does not touch the index file nor the working tree
263 * at all, but requires them in a good order. Other resets reset
264 * the index file to the tree object we are switching to. */
265 if (reset_type == SOFT) {
266 if (is_merge() || read_cache() < 0 || unmerged_cache())
267 die("Cannot do a soft reset in the middle of a merge.");
269 else if (reset_index_file(sha1, (reset_type == HARD), quiet))
270 die("Could not reset index file to revision '%s'.", rev);
272 /* Any resets update HEAD to the head being switched to,
273 * saving the previous head in ORIG_HEAD before. */
274 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
275 old_orig = sha1_old_orig;
276 if (!get_sha1("HEAD", sha1_orig)) {
277 orig = sha1_orig;
278 prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
279 update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
281 else if (old_orig)
282 delete_ref("ORIG_HEAD", old_orig);
283 prepend_reflog_action("updating HEAD", msg, sizeof(msg));
284 update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
286 switch (reset_type) {
287 case HARD:
288 if (!update_ref_status && !quiet)
289 print_new_head_line(commit);
290 break;
291 case SOFT: /* Nothing else to do. */
292 break;
293 case MIXED: /* Report what has not been updated. */
294 update_index_refresh(0, NULL,
295 quiet ? REFRESH_QUIET : REFRESH_SAY_CHANGED);
296 break;
299 remove_branch_state();
301 free(reflog_action);
303 return update_ref_status;