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 line_termination
= '\n';
15 static int checkout_stage
; /* default to checkout stage0 */
16 static int to_tempfile
;
17 static char topath
[4][PATH_MAX
+ 1];
19 static struct checkout state
;
21 static void write_tempfile_record(const char *name
, int prefix_length
)
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(name
+ prefix_length
, stdout
, line_termination
);
40 for (i
= 0; i
< 4; i
++) {
45 static int checkout_file(const char *name
, int prefix_length
)
47 int namelen
= strlen(name
);
48 int pos
= cache_name_pos(name
, namelen
);
49 int has_same_name
= 0;
56 while (pos
< active_nr
) {
57 struct cache_entry
*ce
= active_cache
[pos
];
58 if (ce_namelen(ce
) != namelen
||
59 memcmp(ce
->name
, name
, namelen
))
63 if (ce_stage(ce
) != checkout_stage
64 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
67 if (checkout_entry(ce
, &state
,
68 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
74 write_tempfile_record(name
, prefix_length
);
75 return errs
> 0 ? -1 : 0;
79 fprintf(stderr
, "git checkout-index: %s ", name
);
81 fprintf(stderr
, "is not in the cache");
82 else if (checkout_stage
)
83 fprintf(stderr
, "does not exist at stage %d",
86 fprintf(stderr
, "is unmerged");
92 static void checkout_all(const char *prefix
, int prefix_length
)
95 struct cache_entry
*last_ce
= NULL
;
97 for (i
= 0; i
< active_nr
; i
++) {
98 struct cache_entry
*ce
= active_cache
[i
];
99 if (ce_stage(ce
) != checkout_stage
100 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
102 if (prefix
&& *prefix
&&
103 (ce_namelen(ce
) <= prefix_length
||
104 memcmp(prefix
, ce
->name
, prefix_length
)))
106 if (last_ce
&& to_tempfile
) {
107 if (ce_namelen(last_ce
) != ce_namelen(ce
)
108 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
109 write_tempfile_record(last_ce
->name
, prefix_length
);
111 if (checkout_entry(ce
, &state
,
112 to_tempfile
? topath
[ce_stage(ce
)] : NULL
) < 0)
116 if (last_ce
&& to_tempfile
)
117 write_tempfile_record(last_ce
->name
, prefix_length
);
119 /* we have already done our error reporting.
120 * exit with the same code as die().
125 static const char * const builtin_checkout_index_usage
[] = {
126 N_("git checkout-index [options] [--] [<file>...]"),
130 static struct lock_file lock_file
;
132 static int option_parse_u(const struct option
*opt
,
133 const char *arg
, int unset
)
135 int *newfd
= opt
->value
;
137 state
.refresh_cache
= 1;
139 *newfd
= hold_locked_index(&lock_file
, 1);
143 static int option_parse_z(const struct option
*opt
,
144 const char *arg
, int unset
)
147 line_termination
= '\n';
149 line_termination
= 0;
153 static int option_parse_prefix(const struct option
*opt
,
154 const char *arg
, int unset
)
156 state
.base_dir
= arg
;
157 state
.base_dir_len
= strlen(arg
);
161 static int option_parse_stage(const struct option
*opt
,
162 const char *arg
, int unset
)
164 if (!strcmp(arg
, "all")) {
166 checkout_stage
= CHECKOUT_ALL
;
169 if ('1' <= ch
&& ch
<= '3')
170 checkout_stage
= arg
[0] - '0';
172 die("stage should be between 1 and 3 or all");
177 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
182 int read_from_stdin
= 0;
184 int force
= 0, quiet
= 0, not_new
= 0;
185 struct option builtin_checkout_index_options
[] = {
186 OPT_BOOL('a', "all", &all
,
187 N_("check out all files in the index")),
188 OPT__FORCE(&force
, N_("force overwrite of existing files")),
190 N_("no warning for existing files and files not in index")),
191 OPT_BOOLEAN('n', "no-create", ¬_new
,
192 N_("don't checkout new files")),
193 { OPTION_CALLBACK
, 'u', "index", &newfd
, NULL
,
194 N_("update stat information in the index file"),
195 PARSE_OPT_NOARG
, option_parse_u
},
196 { OPTION_CALLBACK
, 'z', NULL
, NULL
, NULL
,
197 N_("paths are separated with NUL character"),
198 PARSE_OPT_NOARG
, option_parse_z
},
199 OPT_BOOL(0, "stdin", &read_from_stdin
,
200 N_("read list of paths from the standard input")),
201 OPT_BOOL(0, "temp", &to_tempfile
,
202 N_("write the content to temporary files")),
203 OPT_CALLBACK(0, "prefix", NULL
, N_("string"),
204 N_("when creating files, prepend <string>"),
205 option_parse_prefix
),
206 OPT_CALLBACK(0, "stage", NULL
, NULL
,
207 N_("copy out the files from named stage"),
212 if (argc
== 2 && !strcmp(argv
[1], "-h"))
213 usage_with_options(builtin_checkout_index_usage
,
214 builtin_checkout_index_options
);
215 git_config(git_default_config
, NULL
);
217 prefix_length
= prefix
? strlen(prefix
) : 0;
219 if (read_cache() < 0) {
220 die("invalid cache");
223 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
224 builtin_checkout_index_usage
, 0);
227 state
.not_new
= not_new
;
229 if (state
.base_dir_len
|| to_tempfile
) {
230 /* when --prefix is specified we do not
231 * want to update cache.
233 if (state
.refresh_cache
) {
234 rollback_lock_file(&lock_file
);
237 state
.refresh_cache
= 0;
240 /* Check out named files first */
241 for (i
= 0; i
< argc
; i
++) {
242 const char *arg
= argv
[i
];
246 die("git checkout-index: don't mix '--all' and explicit filenames");
248 die("git checkout-index: don't mix '--stdin' and explicit filenames");
249 p
= prefix_path(prefix
, prefix_length
, arg
);
250 checkout_file(p
, prefix_length
);
251 if (p
< arg
|| p
> arg
+ strlen(arg
))
255 if (read_from_stdin
) {
256 struct strbuf buf
= STRBUF_INIT
, nbuf
= STRBUF_INIT
;
259 die("git checkout-index: don't mix '--all' and '--stdin'");
261 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
263 if (line_termination
&& 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_length
);
271 if (p
< buf
.buf
|| p
> buf
.buf
+ buf
.len
)
274 strbuf_release(&nbuf
);
275 strbuf_release(&buf
);
279 checkout_all(prefix
, prefix_length
);
282 (write_cache(newfd
, active_cache
, active_nr
) ||
283 commit_locked_index(&lock_file
)))
284 die("Unable to write new index file");