2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_INDEX_VARIABLE
13 #include "repository.h"
14 #include "cache-tree.h"
15 #include "parse-options.h"
17 #include "parallel-checkout.h"
18 #include "read-cache-ll.h"
20 #include "sparse-index.h"
22 #define CHECKOUT_ALL 4
23 static int nul_term_line
;
24 static int checkout_stage
; /* default to checkout stage0 */
25 static int ignore_skip_worktree
; /* default to 0 */
26 static int to_tempfile
= -1;
27 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
29 static struct checkout state
= CHECKOUT_INIT
;
31 static void write_tempfile_record(const char *name
, const char *prefix
)
34 int have_tempname
= 0;
36 if (CHECKOUT_ALL
== checkout_stage
) {
37 for (i
= 1; i
< 4; i
++)
44 for (i
= 1; i
< 4; i
++) {
48 fputs(topath
[i
], stdout
);
53 } else if (topath
[checkout_stage
][0]) {
55 fputs(topath
[checkout_stage
], stdout
);
60 write_name_quoted_relative(name
, prefix
, stdout
,
61 nul_term_line
? '\0' : '\n');
64 for (i
= 0; i
< 4; i
++) {
69 static int checkout_file(const char *name
, const char *prefix
)
71 int namelen
= strlen(name
);
72 int pos
= index_name_pos(&the_index
, name
, namelen
);
73 int has_same_name
= 0;
82 while (pos
< the_index
.cache_nr
) {
83 struct cache_entry
*ce
= the_index
.cache
[pos
];
84 if (ce_namelen(ce
) != namelen
||
85 memcmp(ce
->name
, name
, namelen
))
89 if (S_ISSPARSEDIR(ce
->ce_mode
))
92 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
95 if (ce_stage(ce
) != checkout_stage
96 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
99 if (checkout_entry(ce
, &state
,
100 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
107 write_tempfile_record(name
, prefix
);
108 return errs
> 0 ? -1 : 0;
112 * At this point we know we didn't try to check anything out. If it was
113 * because we did find an entry but it was stage 0, that's not an
116 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
120 fprintf(stderr
, "git checkout-index: %s ", name
);
122 fprintf(stderr
, "is not in the cache");
124 fprintf(stderr
, "is a sparse directory");
126 fprintf(stderr
, "has skip-worktree enabled; "
127 "use '--ignore-skip-worktree-bits' to checkout");
128 else if (checkout_stage
)
129 fprintf(stderr
, "does not exist at stage %d",
132 fprintf(stderr
, "is unmerged");
138 static int checkout_all(const char *prefix
, int prefix_length
)
141 struct cache_entry
*last_ce
= NULL
;
143 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
144 struct cache_entry
*ce
= the_index
.cache
[i
];
146 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
147 if (!ce_skip_worktree(ce
))
148 BUG("sparse directory '%s' does not have skip-worktree set", ce
->name
);
151 * If the current entry is a sparse directory and skip-worktree
152 * entries are being checked out, expand the index and continue
153 * the loop on the current index position (now pointing to the
154 * first entry inside the expanded sparse directory).
156 if (ignore_skip_worktree
) {
157 ensure_full_index(&the_index
);
158 ce
= the_index
.cache
[i
];
162 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
164 if (ce_stage(ce
) != checkout_stage
165 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
167 if (prefix
&& *prefix
&&
168 (ce_namelen(ce
) <= prefix_length
||
169 memcmp(prefix
, ce
->name
, prefix_length
)))
171 if (last_ce
&& to_tempfile
) {
172 if (ce_namelen(last_ce
) != ce_namelen(ce
)
173 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
174 write_tempfile_record(last_ce
->name
, prefix
);
176 if (checkout_entry(ce
, &state
,
177 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
182 if (last_ce
&& to_tempfile
)
183 write_tempfile_record(last_ce
->name
, prefix
);
187 static const char * const builtin_checkout_index_usage
[] = {
188 N_("git checkout-index [<options>] [--] [<file>...]"),
192 static int option_parse_stage(const struct option
*opt
,
193 const char *arg
, int unset
)
195 int *stage
= opt
->value
;
197 BUG_ON_OPT_NEG(unset
);
199 if (!strcmp(arg
, "all")) {
200 *stage
= CHECKOUT_ALL
;
203 if ('1' <= ch
&& ch
<= '3')
204 *stage
= arg
[0] - '0';
206 die(_("stage should be between 1 and 3 or all"));
211 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
214 struct lock_file lock_file
= LOCK_INIT
;
216 int read_from_stdin
= 0;
218 int force
= 0, quiet
= 0, not_new
= 0;
221 int pc_workers
, pc_threshold
;
222 struct option builtin_checkout_index_options
[] = {
223 OPT_BOOL('a', "all", &all
,
224 N_("check out all files in the index")),
225 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree
,
226 N_("do not skip files with skip-worktree set")),
227 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
229 N_("no warning for existing files and files not in index")),
230 OPT_BOOL('n', "no-create", ¬_new
,
231 N_("don't checkout new files")),
232 OPT_BOOL('u', "index", &index_opt
,
233 N_("update stat information in the index file")),
234 OPT_BOOL('z', NULL
, &nul_term_line
,
235 N_("paths are separated with NUL character")),
236 OPT_BOOL(0, "stdin", &read_from_stdin
,
237 N_("read list of paths from the standard input")),
238 OPT_BOOL(0, "temp", &to_tempfile
,
239 N_("write the content to temporary files")),
240 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
241 N_("when creating files, prepend <string>")),
242 OPT_CALLBACK_F(0, "stage", &checkout_stage
, "(1|2|3|all)",
243 N_("copy out the files from named stage"),
244 PARSE_OPT_NONEG
, option_parse_stage
),
248 if (argc
== 2 && !strcmp(argv
[1], "-h"))
249 usage_with_options(builtin_checkout_index_usage
,
250 builtin_checkout_index_options
);
251 git_config(git_default_config
, NULL
);
252 prefix_length
= prefix
? strlen(prefix
) : 0;
254 prepare_repo_settings(the_repository
);
255 the_repository
->settings
.command_requires_full_index
= 0;
257 if (repo_read_index(the_repository
) < 0) {
258 die("invalid cache");
261 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
262 builtin_checkout_index_usage
, 0);
263 state
.istate
= &the_index
;
266 state
.not_new
= not_new
;
270 state
.base_dir_len
= strlen(state
.base_dir
);
273 to_tempfile
= (checkout_stage
== CHECKOUT_ALL
);
274 if (!to_tempfile
&& checkout_stage
== CHECKOUT_ALL
)
275 die(_("options '%s' and '%s' cannot be used together"),
276 "--stage=all", "--no-temp");
279 * when --prefix is specified we do not want to update cache.
281 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
282 state
.refresh_cache
= 1;
283 state
.istate
= &the_index
;
284 repo_hold_locked_index(the_repository
, &lock_file
,
288 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
290 init_parallel_checkout();
292 /* Check out named files first */
293 for (i
= 0; i
< argc
; i
++) {
294 const char *arg
= argv
[i
];
298 die("git checkout-index: don't mix '--all' and explicit filenames");
300 die("git checkout-index: don't mix '--stdin' and explicit filenames");
301 p
= prefix_path(prefix
, prefix_length
, arg
);
302 err
|= checkout_file(p
, prefix
);
306 if (read_from_stdin
) {
307 struct strbuf buf
= STRBUF_INIT
;
308 struct strbuf unquoted
= STRBUF_INIT
;
309 strbuf_getline_fn getline_fn
;
312 die("git checkout-index: don't mix '--all' and '--stdin'");
314 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
315 while (getline_fn(&buf
, stdin
) != EOF
) {
317 if (!nul_term_line
&& buf
.buf
[0] == '"') {
318 strbuf_reset(&unquoted
);
319 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
320 die("line is badly quoted");
321 strbuf_swap(&buf
, &unquoted
);
323 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
324 err
|= checkout_file(p
, prefix
);
327 strbuf_release(&unquoted
);
328 strbuf_release(&buf
);
332 err
|= checkout_all(prefix
, prefix_length
);
335 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
341 if (is_lock_file_locked(&lock_file
) &&
342 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
343 die("Unable to write new index file");