Merge branch 'jc/diffcore-rotate'
[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         for (i = 0; i < active_nr ; i++) {
123                 struct cache_entry *ce = active_cache[i];
124                 if (ce_stage(ce) != checkout_stage
125                     && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
126                         continue;
127                 if (prefix && *prefix &&
128                     (ce_namelen(ce) <= prefix_length ||
129                      memcmp(prefix, ce->name, prefix_length)))
130                         continue;
131                 if (last_ce && to_tempfile) {
132                         if (ce_namelen(last_ce) != ce_namelen(ce)
133                             || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
134                                 write_tempfile_record(last_ce->name, prefix);
135                 }
136                 if (checkout_entry(ce, &state,
137                                    to_tempfile ? topath[ce_stage(ce)] : NULL,
138                                    NULL) < 0)
139                         errs++;
140                 last_ce = ce;
141         }
142         if (last_ce && to_tempfile)
143                 write_tempfile_record(last_ce->name, prefix);
144         if (errs)
145                 /* we have already done our error reporting.
146                  * exit with the same code as die().
147                  */
148                 exit(128);
149 }
150
151 static const char * const builtin_checkout_index_usage[] = {
152         N_("git checkout-index [<options>] [--] [<file>...]"),
153         NULL
154 };
155
156 static int option_parse_stage(const struct option *opt,
157                               const char *arg, int unset)
158 {
159         BUG_ON_OPT_NEG(unset);
160
161         if (!strcmp(arg, "all")) {
162                 to_tempfile = 1;
163                 checkout_stage = CHECKOUT_ALL;
164         } else {
165                 int ch = arg[0];
166                 if ('1' <= ch && ch <= '3')
167                         checkout_stage = arg[0] - '0';
168                 else
169                         die(_("stage should be between 1 and 3 or all"));
170         }
171         return 0;
172 }
173
174 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
175 {
176         int i;
177         struct lock_file lock_file = LOCK_INIT;
178         int all = 0;
179         int read_from_stdin = 0;
180         int prefix_length;
181         int force = 0, quiet = 0, not_new = 0;
182         int index_opt = 0;
183         int err = 0;
184         struct option builtin_checkout_index_options[] = {
185                 OPT_BOOL('a', "all", &all,
186                         N_("check out all files in the index")),
187                 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
188                 OPT__QUIET(&quiet,
189                         N_("no warning for existing files and files not in index")),
190                 OPT_BOOL('n', "no-create", &not_new,
191                         N_("don't checkout new files")),
192                 OPT_BOOL('u', "index", &index_opt,
193                          N_("update stat information in the index file")),
194                 OPT_BOOL('z', NULL, &nul_term_line,
195                         N_("paths are separated with NUL character")),
196                 OPT_BOOL(0, "stdin", &read_from_stdin,
197                         N_("read list of paths from the standard input")),
198                 OPT_BOOL(0, "temp", &to_tempfile,
199                         N_("write the content to temporary files")),
200                 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
201                         N_("when creating files, prepend <string>")),
202                 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
203                         N_("copy out the files from named stage"),
204                         PARSE_OPT_NONEG, option_parse_stage),
205                 OPT_END()
206         };
207
208         if (argc == 2 && !strcmp(argv[1], "-h"))
209                 usage_with_options(builtin_checkout_index_usage,
210                                    builtin_checkout_index_options);
211         git_config(git_default_config, NULL);
212         prefix_length = prefix ? strlen(prefix) : 0;
213
214         if (read_cache() < 0) {
215                 die("invalid cache");
216         }
217
218         argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
219                         builtin_checkout_index_usage, 0);
220         state.istate = &the_index;
221         state.force = force;
222         state.quiet = quiet;
223         state.not_new = not_new;
224
225         if (!state.base_dir)
226                 state.base_dir = "";
227         state.base_dir_len = strlen(state.base_dir);
228
229         /*
230          * when --prefix is specified we do not want to update cache.
231          */
232         if (index_opt && !state.base_dir_len && !to_tempfile) {
233                 state.refresh_cache = 1;
234                 state.istate = &the_index;
235                 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
236         }
237
238         /* Check out named files first */
239         for (i = 0; i < argc; i++) {
240                 const char *arg = argv[i];
241                 char *p;
242
243                 if (all)
244                         die("git checkout-index: don't mix '--all' and explicit filenames");
245                 if (read_from_stdin)
246                         die("git checkout-index: don't mix '--stdin' and explicit filenames");
247                 p = prefix_path(prefix, prefix_length, arg);
248                 err |= checkout_file(p, prefix);
249                 free(p);
250         }
251
252         if (read_from_stdin) {
253                 struct strbuf buf = STRBUF_INIT;
254                 struct strbuf unquoted = STRBUF_INIT;
255                 strbuf_getline_fn getline_fn;
256
257                 if (all)
258                         die("git checkout-index: don't mix '--all' and '--stdin'");
259
260                 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
261                 while (getline_fn(&buf, stdin) != EOF) {
262                         char *p;
263                         if (!nul_term_line && buf.buf[0] == '"') {
264                                 strbuf_reset(&unquoted);
265                                 if (unquote_c_style(&unquoted, buf.buf, NULL))
266                                         die("line is badly quoted");
267                                 strbuf_swap(&buf, &unquoted);
268                         }
269                         p = prefix_path(prefix, prefix_length, buf.buf);
270                         err |= checkout_file(p, prefix);
271                         free(p);
272                 }
273                 strbuf_release(&unquoted);
274                 strbuf_release(&buf);
275         }
276
277         if (err)
278                 return 1;
279
280         if (all)
281                 checkout_all(prefix, prefix_length);
282
283         if (is_lock_file_locked(&lock_file) &&
284             write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
285                 die("Unable to write new index file");
286         return 0;
287 }