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