]>
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 | */ |
baffc0e7 | 7 | #include "builtin.h" |
697cc8ef | 8 | #include "lockfile.h" |
9debe63d | 9 | #include "quote.h" |
bad68ec9 | 10 | #include "cache-tree.h" |
a6c7db1b | 11 | #include "parse-options.h" |
33db5f4d | 12 | |
de84f99c | 13 | #define CHECKOUT_ALL 4 |
a392f57d | 14 | static int nul_term_line; |
3bd348ae | 15 | static int checkout_stage; /* default to checkout stage0 */ |
de84f99c | 16 | static int to_tempfile; |
af2a651d | 17 | static char topath[4][TEMPORARY_FILENAME_LENGTH + 1]; |
c3e9a653 | 18 | |
344c52ae | 19 | static struct checkout state; |
33db5f4d | 20 | |
74c4de58 | 21 | static void write_tempfile_record(const char *name, const char *prefix) |
de84f99c SP |
22 | { |
23 | int i; | |
24 | ||
25 | if (CHECKOUT_ALL == checkout_stage) { | |
26 | for (i = 1; i < 4; i++) { | |
27 | if (i > 1) | |
28 | putchar(' '); | |
29 | if (topath[i][0]) | |
30 | fputs(topath[i], stdout); | |
31 | else | |
32 | putchar('.'); | |
33 | } | |
34 | } else | |
35 | fputs(topath[checkout_stage], stdout); | |
36 | ||
37 | putchar('\t'); | |
a392f57d JH |
38 | write_name_quoted_relative(name, prefix, stdout, |
39 | nul_term_line ? '\0' : '\n'); | |
de84f99c SP |
40 | |
41 | for (i = 0; i < 4; i++) { | |
42 | topath[i][0] = 0; | |
43 | } | |
44 | } | |
45 | ||
74c4de58 | 46 | static int checkout_file(const char *name, const char *prefix) |
33db5f4d | 47 | { |
3bd348ae JH |
48 | int namelen = strlen(name); |
49 | int pos = cache_name_pos(name, namelen); | |
50 | int has_same_name = 0; | |
de84f99c SP |
51 | int did_checkout = 0; |
52 | int errs = 0; | |
3bd348ae JH |
53 | |
54 | if (pos < 0) | |
55 | pos = -pos - 1; | |
56 | ||
57 | while (pos < active_nr) { | |
58 | struct cache_entry *ce = active_cache[pos]; | |
f4f9adae | 59 | if (ce_namelen(ce) != namelen || |
3bd348ae JH |
60 | memcmp(ce->name, name, namelen)) |
61 | break; | |
62 | has_same_name = 1; | |
3bd348ae | 63 | pos++; |
de84f99c SP |
64 | if (ce_stage(ce) != checkout_stage |
65 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
66 | continue; | |
67 | did_checkout = 1; | |
68 | if (checkout_entry(ce, &state, | |
69 | to_tempfile ? topath[ce_stage(ce)] : NULL) < 0) | |
70 | errs++; | |
71 | } | |
72 | ||
73 | if (did_checkout) { | |
74 | if (to_tempfile) | |
74c4de58 | 75 | write_tempfile_record(name, prefix); |
de84f99c | 76 | return errs > 0 ? -1 : 0; |
33db5f4d | 77 | } |
3bd348ae JH |
78 | |
79 | if (!state.quiet) { | |
05207a28 | 80 | fprintf(stderr, "git checkout-index: %s ", name); |
3bd348ae JH |
81 | if (!has_same_name) |
82 | fprintf(stderr, "is not in the cache"); | |
83 | else if (checkout_stage) | |
84 | fprintf(stderr, "does not exist at stage %d", | |
85 | checkout_stage); | |
86 | else | |
87 | fprintf(stderr, "is unmerged"); | |
88 | fputc('\n', stderr); | |
89 | } | |
90 | return -1; | |
33db5f4d LT |
91 | } |
92 | ||
f7f0fbfc | 93 | static void checkout_all(const char *prefix, int prefix_length) |
33db5f4d | 94 | { |
4b12dae6 | 95 | int i, errs = 0; |
4b25d091 | 96 | struct cache_entry *last_ce = NULL; |
33db5f4d LT |
97 | |
98 | for (i = 0; i < active_nr ; i++) { | |
99 | struct cache_entry *ce = active_cache[i]; | |
de84f99c SP |
100 | if (ce_stage(ce) != checkout_stage |
101 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
d9f98eeb | 102 | continue; |
c3e9a653 | 103 | if (prefix && *prefix && |
3bd348ae JH |
104 | (ce_namelen(ce) <= prefix_length || |
105 | memcmp(prefix, ce->name, prefix_length))) | |
c3e9a653 | 106 | continue; |
de84f99c SP |
107 | if (last_ce && to_tempfile) { |
108 | if (ce_namelen(last_ce) != ce_namelen(ce) | |
109 | || memcmp(last_ce->name, ce->name, ce_namelen(ce))) | |
74c4de58 | 110 | write_tempfile_record(last_ce->name, prefix); |
de84f99c SP |
111 | } |
112 | if (checkout_entry(ce, &state, | |
113 | to_tempfile ? topath[ce_stage(ce)] : NULL) < 0) | |
4b12dae6 | 114 | errs++; |
de84f99c | 115 | last_ce = ce; |
33db5f4d | 116 | } |
de84f99c | 117 | if (last_ce && to_tempfile) |
74c4de58 | 118 | write_tempfile_record(last_ce->name, prefix); |
4b12dae6 JH |
119 | if (errs) |
120 | /* we have already done our error reporting. | |
121 | * exit with the same code as die(). | |
122 | */ | |
123 | exit(128); | |
33db5f4d LT |
124 | } |
125 | ||
a6c7db1b | 126 | static const char * const builtin_checkout_index_usage[] = { |
9c9b4f2f | 127 | N_("git checkout-index [<options>] [--] [<file>...]"), |
a6c7db1b MV |
128 | NULL |
129 | }; | |
d46ad9c9 | 130 | |
021b6e45 | 131 | static struct lock_file lock_file; |
31f584c2 | 132 | |
a6c7db1b MV |
133 | static int option_parse_u(const struct option *opt, |
134 | const char *arg, int unset) | |
135 | { | |
136 | int *newfd = opt->value; | |
137 | ||
138 | state.refresh_cache = 1; | |
d4a2024a | 139 | state.istate = &the_index; |
a6c7db1b MV |
140 | if (*newfd < 0) |
141 | *newfd = hold_locked_index(&lock_file, 1); | |
142 | return 0; | |
143 | } | |
144 | ||
145 | static int option_parse_z(const struct option *opt, | |
146 | const char *arg, int unset) | |
147 | { | |
a392f57d | 148 | nul_term_line = !unset; |
a6c7db1b MV |
149 | return 0; |
150 | } | |
151 | ||
152 | static int option_parse_prefix(const struct option *opt, | |
153 | const char *arg, int unset) | |
154 | { | |
155 | state.base_dir = arg; | |
156 | state.base_dir_len = strlen(arg); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | static int option_parse_stage(const struct option *opt, | |
161 | const char *arg, int unset) | |
162 | { | |
163 | if (!strcmp(arg, "all")) { | |
164 | to_tempfile = 1; | |
165 | checkout_stage = CHECKOUT_ALL; | |
166 | } else { | |
167 | int ch = arg[0]; | |
168 | if ('1' <= ch && ch <= '3') | |
169 | checkout_stage = arg[0] - '0'; | |
170 | else | |
171 | die("stage should be between 1 and 3 or all"); | |
172 | } | |
173 | return 0; | |
174 | } | |
175 | ||
e414156a | 176 | int cmd_checkout_index(int argc, const char **argv, const char *prefix) |
33db5f4d | 177 | { |
a65a686f | 178 | int i; |
415e96c8 | 179 | int newfd = -1; |
a65a686f | 180 | int all = 0; |
9debe63d | 181 | int read_from_stdin = 0; |
e414156a | 182 | int prefix_length; |
a6c7db1b MV |
183 | int force = 0, quiet = 0, not_new = 0; |
184 | struct option builtin_checkout_index_options[] = { | |
d5d09d47 | 185 | OPT_BOOL('a', "all", &all, |
f63cf8c9 NTND |
186 | N_("check out all files in the index")), |
187 | OPT__FORCE(&force, N_("force overwrite of existing files")), | |
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")), |
a6c7db1b | 192 | { OPTION_CALLBACK, 'u', "index", &newfd, NULL, |
0ed21718 | 193 | N_("update stat information in the index file"), |
a6c7db1b MV |
194 | PARSE_OPT_NOARG, option_parse_u }, |
195 | { OPTION_CALLBACK, 'z', NULL, NULL, NULL, | |
0ed21718 | 196 | N_("paths are separated with NUL character"), |
a6c7db1b | 197 | PARSE_OPT_NOARG, option_parse_z }, |
d5d09d47 | 198 | OPT_BOOL(0, "stdin", &read_from_stdin, |
0ed21718 | 199 | N_("read list of paths from the standard input")), |
d5d09d47 | 200 | OPT_BOOL(0, "temp", &to_tempfile, |
0ed21718 NTND |
201 | N_("write the content to temporary files")), |
202 | OPT_CALLBACK(0, "prefix", NULL, N_("string"), | |
203 | N_("when creating files, prepend <string>"), | |
a6c7db1b MV |
204 | option_parse_prefix), |
205 | OPT_CALLBACK(0, "stage", NULL, NULL, | |
0ed21718 | 206 | N_("copy out the files from named stage"), |
a6c7db1b MV |
207 | option_parse_stage), |
208 | OPT_END() | |
209 | }; | |
33db5f4d | 210 | |
cf9d52e4 NTND |
211 | if (argc == 2 && !strcmp(argv[1], "-h")) |
212 | usage_with_options(builtin_checkout_index_usage, | |
213 | builtin_checkout_index_options); | |
ef90d6d4 | 214 | git_config(git_default_config, NULL); |
e414156a | 215 | state.base_dir = ""; |
c3e9a653 JH |
216 | prefix_length = prefix ? strlen(prefix) : 0; |
217 | ||
33db5f4d | 218 | if (read_cache() < 0) { |
2de381f9 | 219 | die("invalid cache"); |
33db5f4d LT |
220 | } |
221 | ||
37782920 | 222 | argc = parse_options(argc, argv, prefix, builtin_checkout_index_options, |
a6c7db1b MV |
223 | builtin_checkout_index_usage, 0); |
224 | state.force = force; | |
225 | state.quiet = quiet; | |
226 | state.not_new = not_new; | |
a65a686f | 227 | |
de84f99c | 228 | if (state.base_dir_len || to_tempfile) { |
a65a686f LT |
229 | /* when --prefix is specified we do not |
230 | * want to update cache. | |
231 | */ | |
232 | if (state.refresh_cache) { | |
021b6e45 | 233 | rollback_lock_file(&lock_file); |
4ed7cd3a | 234 | newfd = -1; |
a65a686f LT |
235 | } |
236 | state.refresh_cache = 0; | |
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); |
74c4de58 | 249 | checkout_file(p, prefix); |
d7a643b7 | 250 | free(p); |
33db5f4d | 251 | } |
415e96c8 | 252 | |
9debe63d | 253 | if (read_from_stdin) { |
f285a2d7 | 254 | struct strbuf buf = STRBUF_INIT, nbuf = 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] == '"') { |
7fb1011e PH |
264 | strbuf_reset(&nbuf); |
265 | if (unquote_c_style(&nbuf, buf.buf, NULL)) | |
266 | die("line is badly quoted"); | |
267 | strbuf_swap(&buf, &nbuf); | |
268 | } | |
269 | p = prefix_path(prefix, prefix_length, buf.buf); | |
74c4de58 | 270 | checkout_file(p, prefix); |
d7a643b7 | 271 | free(p); |
9debe63d | 272 | } |
7fb1011e | 273 | strbuf_release(&nbuf); |
e6c019d0 | 274 | strbuf_release(&buf); |
9debe63d SP |
275 | } |
276 | ||
a65a686f | 277 | if (all) |
e414156a | 278 | checkout_all(prefix, prefix_length); |
a65a686f | 279 | |
415e96c8 | 280 | if (0 <= newfd && |
03b86647 | 281 | write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
021b6e45 | 282 | die("Unable to write new index file"); |
33db5f4d LT |
283 | return 0; |
284 | } |