git-reset: refuse to do hard reset in a bare repository
[git/git.git] / builtin-reset.c
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
20 static const char builtin_reset_usage[] =
21 "git-reset [--mixed | --soft | --hard] [-q] [<commit-ish>] [ [--] <paths>...]";
22
23 static 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
42 static inline int is_merge(void)
43 {
44 return !access(git_path("MERGE_HEAD"), F_OK);
45 }
46
47 static int unmerged_files(void)
48 {
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;
57 }
58
59 static 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
75 static 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
98 static int update_index_refresh(int fd, struct lock_file *index_lock)
99 {
100 int result;
101
102 if (!index_lock) {
103 index_lock = xcalloc(1, sizeof(struct lock_file));
104 fd = hold_locked_index(index_lock, 1);
105 }
106
107 if (read_cache() < 0)
108 return error("Could not read index");
109 result = refresh_cache(0) ? 1 : 0;
110 if (write_cache(fd, active_cache, active_nr) ||
111 close(fd) ||
112 commit_locked_index(index_lock))
113 return error ("Could not refresh index");
114 return result;
115 }
116
117 static void update_index_from_diff(struct diff_queue_struct *q,
118 struct diff_options *opt, void *data)
119 {
120 int i;
121 int *discard_flag = data;
122
123 /* do_diff_cache() mangled the index */
124 discard_cache();
125 *discard_flag = 1;
126 read_cache();
127
128 for (i = 0; i < q->nr; i++) {
129 struct diff_filespec *one = q->queue[i]->one;
130 if (one->mode) {
131 struct cache_entry *ce;
132 ce = make_cache_entry(one->mode, one->sha1, one->path,
133 0, 0);
134 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD |
135 ADD_CACHE_OK_TO_REPLACE);
136 } else
137 remove_file_from_cache(one->path);
138 }
139 }
140
141 static int read_from_tree(const char *prefix, const char **argv,
142 unsigned char *tree_sha1)
143 {
144 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
145 int index_fd, index_was_discarded = 0;
146 struct diff_options opt;
147
148 memset(&opt, 0, sizeof(opt));
149 diff_tree_setup_paths(get_pathspec(prefix, (const char **)argv), &opt);
150 opt.output_format = DIFF_FORMAT_CALLBACK;
151 opt.format_callback = update_index_from_diff;
152 opt.format_callback_data = &index_was_discarded;
153
154 index_fd = hold_locked_index(lock, 1);
155 index_was_discarded = 0;
156 read_cache();
157 if (do_diff_cache(tree_sha1, &opt))
158 return 1;
159 diffcore_std(&opt);
160 diff_flush(&opt);
161 diff_tree_release_paths(&opt);
162
163 if (!index_was_discarded)
164 /* The index is still clobbered from do_diff_cache() */
165 discard_cache();
166 return update_index_refresh(index_fd, lock);
167 }
168
169 static void prepend_reflog_action(const char *action, char *buf, size_t size)
170 {
171 const char *sep = ": ";
172 const char *rla = getenv("GIT_REFLOG_ACTION");
173 if (!rla)
174 rla = sep = "";
175 if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
176 warning("Reflog action message too long: %.*s...", 50, buf);
177 }
178
179 enum reset_type { MIXED, SOFT, HARD, NONE };
180 static const char *reset_type_names[] = { "mixed", "soft", "hard", NULL };
181
182 int cmd_reset(int argc, const char **argv, const char *prefix)
183 {
184 int i = 1, reset_type = NONE, update_ref_status = 0, quiet = 0;
185 const char *rev = "HEAD";
186 unsigned char sha1[20], *orig = NULL, sha1_orig[20],
187 *old_orig = NULL, sha1_old_orig[20];
188 struct commit *commit;
189 char *reflog_action, msg[1024];
190
191 git_config(git_default_config);
192
193 reflog_action = args_to_str(argv);
194 setenv("GIT_REFLOG_ACTION", reflog_action, 0);
195
196 while (i < argc) {
197 if (!strcmp(argv[i], "--mixed")) {
198 reset_type = MIXED;
199 i++;
200 }
201 else if (!strcmp(argv[i], "--soft")) {
202 reset_type = SOFT;
203 i++;
204 }
205 else if (!strcmp(argv[i], "--hard")) {
206 reset_type = HARD;
207 i++;
208 }
209 else if (!strcmp(argv[i], "-q")) {
210 quiet = 1;
211 i++;
212 }
213 else
214 break;
215 }
216
217 if (i < argc && argv[i][0] != '-')
218 rev = argv[i++];
219
220 if (get_sha1(rev, sha1))
221 die("Failed to resolve '%s' as a valid ref.", rev);
222
223 commit = lookup_commit_reference(sha1);
224 if (!commit)
225 die("Could not parse object '%s'.", rev);
226 hashcpy(sha1, commit->object.sha1);
227
228 if (i < argc && !strcmp(argv[i], "--"))
229 i++;
230 else if (i < argc && argv[i][0] == '-')
231 usage(builtin_reset_usage);
232
233 /* git reset tree [--] paths... can be used to
234 * load chosen paths from the tree into the index without
235 * affecting the working tree nor HEAD. */
236 if (i < argc) {
237 if (reset_type == MIXED)
238 warning("--mixed option is deprecated with paths.");
239 else if (reset_type != NONE)
240 die("Cannot do %s reset with paths.",
241 reset_type_names[reset_type]);
242 return read_from_tree(prefix, argv + i, sha1);
243 }
244 if (reset_type == NONE)
245 reset_type = MIXED; /* by default */
246
247 if (reset_type == HARD && is_bare_repository())
248 die("hard reset makes no sense in a bare repository");
249
250 /* Soft reset does not touch the index file nor the working tree
251 * at all, but requires them in a good order. Other resets reset
252 * the index file to the tree object we are switching to. */
253 if (reset_type == SOFT) {
254 if (is_merge() || unmerged_files())
255 die("Cannot do a soft reset in the middle of a merge.");
256 }
257 else if (reset_index_file(sha1, (reset_type == HARD)))
258 die("Could not reset index file to revision '%s'.", rev);
259
260 /* Any resets update HEAD to the head being switched to,
261 * saving the previous head in ORIG_HEAD before. */
262 if (!get_sha1("ORIG_HEAD", sha1_old_orig))
263 old_orig = sha1_old_orig;
264 if (!get_sha1("HEAD", sha1_orig)) {
265 orig = sha1_orig;
266 prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
267 update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
268 }
269 else if (old_orig)
270 delete_ref("ORIG_HEAD", old_orig);
271 prepend_reflog_action("updating HEAD", msg, sizeof(msg));
272 update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
273
274 switch (reset_type) {
275 case HARD:
276 if (!update_ref_status && !quiet)
277 print_new_head_line(commit);
278 break;
279 case SOFT: /* Nothing else to do. */
280 break;
281 case MIXED: /* Report what has not been updated. */
282 update_index_refresh(0, NULL);
283 break;
284 }
285
286 unlink(git_path("MERGE_HEAD"));
287 unlink(git_path("rr-cache/MERGE_RR"));
288 unlink(git_path("MERGE_MSG"));
289 unlink(git_path("SQUASH_MSG"));
290
291 free(reflog_action);
292
293 return update_ref_status;
294 }