cosmetics: change from 'See-Also' to 'See Also'
[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 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"
33db5f4d 42
c3e9a653
JH
43static const char *prefix;
44static int prefix_length;
3bd348ae 45static int checkout_stage; /* default to checkout stage0 */
c3e9a653 46
12dccc16
LT
47static struct checkout state = {
48 .base_dir = "",
49 .base_dir_len = 0,
50 .force = 0,
51 .quiet = 0,
52 .not_new = 0,
53 .refresh_cache = 0,
54};
33db5f4d 55
d7f6ea3d 56static int checkout_file(const char *name)
33db5f4d 57{
3bd348ae
JH
58 int namelen = strlen(name);
59 int pos = cache_name_pos(name, namelen);
60 int has_same_name = 0;
61
62 if (pos < 0)
63 pos = -pos - 1;
64
65 while (pos < active_nr) {
66 struct cache_entry *ce = active_cache[pos];
f4f9adae 67 if (ce_namelen(ce) != namelen ||
3bd348ae
JH
68 memcmp(ce->name, name, namelen))
69 break;
70 has_same_name = 1;
71 if (checkout_stage == ce_stage(ce))
72 return checkout_entry(ce, &state);
73 pos++;
33db5f4d 74 }
3bd348ae
JH
75
76 if (!state.quiet) {
77 fprintf(stderr, "git-checkout-index: %s ", name);
78 if (!has_same_name)
79 fprintf(stderr, "is not in the cache");
80 else if (checkout_stage)
81 fprintf(stderr, "does not exist at stage %d",
82 checkout_stage);
83 else
84 fprintf(stderr, "is unmerged");
85 fputc('\n', stderr);
86 }
87 return -1;
33db5f4d
LT
88}
89
d7f6ea3d 90static int checkout_all(void)
33db5f4d 91{
4b12dae6 92 int i, errs = 0;
33db5f4d
LT
93
94 for (i = 0; i < active_nr ; i++) {
95 struct cache_entry *ce = active_cache[i];
3bd348ae 96 if (ce_stage(ce) != checkout_stage)
d9f98eeb 97 continue;
c3e9a653 98 if (prefix && *prefix &&
3bd348ae
JH
99 (ce_namelen(ce) <= prefix_length ||
100 memcmp(prefix, ce->name, prefix_length)))
c3e9a653 101 continue;
12dccc16 102 if (checkout_entry(ce, &state) < 0)
4b12dae6 103 errs++;
33db5f4d 104 }
4b12dae6
JH
105 if (errs)
106 /* we have already done our error reporting.
107 * exit with the same code as die().
108 */
109 exit(128);
33db5f4d
LT
110 return 0;
111}
112
4d1f1190 113static const char checkout_cache_usage[] =
3bd348ae 114"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";
d46ad9c9 115
31f584c2
JB
116static struct cache_file cache_file;
117
33db5f4d
LT
118int main(int argc, char **argv)
119{
a65a686f 120 int i;
415e96c8 121 int newfd = -1;
a65a686f 122 int all = 0;
9debe63d
SP
123 int read_from_stdin = 0;
124 int line_termination = '\n';
33db5f4d 125
c3e9a653 126 prefix = setup_git_directory();
5f73076c 127 git_config(git_default_config);
c3e9a653
JH
128 prefix_length = prefix ? strlen(prefix) : 0;
129
33db5f4d 130 if (read_cache() < 0) {
2de381f9 131 die("invalid cache");
33db5f4d
LT
132 }
133
134 for (i = 1; i < argc; i++) {
135 const char *arg = argv[i];
a65a686f
LT
136
137 if (!strcmp(arg, "--")) {
138 i++;
139 break;
140 }
141 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
142 all = 1;
143 continue;
33db5f4d 144 }
a65a686f
LT
145 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
146 state.force = 1;
147 continue;
33db5f4d 148 }
a65a686f
LT
149 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
150 state.quiet = 1;
151 continue;
415e96c8 152 }
a65a686f
LT
153 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
154 state.not_new = 1;
155 continue;
156 }
157 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
158 state.refresh_cache = 1;
159 if (newfd < 0)
160 newfd = hold_index_file_for_update
161 (&cache_file,
162 get_index_file());
163 if (newfd < 0)
164 die("cannot open index.lock file.");
165 continue;
415e96c8 166 }
9debe63d
SP
167 if (!strcmp(arg, "-z")) {
168 line_termination = 0;
169 continue;
170 }
171 if (!strcmp(arg, "--stdin")) {
172 if (i != argc - 1)
173 die("--stdin must be at the end");
174 read_from_stdin = 1;
175 i++; /* do not consider arg as a file name */
176 break;
177 }
3bd348ae 178 if (!strncmp(arg, "--prefix=", 9)) {
a65a686f
LT
179 state.base_dir = arg+9;
180 state.base_dir_len = strlen(state.base_dir);
181 continue;
182 }
3bd348ae
JH
183 if (!strncmp(arg, "--stage=", 8)) {
184 int ch = arg[8];
185 if ('1' <= ch && ch <= '3')
186 checkout_stage = arg[8] - '0';
187 else
188 die("stage should be between 1 and 3");
189 continue;
190 }
a65a686f
LT
191 if (arg[0] == '-')
192 usage(checkout_cache_usage);
193 break;
194 }
195
196 if (state.base_dir_len) {
197 /* when --prefix is specified we do not
198 * want to update cache.
199 */
200 if (state.refresh_cache) {
201 close(newfd); newfd = -1;
202 rollback_index_file(&cache_file);
203 }
204 state.refresh_cache = 0;
205 }
206
207 /* Check out named files first */
208 for ( ; i < argc; i++) {
209 const char *arg = argv[i];
210
211 if (all)
212 die("git-checkout-index: don't mix '--all' and explicit filenames");
9debe63d
SP
213 if (read_from_stdin)
214 die("git-checkout-index: don't mix '--stdin' and explicit filenames");
c3e9a653 215 checkout_file(prefix_path(prefix, prefix_length, arg));
33db5f4d 216 }
415e96c8 217
9debe63d
SP
218 if (read_from_stdin) {
219 struct strbuf buf;
220 if (all)
221 die("git-checkout-index: don't mix '--all' and '--stdin'");
222 strbuf_init(&buf);
223 while (1) {
224 char *path_name;
225 read_line(&buf, stdin, line_termination);
226 if (buf.eof)
227 break;
228 if (line_termination && buf.buf[0] == '"')
229 path_name = unquote_c_style(buf.buf, NULL);
230 else
231 path_name = buf.buf;
232 checkout_file(prefix_path(prefix, prefix_length, path_name));
233 if (path_name != buf.buf)
234 free(path_name);
235 }
236 }
237
a65a686f
LT
238 if (all)
239 checkout_all();
240
415e96c8
JH
241 if (0 <= newfd &&
242 (write_cache(newfd, active_cache, active_nr) ||
243 commit_index_file(&cache_file)))
244 die("Unable to write new cachefile");
33db5f4d
LT
245 return 0;
246}