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
)
27 if (CHECKOUT_ALL
== checkout_stage
) {
28 for (i
= 1; i
< 4; i
++) {
32 fputs(topath
[i
], stdout
);
37 fputs(topath
[checkout_stage
], stdout
);
40 write_name_quoted_relative(name
, prefix
, stdout
,
41 nul_term_line
? '\0' : '\n');
43 for (i
= 0; i
< 4; i
++) {
48 static int checkout_file(const char *name
, const char *prefix
)
50 int namelen
= strlen(name
);
51 int pos
= cache_name_pos(name
, namelen
);
52 int has_same_name
= 0;
59 while (pos
< active_nr
) {
60 struct cache_entry
*ce
= active_cache
[pos
];
61 if (ce_namelen(ce
) != namelen
||
62 memcmp(ce
->name
, name
, namelen
))
66 if (ce_stage(ce
) != checkout_stage
67 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
70 if (checkout_entry(ce
, &state
,
71 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
78 write_tempfile_record(name
, prefix
);
79 return errs
> 0 ? -1 : 0;
83 * At this point we know we didn't try to check anything out. If it was
84 * because we did find an entry but it was stage 0, that's not an
87 if (has_same_name
&& checkout_stage
== CHECKOUT_ALL
)
91 fprintf(stderr
, "git checkout-index: %s ", name
);
93 fprintf(stderr
, "is not in the cache");
94 else if (checkout_stage
)
95 fprintf(stderr
, "does not exist at stage %d",
98 fprintf(stderr
, "is unmerged");
104 static void checkout_all(const char *prefix
, int prefix_length
)
107 struct cache_entry
*last_ce
= NULL
;
109 for (i
= 0; i
< active_nr
; i
++) {
110 struct cache_entry
*ce
= active_cache
[i
];
111 if (ce_stage(ce
) != checkout_stage
112 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
114 if (prefix
&& *prefix
&&
115 (ce_namelen(ce
) <= prefix_length
||
116 memcmp(prefix
, ce
->name
, prefix_length
)))
118 if (last_ce
&& to_tempfile
) {
119 if (ce_namelen(last_ce
) != ce_namelen(ce
)
120 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
121 write_tempfile_record(last_ce
->name
, prefix
);
123 if (checkout_entry(ce
, &state
,
124 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
129 if (last_ce
&& to_tempfile
)
130 write_tempfile_record(last_ce
->name
, prefix
);
132 /* we have already done our error reporting.
133 * exit with the same code as die().
138 static const char * const builtin_checkout_index_usage
[] = {
139 N_("git checkout-index [<options>] [--] [<file>...]"),
143 static int option_parse_stage(const struct option
*opt
,
144 const char *arg
, int unset
)
146 BUG_ON_OPT_NEG(unset
);
148 if (!strcmp(arg
, "all")) {
150 checkout_stage
= CHECKOUT_ALL
;
153 if ('1' <= ch
&& ch
<= '3')
154 checkout_stage
= arg
[0] - '0';
156 die(_("stage should be between 1 and 3 or all"));
161 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
164 struct lock_file lock_file
= LOCK_INIT
;
166 int read_from_stdin
= 0;
168 int force
= 0, quiet
= 0, not_new
= 0;
171 struct option builtin_checkout_index_options
[] = {
172 OPT_BOOL('a', "all", &all
,
173 N_("check out all files in the index")),
174 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
176 N_("no warning for existing files and files not in index")),
177 OPT_BOOL('n', "no-create", ¬_new
,
178 N_("don't checkout new files")),
179 OPT_BOOL('u', "index", &index_opt
,
180 N_("update stat information in the index file")),
181 OPT_BOOL('z', NULL
, &nul_term_line
,
182 N_("paths are separated with NUL character")),
183 OPT_BOOL(0, "stdin", &read_from_stdin
,
184 N_("read list of paths from the standard input")),
185 OPT_BOOL(0, "temp", &to_tempfile
,
186 N_("write the content to temporary files")),
187 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
188 N_("when creating files, prepend <string>")),
189 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
190 N_("copy out the files from named stage"),
191 PARSE_OPT_NONEG
, option_parse_stage
),
195 if (argc
== 2 && !strcmp(argv
[1], "-h"))
196 usage_with_options(builtin_checkout_index_usage
,
197 builtin_checkout_index_options
);
198 git_config(git_default_config
, NULL
);
199 prefix_length
= prefix
? strlen(prefix
) : 0;
201 if (read_cache() < 0) {
202 die("invalid cache");
205 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
206 builtin_checkout_index_usage
, 0);
207 state
.istate
= &the_index
;
210 state
.not_new
= not_new
;
214 state
.base_dir_len
= strlen(state
.base_dir
);
217 * when --prefix is specified we do not want to update cache.
219 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
220 state
.refresh_cache
= 1;
221 state
.istate
= &the_index
;
222 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
225 /* Check out named files first */
226 for (i
= 0; i
< argc
; i
++) {
227 const char *arg
= argv
[i
];
231 die("git checkout-index: don't mix '--all' and explicit filenames");
233 die("git checkout-index: don't mix '--stdin' and explicit filenames");
234 p
= prefix_path(prefix
, prefix_length
, arg
);
235 err
|= checkout_file(p
, prefix
);
239 if (read_from_stdin
) {
240 struct strbuf buf
= STRBUF_INIT
;
241 struct strbuf unquoted
= STRBUF_INIT
;
242 strbuf_getline_fn getline_fn
;
245 die("git checkout-index: don't mix '--all' and '--stdin'");
247 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
248 while (getline_fn(&buf
, stdin
) != EOF
) {
250 if (!nul_term_line
&& buf
.buf
[0] == '"') {
251 strbuf_reset(&unquoted
);
252 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
253 die("line is badly quoted");
254 strbuf_swap(&buf
, &unquoted
);
256 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
257 err
|= checkout_file(p
, prefix
);
260 strbuf_release(&unquoted
);
261 strbuf_release(&buf
);
268 checkout_all(prefix
, prefix_length
);
270 if (is_lock_file_locked(&lock_file
) &&
271 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
272 die("Unable to write new index file");