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