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"
20 #define CHECKOUT_ALL 4
21 static int nul_term_line
;
22 static int checkout_stage
; /* default to checkout stage0 */
23 static int ignore_skip_worktree
; /* default to 0 */
24 static int to_tempfile
;
25 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
27 static struct checkout state
= CHECKOUT_INIT
;
29 static void write_tempfile_record(const char *name
, const char *prefix
)
32 int have_tempname
= 0;
34 if (CHECKOUT_ALL
== checkout_stage
) {
35 for (i
= 1; i
< 4; i
++)
42 for (i
= 1; i
< 4; i
++) {
46 fputs(topath
[i
], stdout
);
51 } else if (topath
[checkout_stage
][0]) {
53 fputs(topath
[checkout_stage
], stdout
);
58 write_name_quoted_relative(name
, prefix
, stdout
,
59 nul_term_line
? '\0' : '\n');
62 for (i
= 0; i
< 4; i
++) {
67 static int checkout_file(const char *name
, const char *prefix
)
69 int namelen
= strlen(name
);
70 int pos
= index_name_pos(&the_index
, name
, namelen
);
71 int has_same_name
= 0;
80 while (pos
< the_index
.cache_nr
) {
81 struct cache_entry
*ce
= the_index
.cache
[pos
];
82 if (ce_namelen(ce
) != namelen
||
83 memcmp(ce
->name
, name
, namelen
))
87 if (S_ISSPARSEDIR(ce
->ce_mode
))
90 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
93 if (ce_stage(ce
) != checkout_stage
94 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
97 if (checkout_entry(ce
, &state
,
98 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
105 write_tempfile_record(name
, prefix
);
106 return errs
> 0 ? -1 : 0;
110 * At this point we know we didn't try to check anything out. If it was
111 * because we did find an entry but it was stage 0, that's not an
114 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
118 fprintf(stderr
, "git checkout-index: %s ", name
);
120 fprintf(stderr
, "is not in the cache");
122 fprintf(stderr
, "is a sparse directory");
124 fprintf(stderr
, "has skip-worktree enabled; "
125 "use '--ignore-skip-worktree-bits' to checkout");
126 else if (checkout_stage
)
127 fprintf(stderr
, "does not exist at stage %d",
130 fprintf(stderr
, "is unmerged");
136 static int checkout_all(const char *prefix
, int prefix_length
)
139 struct cache_entry
*last_ce
= NULL
;
141 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
142 struct cache_entry
*ce
= the_index
.cache
[i
];
144 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
145 if (!ce_skip_worktree(ce
))
146 BUG("sparse directory '%s' does not have skip-worktree set", ce
->name
);
149 * If the current entry is a sparse directory and skip-worktree
150 * entries are being checked out, expand the index and continue
151 * the loop on the current index position (now pointing to the
152 * first entry inside the expanded sparse directory).
154 if (ignore_skip_worktree
) {
155 ensure_full_index(&the_index
);
156 ce
= the_index
.cache
[i
];
160 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
162 if (ce_stage(ce
) != checkout_stage
163 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
165 if (prefix
&& *prefix
&&
166 (ce_namelen(ce
) <= prefix_length
||
167 memcmp(prefix
, ce
->name
, prefix_length
)))
169 if (last_ce
&& to_tempfile
) {
170 if (ce_namelen(last_ce
) != ce_namelen(ce
)
171 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
172 write_tempfile_record(last_ce
->name
, prefix
);
174 if (checkout_entry(ce
, &state
,
175 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
180 if (last_ce
&& to_tempfile
)
181 write_tempfile_record(last_ce
->name
, prefix
);
185 static const char * const builtin_checkout_index_usage
[] = {
186 N_("git checkout-index [<options>] [--] [<file>...]"),
190 static int option_parse_stage(const struct option
*opt
,
191 const char *arg
, int unset
)
193 BUG_ON_OPT_NEG(unset
);
195 if (!strcmp(arg
, "all")) {
197 checkout_stage
= CHECKOUT_ALL
;
200 if ('1' <= ch
&& ch
<= '3')
201 checkout_stage
= arg
[0] - '0';
203 die(_("stage should be between 1 and 3 or all"));
208 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
211 struct lock_file lock_file
= LOCK_INIT
;
213 int read_from_stdin
= 0;
215 int force
= 0, quiet
= 0, not_new
= 0;
218 int pc_workers
, pc_threshold
;
219 struct option builtin_checkout_index_options
[] = {
220 OPT_BOOL('a', "all", &all
,
221 N_("check out all files in the index")),
222 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree
,
223 N_("do not skip files with skip-worktree set")),
224 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
226 N_("no warning for existing files and files not in index")),
227 OPT_BOOL('n', "no-create", ¬_new
,
228 N_("don't checkout new files")),
229 OPT_BOOL('u', "index", &index_opt
,
230 N_("update stat information in the index file")),
231 OPT_BOOL('z', NULL
, &nul_term_line
,
232 N_("paths are separated with NUL character")),
233 OPT_BOOL(0, "stdin", &read_from_stdin
,
234 N_("read list of paths from the standard input")),
235 OPT_BOOL(0, "temp", &to_tempfile
,
236 N_("write the content to temporary files")),
237 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
238 N_("when creating files, prepend <string>")),
239 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
240 N_("copy out the files from named stage"),
241 PARSE_OPT_NONEG
, option_parse_stage
),
245 if (argc
== 2 && !strcmp(argv
[1], "-h"))
246 usage_with_options(builtin_checkout_index_usage
,
247 builtin_checkout_index_options
);
248 git_config(git_default_config
, NULL
);
249 prefix_length
= prefix
? strlen(prefix
) : 0;
251 prepare_repo_settings(the_repository
);
252 the_repository
->settings
.command_requires_full_index
= 0;
254 if (repo_read_index(the_repository
) < 0) {
255 die("invalid cache");
258 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
259 builtin_checkout_index_usage
, 0);
260 state
.istate
= &the_index
;
263 state
.not_new
= not_new
;
267 state
.base_dir_len
= strlen(state
.base_dir
);
270 * when --prefix is specified we do not want to update cache.
272 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
273 state
.refresh_cache
= 1;
274 state
.istate
= &the_index
;
275 repo_hold_locked_index(the_repository
, &lock_file
,
279 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
281 init_parallel_checkout();
283 /* Check out named files first */
284 for (i
= 0; i
< argc
; i
++) {
285 const char *arg
= argv
[i
];
289 die("git checkout-index: don't mix '--all' and explicit filenames");
291 die("git checkout-index: don't mix '--stdin' and explicit filenames");
292 p
= prefix_path(prefix
, prefix_length
, arg
);
293 err
|= checkout_file(p
, prefix
);
297 if (read_from_stdin
) {
298 struct strbuf buf
= STRBUF_INIT
;
299 struct strbuf unquoted
= STRBUF_INIT
;
300 strbuf_getline_fn getline_fn
;
303 die("git checkout-index: don't mix '--all' and '--stdin'");
305 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
306 while (getline_fn(&buf
, stdin
) != EOF
) {
308 if (!nul_term_line
&& buf
.buf
[0] == '"') {
309 strbuf_reset(&unquoted
);
310 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
311 die("line is badly quoted");
312 strbuf_swap(&buf
, &unquoted
);
314 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
315 err
|= checkout_file(p
, prefix
);
318 strbuf_release(&unquoted
);
319 strbuf_release(&buf
);
323 err
|= checkout_all(prefix
, prefix_length
);
326 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
332 if (is_lock_file_locked(&lock_file
) &&
333 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
334 die("Unable to write new index file");