2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
13 #include "cache-tree.h"
14 #include "parse-options.h"
16 #include "parallel-checkout.h"
18 #define CHECKOUT_ALL 4
19 static int nul_term_line
;
20 static int checkout_stage
; /* default to checkout stage0 */
21 static int ignore_skip_worktree
; /* default to 0 */
22 static int to_tempfile
;
23 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
25 static struct checkout state
= CHECKOUT_INIT
;
27 static void write_tempfile_record(const char *name
, const char *prefix
)
30 int have_tempname
= 0;
32 if (CHECKOUT_ALL
== checkout_stage
) {
33 for (i
= 1; i
< 4; i
++)
40 for (i
= 1; i
< 4; i
++) {
44 fputs(topath
[i
], stdout
);
49 } else if (topath
[checkout_stage
][0]) {
51 fputs(topath
[checkout_stage
], stdout
);
56 write_name_quoted_relative(name
, prefix
, stdout
,
57 nul_term_line
? '\0' : '\n');
60 for (i
= 0; i
< 4; i
++) {
65 static int checkout_file(const char *name
, const char *prefix
)
67 int namelen
= strlen(name
);
68 int pos
= cache_name_pos(name
, namelen
);
69 int has_same_name
= 0;
77 while (pos
< active_nr
) {
78 struct cache_entry
*ce
= active_cache
[pos
];
79 if (ce_namelen(ce
) != namelen
||
80 memcmp(ce
->name
, name
, namelen
))
84 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
87 if (ce_stage(ce
) != checkout_stage
88 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
91 if (checkout_entry(ce
, &state
,
92 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
99 write_tempfile_record(name
, prefix
);
100 return errs
> 0 ? -1 : 0;
104 * At this point we know we didn't try to check anything out. If it was
105 * because we did find an entry but it was stage 0, that's not an
108 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
112 fprintf(stderr
, "git checkout-index: %s ", name
);
114 fprintf(stderr
, "is not in the cache");
116 fprintf(stderr
, "has skip-worktree enabled; "
117 "use '--ignore-skip-worktree-bits' to checkout");
118 else if (checkout_stage
)
119 fprintf(stderr
, "does not exist at stage %d",
122 fprintf(stderr
, "is unmerged");
128 static int checkout_all(const char *prefix
, int prefix_length
)
131 struct cache_entry
*last_ce
= NULL
;
133 /* TODO: audit for interaction with sparse-index. */
134 ensure_full_index(&the_index
);
135 for (i
= 0; i
< active_nr
; i
++) {
136 struct cache_entry
*ce
= active_cache
[i
];
137 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
139 if (ce_stage(ce
) != checkout_stage
140 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
142 if (prefix
&& *prefix
&&
143 (ce_namelen(ce
) <= prefix_length
||
144 memcmp(prefix
, ce
->name
, prefix_length
)))
146 if (last_ce
&& to_tempfile
) {
147 if (ce_namelen(last_ce
) != ce_namelen(ce
)
148 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
149 write_tempfile_record(last_ce
->name
, prefix
);
151 if (checkout_entry(ce
, &state
,
152 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
157 if (last_ce
&& to_tempfile
)
158 write_tempfile_record(last_ce
->name
, prefix
);
162 static const char * const builtin_checkout_index_usage
[] = {
163 N_("git checkout-index [<options>] [--] [<file>...]"),
167 static int option_parse_stage(const struct option
*opt
,
168 const char *arg
, int unset
)
170 BUG_ON_OPT_NEG(unset
);
172 if (!strcmp(arg
, "all")) {
174 checkout_stage
= CHECKOUT_ALL
;
177 if ('1' <= ch
&& ch
<= '3')
178 checkout_stage
= arg
[0] - '0';
180 die(_("stage should be between 1 and 3 or all"));
185 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
188 struct lock_file lock_file
= LOCK_INIT
;
190 int read_from_stdin
= 0;
192 int force
= 0, quiet
= 0, not_new
= 0;
195 int pc_workers
, pc_threshold
;
196 struct option builtin_checkout_index_options
[] = {
197 OPT_BOOL('a', "all", &all
,
198 N_("check out all files in the index")),
199 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree
,
200 N_("do not skip files with skip-worktree set")),
201 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
203 N_("no warning for existing files and files not in index")),
204 OPT_BOOL('n', "no-create", ¬_new
,
205 N_("don't checkout new files")),
206 OPT_BOOL('u', "index", &index_opt
,
207 N_("update stat information in the index file")),
208 OPT_BOOL('z', NULL
, &nul_term_line
,
209 N_("paths are separated with NUL character")),
210 OPT_BOOL(0, "stdin", &read_from_stdin
,
211 N_("read list of paths from the standard input")),
212 OPT_BOOL(0, "temp", &to_tempfile
,
213 N_("write the content to temporary files")),
214 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
215 N_("when creating files, prepend <string>")),
216 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
217 N_("copy out the files from named stage"),
218 PARSE_OPT_NONEG
, option_parse_stage
),
222 if (argc
== 2 && !strcmp(argv
[1], "-h"))
223 usage_with_options(builtin_checkout_index_usage
,
224 builtin_checkout_index_options
);
225 git_config(git_default_config
, NULL
);
226 prefix_length
= prefix
? strlen(prefix
) : 0;
228 if (read_cache() < 0) {
229 die("invalid cache");
232 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
233 builtin_checkout_index_usage
, 0);
234 state
.istate
= &the_index
;
237 state
.not_new
= not_new
;
241 state
.base_dir_len
= strlen(state
.base_dir
);
244 * when --prefix is specified we do not want to update cache.
246 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
247 state
.refresh_cache
= 1;
248 state
.istate
= &the_index
;
249 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
252 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
254 init_parallel_checkout();
256 /* Check out named files first */
257 for (i
= 0; i
< argc
; i
++) {
258 const char *arg
= argv
[i
];
262 die("git checkout-index: don't mix '--all' and explicit filenames");
264 die("git checkout-index: don't mix '--stdin' and explicit filenames");
265 p
= prefix_path(prefix
, prefix_length
, arg
);
266 err
|= checkout_file(p
, prefix
);
270 if (read_from_stdin
) {
271 struct strbuf buf
= STRBUF_INIT
;
272 struct strbuf unquoted
= STRBUF_INIT
;
273 strbuf_getline_fn getline_fn
;
276 die("git checkout-index: don't mix '--all' and '--stdin'");
278 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
279 while (getline_fn(&buf
, stdin
) != EOF
) {
281 if (!nul_term_line
&& buf
.buf
[0] == '"') {
282 strbuf_reset(&unquoted
);
283 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
284 die("line is badly quoted");
285 strbuf_swap(&buf
, &unquoted
);
287 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
288 err
|= checkout_file(p
, prefix
);
291 strbuf_release(&unquoted
);
292 strbuf_release(&buf
);
296 err
|= checkout_all(prefix
, prefix_length
);
299 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
305 if (is_lock_file_locked(&lock_file
) &&
306 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
307 die("Unable to write new index file");