2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_INDEX_VARIABLE
14 #include "cache-tree.h"
15 #include "parse-options.h"
17 #include "parallel-checkout.h"
19 #define CHECKOUT_ALL 4
20 static int nul_term_line
;
21 static int checkout_stage
; /* default to checkout stage0 */
22 static int ignore_skip_worktree
; /* default to 0 */
23 static int to_tempfile
;
24 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
26 static struct checkout state
= CHECKOUT_INIT
;
28 static void write_tempfile_record(const char *name
, const char *prefix
)
31 int have_tempname
= 0;
33 if (CHECKOUT_ALL
== checkout_stage
) {
34 for (i
= 1; i
< 4; i
++)
41 for (i
= 1; i
< 4; i
++) {
45 fputs(topath
[i
], stdout
);
50 } else if (topath
[checkout_stage
][0]) {
52 fputs(topath
[checkout_stage
], stdout
);
57 write_name_quoted_relative(name
, prefix
, stdout
,
58 nul_term_line
? '\0' : '\n');
61 for (i
= 0; i
< 4; i
++) {
66 static int checkout_file(const char *name
, const char *prefix
)
68 int namelen
= strlen(name
);
69 int pos
= index_name_pos(&the_index
, name
, namelen
);
70 int has_same_name
= 0;
79 while (pos
< the_index
.cache_nr
) {
80 struct cache_entry
*ce
= the_index
.cache
[pos
];
81 if (ce_namelen(ce
) != namelen
||
82 memcmp(ce
->name
, name
, namelen
))
86 if (S_ISSPARSEDIR(ce
->ce_mode
))
89 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
92 if (ce_stage(ce
) != checkout_stage
93 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
96 if (checkout_entry(ce
, &state
,
97 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
104 write_tempfile_record(name
, prefix
);
105 return errs
> 0 ? -1 : 0;
109 * At this point we know we didn't try to check anything out. If it was
110 * because we did find an entry but it was stage 0, that's not an
113 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
117 fprintf(stderr
, "git checkout-index: %s ", name
);
119 fprintf(stderr
, "is not in the cache");
121 fprintf(stderr
, "is a sparse directory");
123 fprintf(stderr
, "has skip-worktree enabled; "
124 "use '--ignore-skip-worktree-bits' to checkout");
125 else if (checkout_stage
)
126 fprintf(stderr
, "does not exist at stage %d",
129 fprintf(stderr
, "is unmerged");
135 static int checkout_all(const char *prefix
, int prefix_length
)
138 struct cache_entry
*last_ce
= NULL
;
140 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
141 struct cache_entry
*ce
= the_index
.cache
[i
];
143 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
144 if (!ce_skip_worktree(ce
))
145 BUG("sparse directory '%s' does not have skip-worktree set", ce
->name
);
148 * If the current entry is a sparse directory and skip-worktree
149 * entries are being checked out, expand the index and continue
150 * the loop on the current index position (now pointing to the
151 * first entry inside the expanded sparse directory).
153 if (ignore_skip_worktree
) {
154 ensure_full_index(&the_index
);
155 ce
= the_index
.cache
[i
];
159 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
161 if (ce_stage(ce
) != checkout_stage
162 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
164 if (prefix
&& *prefix
&&
165 (ce_namelen(ce
) <= prefix_length
||
166 memcmp(prefix
, ce
->name
, prefix_length
)))
168 if (last_ce
&& to_tempfile
) {
169 if (ce_namelen(last_ce
) != ce_namelen(ce
)
170 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
171 write_tempfile_record(last_ce
->name
, prefix
);
173 if (checkout_entry(ce
, &state
,
174 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
179 if (last_ce
&& to_tempfile
)
180 write_tempfile_record(last_ce
->name
, prefix
);
184 static const char * const builtin_checkout_index_usage
[] = {
185 N_("git checkout-index [<options>] [--] [<file>...]"),
189 static int option_parse_stage(const struct option
*opt
,
190 const char *arg
, int unset
)
192 BUG_ON_OPT_NEG(unset
);
194 if (!strcmp(arg
, "all")) {
196 checkout_stage
= CHECKOUT_ALL
;
199 if ('1' <= ch
&& ch
<= '3')
200 checkout_stage
= arg
[0] - '0';
202 die(_("stage should be between 1 and 3 or all"));
207 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
210 struct lock_file lock_file
= LOCK_INIT
;
212 int read_from_stdin
= 0;
214 int force
= 0, quiet
= 0, not_new
= 0;
217 int pc_workers
, pc_threshold
;
218 struct option builtin_checkout_index_options
[] = {
219 OPT_BOOL('a', "all", &all
,
220 N_("check out all files in the index")),
221 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree
,
222 N_("do not skip files with skip-worktree set")),
223 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
225 N_("no warning for existing files and files not in index")),
226 OPT_BOOL('n', "no-create", ¬_new
,
227 N_("don't checkout new files")),
228 OPT_BOOL('u', "index", &index_opt
,
229 N_("update stat information in the index file")),
230 OPT_BOOL('z', NULL
, &nul_term_line
,
231 N_("paths are separated with NUL character")),
232 OPT_BOOL(0, "stdin", &read_from_stdin
,
233 N_("read list of paths from the standard input")),
234 OPT_BOOL(0, "temp", &to_tempfile
,
235 N_("write the content to temporary files")),
236 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
237 N_("when creating files, prepend <string>")),
238 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
239 N_("copy out the files from named stage"),
240 PARSE_OPT_NONEG
, option_parse_stage
),
244 if (argc
== 2 && !strcmp(argv
[1], "-h"))
245 usage_with_options(builtin_checkout_index_usage
,
246 builtin_checkout_index_options
);
247 git_config(git_default_config
, NULL
);
248 prefix_length
= prefix
? strlen(prefix
) : 0;
250 prepare_repo_settings(the_repository
);
251 the_repository
->settings
.command_requires_full_index
= 0;
253 if (repo_read_index(the_repository
) < 0) {
254 die("invalid cache");
257 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
258 builtin_checkout_index_usage
, 0);
259 state
.istate
= &the_index
;
262 state
.not_new
= not_new
;
266 state
.base_dir_len
= strlen(state
.base_dir
);
269 * when --prefix is specified we do not want to update cache.
271 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
272 state
.refresh_cache
= 1;
273 state
.istate
= &the_index
;
274 repo_hold_locked_index(the_repository
, &lock_file
,
278 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
280 init_parallel_checkout();
282 /* Check out named files first */
283 for (i
= 0; i
< argc
; i
++) {
284 const char *arg
= argv
[i
];
288 die("git checkout-index: don't mix '--all' and explicit filenames");
290 die("git checkout-index: don't mix '--stdin' and explicit filenames");
291 p
= prefix_path(prefix
, prefix_length
, arg
);
292 err
|= checkout_file(p
, prefix
);
296 if (read_from_stdin
) {
297 struct strbuf buf
= STRBUF_INIT
;
298 struct strbuf unquoted
= STRBUF_INIT
;
299 strbuf_getline_fn getline_fn
;
302 die("git checkout-index: don't mix '--all' and '--stdin'");
304 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
305 while (getline_fn(&buf
, stdin
) != EOF
) {
307 if (!nul_term_line
&& buf
.buf
[0] == '"') {
308 strbuf_reset(&unquoted
);
309 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
310 die("line is badly quoted");
311 strbuf_swap(&buf
, &unquoted
);
313 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
314 err
|= checkout_file(p
, prefix
);
317 strbuf_release(&unquoted
);
318 strbuf_release(&buf
);
322 err
|= checkout_all(prefix
, prefix_length
);
325 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
331 if (is_lock_file_locked(&lock_file
) &&
332 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
333 die("Unable to write new index file");