2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_INDEX_VARIABLE
14 #include "repository.h"
15 #include "cache-tree.h"
16 #include "parse-options.h"
18 #include "parallel-checkout.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
;
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_index
, name
, namelen
);
72 int has_same_name
= 0;
81 while (pos
< the_index
.cache_nr
) {
82 struct cache_entry
*ce
= the_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_index
.cache_nr
; i
++) {
143 struct cache_entry
*ce
= the_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_index
);
157 ce
= the_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 BUG_ON_OPT_NEG(unset
);
196 if (!strcmp(arg
, "all")) {
198 checkout_stage
= CHECKOUT_ALL
;
201 if ('1' <= ch
&& ch
<= '3')
202 checkout_stage
= arg
[0] - '0';
204 die(_("stage should be between 1 and 3 or all"));
209 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
212 struct lock_file lock_file
= LOCK_INIT
;
214 int read_from_stdin
= 0;
216 int force
= 0, quiet
= 0, not_new
= 0;
219 int pc_workers
, pc_threshold
;
220 struct option builtin_checkout_index_options
[] = {
221 OPT_BOOL('a', "all", &all
,
222 N_("check out all files in the index")),
223 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree
,
224 N_("do not skip files with skip-worktree set")),
225 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
227 N_("no warning for existing files and files not in index")),
228 OPT_BOOL('n', "no-create", ¬_new
,
229 N_("don't checkout new files")),
230 OPT_BOOL('u', "index", &index_opt
,
231 N_("update stat information in the index file")),
232 OPT_BOOL('z', NULL
, &nul_term_line
,
233 N_("paths are separated with NUL character")),
234 OPT_BOOL(0, "stdin", &read_from_stdin
,
235 N_("read list of paths from the standard input")),
236 OPT_BOOL(0, "temp", &to_tempfile
,
237 N_("write the content to temporary files")),
238 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
239 N_("when creating files, prepend <string>")),
240 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
241 N_("copy out the files from named stage"),
242 PARSE_OPT_NONEG
, option_parse_stage
),
246 if (argc
== 2 && !strcmp(argv
[1], "-h"))
247 usage_with_options(builtin_checkout_index_usage
,
248 builtin_checkout_index_options
);
249 git_config(git_default_config
, NULL
);
250 prefix_length
= prefix
? strlen(prefix
) : 0;
252 prepare_repo_settings(the_repository
);
253 the_repository
->settings
.command_requires_full_index
= 0;
255 if (repo_read_index(the_repository
) < 0) {
256 die("invalid cache");
259 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
260 builtin_checkout_index_usage
, 0);
261 state
.istate
= &the_index
;
264 state
.not_new
= not_new
;
268 state
.base_dir_len
= strlen(state
.base_dir
);
271 * when --prefix is specified we do not want to update cache.
273 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
274 state
.refresh_cache
= 1;
275 state
.istate
= &the_index
;
276 repo_hold_locked_index(the_repository
, &lock_file
,
280 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
282 init_parallel_checkout();
284 /* Check out named files first */
285 for (i
= 0; i
< argc
; i
++) {
286 const char *arg
= argv
[i
];
290 die("git checkout-index: don't mix '--all' and explicit filenames");
292 die("git checkout-index: don't mix '--stdin' and explicit filenames");
293 p
= prefix_path(prefix
, prefix_length
, arg
);
294 err
|= checkout_file(p
, prefix
);
298 if (read_from_stdin
) {
299 struct strbuf buf
= STRBUF_INIT
;
300 struct strbuf unquoted
= STRBUF_INIT
;
301 strbuf_getline_fn getline_fn
;
304 die("git checkout-index: don't mix '--all' and '--stdin'");
306 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
307 while (getline_fn(&buf
, stdin
) != EOF
) {
309 if (!nul_term_line
&& buf
.buf
[0] == '"') {
310 strbuf_reset(&unquoted
);
311 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
312 die("line is badly quoted");
313 strbuf_swap(&buf
, &unquoted
);
315 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
316 err
|= checkout_file(p
, prefix
);
319 strbuf_release(&unquoted
);
320 strbuf_release(&buf
);
324 err
|= checkout_all(prefix
, prefix_length
);
327 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
333 if (is_lock_file_locked(&lock_file
) &&
334 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
335 die("Unable to write new index file");