]> git.ipfire.org Git - thirdparty/git.git/blame - checkout-index.c
Ensure git-clone exits with error if perl script fails.
[thirdparty/git.git] / checkout-index.c
CommitLineData
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"
bad68ec9 42#include "cache-tree.h"
33db5f4d 43
de84f99c 44#define CHECKOUT_ALL 4
c3e9a653
JH
45static const char *prefix;
46static int prefix_length;
de84f99c 47static int line_termination = '\n';
3bd348ae 48static int checkout_stage; /* default to checkout stage0 */
de84f99c
SP
49static int to_tempfile;
50static char topath[4][MAXPATHLEN+1];
c3e9a653 51
344c52ae 52static struct checkout state;
33db5f4d 53
de84f99c
SP
54static void write_tempfile_record (const char *name)
55{
56 int i;
57
58 if (CHECKOUT_ALL == checkout_stage) {
59 for (i = 1; i < 4; i++) {
60 if (i > 1)
61 putchar(' ');
62 if (topath[i][0])
63 fputs(topath[i], stdout);
64 else
65 putchar('.');
66 }
67 } else
68 fputs(topath[checkout_stage], stdout);
69
70 putchar('\t');
71 write_name_quoted("", 0, name + prefix_length,
72 line_termination, stdout);
73 putchar(line_termination);
74
75 for (i = 0; i < 4; i++) {
76 topath[i][0] = 0;
77 }
78}
79
d7f6ea3d 80static int checkout_file(const char *name)
33db5f4d 81{
3bd348ae
JH
82 int namelen = strlen(name);
83 int pos = cache_name_pos(name, namelen);
84 int has_same_name = 0;
de84f99c
SP
85 int did_checkout = 0;
86 int errs = 0;
3bd348ae
JH
87
88 if (pos < 0)
89 pos = -pos - 1;
90
91 while (pos < active_nr) {
92 struct cache_entry *ce = active_cache[pos];
f4f9adae 93 if (ce_namelen(ce) != namelen ||
3bd348ae
JH
94 memcmp(ce->name, name, namelen))
95 break;
96 has_same_name = 1;
3bd348ae 97 pos++;
de84f99c
SP
98 if (ce_stage(ce) != checkout_stage
99 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
100 continue;
101 did_checkout = 1;
102 if (checkout_entry(ce, &state,
103 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
104 errs++;
105 }
106
107 if (did_checkout) {
108 if (to_tempfile)
109 write_tempfile_record(name);
110 return errs > 0 ? -1 : 0;
33db5f4d 111 }
3bd348ae
JH
112
113 if (!state.quiet) {
114 fprintf(stderr, "git-checkout-index: %s ", name);
115 if (!has_same_name)
116 fprintf(stderr, "is not in the cache");
117 else if (checkout_stage)
118 fprintf(stderr, "does not exist at stage %d",
119 checkout_stage);
120 else
121 fprintf(stderr, "is unmerged");
122 fputc('\n', stderr);
123 }
124 return -1;
33db5f4d
LT
125}
126
d7f6ea3d 127static int checkout_all(void)
33db5f4d 128{
4b12dae6 129 int i, errs = 0;
5142db69 130 struct cache_entry* last_ce = NULL;
33db5f4d
LT
131
132 for (i = 0; i < active_nr ; i++) {
133 struct cache_entry *ce = active_cache[i];
de84f99c
SP
134 if (ce_stage(ce) != checkout_stage
135 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
d9f98eeb 136 continue;
c3e9a653 137 if (prefix && *prefix &&
3bd348ae
JH
138 (ce_namelen(ce) <= prefix_length ||
139 memcmp(prefix, ce->name, prefix_length)))
c3e9a653 140 continue;
de84f99c
SP
141 if (last_ce && to_tempfile) {
142 if (ce_namelen(last_ce) != ce_namelen(ce)
143 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
144 write_tempfile_record(last_ce->name);
145 }
146 if (checkout_entry(ce, &state,
147 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
4b12dae6 148 errs++;
de84f99c 149 last_ce = ce;
33db5f4d 150 }
de84f99c
SP
151 if (last_ce && to_tempfile)
152 write_tempfile_record(last_ce->name);
4b12dae6
JH
153 if (errs)
154 /* we have already done our error reporting.
155 * exit with the same code as die().
156 */
157 exit(128);
33db5f4d
LT
158 return 0;
159}
160
4d1f1190 161static const char checkout_cache_usage[] =
de84f99c 162"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
d46ad9c9 163
021b6e45 164static struct lock_file lock_file;
31f584c2 165
33db5f4d
LT
166int main(int argc, char **argv)
167{
a65a686f 168 int i;
415e96c8 169 int newfd = -1;
a65a686f 170 int all = 0;
9debe63d 171 int read_from_stdin = 0;
33db5f4d 172
344c52ae 173 state.base_dir = "";
c3e9a653 174 prefix = setup_git_directory();
5f73076c 175 git_config(git_default_config);
c3e9a653
JH
176 prefix_length = prefix ? strlen(prefix) : 0;
177
33db5f4d 178 if (read_cache() < 0) {
2de381f9 179 die("invalid cache");
33db5f4d
LT
180 }
181
182 for (i = 1; i < argc; i++) {
183 const char *arg = argv[i];
a65a686f
LT
184
185 if (!strcmp(arg, "--")) {
186 i++;
187 break;
188 }
189 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
190 all = 1;
191 continue;
33db5f4d 192 }
a65a686f
LT
193 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
194 state.force = 1;
195 continue;
33db5f4d 196 }
a65a686f
LT
197 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
198 state.quiet = 1;
199 continue;
415e96c8 200 }
a65a686f
LT
201 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
202 state.not_new = 1;
203 continue;
204 }
205 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
206 state.refresh_cache = 1;
207 if (newfd < 0)
021b6e45
JH
208 newfd = hold_lock_file_for_update
209 (&lock_file, get_index_file());
a65a686f
LT
210 if (newfd < 0)
211 die("cannot open index.lock file.");
212 continue;
415e96c8 213 }
9debe63d
SP
214 if (!strcmp(arg, "-z")) {
215 line_termination = 0;
216 continue;
217 }
218 if (!strcmp(arg, "--stdin")) {
219 if (i != argc - 1)
220 die("--stdin must be at the end");
221 read_from_stdin = 1;
222 i++; /* do not consider arg as a file name */
223 break;
224 }
de84f99c
SP
225 if (!strcmp(arg, "--temp")) {
226 to_tempfile = 1;
227 continue;
228 }
3bd348ae 229 if (!strncmp(arg, "--prefix=", 9)) {
a65a686f
LT
230 state.base_dir = arg+9;
231 state.base_dir_len = strlen(state.base_dir);
232 continue;
233 }
3bd348ae 234 if (!strncmp(arg, "--stage=", 8)) {
de84f99c
SP
235 if (!strcmp(arg + 8, "all")) {
236 to_tempfile = 1;
237 checkout_stage = CHECKOUT_ALL;
238 } else {
239 int ch = arg[8];
240 if ('1' <= ch && ch <= '3')
241 checkout_stage = arg[8] - '0';
242 else
243 die("stage should be between 1 and 3 or all");
244 }
3bd348ae
JH
245 continue;
246 }
a65a686f
LT
247 if (arg[0] == '-')
248 usage(checkout_cache_usage);
249 break;
250 }
251
de84f99c 252 if (state.base_dir_len || to_tempfile) {
a65a686f
LT
253 /* when --prefix is specified we do not
254 * want to update cache.
255 */
256 if (state.refresh_cache) {
257 close(newfd); newfd = -1;
021b6e45 258 rollback_lock_file(&lock_file);
a65a686f
LT
259 }
260 state.refresh_cache = 0;
261 }
262
263 /* Check out named files first */
264 for ( ; i < argc; i++) {
265 const char *arg = argv[i];
dc46da22 266 const char *p;
a65a686f
LT
267
268 if (all)
269 die("git-checkout-index: don't mix '--all' and explicit filenames");
9debe63d
SP
270 if (read_from_stdin)
271 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
dc46da22
JH
272 p = prefix_path(prefix, prefix_length, arg);
273 checkout_file(p);
be65e7d9 274 if (p < arg || p > arg + strlen(arg))
dc46da22 275 free((char*)p);
33db5f4d 276 }
415e96c8 277
9debe63d
SP
278 if (read_from_stdin) {
279 struct strbuf buf;
280 if (all)
281 die("git-checkout-index: don't mix '--all' and '--stdin'");
282 strbuf_init(&buf);
283 while (1) {
284 char *path_name;
dc46da22
JH
285 const char *p;
286
9debe63d
SP
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;
dc46da22
JH
294 p = prefix_path(prefix, prefix_length, path_name);
295 checkout_file(p);
be65e7d9 296 if (p < path_name || p > path_name + strlen(path_name))
dc46da22 297 free((char *)p);
9debe63d
SP
298 if (path_name != buf.buf)
299 free(path_name);
300 }
301 }
302
a65a686f
LT
303 if (all)
304 checkout_all();
305
415e96c8
JH
306 if (0 <= newfd &&
307 (write_cache(newfd, active_cache, active_nr) ||
6244b249 308 close(newfd) || commit_lock_file(&lock_file)))
021b6e45 309 die("Unable to write new index file");
33db5f4d
LT
310 return 0;
311}