]>
Commit | Line | Data |
---|---|---|
33db5f4d LT |
1 | /* |
2 | * Check-out files from the "current cache directory" | |
3 | * | |
4 | * Copyright (C) 2005 Linus Torvalds | |
5 | * | |
6 | * Careful: order of argument flags does matter. For example, | |
7 | * | |
215a7ad1 | 8 | * git-checkout-index -a -f file.c |
33db5f4d LT |
9 | * |
10 | * Will first check out all files listed in the cache (but not | |
11 | * overwrite any old ones), and then force-checkout "file.c" a | |
12 | * second time (ie that one _will_ overwrite any old contents | |
13 | * with the same filename). | |
14 | * | |
215a7ad1 JH |
15 | * Also, just doing "git-checkout-index" does nothing. You probably |
16 | * meant "git-checkout-index -a". And if you want to force it, you | |
17 | * want "git-checkout-index -f -a". | |
33db5f4d LT |
18 | * |
19 | * Intuitiveness is not the goal here. Repeatability is. The | |
20 | * reason for the "no arguments means no work" thing is that | |
21 | * from scripts you are supposed to be able to do things like | |
22 | * | |
215a7ad1 | 23 | * find . -name '*.h' -print0 | xargs -0 git-checkout-index -f -- |
33db5f4d | 24 | * |
9debe63d SP |
25 | * or: |
26 | * | |
27 | * find . -name '*.h' -print0 | git-checkout-index -f -z --stdin | |
28 | * | |
33db5f4d LT |
29 | * which will force all existing *.h files to be replaced with |
30 | * their cached copies. If an empty command line implied "all", | |
31 | * then this would force-refresh everything in the cache, which | |
32 | * was not the point. | |
33 | * | |
34 | * Oh, and the "--" is just a good idea when you know the rest | |
35 | * will be filenames. Just so that you wouldn't have a filename | |
36 | * of "-a" causing problems (not possible in the above example, | |
37 | * but get used to it in scripting!). | |
38 | */ | |
39 | #include "cache.h" | |
9debe63d SP |
40 | #include "strbuf.h" |
41 | #include "quote.h" | |
33db5f4d | 42 | |
de84f99c | 43 | #define CHECKOUT_ALL 4 |
c3e9a653 JH |
44 | static const char *prefix; |
45 | static int prefix_length; | |
de84f99c | 46 | static int line_termination = '\n'; |
3bd348ae | 47 | static int checkout_stage; /* default to checkout stage0 */ |
de84f99c SP |
48 | static int to_tempfile; |
49 | static char topath[4][MAXPATHLEN+1]; | |
c3e9a653 | 50 | |
12dccc16 LT |
51 | static struct checkout state = { |
52 | .base_dir = "", | |
53 | .base_dir_len = 0, | |
54 | .force = 0, | |
55 | .quiet = 0, | |
56 | .not_new = 0, | |
57 | .refresh_cache = 0, | |
58 | }; | |
33db5f4d | 59 | |
de84f99c SP |
60 | static void write_tempfile_record (const char *name) |
61 | { | |
62 | int i; | |
63 | ||
64 | if (CHECKOUT_ALL == checkout_stage) { | |
65 | for (i = 1; i < 4; i++) { | |
66 | if (i > 1) | |
67 | putchar(' '); | |
68 | if (topath[i][0]) | |
69 | fputs(topath[i], stdout); | |
70 | else | |
71 | putchar('.'); | |
72 | } | |
73 | } else | |
74 | fputs(topath[checkout_stage], stdout); | |
75 | ||
76 | putchar('\t'); | |
77 | write_name_quoted("", 0, name + prefix_length, | |
78 | line_termination, stdout); | |
79 | putchar(line_termination); | |
80 | ||
81 | for (i = 0; i < 4; i++) { | |
82 | topath[i][0] = 0; | |
83 | } | |
84 | } | |
85 | ||
d7f6ea3d | 86 | static int checkout_file(const char *name) |
33db5f4d | 87 | { |
3bd348ae JH |
88 | int namelen = strlen(name); |
89 | int pos = cache_name_pos(name, namelen); | |
90 | int has_same_name = 0; | |
de84f99c SP |
91 | int did_checkout = 0; |
92 | int errs = 0; | |
3bd348ae JH |
93 | |
94 | if (pos < 0) | |
95 | pos = -pos - 1; | |
96 | ||
97 | while (pos < active_nr) { | |
98 | struct cache_entry *ce = active_cache[pos]; | |
f4f9adae | 99 | if (ce_namelen(ce) != namelen || |
3bd348ae JH |
100 | memcmp(ce->name, name, namelen)) |
101 | break; | |
102 | has_same_name = 1; | |
3bd348ae | 103 | pos++; |
de84f99c SP |
104 | if (ce_stage(ce) != checkout_stage |
105 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
106 | continue; | |
107 | did_checkout = 1; | |
108 | if (checkout_entry(ce, &state, | |
109 | to_tempfile ? topath[ce_stage(ce)] : NULL) < 0) | |
110 | errs++; | |
111 | } | |
112 | ||
113 | if (did_checkout) { | |
114 | if (to_tempfile) | |
115 | write_tempfile_record(name); | |
116 | return errs > 0 ? -1 : 0; | |
33db5f4d | 117 | } |
3bd348ae JH |
118 | |
119 | if (!state.quiet) { | |
120 | fprintf(stderr, "git-checkout-index: %s ", name); | |
121 | if (!has_same_name) | |
122 | fprintf(stderr, "is not in the cache"); | |
123 | else if (checkout_stage) | |
124 | fprintf(stderr, "does not exist at stage %d", | |
125 | checkout_stage); | |
126 | else | |
127 | fprintf(stderr, "is unmerged"); | |
128 | fputc('\n', stderr); | |
129 | } | |
130 | return -1; | |
33db5f4d LT |
131 | } |
132 | ||
d7f6ea3d | 133 | static int checkout_all(void) |
33db5f4d | 134 | { |
4b12dae6 | 135 | int i, errs = 0; |
5142db69 | 136 | struct cache_entry* last_ce = NULL; |
33db5f4d LT |
137 | |
138 | for (i = 0; i < active_nr ; i++) { | |
139 | struct cache_entry *ce = active_cache[i]; | |
de84f99c SP |
140 | if (ce_stage(ce) != checkout_stage |
141 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) | |
d9f98eeb | 142 | continue; |
c3e9a653 | 143 | if (prefix && *prefix && |
3bd348ae JH |
144 | (ce_namelen(ce) <= prefix_length || |
145 | memcmp(prefix, ce->name, prefix_length))) | |
c3e9a653 | 146 | continue; |
de84f99c SP |
147 | if (last_ce && to_tempfile) { |
148 | if (ce_namelen(last_ce) != ce_namelen(ce) | |
149 | || memcmp(last_ce->name, ce->name, ce_namelen(ce))) | |
150 | write_tempfile_record(last_ce->name); | |
151 | } | |
152 | if (checkout_entry(ce, &state, | |
153 | to_tempfile ? topath[ce_stage(ce)] : NULL) < 0) | |
4b12dae6 | 154 | errs++; |
de84f99c | 155 | last_ce = ce; |
33db5f4d | 156 | } |
de84f99c SP |
157 | if (last_ce && to_tempfile) |
158 | write_tempfile_record(last_ce->name); | |
4b12dae6 JH |
159 | if (errs) |
160 | /* we have already done our error reporting. | |
161 | * exit with the same code as die(). | |
162 | */ | |
163 | exit(128); | |
33db5f4d LT |
164 | return 0; |
165 | } | |
166 | ||
4d1f1190 | 167 | static const char checkout_cache_usage[] = |
de84f99c | 168 | "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>..."; |
d46ad9c9 | 169 | |
31f584c2 JB |
170 | static struct cache_file cache_file; |
171 | ||
33db5f4d LT |
172 | int main(int argc, char **argv) |
173 | { | |
a65a686f | 174 | int i; |
415e96c8 | 175 | int newfd = -1; |
a65a686f | 176 | int all = 0; |
9debe63d | 177 | int read_from_stdin = 0; |
33db5f4d | 178 | |
c3e9a653 | 179 | prefix = setup_git_directory(); |
5f73076c | 180 | git_config(git_default_config); |
c3e9a653 JH |
181 | prefix_length = prefix ? strlen(prefix) : 0; |
182 | ||
33db5f4d | 183 | if (read_cache() < 0) { |
2de381f9 | 184 | die("invalid cache"); |
33db5f4d LT |
185 | } |
186 | ||
187 | for (i = 1; i < argc; i++) { | |
188 | const char *arg = argv[i]; | |
a65a686f LT |
189 | |
190 | if (!strcmp(arg, "--")) { | |
191 | i++; | |
192 | break; | |
193 | } | |
194 | if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) { | |
195 | all = 1; | |
196 | continue; | |
33db5f4d | 197 | } |
a65a686f LT |
198 | if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) { |
199 | state.force = 1; | |
200 | continue; | |
33db5f4d | 201 | } |
a65a686f LT |
202 | if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) { |
203 | state.quiet = 1; | |
204 | continue; | |
415e96c8 | 205 | } |
a65a686f LT |
206 | if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) { |
207 | state.not_new = 1; | |
208 | continue; | |
209 | } | |
210 | if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) { | |
211 | state.refresh_cache = 1; | |
212 | if (newfd < 0) | |
213 | newfd = hold_index_file_for_update | |
214 | (&cache_file, | |
215 | get_index_file()); | |
216 | if (newfd < 0) | |
217 | die("cannot open index.lock file."); | |
218 | continue; | |
415e96c8 | 219 | } |
9debe63d SP |
220 | if (!strcmp(arg, "-z")) { |
221 | line_termination = 0; | |
222 | continue; | |
223 | } | |
224 | if (!strcmp(arg, "--stdin")) { | |
225 | if (i != argc - 1) | |
226 | die("--stdin must be at the end"); | |
227 | read_from_stdin = 1; | |
228 | i++; /* do not consider arg as a file name */ | |
229 | break; | |
230 | } | |
de84f99c SP |
231 | if (!strcmp(arg, "--temp")) { |
232 | to_tempfile = 1; | |
233 | continue; | |
234 | } | |
3bd348ae | 235 | if (!strncmp(arg, "--prefix=", 9)) { |
a65a686f LT |
236 | state.base_dir = arg+9; |
237 | state.base_dir_len = strlen(state.base_dir); | |
238 | continue; | |
239 | } | |
3bd348ae | 240 | if (!strncmp(arg, "--stage=", 8)) { |
de84f99c SP |
241 | if (!strcmp(arg + 8, "all")) { |
242 | to_tempfile = 1; | |
243 | checkout_stage = CHECKOUT_ALL; | |
244 | } else { | |
245 | int ch = arg[8]; | |
246 | if ('1' <= ch && ch <= '3') | |
247 | checkout_stage = arg[8] - '0'; | |
248 | else | |
249 | die("stage should be between 1 and 3 or all"); | |
250 | } | |
3bd348ae JH |
251 | continue; |
252 | } | |
a65a686f LT |
253 | if (arg[0] == '-') |
254 | usage(checkout_cache_usage); | |
255 | break; | |
256 | } | |
257 | ||
de84f99c | 258 | if (state.base_dir_len || to_tempfile) { |
a65a686f LT |
259 | /* when --prefix is specified we do not |
260 | * want to update cache. | |
261 | */ | |
262 | if (state.refresh_cache) { | |
263 | close(newfd); newfd = -1; | |
264 | rollback_index_file(&cache_file); | |
265 | } | |
266 | state.refresh_cache = 0; | |
267 | } | |
268 | ||
269 | /* Check out named files first */ | |
270 | for ( ; i < argc; i++) { | |
271 | const char *arg = argv[i]; | |
272 | ||
273 | if (all) | |
274 | die("git-checkout-index: don't mix '--all' and explicit filenames"); | |
9debe63d SP |
275 | if (read_from_stdin) |
276 | die("git-checkout-index: don't mix '--stdin' and explicit filenames"); | |
c3e9a653 | 277 | checkout_file(prefix_path(prefix, prefix_length, arg)); |
33db5f4d | 278 | } |
415e96c8 | 279 | |
9debe63d SP |
280 | if (read_from_stdin) { |
281 | struct strbuf buf; | |
282 | if (all) | |
283 | die("git-checkout-index: don't mix '--all' and '--stdin'"); | |
284 | strbuf_init(&buf); | |
285 | while (1) { | |
286 | char *path_name; | |
287 | read_line(&buf, stdin, line_termination); | |
288 | if (buf.eof) | |
289 | break; | |
290 | if (line_termination && buf.buf[0] == '"') | |
291 | path_name = unquote_c_style(buf.buf, NULL); | |
292 | else | |
293 | path_name = buf.buf; | |
294 | checkout_file(prefix_path(prefix, prefix_length, path_name)); | |
295 | if (path_name != buf.buf) | |
296 | free(path_name); | |
297 | } | |
298 | } | |
299 | ||
a65a686f LT |
300 | if (all) |
301 | checkout_all(); | |
302 | ||
415e96c8 JH |
303 | if (0 <= newfd && |
304 | (write_cache(newfd, active_cache, active_nr) || | |
305 | commit_index_file(&cache_file))) | |
306 | die("Unable to write new cachefile"); | |
33db5f4d LT |
307 | return 0; |
308 | } |