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