Remove unused "zeropad" entry from tree_list_entry
[git/git-svn.git] / checkout-index.c
blob9876af6fd60a81d871e6be30060cc93d0e0259c7
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 "cache.h"
40 #include "strbuf.h"
41 #include "quote.h"
42 #include "cache-tree.h"
44 #define CHECKOUT_ALL 4
45 static const char *prefix;
46 static int prefix_length;
47 static int line_termination = '\n';
48 static int checkout_stage; /* default to checkout stage0 */
49 static int to_tempfile;
50 static char topath[4][MAXPATHLEN+1];
52 static struct checkout state = {
53 .base_dir = "",
54 .base_dir_len = 0,
55 .force = 0,
56 .quiet = 0,
57 .not_new = 0,
58 .refresh_cache = 0,
61 static void write_tempfile_record (const char *name)
63 int i;
65 if (CHECKOUT_ALL == checkout_stage) {
66 for (i = 1; i < 4; i++) {
67 if (i > 1)
68 putchar(' ');
69 if (topath[i][0])
70 fputs(topath[i], stdout);
71 else
72 putchar('.');
74 } else
75 fputs(topath[checkout_stage], stdout);
77 putchar('\t');
78 write_name_quoted("", 0, name + prefix_length,
79 line_termination, stdout);
80 putchar(line_termination);
82 for (i = 0; i < 4; i++) {
83 topath[i][0] = 0;
87 static int checkout_file(const char *name)
89 int namelen = strlen(name);
90 int pos = cache_name_pos(name, namelen);
91 int has_same_name = 0;
92 int did_checkout = 0;
93 int errs = 0;
95 if (pos < 0)
96 pos = -pos - 1;
98 while (pos < active_nr) {
99 struct cache_entry *ce = active_cache[pos];
100 if (ce_namelen(ce) != namelen ||
101 memcmp(ce->name, name, namelen))
102 break;
103 has_same_name = 1;
104 pos++;
105 if (ce_stage(ce) != checkout_stage
106 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
107 continue;
108 did_checkout = 1;
109 if (checkout_entry(ce, &state,
110 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
111 errs++;
114 if (did_checkout) {
115 if (to_tempfile)
116 write_tempfile_record(name);
117 return errs > 0 ? -1 : 0;
120 if (!state.quiet) {
121 fprintf(stderr, "git-checkout-index: %s ", name);
122 if (!has_same_name)
123 fprintf(stderr, "is not in the cache");
124 else if (checkout_stage)
125 fprintf(stderr, "does not exist at stage %d",
126 checkout_stage);
127 else
128 fprintf(stderr, "is unmerged");
129 fputc('\n', stderr);
131 return -1;
134 static int checkout_all(void)
136 int i, errs = 0;
137 struct cache_entry* last_ce = NULL;
139 for (i = 0; i < active_nr ; i++) {
140 struct cache_entry *ce = active_cache[i];
141 if (ce_stage(ce) != checkout_stage
142 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
143 continue;
144 if (prefix && *prefix &&
145 (ce_namelen(ce) <= prefix_length ||
146 memcmp(prefix, ce->name, prefix_length)))
147 continue;
148 if (last_ce && to_tempfile) {
149 if (ce_namelen(last_ce) != ce_namelen(ce)
150 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
151 write_tempfile_record(last_ce->name);
153 if (checkout_entry(ce, &state,
154 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
155 errs++;
156 last_ce = ce;
158 if (last_ce && to_tempfile)
159 write_tempfile_record(last_ce->name);
160 if (errs)
161 /* we have already done our error reporting.
162 * exit with the same code as die().
164 exit(128);
165 return 0;
168 static const char checkout_cache_usage[] =
169 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
171 static struct cache_file cache_file;
173 int main(int argc, char **argv)
175 int i;
176 int newfd = -1;
177 int all = 0;
178 int read_from_stdin = 0;
180 prefix = setup_git_directory();
181 git_config(git_default_config);
182 prefix_length = prefix ? strlen(prefix) : 0;
184 if (read_cache() < 0) {
185 die("invalid cache");
188 for (i = 1; i < argc; i++) {
189 const char *arg = argv[i];
191 if (!strcmp(arg, "--")) {
192 i++;
193 break;
195 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
196 all = 1;
197 continue;
199 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
200 state.force = 1;
201 continue;
203 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
204 state.quiet = 1;
205 continue;
207 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
208 state.not_new = 1;
209 continue;
211 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
212 state.refresh_cache = 1;
213 if (newfd < 0)
214 newfd = hold_index_file_for_update
215 (&cache_file,
216 get_index_file());
217 if (newfd < 0)
218 die("cannot open index.lock file.");
219 continue;
221 if (!strcmp(arg, "-z")) {
222 line_termination = 0;
223 continue;
225 if (!strcmp(arg, "--stdin")) {
226 if (i != argc - 1)
227 die("--stdin must be at the end");
228 read_from_stdin = 1;
229 i++; /* do not consider arg as a file name */
230 break;
232 if (!strcmp(arg, "--temp")) {
233 to_tempfile = 1;
234 continue;
236 if (!strncmp(arg, "--prefix=", 9)) {
237 state.base_dir = arg+9;
238 state.base_dir_len = strlen(state.base_dir);
239 continue;
241 if (!strncmp(arg, "--stage=", 8)) {
242 if (!strcmp(arg + 8, "all")) {
243 to_tempfile = 1;
244 checkout_stage = CHECKOUT_ALL;
245 } else {
246 int ch = arg[8];
247 if ('1' <= ch && ch <= '3')
248 checkout_stage = arg[8] - '0';
249 else
250 die("stage should be between 1 and 3 or all");
252 continue;
254 if (arg[0] == '-')
255 usage(checkout_cache_usage);
256 break;
259 if (state.base_dir_len || to_tempfile) {
260 /* when --prefix is specified we do not
261 * want to update cache.
263 if (state.refresh_cache) {
264 close(newfd); newfd = -1;
265 rollback_index_file(&cache_file);
267 state.refresh_cache = 0;
270 /* Check out named files first */
271 for ( ; i < argc; i++) {
272 const char *arg = argv[i];
273 const char *p;
275 if (all)
276 die("git-checkout-index: don't mix '--all' and explicit filenames");
277 if (read_from_stdin)
278 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
279 p = prefix_path(prefix, prefix_length, arg);
280 checkout_file(p);
281 if (p < arg || p > arg + strlen(arg))
282 free((char*)p);
285 if (read_from_stdin) {
286 struct strbuf buf;
287 if (all)
288 die("git-checkout-index: don't mix '--all' and '--stdin'");
289 strbuf_init(&buf);
290 while (1) {
291 char *path_name;
292 const char *p;
294 read_line(&buf, stdin, line_termination);
295 if (buf.eof)
296 break;
297 if (line_termination && buf.buf[0] == '"')
298 path_name = unquote_c_style(buf.buf, NULL);
299 else
300 path_name = buf.buf;
301 p = prefix_path(prefix, prefix_length, path_name);
302 checkout_file(p);
303 if (p < path_name || p > path_name + strlen(path_name))
304 free((char *)p);
305 if (path_name != buf.buf)
306 free(path_name);
310 if (all)
311 checkout_all();
313 if (0 <= newfd &&
314 (write_cache(newfd, active_cache, active_nr) ||
315 commit_index_file(&cache_file)))
316 die("Unable to write new cachefile");
317 return 0;