git.c: allow alias expansion without a git directory
[git/jnareb-git/bp-gitweb.git] / checkout-index.c
blob61152f34b7fc00e745a2f7524c4da5b5a1a780a2
1 /*
2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 * Careful: order of argument flags does matter. For example,
8 * git-checkout-index -a -f file.c
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).
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".
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
23 * find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
25 * or:
27 * find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
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.
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!).
39 #include "cache.h"
40 #include "strbuf.h"
41 #include "quote.h"
42 #include "cache-tree.h"
44 #define CHECKOUT_ALL 4
45 static const char *prefix;
46 static int prefix_length;
47 static int line_termination = '\n';
48 static int checkout_stage; /* default to checkout stage0 */
49 static int to_tempfile;
50 static char topath[4][MAXPATHLEN+1];
52 static struct checkout state;
54 static void write_tempfile_record (const char *name)
56 int i;
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('.');
67 } else
68 fputs(topath[checkout_stage], stdout);
70 putchar('\t');
71 write_name_quoted("", 0, name + prefix_length,
72 line_termination, stdout);
73 putchar(line_termination);
75 for (i = 0; i < 4; i++) {
76 topath[i][0] = 0;
80 static int checkout_file(const char *name)
82 int namelen = strlen(name);
83 int pos = cache_name_pos(name, namelen);
84 int has_same_name = 0;
85 int did_checkout = 0;
86 int errs = 0;
88 if (pos < 0)
89 pos = -pos - 1;
91 while (pos < active_nr) {
92 struct cache_entry *ce = active_cache[pos];
93 if (ce_namelen(ce) != namelen ||
94 memcmp(ce->name, name, namelen))
95 break;
96 has_same_name = 1;
97 pos++;
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++;
107 if (did_checkout) {
108 if (to_tempfile)
109 write_tempfile_record(name);
110 return errs > 0 ? -1 : 0;
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);
124 return -1;
127 static int checkout_all(void)
129 int i, errs = 0;
130 struct cache_entry* last_ce = NULL;
132 for (i = 0; i < active_nr ; i++) {
133 struct cache_entry *ce = active_cache[i];
134 if (ce_stage(ce) != checkout_stage
135 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
136 continue;
137 if (prefix && *prefix &&
138 (ce_namelen(ce) <= prefix_length ||
139 memcmp(prefix, ce->name, prefix_length)))
140 continue;
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);
146 if (checkout_entry(ce, &state,
147 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
148 errs++;
149 last_ce = ce;
151 if (last_ce && to_tempfile)
152 write_tempfile_record(last_ce->name);
153 if (errs)
154 /* we have already done our error reporting.
155 * exit with the same code as die().
157 exit(128);
158 return 0;
161 static const char checkout_cache_usage[] =
162 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
164 static struct lock_file lock_file;
166 int main(int argc, char **argv)
168 int i;
169 int newfd = -1;
170 int all = 0;
171 int read_from_stdin = 0;
173 state.base_dir = "";
174 prefix = setup_git_directory();
175 git_config(git_default_config);
176 prefix_length = prefix ? strlen(prefix) : 0;
178 if (read_cache() < 0) {
179 die("invalid cache");
182 for (i = 1; i < argc; i++) {
183 const char *arg = argv[i];
185 if (!strcmp(arg, "--")) {
186 i++;
187 break;
189 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
190 all = 1;
191 continue;
193 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
194 state.force = 1;
195 continue;
197 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
198 state.quiet = 1;
199 continue;
201 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
202 state.not_new = 1;
203 continue;
205 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
206 state.refresh_cache = 1;
207 if (newfd < 0)
208 newfd = hold_lock_file_for_update
209 (&lock_file, get_index_file());
210 if (newfd < 0)
211 die("cannot open index.lock file.");
212 continue;
214 if (!strcmp(arg, "-z")) {
215 line_termination = 0;
216 continue;
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;
225 if (!strcmp(arg, "--temp")) {
226 to_tempfile = 1;
227 continue;
229 if (!strncmp(arg, "--prefix=", 9)) {
230 state.base_dir = arg+9;
231 state.base_dir_len = strlen(state.base_dir);
232 continue;
234 if (!strncmp(arg, "--stage=", 8)) {
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");
245 continue;
247 if (arg[0] == '-')
248 usage(checkout_cache_usage);
249 break;
252 if (state.base_dir_len || to_tempfile) {
253 /* when --prefix is specified we do not
254 * want to update cache.
256 if (state.refresh_cache) {
257 close(newfd); newfd = -1;
258 rollback_lock_file(&lock_file);
260 state.refresh_cache = 0;
263 /* Check out named files first */
264 for ( ; i < argc; i++) {
265 const char *arg = argv[i];
266 const char *p;
268 if (all)
269 die("git-checkout-index: don't mix '--all' and explicit filenames");
270 if (read_from_stdin)
271 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
272 p = prefix_path(prefix, prefix_length, arg);
273 checkout_file(p);
274 if (p < arg || p > arg + strlen(arg))
275 free((char*)p);
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;
285 const char *p;
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 p = prefix_path(prefix, prefix_length, path_name);
295 checkout_file(p);
296 if (p < path_name || p > path_name + strlen(path_name))
297 free((char *)p);
298 if (path_name != buf.buf)
299 free(path_name);
303 if (all)
304 checkout_all();
306 if (0 <= newfd &&
307 (write_cache(newfd, active_cache, active_nr) ||
308 close(newfd) || commit_lock_file(&lock_file)))
309 die("Unable to write new index file");
310 return 0;