2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_REPOSITORY_VARIABLE
13 #include "cache-tree.h"
14 #include "parse-options.h"
16 #include "parallel-checkout.h"
17 #include "read-cache-ll.h"
19 #include "sparse-index.h"
21 #define CHECKOUT_ALL 4
22 static int nul_term_line
;
23 static int checkout_stage
; /* default to checkout stage0 */
24 static int ignore_skip_worktree
; /* default to 0 */
25 static int to_tempfile
= -1;
26 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
28 static struct checkout state
= CHECKOUT_INIT
;
30 static void write_tempfile_record(const char *name
, const char *prefix
)
33 int have_tempname
= 0;
35 if (CHECKOUT_ALL
== checkout_stage
) {
36 for (i
= 1; i
< 4; i
++)
43 for (i
= 1; i
< 4; i
++) {
47 fputs(topath
[i
], stdout
);
52 } else if (topath
[checkout_stage
][0]) {
54 fputs(topath
[checkout_stage
], stdout
);
59 write_name_quoted_relative(name
, prefix
, stdout
,
60 nul_term_line
? '\0' : '\n');
63 for (i
= 0; i
< 4; i
++) {
68 static int checkout_file(const char *name
, const char *prefix
)
70 int namelen
= strlen(name
);
71 int pos
= index_name_pos(the_repository
->index
, name
, namelen
);
72 int has_same_name
= 0;
81 while (pos
<the_repository
->index
->cache_nr
) {
82 struct cache_entry
*ce
=the_repository
->index
->cache
[pos
];
83 if (ce_namelen(ce
) != namelen
||
84 memcmp(ce
->name
, name
, namelen
))
88 if (S_ISSPARSEDIR(ce
->ce_mode
))
91 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
94 if (ce_stage(ce
) != checkout_stage
95 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
98 if (checkout_entry(ce
, &state
,
99 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
106 write_tempfile_record(name
, prefix
);
107 return errs
> 0 ? -1 : 0;
111 * At this point we know we didn't try to check anything out. If it was
112 * because we did find an entry but it was stage 0, that's not an
115 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
119 fprintf(stderr
, "git checkout-index: %s ", name
);
121 fprintf(stderr
, "is not in the cache");
123 fprintf(stderr
, "is a sparse directory");
125 fprintf(stderr
, "has skip-worktree enabled; "
126 "use '--ignore-skip-worktree-bits' to checkout");
127 else if (checkout_stage
)
128 fprintf(stderr
, "does not exist at stage %d",
131 fprintf(stderr
, "is unmerged");
137 static int checkout_all(const char *prefix
, int prefix_length
)
140 struct cache_entry
*last_ce
= NULL
;
142 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
143 struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
145 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
146 if (!ce_skip_worktree(ce
))
147 BUG("sparse directory '%s' does not have skip-worktree set", ce
->name
);
150 * If the current entry is a sparse directory and skip-worktree
151 * entries are being checked out, expand the index and continue
152 * the loop on the current index position (now pointing to the
153 * first entry inside the expanded sparse directory).
155 if (ignore_skip_worktree
) {
156 ensure_full_index(the_repository
->index
);
157 ce
= the_repository
->index
->cache
[i
];
161 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
163 if (ce_stage(ce
) != checkout_stage
164 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
166 if (prefix
&& *prefix
&&
167 (ce_namelen(ce
) <= prefix_length
||
168 memcmp(prefix
, ce
->name
, prefix_length
)))
170 if (last_ce
&& to_tempfile
) {
171 if (ce_namelen(last_ce
) != ce_namelen(ce
)
172 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
173 write_tempfile_record(last_ce
->name
, prefix
);
175 if (checkout_entry(ce
, &state
,
176 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
181 if (last_ce
&& to_tempfile
)
182 write_tempfile_record(last_ce
->name
, prefix
);
186 static const char * const builtin_checkout_index_usage
[] = {
187 N_("git checkout-index [<options>] [--] [<file>...]"),
191 static int option_parse_stage(const struct option
*opt
,
192 const char *arg
, int unset
)
194 int *stage
= opt
->value
;
196 BUG_ON_OPT_NEG(unset
);
198 if (!strcmp(arg
, "all")) {
199 *stage
= CHECKOUT_ALL
;
202 if ('1' <= ch
&& ch
<= '3')
203 *stage
= arg
[0] - '0';
205 die(_("stage should be between 1 and 3 or all"));
210 int cmd_checkout_index(int argc
,
213 struct repository
*repo UNUSED
)
216 struct lock_file lock_file
= LOCK_INIT
;
218 int read_from_stdin
= 0;
220 int force
= 0, quiet
= 0, not_new
= 0;
223 int pc_workers
, pc_threshold
;
224 struct option builtin_checkout_index_options
[] = {
225 OPT_BOOL('a', "all", &all
,
226 N_("check out all files in the index")),
227 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree
,
228 N_("do not skip files with skip-worktree set")),
229 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
231 N_("no warning for existing files and files not in index")),
232 OPT_BOOL('n', "no-create", ¬_new
,
233 N_("don't checkout new files")),
234 OPT_BOOL('u', "index", &index_opt
,
235 N_("update stat information in the index file")),
236 OPT_BOOL('z', NULL
, &nul_term_line
,
237 N_("paths are separated with NUL character")),
238 OPT_BOOL(0, "stdin", &read_from_stdin
,
239 N_("read list of paths from the standard input")),
240 OPT_BOOL(0, "temp", &to_tempfile
,
241 N_("write the content to temporary files")),
242 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
243 N_("when creating files, prepend <string>")),
244 OPT_CALLBACK_F(0, "stage", &checkout_stage
, "(1|2|3|all)",
245 N_("copy out the files from named stage"),
246 PARSE_OPT_NONEG
, option_parse_stage
),
250 if (argc
== 2 && !strcmp(argv
[1], "-h"))
251 usage_with_options(builtin_checkout_index_usage
,
252 builtin_checkout_index_options
);
253 git_config(git_default_config
, NULL
);
254 prefix_length
= prefix
? strlen(prefix
) : 0;
256 prepare_repo_settings(the_repository
);
257 the_repository
->settings
.command_requires_full_index
= 0;
259 if (repo_read_index(the_repository
) < 0) {
260 die("invalid cache");
263 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
264 builtin_checkout_index_usage
, 0);
265 state
.istate
= the_repository
->index
;
268 state
.not_new
= not_new
;
272 state
.base_dir_len
= strlen(state
.base_dir
);
275 to_tempfile
= (checkout_stage
== CHECKOUT_ALL
);
276 if (!to_tempfile
&& checkout_stage
== CHECKOUT_ALL
)
277 die(_("options '%s' and '%s' cannot be used together"),
278 "--stage=all", "--no-temp");
281 * when --prefix is specified we do not want to update cache.
283 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
284 state
.refresh_cache
= 1;
285 state
.istate
= the_repository
->index
;
286 repo_hold_locked_index(the_repository
, &lock_file
,
290 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
292 init_parallel_checkout();
294 /* Check out named files first */
295 for (i
= 0; i
< argc
; i
++) {
296 const char *arg
= argv
[i
];
300 die("git checkout-index: don't mix '--all' and explicit filenames");
302 die("git checkout-index: don't mix '--stdin' and explicit filenames");
303 p
= prefix_path(prefix
, prefix_length
, arg
);
304 err
|= checkout_file(p
, prefix
);
308 if (read_from_stdin
) {
309 struct strbuf buf
= STRBUF_INIT
;
310 struct strbuf unquoted
= STRBUF_INIT
;
311 strbuf_getline_fn getline_fn
;
314 die("git checkout-index: don't mix '--all' and '--stdin'");
316 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
317 while (getline_fn(&buf
, stdin
) != EOF
) {
319 if (!nul_term_line
&& buf
.buf
[0] == '"') {
320 strbuf_reset(&unquoted
);
321 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
322 die("line is badly quoted");
323 strbuf_swap(&buf
, &unquoted
);
325 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
326 err
|= checkout_file(p
, prefix
);
329 strbuf_release(&unquoted
);
330 strbuf_release(&buf
);
334 err
|= checkout_all(prefix
, prefix_length
);
337 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
343 if (is_lock_file_locked(&lock_file
) &&
344 write_locked_index(the_repository
->index
, &lock_file
, COMMIT_LOCK
))
345 die("Unable to write new index file");