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