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