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