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