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