]>
Commit | Line | Data |
---|---|---|
33db5f4d LT |
1 | /* |
2 | * Check-out files from the "current cache directory" | |
3 | * | |
4 | * Copyright (C) 2005 Linus Torvalds | |
5 | * | |
33db5f4d | 6 | */ |
f8adbec9 | 7 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
baffc0e7 | 8 | #include "builtin.h" |
b2141fc1 | 9 | #include "config.h" |
697cc8ef | 10 | #include "lockfile.h" |
9debe63d | 11 | #include "quote.h" |
bad68ec9 | 12 | #include "cache-tree.h" |
a6c7db1b | 13 | #include "parse-options.h" |
d052cc03 | 14 | #include "entry.h" |
33db5f4d | 15 | |
de84f99c | 16 | #define CHECKOUT_ALL 4 |
a392f57d | 17 | static int nul_term_line; |
3bd348ae | 18 | static int checkout_stage; /* default to checkout stage0 */ |
de84f99c | 19 | static int to_tempfile; |
af2a651d | 20 | static char topath[4][TEMPORARY_FILENAME_LENGTH + 1]; |
c3e9a653 | 21 | |
68e3d629 | 22 | static struct checkout state = CHECKOUT_INIT; |
33db5f4d | 23 | |
74c4de58 | 24 | static void write_tempfile_record(const char *name, const char *prefix) |
de84f99c SP |
25 | { |
26 | int i; | |
3f7ba603 | 27 | int have_tempname = 0; |
de84f99c SP |
28 | |
29 | if (CHECKOUT_ALL == checkout_stage) { | |
3f7ba603 MT |
30 | for (i = 1; i < 4; i++) |
31 | if (topath[i][0]) { | |
32 | have_tempname = 1; | |
33 | break; | |
34 | } | |
35 | ||
36 | if (have_tempname) { | |
37 | for (i = 1; i < 4; i++) { | |
38 | if (i > 1) | |
39 | putchar(' '); | |
40 | if (topath[i][0]) | |
41 | fputs(topath[i], stdout); | |
42 | else | |
43 | putchar('.'); | |
44 | } | |
de84f99c | 45 | } |
3f7ba603 MT |
46 | } else if (topath[checkout_stage][0]) { |
47 | have_tempname = 1; | |
de84f99c | 48 | fputs(topath[checkout_stage], stdout); |
3f7ba603 | 49 | } |
de84f99c | 50 | |
3f7ba603 MT |
51 | if (have_tempname) { |
52 | putchar('\t'); | |
53 | write_name_quoted_relative(name, prefix, stdout, | |
54 | nul_term_line ? '\0' : '\n'); | |
55 | } | |
de84f99c SP |
56 | |
57 | for (i = 0; i < 4; i++) { | |
58 | topath[i][0] = 0; | |
59 | } | |
60 | } | |
61 | ||
74c4de58 | 62 | static int checkout_file(const char *name, const char *prefix) |
33db5f4d | 63 | { |
3bd348ae JH |
64 | int namelen = strlen(name); |
65 | int pos = cache_name_pos(name, namelen); | |
66 | int has_same_name = 0; | |
de84f99c SP |
67 | int did_checkout = 0; |
68 | int errs = 0; | |
3bd348ae JH |
69 | |
70 | if (pos < 0) | |
71 | pos = -pos - 1; | |
72 | ||
73 | while (pos < active_nr) { | |
74 | struct cache_entry *ce = active_cache[pos]; | |
f4f9adae | 75 | if (ce_namelen(ce) != namelen || |
3bd348ae JH |
76 | memcmp(ce->name, name, namelen)) |
77 | break; | |
78 | has_same_name = 1; | |
3bd348ae | 79 | pos++; |
de84f99c SP |
80 | if (ce_stage(ce) != checkout_stage |
81 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
82 | continue; | |
83 | did_checkout = 1; | |
84 | if (checkout_entry(ce, &state, | |
0f086e6d NTND |
85 | to_tempfile ? topath[ce_stage(ce)] : NULL, |
86 | NULL) < 0) | |
de84f99c SP |
87 | errs++; |
88 | } | |
89 | ||
90 | if (did_checkout) { | |
91 | if (to_tempfile) | |
74c4de58 | 92 | write_tempfile_record(name, prefix); |
de84f99c | 93 | return errs > 0 ? -1 : 0; |
33db5f4d | 94 | } |
3bd348ae | 95 | |
0b809c82 JK |
96 | /* |
97 | * At this point we know we didn't try to check anything out. If it was | |
98 | * because we did find an entry but it was stage 0, that's not an | |
99 | * error. | |
100 | */ | |
101 | if (has_same_name && checkout_stage == CHECKOUT_ALL) | |
102 | return 0; | |
103 | ||
3bd348ae | 104 | if (!state.quiet) { |
05207a28 | 105 | fprintf(stderr, "git checkout-index: %s ", name); |
3bd348ae JH |
106 | if (!has_same_name) |
107 | fprintf(stderr, "is not in the cache"); | |
108 | else if (checkout_stage) | |
109 | fprintf(stderr, "does not exist at stage %d", | |
110 | checkout_stage); | |
111 | else | |
112 | fprintf(stderr, "is unmerged"); | |
113 | fputc('\n', stderr); | |
114 | } | |
115 | return -1; | |
33db5f4d LT |
116 | } |
117 | ||
f7f0fbfc | 118 | static void checkout_all(const char *prefix, int prefix_length) |
33db5f4d | 119 | { |
4b12dae6 | 120 | int i, errs = 0; |
4b25d091 | 121 | struct cache_entry *last_ce = NULL; |
33db5f4d LT |
122 | |
123 | for (i = 0; i < active_nr ; i++) { | |
124 | struct cache_entry *ce = active_cache[i]; | |
de84f99c SP |
125 | if (ce_stage(ce) != checkout_stage |
126 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
d9f98eeb | 127 | continue; |
c3e9a653 | 128 | if (prefix && *prefix && |
3bd348ae JH |
129 | (ce_namelen(ce) <= prefix_length || |
130 | memcmp(prefix, ce->name, prefix_length))) | |
c3e9a653 | 131 | continue; |
de84f99c SP |
132 | if (last_ce && to_tempfile) { |
133 | if (ce_namelen(last_ce) != ce_namelen(ce) | |
134 | || memcmp(last_ce->name, ce->name, ce_namelen(ce))) | |
74c4de58 | 135 | write_tempfile_record(last_ce->name, prefix); |
de84f99c SP |
136 | } |
137 | if (checkout_entry(ce, &state, | |
0f086e6d NTND |
138 | to_tempfile ? topath[ce_stage(ce)] : NULL, |
139 | NULL) < 0) | |
4b12dae6 | 140 | errs++; |
de84f99c | 141 | last_ce = ce; |
33db5f4d | 142 | } |
de84f99c | 143 | if (last_ce && to_tempfile) |
74c4de58 | 144 | write_tempfile_record(last_ce->name, prefix); |
4b12dae6 JH |
145 | if (errs) |
146 | /* we have already done our error reporting. | |
147 | * exit with the same code as die(). | |
148 | */ | |
149 | exit(128); | |
33db5f4d LT |
150 | } |
151 | ||
a6c7db1b | 152 | static const char * const builtin_checkout_index_usage[] = { |
9c9b4f2f | 153 | N_("git checkout-index [<options>] [--] [<file>...]"), |
a6c7db1b MV |
154 | NULL |
155 | }; | |
d46ad9c9 | 156 | |
a6c7db1b MV |
157 | static int option_parse_stage(const struct option *opt, |
158 | const char *arg, int unset) | |
159 | { | |
517fe807 JK |
160 | BUG_ON_OPT_NEG(unset); |
161 | ||
a6c7db1b MV |
162 | if (!strcmp(arg, "all")) { |
163 | to_tempfile = 1; | |
164 | checkout_stage = CHECKOUT_ALL; | |
165 | } else { | |
166 | int ch = arg[0]; | |
167 | if ('1' <= ch && ch <= '3') | |
168 | checkout_stage = arg[0] - '0'; | |
169 | else | |
22396175 | 170 | die(_("stage should be between 1 and 3 or all")); |
a6c7db1b MV |
171 | } |
172 | return 0; | |
173 | } | |
174 | ||
e414156a | 175 | int cmd_checkout_index(int argc, const char **argv, const char *prefix) |
33db5f4d | 176 | { |
a65a686f | 177 | int i; |
02ae242f | 178 | struct lock_file lock_file = LOCK_INIT; |
a65a686f | 179 | int all = 0; |
9debe63d | 180 | int read_from_stdin = 0; |
e414156a | 181 | int prefix_length; |
a6c7db1b | 182 | int force = 0, quiet = 0, not_new = 0; |
6a6df8aa | 183 | int index_opt = 0; |
7e410615 | 184 | int err = 0; |
a6c7db1b | 185 | struct option builtin_checkout_index_options[] = { |
d5d09d47 | 186 | OPT_BOOL('a', "all", &all, |
f63cf8c9 | 187 | N_("check out all files in the index")), |
1224781d | 188 | OPT__FORCE(&force, N_("force overwrite of existing files"), 0), |
8c839683 | 189 | OPT__QUIET(&quiet, |
0ed21718 | 190 | N_("no warning for existing files and files not in index")), |
5d4d1440 | 191 | OPT_BOOL('n', "no-create", ¬_new, |
0ed21718 | 192 | N_("don't checkout new files")), |
6a6df8aa JK |
193 | OPT_BOOL('u', "index", &index_opt, |
194 | N_("update stat information in the index file")), | |
9096ee16 JK |
195 | OPT_BOOL('z', NULL, &nul_term_line, |
196 | N_("paths are separated with NUL character")), | |
d5d09d47 | 197 | OPT_BOOL(0, "stdin", &read_from_stdin, |
0ed21718 | 198 | N_("read list of paths from the standard input")), |
d5d09d47 | 199 | OPT_BOOL(0, "temp", &to_tempfile, |
0ed21718 | 200 | N_("write the content to temporary files")), |
5ed5f671 JK |
201 | OPT_STRING(0, "prefix", &state.base_dir, N_("string"), |
202 | N_("when creating files, prepend <string>")), | |
203c8533 | 203 | OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)", |
0ed21718 | 204 | N_("copy out the files from named stage"), |
203c8533 | 205 | PARSE_OPT_NONEG, option_parse_stage), |
a6c7db1b MV |
206 | OPT_END() |
207 | }; | |
33db5f4d | 208 | |
cf9d52e4 NTND |
209 | if (argc == 2 && !strcmp(argv[1], "-h")) |
210 | usage_with_options(builtin_checkout_index_usage, | |
211 | builtin_checkout_index_options); | |
ef90d6d4 | 212 | git_config(git_default_config, NULL); |
c3e9a653 JH |
213 | prefix_length = prefix ? strlen(prefix) : 0; |
214 | ||
33db5f4d | 215 | if (read_cache() < 0) { |
2de381f9 | 216 | die("invalid cache"); |
33db5f4d LT |
217 | } |
218 | ||
37782920 | 219 | argc = parse_options(argc, argv, prefix, builtin_checkout_index_options, |
a6c7db1b | 220 | builtin_checkout_index_usage, 0); |
74cfc0ee | 221 | state.istate = &the_index; |
a6c7db1b MV |
222 | state.force = force; |
223 | state.quiet = quiet; | |
224 | state.not_new = not_new; | |
a65a686f | 225 | |
5ed5f671 JK |
226 | if (!state.base_dir) |
227 | state.base_dir = ""; | |
228 | state.base_dir_len = strlen(state.base_dir); | |
229 | ||
6a6df8aa JK |
230 | /* |
231 | * when --prefix is specified we do not want to update cache. | |
232 | */ | |
233 | if (index_opt && !state.base_dir_len && !to_tempfile) { | |
234 | state.refresh_cache = 1; | |
235 | state.istate = &the_index; | |
02ae242f | 236 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
a65a686f LT |
237 | } |
238 | ||
239 | /* Check out named files first */ | |
a6c7db1b | 240 | for (i = 0; i < argc; i++) { |
a65a686f | 241 | const char *arg = argv[i]; |
d7a643b7 | 242 | char *p; |
a65a686f LT |
243 | |
244 | if (all) | |
7e44c935 | 245 | die("git checkout-index: don't mix '--all' and explicit filenames"); |
9debe63d | 246 | if (read_from_stdin) |
7e44c935 | 247 | die("git checkout-index: don't mix '--stdin' and explicit filenames"); |
dc46da22 | 248 | p = prefix_path(prefix, prefix_length, arg); |
7e410615 | 249 | err |= checkout_file(p, prefix); |
d7a643b7 | 250 | free(p); |
33db5f4d | 251 | } |
415e96c8 | 252 | |
9debe63d | 253 | if (read_from_stdin) { |
0d4cc1b4 JK |
254 | struct strbuf buf = STRBUF_INIT; |
255 | struct strbuf unquoted = STRBUF_INIT; | |
a392f57d | 256 | strbuf_getline_fn getline_fn; |
7fb1011e | 257 | |
9debe63d | 258 | if (all) |
7e44c935 | 259 | die("git checkout-index: don't mix '--all' and '--stdin'"); |
7fb1011e | 260 | |
a392f57d JH |
261 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
262 | while (getline_fn(&buf, stdin) != EOF) { | |
d7a643b7 | 263 | char *p; |
a392f57d | 264 | if (!nul_term_line && buf.buf[0] == '"') { |
0d4cc1b4 JK |
265 | strbuf_reset(&unquoted); |
266 | if (unquote_c_style(&unquoted, buf.buf, NULL)) | |
7fb1011e | 267 | die("line is badly quoted"); |
0d4cc1b4 | 268 | strbuf_swap(&buf, &unquoted); |
7fb1011e PH |
269 | } |
270 | p = prefix_path(prefix, prefix_length, buf.buf); | |
7e410615 | 271 | err |= checkout_file(p, prefix); |
d7a643b7 | 272 | free(p); |
9debe63d | 273 | } |
0d4cc1b4 | 274 | strbuf_release(&unquoted); |
e6c019d0 | 275 | strbuf_release(&buf); |
9debe63d SP |
276 | } |
277 | ||
7e410615 JK |
278 | if (err) |
279 | return 1; | |
280 | ||
a65a686f | 281 | if (all) |
e414156a | 282 | checkout_all(prefix, prefix_length); |
a65a686f | 283 | |
02ae242f | 284 | if (is_lock_file_locked(&lock_file) && |
03b86647 | 285 | write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
021b6e45 | 286 | die("Unable to write new index file"); |
33db5f4d LT |
287 | return 0; |
288 | } |