2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
10 #include "cache-tree.h"
11 #include "parse-options.h"
13 #define CHECKOUT_ALL 4
14 static int nul_term_line
;
15 static int checkout_stage
; /* default to checkout stage0 */
16 static int to_tempfile
;
17 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
19 static struct checkout state
;
21 static void write_tempfile_record(const char *name
, const char *prefix
)
25 if (CHECKOUT_ALL
== checkout_stage
) {
26 for (i
= 1; i
< 4; i
++) {
30 fputs(topath
[i
], stdout
);
35 fputs(topath
[checkout_stage
], stdout
);
38 write_name_quoted_relative(name
, prefix
, stdout
,
39 nul_term_line
? '\0' : '\n');
41 for (i
= 0; i
< 4; i
++) {
46 static int checkout_file(const char *name
, const char *prefix
)
48 int namelen
= strlen(name
);
49 int pos
= cache_name_pos(name
, namelen
);
50 int has_same_name
= 0;
57 while (pos
< active_nr
) {
58 struct cache_entry
*ce
= active_cache
[pos
];
59 if (ce_namelen(ce
) != namelen
||
60 memcmp(ce
->name
, name
, namelen
))
64 if (ce_stage(ce
) != checkout_stage
65 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
68 if (checkout_entry(ce
, &state
,
69 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
75 write_tempfile_record(name
, prefix
);
76 return errs
> 0 ? -1 : 0;
80 fprintf(stderr
, "git checkout-index: %s ", name
);
82 fprintf(stderr
, "is not in the cache");
83 else if (checkout_stage
)
84 fprintf(stderr
, "does not exist at stage %d",
87 fprintf(stderr
, "is unmerged");
93 static void checkout_all(const char *prefix
, int prefix_length
)
96 struct cache_entry
*last_ce
= NULL
;
98 for (i
= 0; i
< active_nr
; i
++) {
99 struct cache_entry
*ce
= active_cache
[i
];
100 if (ce_stage(ce
) != checkout_stage
101 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
103 if (prefix
&& *prefix
&&
104 (ce_namelen(ce
) <= prefix_length
||
105 memcmp(prefix
, ce
->name
, prefix_length
)))
107 if (last_ce
&& to_tempfile
) {
108 if (ce_namelen(last_ce
) != ce_namelen(ce
)
109 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
110 write_tempfile_record(last_ce
->name
, prefix
);
112 if (checkout_entry(ce
, &state
,
113 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
117 if (last_ce
&& to_tempfile
)
118 write_tempfile_record(last_ce
->name
, prefix
);
120 /* we have already done our error reporting.
121 * exit with the same code as die().
126 static const char * const builtin_checkout_index_usage
[] = {
127 N_("git checkout-index [<options>] [--] [<file>...]"),
131 static struct lock_file lock_file
;
133 static int option_parse_u(const struct option
*opt
,
134 const char *arg
, int unset
)
136 int *newfd
= opt
->value
;
138 state
.refresh_cache
= 1;
139 state
.istate
= &the_index
;
141 *newfd
= hold_locked_index(&lock_file
, 1);
145 static int option_parse_z(const struct option
*opt
,
146 const char *arg
, int unset
)
148 nul_term_line
= !unset
;
152 static int option_parse_prefix(const struct option
*opt
,
153 const char *arg
, int unset
)
155 state
.base_dir
= arg
;
156 state
.base_dir_len
= strlen(arg
);
160 static int option_parse_stage(const struct option
*opt
,
161 const char *arg
, int unset
)
163 if (!strcmp(arg
, "all")) {
165 checkout_stage
= CHECKOUT_ALL
;
168 if ('1' <= ch
&& ch
<= '3')
169 checkout_stage
= arg
[0] - '0';
171 die("stage should be between 1 and 3 or all");
176 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
181 int read_from_stdin
= 0;
183 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")),
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 { OPTION_CALLBACK
, 'u', "index", &newfd
, NULL
,
193 N_("update stat information in the index file"),
194 PARSE_OPT_NOARG
, option_parse_u
},
195 { OPTION_CALLBACK
, 'z', NULL
, NULL
, NULL
,
196 N_("paths are separated with NUL character"),
197 PARSE_OPT_NOARG
, option_parse_z
},
198 OPT_BOOL(0, "stdin", &read_from_stdin
,
199 N_("read list of paths from the standard input")),
200 OPT_BOOL(0, "temp", &to_tempfile
,
201 N_("write the content to temporary files")),
202 OPT_CALLBACK(0, "prefix", NULL
, N_("string"),
203 N_("when creating files, prepend <string>"),
204 option_parse_prefix
),
205 OPT_CALLBACK(0, "stage", NULL
, NULL
,
206 N_("copy out the files from named stage"),
211 if (argc
== 2 && !strcmp(argv
[1], "-h"))
212 usage_with_options(builtin_checkout_index_usage
,
213 builtin_checkout_index_options
);
214 git_config(git_default_config
, NULL
);
216 prefix_length
= prefix
? strlen(prefix
) : 0;
218 if (read_cache() < 0) {
219 die("invalid cache");
222 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
223 builtin_checkout_index_usage
, 0);
226 state
.not_new
= not_new
;
228 if (state
.base_dir_len
|| to_tempfile
) {
229 /* when --prefix is specified we do not
230 * want to update cache.
232 if (state
.refresh_cache
) {
233 rollback_lock_file(&lock_file
);
236 state
.refresh_cache
= 0;
239 /* Check out named files first */
240 for (i
= 0; i
< argc
; i
++) {
241 const char *arg
= argv
[i
];
245 die("git checkout-index: don't mix '--all' and explicit filenames");
247 die("git checkout-index: don't mix '--stdin' and explicit filenames");
248 p
= prefix_path(prefix
, prefix_length
, arg
);
249 checkout_file(p
, prefix
);
253 if (read_from_stdin
) {
254 struct strbuf buf
= STRBUF_INIT
, nbuf
= 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] == '"') {
265 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
266 die("line is badly quoted");
267 strbuf_swap(&buf
, &nbuf
);
269 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
270 checkout_file(p
, prefix
);
273 strbuf_release(&nbuf
);
274 strbuf_release(&buf
);
278 checkout_all(prefix
, prefix_length
);
281 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
282 die("Unable to write new index file");