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 #define CHECKOUT_ALL 4
16 static int nul_term_line
;
17 static int checkout_stage
; /* default to checkout stage0 */
18 static int to_tempfile
;
19 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
21 static struct checkout state
= CHECKOUT_INIT
;
23 static void write_tempfile_record(const char *name
, const char *prefix
)
26 int have_tempname
= 0;
28 if (CHECKOUT_ALL
== checkout_stage
) {
29 for (i
= 1; i
< 4; i
++)
36 for (i
= 1; i
< 4; i
++) {
40 fputs(topath
[i
], stdout
);
45 } else if (topath
[checkout_stage
][0]) {
47 fputs(topath
[checkout_stage
], stdout
);
52 write_name_quoted_relative(name
, prefix
, stdout
,
53 nul_term_line
? '\0' : '\n');
56 for (i
= 0; i
< 4; i
++) {
61 static int checkout_file(const char *name
, const char *prefix
)
63 int namelen
= strlen(name
);
64 int pos
= cache_name_pos(name
, namelen
);
65 int has_same_name
= 0;
72 while (pos
< active_nr
) {
73 struct cache_entry
*ce
= active_cache
[pos
];
74 if (ce_namelen(ce
) != namelen
||
75 memcmp(ce
->name
, name
, namelen
))
79 if (ce_stage(ce
) != checkout_stage
80 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
83 if (checkout_entry(ce
, &state
,
84 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
91 write_tempfile_record(name
, prefix
);
92 return errs
> 0 ? -1 : 0;
96 * At this point we know we didn't try to check anything out. If it was
97 * because we did find an entry but it was stage 0, that's not an
100 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
104 fprintf(stderr
, "git checkout-index: %s ", name
);
106 fprintf(stderr
, "is not in the cache");
107 else if (checkout_stage
)
108 fprintf(stderr
, "does not exist at stage %d",
111 fprintf(stderr
, "is unmerged");
117 static void checkout_all(const char *prefix
, int prefix_length
)
120 struct cache_entry
*last_ce
= NULL
;
122 for (i
= 0; i
< active_nr
; i
++) {
123 struct cache_entry
*ce
= active_cache
[i
];
124 if (ce_stage(ce
) != checkout_stage
125 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
127 if (prefix
&& *prefix
&&
128 (ce_namelen(ce
) <= prefix_length
||
129 memcmp(prefix
, ce
->name
, prefix_length
)))
131 if (last_ce
&& to_tempfile
) {
132 if (ce_namelen(last_ce
) != ce_namelen(ce
)
133 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
134 write_tempfile_record(last_ce
->name
, prefix
);
136 if (checkout_entry(ce
, &state
,
137 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
142 if (last_ce
&& to_tempfile
)
143 write_tempfile_record(last_ce
->name
, prefix
);
145 /* we have already done our error reporting.
146 * exit with the same code as die().
151 static const char * const builtin_checkout_index_usage
[] = {
152 N_("git checkout-index [<options>] [--] [<file>...]"),
156 static int option_parse_stage(const struct option
*opt
,
157 const char *arg
, int unset
)
159 BUG_ON_OPT_NEG(unset
);
161 if (!strcmp(arg
, "all")) {
163 checkout_stage
= CHECKOUT_ALL
;
166 if ('1' <= ch
&& ch
<= '3')
167 checkout_stage
= arg
[0] - '0';
169 die(_("stage should be between 1 and 3 or all"));
174 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
177 struct lock_file lock_file
= LOCK_INIT
;
179 int read_from_stdin
= 0;
181 int force
= 0, quiet
= 0, not_new
= 0;
184 struct option builtin_checkout_index_options
[] = {
185 OPT_BOOL('a', "all", &all
,
186 N_("check out all files in the index")),
187 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
189 N_("no warning for existing files and files not in index")),
190 OPT_BOOL('n', "no-create", ¬_new
,
191 N_("don't checkout new files")),
192 OPT_BOOL('u', "index", &index_opt
,
193 N_("update stat information in the index file")),
194 OPT_BOOL('z', NULL
, &nul_term_line
,
195 N_("paths are separated with NUL character")),
196 OPT_BOOL(0, "stdin", &read_from_stdin
,
197 N_("read list of paths from the standard input")),
198 OPT_BOOL(0, "temp", &to_tempfile
,
199 N_("write the content to temporary files")),
200 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
201 N_("when creating files, prepend <string>")),
202 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
203 N_("copy out the files from named stage"),
204 PARSE_OPT_NONEG
, option_parse_stage
),
208 if (argc
== 2 && !strcmp(argv
[1], "-h"))
209 usage_with_options(builtin_checkout_index_usage
,
210 builtin_checkout_index_options
);
211 git_config(git_default_config
, NULL
);
212 prefix_length
= prefix
? strlen(prefix
) : 0;
214 if (read_cache() < 0) {
215 die("invalid cache");
218 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
219 builtin_checkout_index_usage
, 0);
220 state
.istate
= &the_index
;
223 state
.not_new
= not_new
;
227 state
.base_dir_len
= strlen(state
.base_dir
);
230 * when --prefix is specified we do not want to update cache.
232 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
233 state
.refresh_cache
= 1;
234 state
.istate
= &the_index
;
235 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
238 /* Check out named files first */
239 for (i
= 0; i
< argc
; i
++) {
240 const char *arg
= argv
[i
];
244 die("git checkout-index: don't mix '--all' and explicit filenames");
246 die("git checkout-index: don't mix '--stdin' and explicit filenames");
247 p
= prefix_path(prefix
, prefix_length
, arg
);
248 err
|= checkout_file(p
, prefix
);
252 if (read_from_stdin
) {
253 struct strbuf buf
= STRBUF_INIT
;
254 struct strbuf unquoted
= STRBUF_INIT
;
255 strbuf_getline_fn getline_fn
;
258 die("git checkout-index: don't mix '--all' and '--stdin'");
260 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
261 while (getline_fn(&buf
, stdin
) != EOF
) {
263 if (!nul_term_line
&& buf
.buf
[0] == '"') {
264 strbuf_reset(&unquoted
);
265 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
266 die("line is badly quoted");
267 strbuf_swap(&buf
, &unquoted
);
269 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
270 err
|= checkout_file(p
, prefix
);
273 strbuf_release(&unquoted
);
274 strbuf_release(&buf
);
281 checkout_all(prefix
, prefix_length
);
283 if (is_lock_file_locked(&lock_file
) &&
284 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
285 die("Unable to write new index file");