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