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 fprintf(stderr
, "git checkout-index: %s ", name
);
85 fprintf(stderr
, "is not in the cache");
86 else if (checkout_stage
)
87 fprintf(stderr
, "does not exist at stage %d",
90 fprintf(stderr
, "is unmerged");
96 static void checkout_all(const char *prefix
, int prefix_length
)
99 struct cache_entry
*last_ce
= NULL
;
101 for (i
= 0; i
< active_nr
; i
++) {
102 struct cache_entry
*ce
= active_cache
[i
];
103 if (ce_stage(ce
) != checkout_stage
104 && (CHECKOUT_ALL
!= checkout_stage
|| !ce_stage(ce
)))
106 if (prefix
&& *prefix
&&
107 (ce_namelen(ce
) <= prefix_length
||
108 memcmp(prefix
, ce
->name
, prefix_length
)))
110 if (last_ce
&& to_tempfile
) {
111 if (ce_namelen(last_ce
) != ce_namelen(ce
)
112 || memcmp(last_ce
->name
, ce
->name
, ce_namelen(ce
)))
113 write_tempfile_record(last_ce
->name
, prefix
);
115 if (checkout_entry(ce
, &state
,
116 to_tempfile
? topath
[ce_stage(ce
)] : NULL
,
121 if (last_ce
&& to_tempfile
)
122 write_tempfile_record(last_ce
->name
, prefix
);
124 /* we have already done our error reporting.
125 * exit with the same code as die().
130 static const char * const builtin_checkout_index_usage
[] = {
131 N_("git checkout-index [<options>] [--] [<file>...]"),
135 static int option_parse_stage(const struct option
*opt
,
136 const char *arg
, int unset
)
138 BUG_ON_OPT_NEG(unset
);
140 if (!strcmp(arg
, "all")) {
142 checkout_stage
= CHECKOUT_ALL
;
145 if ('1' <= ch
&& ch
<= '3')
146 checkout_stage
= arg
[0] - '0';
148 die(_("stage should be between 1 and 3 or all"));
153 int cmd_checkout_index(int argc
, const char **argv
, const char *prefix
)
156 struct lock_file lock_file
= LOCK_INIT
;
158 int read_from_stdin
= 0;
160 int force
= 0, quiet
= 0, not_new
= 0;
162 struct option builtin_checkout_index_options
[] = {
163 OPT_BOOL('a', "all", &all
,
164 N_("check out all files in the index")),
165 OPT__FORCE(&force
, N_("force overwrite of existing files"), 0),
167 N_("no warning for existing files and files not in index")),
168 OPT_BOOL('n', "no-create", ¬_new
,
169 N_("don't checkout new files")),
170 OPT_BOOL('u', "index", &index_opt
,
171 N_("update stat information in the index file")),
172 OPT_BOOL('z', NULL
, &nul_term_line
,
173 N_("paths are separated with NUL character")),
174 OPT_BOOL(0, "stdin", &read_from_stdin
,
175 N_("read list of paths from the standard input")),
176 OPT_BOOL(0, "temp", &to_tempfile
,
177 N_("write the content to temporary files")),
178 OPT_STRING(0, "prefix", &state
.base_dir
, N_("string"),
179 N_("when creating files, prepend <string>")),
180 OPT_CALLBACK_F(0, "stage", NULL
, "(1|2|3|all)",
181 N_("copy out the files from named stage"),
182 PARSE_OPT_NONEG
, option_parse_stage
),
186 if (argc
== 2 && !strcmp(argv
[1], "-h"))
187 usage_with_options(builtin_checkout_index_usage
,
188 builtin_checkout_index_options
);
189 git_config(git_default_config
, NULL
);
190 prefix_length
= prefix
? strlen(prefix
) : 0;
192 if (read_cache() < 0) {
193 die("invalid cache");
196 argc
= parse_options(argc
, argv
, prefix
, builtin_checkout_index_options
,
197 builtin_checkout_index_usage
, 0);
198 state
.istate
= &the_index
;
201 state
.not_new
= not_new
;
205 state
.base_dir_len
= strlen(state
.base_dir
);
208 * when --prefix is specified we do not want to update cache.
210 if (index_opt
&& !state
.base_dir_len
&& !to_tempfile
) {
211 state
.refresh_cache
= 1;
212 state
.istate
= &the_index
;
213 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
216 /* Check out named files first */
217 for (i
= 0; i
< argc
; i
++) {
218 const char *arg
= argv
[i
];
222 die("git checkout-index: don't mix '--all' and explicit filenames");
224 die("git checkout-index: don't mix '--stdin' and explicit filenames");
225 p
= prefix_path(prefix
, prefix_length
, arg
);
226 checkout_file(p
, prefix
);
230 if (read_from_stdin
) {
231 struct strbuf buf
= STRBUF_INIT
;
232 struct strbuf unquoted
= STRBUF_INIT
;
233 strbuf_getline_fn getline_fn
;
236 die("git checkout-index: don't mix '--all' and '--stdin'");
238 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
239 while (getline_fn(&buf
, stdin
) != EOF
) {
241 if (!nul_term_line
&& buf
.buf
[0] == '"') {
242 strbuf_reset(&unquoted
);
243 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
244 die("line is badly quoted");
245 strbuf_swap(&buf
, &unquoted
);
247 p
= prefix_path(prefix
, prefix_length
, buf
.buf
);
248 checkout_file(p
, prefix
);
251 strbuf_release(&unquoted
);
252 strbuf_release(&buf
);
256 checkout_all(prefix
, prefix_length
);
258 if (is_lock_file_locked(&lock_file
) &&
259 write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
260 die("Unable to write new index file");