2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "cache-tree.h"
13 #include "parse-options.h"
15 #include "parallel-checkout.h"
17 #define CHECKOUT_ALL 4
18 static int nul_term_line
;
19 static int checkout_stage
; /* default to checkout stage0 */
20 static int to_tempfile
;
21 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
23 static struct checkout state
= CHECKOUT_INIT
;
25 static void write_tempfile_record(const char *name
, const char *prefix
)
28 int have_tempname
= 0;
30 if (CHECKOUT_ALL
== checkout_stage
) {
31 for (i
= 1; i
< 4; i
++)
38 for (i
= 1; i
< 4; i
++) {
42 fputs(topath
[i
], stdout
);
47 } else if (topath
[checkout_stage
][0]) {
49 fputs(topath
[checkout_stage
], stdout
);
54 write_name_quoted_relative(name
, prefix
, stdout
,
55 nul_term_line
? '\0' : '\n');
58 for (i
= 0; i
< 4; i
++) {
63 static int checkout_file(const char *name
, const char *prefix
)
65 int namelen
= strlen(name
);
66 int pos
= cache_name_pos(name
, namelen
);
67 int has_same_name
= 0;
74 while (pos
< active_nr
) {
75 struct cache_entry
*ce
= active_cache
[pos
];
76 if (ce_namelen(ce
) != namelen
||
77 memcmp(ce
->name
, name
, namelen
))
81 if (ce_stage(ce
) != checkout_stage
82 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
85 if (checkout_entry(ce
, &state
,
86 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
93 write_tempfile_record(name
, prefix
);
94 return errs
> 0 ? -1 : 0;
98 * At this point we know we didn't try to check anything out. If it was
99 * because we did find an entry but it was stage 0, that's not an
102 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
106 fprintf(stderr
, "git checkout-index: %s ", name
);
108 fprintf(stderr
, "is not in the cache");
109 else if (checkout_stage
)
110 fprintf(stderr
, "does not exist at stage %d",
113 fprintf(stderr
, "is unmerged");
119 static int checkout_all(const char *prefix
, int prefix_length
)
122 struct cache_entry
*last_ce
= NULL
;
124 for (i
= 0; i
< active_nr
; i
++) {
125 struct cache_entry
*ce
= active_cache
[i
];
126 if (ce_stage(ce
) != checkout_stage
127 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
129 if (prefix
&& *prefix
&&
130 (ce_namelen(ce
) <= prefix_length
||
131 memcmp(prefix
, ce
->name
, prefix_length
)))
133 if (last_ce
&& to_tempfile
) {
134 if (ce_namelen(last_ce
) != ce_namelen(ce
)
135 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
136 write_tempfile_record(last_ce
->name
, prefix
);
138 if (checkout_entry(ce
, &state
,
139 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
144 if (last_ce
&& to_tempfile
)
145 write_tempfile_record(last_ce
->name
, prefix
);
149 static const char * const builtin_checkout_index_usage
[] = {
150 N_("git checkout-index [<options>] [--] [<file>...]"),
154 static int option_parse_stage(const struct option
*opt
,
155 const char *arg
, int unset
)
157 BUG_ON_OPT_NEG(unset
);
159 if (!strcmp(arg
, "all")) {
161 checkout_stage
= CHECKOUT_ALL
;
164 if ('1' <= ch
&& ch
<= '3')
165 checkout_stage
= arg
[0] - '0';
167 die(_("stage should be between 1 and 3 or all"));
172 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
175 struct lock_file lock_file
= LOCK_INIT
;
177 int read_from_stdin
= 0;
179 int force
= 0, quiet
= 0, not_new
= 0;
182 int pc_workers
, pc_threshold
;
183 struct option builtin_checkout_index_options
[] = {
184 OPT_BOOL('a', "all", &all
,
185 N_("check out all files in the index")),
186 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
188 N_("no warning for existing files and files not in index")),
189 OPT_BOOL('n', "no-create", ¬_new
,
190 N_("don't checkout new files")),
191 OPT_BOOL('u', "index", &index_opt
,
192 N_("update stat information in the index file")),
193 OPT_BOOL('z', NULL
, &nul_term_line
,
194 N_("paths are separated with NUL character")),
195 OPT_BOOL(0, "stdin", &read_from_stdin
,
196 N_("read list of paths from the standard input")),
197 OPT_BOOL(0, "temp", &to_tempfile
,
198 N_("write the content to temporary files")),
199 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
200 N_("when creating files, prepend <string>")),
201 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
202 N_("copy out the files from named stage"),
203 PARSE_OPT_NONEG
, option_parse_stage
),
207 if (argc
== 2 && !strcmp(argv
[1], "-h"))
208 usage_with_options(builtin_checkout_index_usage
,
209 builtin_checkout_index_options
);
210 git_config(git_default_config
, NULL
);
211 prefix_length
= prefix
? strlen(prefix
) : 0;
213 if (read_cache() < 0) {
214 die("invalid cache");
217 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
218 builtin_checkout_index_usage
, 0);
219 state
.istate
= &the_index
;
222 state
.not_new
= not_new
;
226 state
.base_dir_len
= strlen(state
.base_dir
);
229 * when --prefix is specified we do not want to update cache.
231 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
232 state
.refresh_cache
= 1;
233 state
.istate
= &the_index
;
234 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
237 get_parallel_checkout_configs(&pc_workers
, &pc_threshold
);
239 init_parallel_checkout();
241 /* Check out named files first */
242 for (i
= 0; i
< argc
; i
++) {
243 const char *arg
= argv
[i
];
247 die("git checkout-index: don't mix '--all' and explicit filenames");
249 die("git checkout-index: don't mix '--stdin' and explicit filenames");
250 p
= prefix_path(prefix
, prefix_length
, arg
);
251 err
|= checkout_file(p
, prefix
);
255 if (read_from_stdin
) {
256 struct strbuf buf
= STRBUF_INIT
;
257 struct strbuf unquoted
= STRBUF_INIT
;
258 strbuf_getline_fn getline_fn
;
261 die("git checkout-index: don't mix '--all' and '--stdin'");
263 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
264 while (getline_fn(&buf
, stdin
) != EOF
) {
266 if (!nul_term_line
&& buf
.buf
[0] == '"') {
267 strbuf_reset(&unquoted
);
268 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
269 die("line is badly quoted");
270 strbuf_swap(&buf
, &unquoted
);
272 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
273 err
|= checkout_file(p
, prefix
);
276 strbuf_release(&unquoted
);
277 strbuf_release(&buf
);
281 err
|= checkout_all(prefix
, prefix_length
);
284 err
|= run_parallel_checkout(&state
, pc_workers
, pc_threshold
,
290 if (is_lock_file_locked(&lock_file
) &&
291 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
292 die("Unable to write new index file");