2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_INDEX_VARIABLE
13 #include "cache-tree.h"
14 #include "parse-options.h"
16 #include "parallel-checkout.h"
18 #define CHECKOUT_ALL 4
19 static int nul_term_line
;
20 static int checkout_stage
; /* default to checkout stage0 */
21 static int ignore_skip_worktree
; /* default to 0 */
22 static int to_tempfile
;
23 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
25 static struct checkout state
= CHECKOUT_INIT
;
27 static void write_tempfile_record(const char *name
, const char *prefix
)
30 int have_tempname
= 0;
32 if (CHECKOUT_ALL
== checkout_stage
) {
33 for (i
= 1; i
< 4; i
++)
40 for (i
= 1; i
< 4; i
++) {
44 fputs(topath
[i
], stdout
);
49 } else if (topath
[checkout_stage
][0]) {
51 fputs(topath
[checkout_stage
], stdout
);
56 write_name_quoted_relative(name
, prefix
, stdout
,
57 nul_term_line
? '\0' : '\n');
60 for (i
= 0; i
< 4; i
++) {
65 static int checkout_file(const char *name
, const char *prefix
)
67 int namelen
= strlen(name
);
68 int pos
= index_name_pos(&the_index
, name
, namelen
);
69 int has_same_name
= 0;
78 while (pos
< the_index
.cache_nr
) {
79 struct cache_entry
*ce
= the_index
.cache
[pos
];
80 if (ce_namelen(ce
) != namelen
||
81 memcmp(ce
->name
, name
, namelen
))
85 if (S_ISSPARSEDIR(ce
->ce_mode
))
88 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
91 if (ce_stage(ce
) != checkout_stage
92 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
95 if (checkout_entry(ce
, &state
,
96 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
103 write_tempfile_record(name
, prefix
);
104 return errs
> 0 ? -1 : 0;
108 * At this point we know we didn't try to check anything out. If it was
109 * because we did find an entry but it was stage 0, that's not an
112 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
116 fprintf(stderr
, "git checkout-index: %s ", name
);
118 fprintf(stderr
, "is not in the cache");
120 fprintf(stderr
, "is a sparse directory");
122 fprintf(stderr
, "has skip-worktree enabled; "
123 "use '--ignore-skip-worktree-bits' to checkout");
124 else if (checkout_stage
)
125 fprintf(stderr
, "does not exist at stage %d",
128 fprintf(stderr
, "is unmerged");
134 static int checkout_all(const char *prefix
, int prefix_length
)
137 struct cache_entry
*last_ce
= NULL
;
139 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
140 struct cache_entry
*ce
= the_index
.cache
[i
];
142 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
143 if (!ce_skip_worktree(ce
))
144 BUG("sparse directory '%s' does not have skip-worktree set", ce
->name
);
147 * If the current entry is a sparse directory and skip-worktree
148 * entries are being checked out, expand the index and continue
149 * the loop on the current index position (now pointing to the
150 * first entry inside the expanded sparse directory).
152 if (ignore_skip_worktree
) {
153 ensure_full_index(&the_index
);
154 ce
= the_index
.cache
[i
];
158 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
160 if (ce_stage(ce
) != checkout_stage
161 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
163 if (prefix
&& *prefix
&&
164 (ce_namelen(ce
) <= prefix_length
||
165 memcmp(prefix
, ce
->name
, prefix_length
)))
167 if (last_ce
&& to_tempfile
) {
168 if (ce_namelen(last_ce
) != ce_namelen(ce
)
169 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
170 write_tempfile_record(last_ce
->name
, prefix
);
172 if (checkout_entry(ce
, &state
,
173 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
178 if (last_ce
&& to_tempfile
)
179 write_tempfile_record(last_ce
->name
, prefix
);
183 static const char * const builtin_checkout_index_usage
[] = {
184 N_("git checkout-index [<options>] [--] [<file>...]"),
188 static int option_parse_stage(const struct option
*opt
,
189 const char *arg
, int unset
)
191 BUG_ON_OPT_NEG(unset
);
193 if (!strcmp(arg
, "all")) {
195 checkout_stage
= CHECKOUT_ALL
;
198 if ('1' <= ch
&& ch
<= '3')
199 checkout_stage
= arg
[0] - '0';
201 die(_("stage should be between 1 and 3 or all"));
206 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
209 struct lock_file lock_file
= LOCK_INIT
;
211 int read_from_stdin
= 0;
213 int force
= 0, quiet
= 0, not_new
= 0;
216 int pc_workers
, pc_threshold
;
217 struct option builtin_checkout_index_options
[] = {
218 OPT_BOOL('a', "all", &all
,
219 N_("check out all files in the index")),
220 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree
,
221 N_("do not skip files with skip-worktree set")),
222 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
224 N_("no warning for existing files and files not in index")),
225 OPT_BOOL('n', "no-create", ¬_new
,
226 N_("don't checkout new files")),
227 OPT_BOOL('u', "index", &index_opt
,
228 N_("update stat information in the index file")),
229 OPT_BOOL('z', NULL
, &nul_term_line
,
230 N_("paths are separated with NUL character")),
231 OPT_BOOL(0, "stdin", &read_from_stdin
,
232 N_("read list of paths from the standard input")),
233 OPT_BOOL(0, "temp", &to_tempfile
,
234 N_("write the content to temporary files")),
235 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
236 N_("when creating files, prepend <string>")),
237 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
238 N_("copy out the files from named stage"),
239 PARSE_OPT_NONEG
, option_parse_stage
),
243 if (argc
== 2 && !strcmp(argv
[1], "-h"))
244 usage_with_options(builtin_checkout_index_usage
,
245 builtin_checkout_index_options
);
246 git_config(git_default_config
, NULL
);
247 prefix_length
= prefix
? strlen(prefix
) : 0;
249 prepare_repo_settings(the_repository
);
250 the_repository
->settings
.command_requires_full_index
= 0;
252 if (repo_read_index(the_repository
) < 0) {
253 die("invalid cache");
256 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
257 builtin_checkout_index_usage
, 0);
258 state
.istate
= &the_index
;
261 state
.not_new
= not_new
;
265 state
.base_dir_len
= strlen(state
.base_dir
);
268 * when --prefix is specified we do not want to update cache.
270 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
271 state
.refresh_cache
= 1;
272 state
.istate
= &the_index
;
273 repo_hold_locked_index(the_repository
, &lock_file
,
277 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
279 init_parallel_checkout();
281 /* Check out named files first */
282 for (i
= 0; i
< argc
; i
++) {
283 const char *arg
= argv
[i
];
287 die("git checkout-index: don't mix '--all' and explicit filenames");
289 die("git checkout-index: don't mix '--stdin' and explicit filenames");
290 p
= prefix_path(prefix
, prefix_length
, arg
);
291 err
|= checkout_file(p
, prefix
);
295 if (read_from_stdin
) {
296 struct strbuf buf
= STRBUF_INIT
;
297 struct strbuf unquoted
= STRBUF_INIT
;
298 strbuf_getline_fn getline_fn
;
301 die("git checkout-index: don't mix '--all' and '--stdin'");
303 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
304 while (getline_fn(&buf
, stdin
) != EOF
) {
306 if (!nul_term_line
&& buf
.buf
[0] == '"') {
307 strbuf_reset(&unquoted
);
308 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
309 die("line is badly quoted");
310 strbuf_swap(&buf
, &unquoted
);
312 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
313 err
|= checkout_file(p
, prefix
);
316 strbuf_release(&unquoted
);
317 strbuf_release(&buf
);
321 err
|= checkout_all(prefix
, prefix_length
);
324 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
330 if (is_lock_file_locked(&lock_file
) &&
331 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
332 die("Unable to write new index file");