cvsserver: Eclipse compat - browsing 'modules' (heads in our case) works
[git/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
LT
24 *
25 * which will force all existing *.h files to be replaced with
26 * their cached copies. If an empty command line implied "all",
27 * then this would force-refresh everything in the cache, which
28 * was not the point.
29 *
30 * Oh, and the "--" is just a good idea when you know the rest
31 * will be filenames. Just so that you wouldn't have a filename
32 * of "-a" causing problems (not possible in the above example,
33 * but get used to it in scripting!).
34 */
35#include "cache.h"
36
c3e9a653
JH
37static const char *prefix;
38static int prefix_length;
3bd348ae 39static int checkout_stage; /* default to checkout stage0 */
c3e9a653 40
12dccc16
LT
41static struct checkout state = {
42 .base_dir = "",
43 .base_dir_len = 0,
44 .force = 0,
45 .quiet = 0,
46 .not_new = 0,
47 .refresh_cache = 0,
48};
33db5f4d 49
d7f6ea3d 50static int checkout_file(const char *name)
33db5f4d 51{
3bd348ae
JH
52 int namelen = strlen(name);
53 int pos = cache_name_pos(name, namelen);
54 int has_same_name = 0;
55
56 if (pos < 0)
57 pos = -pos - 1;
58
59 while (pos < active_nr) {
60 struct cache_entry *ce = active_cache[pos];
f4f9adae 61 if (ce_namelen(ce) != namelen ||
3bd348ae
JH
62 memcmp(ce->name, name, namelen))
63 break;
64 has_same_name = 1;
65 if (checkout_stage == ce_stage(ce))
66 return checkout_entry(ce, &state);
67 pos++;
33db5f4d 68 }
3bd348ae
JH
69
70 if (!state.quiet) {
71 fprintf(stderr, "git-checkout-index: %s ", name);
72 if (!has_same_name)
73 fprintf(stderr, "is not in the cache");
74 else if (checkout_stage)
75 fprintf(stderr, "does not exist at stage %d",
76 checkout_stage);
77 else
78 fprintf(stderr, "is unmerged");
79 fputc('\n', stderr);
80 }
81 return -1;
33db5f4d
LT
82}
83
d7f6ea3d 84static int checkout_all(void)
33db5f4d 85{
4b12dae6 86 int i, errs = 0;
33db5f4d
LT
87
88 for (i = 0; i < active_nr ; i++) {
89 struct cache_entry *ce = active_cache[i];
3bd348ae 90 if (ce_stage(ce) != checkout_stage)
d9f98eeb 91 continue;
c3e9a653 92 if (prefix && *prefix &&
3bd348ae
JH
93 (ce_namelen(ce) <= prefix_length ||
94 memcmp(prefix, ce->name, prefix_length)))
c3e9a653 95 continue;
12dccc16 96 if (checkout_entry(ce, &state) < 0)
4b12dae6 97 errs++;
33db5f4d 98 }
4b12dae6
JH
99 if (errs)
100 /* we have already done our error reporting.
101 * exit with the same code as die().
102 */
103 exit(128);
33db5f4d
LT
104 return 0;
105}
106
4d1f1190 107static const char checkout_cache_usage[] =
3bd348ae 108"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";
d46ad9c9 109
31f584c2
JB
110static struct cache_file cache_file;
111
33db5f4d
LT
112int main(int argc, char **argv)
113{
a65a686f 114 int i;
415e96c8 115 int newfd = -1;
a65a686f 116 int all = 0;
33db5f4d 117
c3e9a653 118 prefix = setup_git_directory();
5f73076c 119 git_config(git_default_config);
c3e9a653
JH
120 prefix_length = prefix ? strlen(prefix) : 0;
121
33db5f4d 122 if (read_cache() < 0) {
2de381f9 123 die("invalid cache");
33db5f4d
LT
124 }
125
126 for (i = 1; i < argc; i++) {
127 const char *arg = argv[i];
a65a686f
LT
128
129 if (!strcmp(arg, "--")) {
130 i++;
131 break;
132 }
133 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
134 all = 1;
135 continue;
33db5f4d 136 }
a65a686f
LT
137 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
138 state.force = 1;
139 continue;
33db5f4d 140 }
a65a686f
LT
141 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
142 state.quiet = 1;
143 continue;
415e96c8 144 }
a65a686f
LT
145 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
146 state.not_new = 1;
147 continue;
148 }
149 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
150 state.refresh_cache = 1;
151 if (newfd < 0)
152 newfd = hold_index_file_for_update
153 (&cache_file,
154 get_index_file());
155 if (newfd < 0)
156 die("cannot open index.lock file.");
157 continue;
415e96c8 158 }
3bd348ae 159 if (!strncmp(arg, "--prefix=", 9)) {
a65a686f
LT
160 state.base_dir = arg+9;
161 state.base_dir_len = strlen(state.base_dir);
162 continue;
163 }
3bd348ae
JH
164 if (!strncmp(arg, "--stage=", 8)) {
165 int ch = arg[8];
166 if ('1' <= ch && ch <= '3')
167 checkout_stage = arg[8] - '0';
168 else
169 die("stage should be between 1 and 3");
170 continue;
171 }
a65a686f
LT
172 if (arg[0] == '-')
173 usage(checkout_cache_usage);
174 break;
175 }
176
177 if (state.base_dir_len) {
178 /* when --prefix is specified we do not
179 * want to update cache.
180 */
181 if (state.refresh_cache) {
182 close(newfd); newfd = -1;
183 rollback_index_file(&cache_file);
184 }
185 state.refresh_cache = 0;
186 }
187
188 /* Check out named files first */
189 for ( ; i < argc; i++) {
190 const char *arg = argv[i];
191
192 if (all)
193 die("git-checkout-index: don't mix '--all' and explicit filenames");
c3e9a653 194 checkout_file(prefix_path(prefix, prefix_length, arg));
33db5f4d 195 }
415e96c8 196
a65a686f
LT
197 if (all)
198 checkout_all();
199
415e96c8
JH
200 if (0 <= newfd &&
201 (write_cache(newfd, active_cache, active_nr) ||
202 commit_index_file(&cache_file)))
203 die("Unable to write new cachefile");
33db5f4d
LT
204 return 0;
205}