2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
11 #include "cache-tree.h"
12 #include "parse-options.h"
14 #define CHECKOUT_ALL 4
15 static int nul_term_line
;
16 static int checkout_stage
; /* default to checkout stage0 */
17 static int to_tempfile
;
18 static char topath
[4][TEMPORARY_FILENAME_LENGTH
+ 1];
20 static struct checkout state
= CHECKOUT_INIT
;
22 static void write_tempfile_record(const char *name
, const char *prefix
)
26 if (CHECKOUT_ALL
== checkout_stage
) {
27 for (i
= 1; i
< 4; i
++) {
31 fputs(topath
[i
], stdout
);
36 fputs(topath
[checkout_stage
], stdout
);
39 write_name_quoted_relative(name
, prefix
, stdout
,
40 nul_term_line
? '\0' : '\n');
42 for (i
= 0; i
< 4; i
++) {
47 static int checkout_file(const char *name
, const char *prefix
)
49 int namelen
= strlen(name
);
50 int pos
= cache_name_pos(name
, namelen
);
51 int has_same_name
= 0;
58 while (pos
< active_nr
) {
59 struct cache_entry
*ce
= active_cache
[pos
];
60 if (ce_namelen(ce
) != namelen
||
61 memcmp(ce
->name
, name
, namelen
))
65 if (ce_stage(ce
) != checkout_stage
66 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
69 if (checkout_entry(ce
, &state
,
70 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
77 write_tempfile_record(name
, prefix
);
78 return errs
> 0 ? -1 : 0;
82 fprintf(stderr
, "git checkout-index: %s ", name
);
84 fprintf(stderr
, "is not in the cache");
85 else if (checkout_stage
)
86 fprintf(stderr
, "does not exist at stage %d",
89 fprintf(stderr
, "is unmerged");
95 static void checkout_all(const char *prefix
, int prefix_length
)
98 struct cache_entry
*last_ce
= NULL
;
100 for (i
= 0; i
< active_nr
; i
++) {
101 struct cache_entry
*ce
= active_cache
[i
];
102 if (ce_stage(ce
) != checkout_stage
103 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
105 if (prefix
&& *prefix
&&
106 (ce_namelen(ce
) <= prefix_length
||
107 memcmp(prefix
, ce
->name
, prefix_length
)))
109 if (last_ce
&& to_tempfile
) {
110 if (ce_namelen(last_ce
) != ce_namelen(ce
)
111 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
112 write_tempfile_record(last_ce
->name
, prefix
);
114 if (checkout_entry(ce
, &state
,
115 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
120 if (last_ce
&& to_tempfile
)
121 write_tempfile_record(last_ce
->name
, prefix
);
123 /* we have already done our error reporting.
124 * exit with the same code as die().
129 static const char * const builtin_checkout_index_usage
[] = {
130 N_("git checkout-index [<options>] [--] [<file>...]"),
134 static int option_parse_stage(const struct option
*opt
,
135 const char *arg
, int unset
)
137 BUG_ON_OPT_NEG(unset
);
139 if (!strcmp(arg
, "all")) {
141 checkout_stage
= CHECKOUT_ALL
;
144 if ('1' <= ch
&& ch
<= '3')
145 checkout_stage
= arg
[0] - '0';
147 die(_("stage should be between 1 and 3 or all"));
152 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
155 struct lock_file lock_file
= LOCK_INIT
;
157 int read_from_stdin
= 0;
159 int force
= 0, quiet
= 0, not_new
= 0;
161 struct option builtin_checkout_index_options
[] = {
162 OPT_BOOL('a', "all", &all
,
163 N_("check out all files in the index")),
164 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
166 N_("no warning for existing files and files not in index")),
167 OPT_BOOL('n', "no-create", ¬_new
,
168 N_("don't checkout new files")),
169 OPT_BOOL('u', "index", &index_opt
,
170 N_("update stat information in the index file")),
171 OPT_BOOL('z', NULL
, &nul_term_line
,
172 N_("paths are separated with NUL character")),
173 OPT_BOOL(0, "stdin", &read_from_stdin
,
174 N_("read list of paths from the standard input")),
175 OPT_BOOL(0, "temp", &to_tempfile
,
176 N_("write the content to temporary files")),
177 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
178 N_("when creating files, prepend <string>")),
179 { OPTION_CALLBACK
, 0, "stage", NULL
, "(1|2|3|all)",
180 N_("copy out the files from named stage"),
181 PARSE_OPT_NONEG
, option_parse_stage
},
185 if (argc
== 2 && !strcmp(argv
[1], "-h"))
186 usage_with_options(builtin_checkout_index_usage
,
187 builtin_checkout_index_options
);
188 git_config(git_default_config
, NULL
);
189 prefix_length
= prefix
? strlen(prefix
) : 0;
191 if (read_cache() < 0) {
192 die("invalid cache");
195 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
196 builtin_checkout_index_usage
, 0);
197 state
.istate
= &the_index
;
200 state
.not_new
= not_new
;
204 state
.base_dir_len
= strlen(state
.base_dir
);
207 * when --prefix is specified we do not want to update cache.
209 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
210 state
.refresh_cache
= 1;
211 state
.istate
= &the_index
;
212 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
215 /* Check out named files first */
216 for (i
= 0; i
< argc
; i
++) {
217 const char *arg
= argv
[i
];
221 die("git checkout-index: don't mix '--all' and explicit filenames");
223 die("git checkout-index: don't mix '--stdin' and explicit filenames");
224 p
= prefix_path(prefix
, prefix_length
, arg
);
225 checkout_file(p
, prefix
);
229 if (read_from_stdin
) {
230 struct strbuf buf
= STRBUF_INIT
;
231 struct strbuf unquoted
= STRBUF_INIT
;
232 strbuf_getline_fn getline_fn
;
235 die("git checkout-index: don't mix '--all' and '--stdin'");
237 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
238 while (getline_fn(&buf
, stdin
) != EOF
) {
240 if (!nul_term_line
&& buf
.buf
[0] == '"') {
241 strbuf_reset(&unquoted
);
242 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
243 die("line is badly quoted");
244 strbuf_swap(&buf
, &unquoted
);
246 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
247 checkout_file(p
, prefix
);
250 strbuf_release(&unquoted
);
251 strbuf_release(&buf
);
255 checkout_all(prefix
, prefix_length
);
257 if (is_lock_file_locked(&lock_file
) &&
258 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
259 die("Unable to write new index file");