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