add OPT__FORCE
[git/git.git] / builtin / checkout-index.c
1 /*
2 * Check-out files from the "current cache directory"
3 *
4 * Copyright (C) 2005 Linus Torvalds
5 *
6 * Careful: order of argument flags does matter. For example,
7 *
8 * git checkout-index -a -f file.c
9 *
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).
14 *
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".
18 *
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
22 *
23 * find . -name '*.h' -print0 | xargs -0 git checkout-index -f --
24 *
25 * or:
26 *
27 * find . -name '*.h' -print0 | git checkout-index -f -z --stdin
28 *
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.
33 *
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!).
38 */
39 #include "builtin.h"
40 #include "cache.h"
41 #include "quote.h"
42 #include "cache-tree.h"
43 #include "parse-options.h"
44
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];
50
51 static struct checkout state;
52
53 static void write_tempfile_record(const char *name, int prefix_length)
54 {
55 int i;
56
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('.');
65 }
66 } else
67 fputs(topath[checkout_stage], stdout);
68
69 putchar('\t');
70 write_name_quoted(name + prefix_length, stdout, line_termination);
71
72 for (i = 0; i < 4; i++) {
73 topath[i][0] = 0;
74 }
75 }
76
77 static int checkout_file(const char *name, int prefix_length)
78 {
79 int namelen = strlen(name);
80 int pos = cache_name_pos(name, namelen);
81 int has_same_name = 0;
82 int did_checkout = 0;
83 int errs = 0;
84
85 if (pos < 0)
86 pos = -pos - 1;
87
88 while (pos < active_nr) {
89 struct cache_entry *ce = active_cache[pos];
90 if (ce_namelen(ce) != namelen ||
91 memcmp(ce->name, name, namelen))
92 break;
93 has_same_name = 1;
94 pos++;
95 if (ce_stage(ce) != checkout_stage
96 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
97 continue;
98 did_checkout = 1;
99 if (checkout_entry(ce, &state,
100 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
101 errs++;
102 }
103
104 if (did_checkout) {
105 if (to_tempfile)
106 write_tempfile_record(name, prefix_length);
107 return errs > 0 ? -1 : 0;
108 }
109
110 if (!state.quiet) {
111 fprintf(stderr, "git checkout-index: %s ", name);
112 if (!has_same_name)
113 fprintf(stderr, "is not in the cache");
114 else if (checkout_stage)
115 fprintf(stderr, "does not exist at stage %d",
116 checkout_stage);
117 else
118 fprintf(stderr, "is unmerged");
119 fputc('\n', stderr);
120 }
121 return -1;
122 }
123
124 static void checkout_all(const char *prefix, int prefix_length)
125 {
126 int i, errs = 0;
127 struct cache_entry *last_ce = NULL;
128
129 for (i = 0; i < active_nr ; i++) {
130 struct cache_entry *ce = active_cache[i];
131 if (ce_stage(ce) != checkout_stage
132 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
133 continue;
134 if (prefix && *prefix &&
135 (ce_namelen(ce) <= prefix_length ||
136 memcmp(prefix, ce->name, prefix_length)))
137 continue;
138 if (last_ce && to_tempfile) {
139 if (ce_namelen(last_ce) != ce_namelen(ce)
140 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
141 write_tempfile_record(last_ce->name, prefix_length);
142 }
143 if (checkout_entry(ce, &state,
144 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
145 errs++;
146 last_ce = ce;
147 }
148 if (last_ce && to_tempfile)
149 write_tempfile_record(last_ce->name, prefix_length);
150 if (errs)
151 /* we have already done our error reporting.
152 * exit with the same code as die().
153 */
154 exit(128);
155 }
156
157 static const char * const builtin_checkout_index_usage[] = {
158 "git checkout-index [options] [--] [<file>...]",
159 NULL
160 };
161
162 static struct lock_file lock_file;
163
164 static int option_parse_u(const struct option *opt,
165 const char *arg, int unset)
166 {
167 int *newfd = opt->value;
168
169 state.refresh_cache = 1;
170 if (*newfd < 0)
171 *newfd = hold_locked_index(&lock_file, 1);
172 return 0;
173 }
174
175 static int option_parse_z(const struct option *opt,
176 const char *arg, int unset)
177 {
178 if (unset)
179 line_termination = '\n';
180 else
181 line_termination = 0;
182 return 0;
183 }
184
185 static int option_parse_prefix(const struct option *opt,
186 const char *arg, int unset)
187 {
188 state.base_dir = arg;
189 state.base_dir_len = strlen(arg);
190 return 0;
191 }
192
193 static int option_parse_stage(const struct option *opt,
194 const char *arg, int unset)
195 {
196 if (!strcmp(arg, "all")) {
197 to_tempfile = 1;
198 checkout_stage = CHECKOUT_ALL;
199 } else {
200 int ch = arg[0];
201 if ('1' <= ch && ch <= '3')
202 checkout_stage = arg[0] - '0';
203 else
204 die("stage should be between 1 and 3 or all");
205 }
206 return 0;
207 }
208
209 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
210 {
211 int i;
212 int newfd = -1;
213 int all = 0;
214 int read_from_stdin = 0;
215 int prefix_length;
216 int force = 0, quiet = 0, not_new = 0;
217 struct option builtin_checkout_index_options[] = {
218 OPT_BOOLEAN('a', "all", &all,
219 "checks out all files in the index"),
220 OPT__FORCE(&force, "forces overwrite of existing files"),
221 OPT__QUIET(&quiet, "be quiet"),
222 OPT_BOOLEAN('n', "no-create", &not_new,
223 "don't checkout new files"),
224 { OPTION_CALLBACK, 'u', "index", &newfd, NULL,
225 "update stat information in the index file",
226 PARSE_OPT_NOARG, option_parse_u },
227 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
228 "paths are separated with NUL character",
229 PARSE_OPT_NOARG, option_parse_z },
230 OPT_BOOLEAN(0, "stdin", &read_from_stdin,
231 "read list of paths from the standard input"),
232 OPT_BOOLEAN(0, "temp", &to_tempfile,
233 "write the content to temporary files"),
234 OPT_CALLBACK(0, "prefix", NULL, "string",
235 "when creating files, prepend <string>",
236 option_parse_prefix),
237 OPT_CALLBACK(0, "stage", NULL, NULL,
238 "copy out the files from named stage",
239 option_parse_stage),
240 OPT_END()
241 };
242
243 git_config(git_default_config, NULL);
244 state.base_dir = "";
245 prefix_length = prefix ? strlen(prefix) : 0;
246
247 if (read_cache() < 0) {
248 die("invalid cache");
249 }
250
251 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
252 builtin_checkout_index_usage, 0);
253 state.force = force;
254 state.quiet = quiet;
255 state.not_new = not_new;
256
257 if (state.base_dir_len || to_tempfile) {
258 /* when --prefix is specified we do not
259 * want to update cache.
260 */
261 if (state.refresh_cache) {
262 rollback_lock_file(&lock_file);
263 newfd = -1;
264 }
265 state.refresh_cache = 0;
266 }
267
268 /* Check out named files first */
269 for (i = 0; i < argc; i++) {
270 const char *arg = argv[i];
271 const char *p;
272
273 if (all)
274 die("git checkout-index: don't mix '--all' and explicit filenames");
275 if (read_from_stdin)
276 die("git checkout-index: don't mix '--stdin' and explicit filenames");
277 p = prefix_path(prefix, prefix_length, arg);
278 checkout_file(p, prefix_length);
279 if (p < arg || p > arg + strlen(arg))
280 free((char *)p);
281 }
282
283 if (read_from_stdin) {
284 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
285
286 if (all)
287 die("git checkout-index: don't mix '--all' and '--stdin'");
288
289 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
290 const char *p;
291 if (line_termination && buf.buf[0] == '"') {
292 strbuf_reset(&nbuf);
293 if (unquote_c_style(&nbuf, buf.buf, NULL))
294 die("line is badly quoted");
295 strbuf_swap(&buf, &nbuf);
296 }
297 p = prefix_path(prefix, prefix_length, buf.buf);
298 checkout_file(p, prefix_length);
299 if (p < buf.buf || p > buf.buf + buf.len)
300 free((char *)p);
301 }
302 strbuf_release(&nbuf);
303 strbuf_release(&buf);
304 }
305
306 if (all)
307 checkout_all(prefix, prefix_length);
308
309 if (0 <= newfd &&
310 (write_cache(newfd, active_cache, active_nr) ||
311 commit_locked_index(&lock_file)))
312 die("Unable to write new index file");
313 return 0;
314 }