checkout: ensure full index
[git] / builtin / checkout-index.c
1 /*
2  * Check-out files from the "current cache directory"
3  *
4  * Copyright (C) 2005 Linus Torvalds
5  *
6  */
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
8 #include "builtin.h"
9 #include "config.h"
10 #include "lockfile.h"
11 #include "quote.h"
12 #include "cache-tree.h"
13 #include "parse-options.h"
14
15 #define CHECKOUT_ALL 4
16 static int nul_term_line;
17 static int checkout_stage; /* default to checkout stage0 */
18 static int to_tempfile;
19 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
20
21 static struct checkout state = CHECKOUT_INIT;
22
23 static void write_tempfile_record(const char *name, const char *prefix)
24 {
25         int i;
26         int have_tempname = 0;
27
28         if (CHECKOUT_ALL == checkout_stage) {
29                 for (i = 1; i < 4; i++)
30                         if (topath[i][0]) {
31                                 have_tempname = 1;
32                                 break;
33                         }
34
35                 if (have_tempname) {
36                         for (i = 1; i < 4; i++) {
37                                 if (i > 1)
38                                         putchar(' ');
39                                 if (topath[i][0])
40                                         fputs(topath[i], stdout);
41                                 else
42                                         putchar('.');
43                         }
44                 }
45         } else if (topath[checkout_stage][0]) {
46                 have_tempname = 1;
47                 fputs(topath[checkout_stage], stdout);
48         }
49
50         if (have_tempname) {
51                 putchar('\t');
52                 write_name_quoted_relative(name, prefix, stdout,
53                                            nul_term_line ? '\0' : '\n');
54         }
55
56         for (i = 0; i < 4; i++) {
57                 topath[i][0] = 0;
58         }
59 }
60
61 static int checkout_file(const char *name, const char *prefix)
62 {
63         int namelen = strlen(name);
64         int pos = cache_name_pos(name, namelen);
65         int has_same_name = 0;
66         int did_checkout = 0;
67         int errs = 0;
68
69         if (pos < 0)
70                 pos = -pos - 1;
71
72         while (pos < active_nr) {
73                 struct cache_entry *ce = active_cache[pos];
74                 if (ce_namelen(ce) != namelen ||
75                     memcmp(ce->name, name, namelen))
76                         break;
77                 has_same_name = 1;
78                 pos++;
79                 if (ce_stage(ce) != checkout_stage
80                     && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
81                         continue;
82                 did_checkout = 1;
83                 if (checkout_entry(ce, &state,
84                                    to_tempfile ? topath[ce_stage(ce)] : NULL,
85                                    NULL) < 0)
86                         errs++;
87         }
88
89         if (did_checkout) {
90                 if (to_tempfile)
91                         write_tempfile_record(name, prefix);
92                 return errs > 0 ? -1 : 0;
93         }
94
95         /*
96          * At this point we know we didn't try to check anything out. If it was
97          * because we did find an entry but it was stage 0, that's not an
98          * error.
99          */
100         if (has_same_name && checkout_stage == CHECKOUT_ALL)
101                 return 0;
102
103         if (!state.quiet) {
104                 fprintf(stderr, "git checkout-index: %s ", name);
105                 if (!has_same_name)
106                         fprintf(stderr, "is not in the cache");
107                 else if (checkout_stage)
108                         fprintf(stderr, "does not exist at stage %d",
109                                 checkout_stage);
110                 else
111                         fprintf(stderr, "is unmerged");
112                 fputc('\n', stderr);
113         }
114         return -1;
115 }
116
117 static void checkout_all(const char *prefix, int prefix_length)
118 {
119         int i, errs = 0;
120         struct cache_entry *last_ce = NULL;
121
122         /* TODO: audit for interaction with sparse-index. */
123         ensure_full_index(&the_index);
124         for (i = 0; i < active_nr ; i++) {
125                 struct cache_entry *ce = active_cache[i];
126                 if (ce_stage(ce) != checkout_stage
127                     && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
128                         continue;
129                 if (prefix && *prefix &&
130                     (ce_namelen(ce) <= prefix_length ||
131                      memcmp(prefix, ce->name, prefix_length)))
132                         continue;
133                 if (last_ce && to_tempfile) {
134                         if (ce_namelen(last_ce) != ce_namelen(ce)
135                             || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
136                                 write_tempfile_record(last_ce->name, prefix);
137                 }
138                 if (checkout_entry(ce, &state,
139                                    to_tempfile ? topath[ce_stage(ce)] : NULL,
140                                    NULL) < 0)
141                         errs++;
142                 last_ce = ce;
143         }
144         if (last_ce && to_tempfile)
145                 write_tempfile_record(last_ce->name, prefix);
146         if (errs)
147                 /* we have already done our error reporting.
148                  * exit with the same code as die().
149                  */
150                 exit(128);
151 }
152
153 static const char * const builtin_checkout_index_usage[] = {
154         N_("git checkout-index [<options>] [--] [<file>...]"),
155         NULL
156 };
157
158 static int option_parse_stage(const struct option *opt,
159                               const char *arg, int unset)
160 {
161         BUG_ON_OPT_NEG(unset);
162
163         if (!strcmp(arg, "all")) {
164                 to_tempfile = 1;
165                 checkout_stage = CHECKOUT_ALL;
166         } else {
167                 int ch = arg[0];
168                 if ('1' <= ch && ch <= '3')
169                         checkout_stage = arg[0] - '0';
170                 else
171                         die(_("stage should be between 1 and 3 or all"));
172         }
173         return 0;
174 }
175
176 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
177 {
178         int i;
179         struct lock_file lock_file = LOCK_INIT;
180         int all = 0;
181         int read_from_stdin = 0;
182         int prefix_length;
183         int force = 0, quiet = 0, not_new = 0;
184         int index_opt = 0;
185         int err = 0;
186         struct option builtin_checkout_index_options[] = {
187                 OPT_BOOL('a', "all", &all,
188                         N_("check out all files in the index")),
189                 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
190                 OPT__QUIET(&quiet,
191                         N_("no warning for existing files and files not in index")),
192                 OPT_BOOL('n', "no-create", &not_new,
193                         N_("don't checkout new files")),
194                 OPT_BOOL('u', "index", &index_opt,
195                          N_("update stat information in the index file")),
196                 OPT_BOOL('z', NULL, &nul_term_line,
197                         N_("paths are separated with NUL character")),
198                 OPT_BOOL(0, "stdin", &read_from_stdin,
199                         N_("read list of paths from the standard input")),
200                 OPT_BOOL(0, "temp", &to_tempfile,
201                         N_("write the content to temporary files")),
202                 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
203                         N_("when creating files, prepend <string>")),
204                 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
205                         N_("copy out the files from named stage"),
206                         PARSE_OPT_NONEG, option_parse_stage),
207                 OPT_END()
208         };
209
210         if (argc == 2 && !strcmp(argv[1], "-h"))
211                 usage_with_options(builtin_checkout_index_usage,
212                                    builtin_checkout_index_options);
213         git_config(git_default_config, NULL);
214         prefix_length = prefix ? strlen(prefix) : 0;
215
216         if (read_cache() < 0) {
217                 die("invalid cache");
218         }
219
220         argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
221                         builtin_checkout_index_usage, 0);
222         state.istate = &the_index;
223         state.force = force;
224         state.quiet = quiet;
225         state.not_new = not_new;
226
227         if (!state.base_dir)
228                 state.base_dir = "";
229         state.base_dir_len = strlen(state.base_dir);
230
231         /*
232          * when --prefix is specified we do not want to update cache.
233          */
234         if (index_opt && !state.base_dir_len && !to_tempfile) {
235                 state.refresh_cache = 1;
236                 state.istate = &the_index;
237                 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
238         }
239
240         /* Check out named files first */
241         for (i = 0; i < argc; i++) {
242                 const char *arg = argv[i];
243                 char *p;
244
245                 if (all)
246                         die("git checkout-index: don't mix '--all' and explicit filenames");
247                 if (read_from_stdin)
248                         die("git checkout-index: don't mix '--stdin' and explicit filenames");
249                 p = prefix_path(prefix, prefix_length, arg);
250                 err |= checkout_file(p, prefix);
251                 free(p);
252         }
253
254         if (read_from_stdin) {
255                 struct strbuf buf = STRBUF_INIT;
256                 struct strbuf unquoted = STRBUF_INIT;
257                 strbuf_getline_fn getline_fn;
258
259                 if (all)
260                         die("git checkout-index: don't mix '--all' and '--stdin'");
261
262                 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
263                 while (getline_fn(&buf, stdin) != EOF) {
264                         char *p;
265                         if (!nul_term_line && buf.buf[0] == '"') {
266                                 strbuf_reset(&unquoted);
267                                 if (unquote_c_style(&unquoted, buf.buf, NULL))
268                                         die("line is badly quoted");
269                                 strbuf_swap(&buf, &unquoted);
270                         }
271                         p = prefix_path(prefix, prefix_length, buf.buf);
272                         err |= checkout_file(p, prefix);
273                         free(p);
274                 }
275                 strbuf_release(&unquoted);
276                 strbuf_release(&buf);
277         }
278
279         if (err)
280                 return 1;
281
282         if (all)
283                 checkout_all(prefix, prefix_length);
284
285         if (is_lock_file_locked(&lock_file) &&
286             write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
287                 die("Unable to write new index file");
288         return 0;
289 }