Move old index entry removal from "unpack_trees()" into the individual functions
[git/mjg.git] / builtin-checkout-index.c
blob75377b9cab75ac75c6d5d7f79fdcf64bdb27cff2
1 /*
2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 * Careful: order of argument flags does matter. For example,
8 * git-checkout-index -a -f file.c
10 * Will first check out all files listed in the cache (but not
11 * overwrite any old ones), and then force-checkout "file.c" a
12 * second time (ie that one _will_ overwrite any old contents
13 * with the same filename).
15 * Also, just doing "git-checkout-index" does nothing. You probably
16 * meant "git-checkout-index -a". And if you want to force it, you
17 * want "git-checkout-index -f -a".
19 * Intuitiveness is not the goal here. Repeatability is. The
20 * reason for the "no arguments means no work" thing is that
21 * from scripts you are supposed to be able to do things like
23 * find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
25 * or:
27 * find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
29 * which will force all existing *.h files to be replaced with
30 * their cached copies. If an empty command line implied "all",
31 * then this would force-refresh everything in the cache, which
32 * was not the point.
34 * Oh, and the "--" is just a good idea when you know the rest
35 * will be filenames. Just so that you wouldn't have a filename
36 * of "-a" causing problems (not possible in the above example,
37 * but get used to it in scripting!).
39 #include "builtin.h"
40 #include "cache.h"
41 #include "strbuf.h"
42 #include "quote.h"
43 #include "cache-tree.h"
45 #define CHECKOUT_ALL 4
46 static int line_termination = '\n';
47 static int checkout_stage; /* default to checkout stage0 */
48 static int to_tempfile;
49 static char topath[4][PATH_MAX + 1];
51 static struct checkout state;
53 static void write_tempfile_record(const char *name, int prefix_length)
55 int i;
57 if (CHECKOUT_ALL == checkout_stage) {
58 for (i = 1; i < 4; i++) {
59 if (i > 1)
60 putchar(' ');
61 if (topath[i][0])
62 fputs(topath[i], stdout);
63 else
64 putchar('.');
66 } else
67 fputs(topath[checkout_stage], stdout);
69 putchar('\t');
70 write_name_quoted("", 0, name + prefix_length,
71 line_termination, stdout);
72 putchar(line_termination);
74 for (i = 0; i < 4; i++) {
75 topath[i][0] = 0;
79 static int checkout_file(const char *name, int prefix_length)
81 int namelen = strlen(name);
82 int pos = cache_name_pos(name, namelen);
83 int has_same_name = 0;
84 int did_checkout = 0;
85 int errs = 0;
87 if (pos < 0)
88 pos = -pos - 1;
90 while (pos < active_nr) {
91 struct cache_entry *ce = active_cache[pos];
92 if (ce_namelen(ce) != namelen ||
93 memcmp(ce->name, name, namelen))
94 break;
95 has_same_name = 1;
96 pos++;
97 if (ce_stage(ce) != checkout_stage
98 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
99 continue;
100 did_checkout = 1;
101 if (checkout_entry(ce, &state,
102 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
103 errs++;
106 if (did_checkout) {
107 if (to_tempfile)
108 write_tempfile_record(name, prefix_length);
109 return errs > 0 ? -1 : 0;
112 if (!state.quiet) {
113 fprintf(stderr, "git-checkout-index: %s ", name);
114 if (!has_same_name)
115 fprintf(stderr, "is not in the cache");
116 else if (checkout_stage)
117 fprintf(stderr, "does not exist at stage %d",
118 checkout_stage);
119 else
120 fprintf(stderr, "is unmerged");
121 fputc('\n', stderr);
123 return -1;
126 static void checkout_all(const char *prefix, int prefix_length)
128 int i, errs = 0;
129 struct cache_entry* last_ce = NULL;
131 for (i = 0; i < active_nr ; i++) {
132 struct cache_entry *ce = active_cache[i];
133 if (ce_stage(ce) != checkout_stage
134 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
135 continue;
136 if (prefix && *prefix &&
137 (ce_namelen(ce) <= prefix_length ||
138 memcmp(prefix, ce->name, prefix_length)))
139 continue;
140 if (last_ce && to_tempfile) {
141 if (ce_namelen(last_ce) != ce_namelen(ce)
142 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
143 write_tempfile_record(last_ce->name, prefix_length);
145 if (checkout_entry(ce, &state,
146 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
147 errs++;
148 last_ce = ce;
150 if (last_ce && to_tempfile)
151 write_tempfile_record(last_ce->name, prefix_length);
152 if (errs)
153 /* we have already done our error reporting.
154 * exit with the same code as die().
156 exit(128);
159 static const char checkout_cache_usage[] =
160 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
162 static struct lock_file lock_file;
164 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
166 int i;
167 int newfd = -1;
168 int all = 0;
169 int read_from_stdin = 0;
170 int prefix_length;
172 git_config(git_default_config);
173 state.base_dir = "";
174 prefix_length = prefix ? strlen(prefix) : 0;
176 if (read_cache() < 0) {
177 die("invalid cache");
180 for (i = 1; i < argc; i++) {
181 const char *arg = argv[i];
183 if (!strcmp(arg, "--")) {
184 i++;
185 break;
187 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
188 all = 1;
189 continue;
191 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
192 state.force = 1;
193 continue;
195 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
196 state.quiet = 1;
197 continue;
199 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
200 state.not_new = 1;
201 continue;
203 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
204 state.refresh_cache = 1;
205 if (newfd < 0)
206 newfd = hold_locked_index(&lock_file, 1);
207 continue;
209 if (!strcmp(arg, "-z")) {
210 line_termination = 0;
211 continue;
213 if (!strcmp(arg, "--stdin")) {
214 if (i != argc - 1)
215 die("--stdin must be at the end");
216 read_from_stdin = 1;
217 i++; /* do not consider arg as a file name */
218 break;
220 if (!strcmp(arg, "--temp")) {
221 to_tempfile = 1;
222 continue;
224 if (!prefixcmp(arg, "--prefix=")) {
225 state.base_dir = arg+9;
226 state.base_dir_len = strlen(state.base_dir);
227 continue;
229 if (!prefixcmp(arg, "--stage=")) {
230 if (!strcmp(arg + 8, "all")) {
231 to_tempfile = 1;
232 checkout_stage = CHECKOUT_ALL;
233 } else {
234 int ch = arg[8];
235 if ('1' <= ch && ch <= '3')
236 checkout_stage = arg[8] - '0';
237 else
238 die("stage should be between 1 and 3 or all");
240 continue;
242 if (arg[0] == '-')
243 usage(checkout_cache_usage);
244 break;
247 if (state.base_dir_len || to_tempfile) {
248 /* when --prefix is specified we do not
249 * want to update cache.
251 if (state.refresh_cache) {
252 close(newfd); newfd = -1;
253 rollback_lock_file(&lock_file);
255 state.refresh_cache = 0;
258 /* Check out named files first */
259 for ( ; i < argc; i++) {
260 const char *arg = argv[i];
261 const char *p;
263 if (all)
264 die("git-checkout-index: don't mix '--all' and explicit filenames");
265 if (read_from_stdin)
266 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
267 p = prefix_path(prefix, prefix_length, arg);
268 checkout_file(p, prefix_length);
269 if (p < arg || p > arg + strlen(arg))
270 free((char*)p);
273 if (read_from_stdin) {
274 struct strbuf buf;
275 if (all)
276 die("git-checkout-index: don't mix '--all' and '--stdin'");
277 strbuf_init(&buf);
278 while (1) {
279 char *path_name;
280 const char *p;
282 read_line(&buf, stdin, line_termination);
283 if (buf.eof)
284 break;
285 if (line_termination && buf.buf[0] == '"')
286 path_name = unquote_c_style(buf.buf, NULL);
287 else
288 path_name = buf.buf;
289 p = prefix_path(prefix, prefix_length, path_name);
290 checkout_file(p, prefix_length);
291 if (p < path_name || p > path_name + strlen(path_name))
292 free((char *)p);
293 if (path_name != buf.buf)
294 free(path_name);
298 if (all)
299 checkout_all(prefix, prefix_length);
301 if (0 <= newfd &&
302 (write_cache(newfd, active_cache, active_nr) ||
303 close(newfd) || commit_locked_index(&lock_file)))
304 die("Unable to write new index file");
305 return 0;