Code

Merge branch 'jc/pack-objects'
[git.git] / builtin-checkout-index.c
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  *
8  *      git-checkout-index -a -f file.c
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  *
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".
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  *
23  *      find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
24  *
25  * or:
26  *
27  *      find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
28  *
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"
40 #include "strbuf.h"
41 #include "quote.h"
42 #include "cache-tree.h"
44 #define CHECKOUT_ALL 4
45 static int line_termination = '\n';
46 static int checkout_stage; /* default to checkout stage0 */
47 static int to_tempfile;
48 static char topath[4][MAXPATHLEN+1];
50 static struct checkout state;
52 static void write_tempfile_record(const char *name, int prefix_length)
53 {
54         int i;
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);
68         putchar('\t');
69         write_name_quoted("", 0, name + prefix_length,
70                 line_termination, stdout);
71         putchar(line_termination);
73         for (i = 0; i < 4; i++) {
74                 topath[i][0] = 0;
75         }
76 }
78 static int checkout_file(const char *name, int prefix_length)
79 {
80         int namelen = strlen(name);
81         int pos = cache_name_pos(name, namelen);
82         int has_same_name = 0;
83         int did_checkout = 0;
84         int errs = 0;
86         if (pos < 0)
87                 pos = -pos - 1;
89         while (pos < active_nr) {
90                 struct cache_entry *ce = active_cache[pos];
91                 if (ce_namelen(ce) != namelen ||
92                     memcmp(ce->name, name, namelen))
93                         break;
94                 has_same_name = 1;
95                 pos++;
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         }
105         if (did_checkout) {
106                 if (to_tempfile)
107                         write_tempfile_record(name, prefix_length);
108                 return errs > 0 ? -1 : 0;
109         }
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;
125 static int checkout_all(const char *prefix, int prefix_length)
127         int i, errs = 0;
128         struct cache_entry* last_ce = NULL;
130         for (i = 0; i < active_nr ; i++) {
131                 struct cache_entry *ce = active_cache[i];
132                 if (ce_stage(ce) != checkout_stage
133                     && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
134                         continue;
135                 if (prefix && *prefix &&
136                     (ce_namelen(ce) <= prefix_length ||
137                      memcmp(prefix, ce->name, prefix_length)))
138                         continue;
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)))
142                                 write_tempfile_record(last_ce->name, prefix_length);
143                 }
144                 if (checkout_entry(ce, &state,
145                     to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
146                         errs++;
147                 last_ce = ce;
148         }
149         if (last_ce && to_tempfile)
150                 write_tempfile_record(last_ce->name, prefix_length);
151         if (errs)
152                 /* we have already done our error reporting.
153                  * exit with the same code as die().
154                  */
155                 exit(128);
156         return 0;
159 static const char checkout_cache_usage[] =
160 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
162 static struct lock_file lock_file;
164 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
166         int i;
167         int newfd = -1;
168         int all = 0;
169         int read_from_stdin = 0;
170         int prefix_length;
172         git_config(git_default_config);
173         state.base_dir = "";
174         prefix_length = prefix ? strlen(prefix) : 0;
176         if (read_cache() < 0) {
177                 die("invalid cache");
178         }
180         for (i = 1; i < argc; i++) {
181                 const char *arg = argv[i];
183                 if (!strcmp(arg, "--")) {
184                         i++;
185                         break;
186                 }
187                 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
188                         all = 1;
189                         continue;
190                 }
191                 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
192                         state.force = 1;
193                         continue;
194                 }
195                 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
196                         state.quiet = 1;
197                         continue;
198                 }
199                 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
200                         state.not_new = 1;
201                         continue;
202                 }
203                 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
204                         state.refresh_cache = 1;
205                         if (newfd < 0)
206                                 newfd = hold_lock_file_for_update
207                                         (&lock_file, get_index_file(), 1);
208                         if (newfd < 0)
209                                 die("cannot open index.lock file.");
210                         continue;
211                 }
212                 if (!strcmp(arg, "-z")) {
213                         line_termination = 0;
214                         continue;
215                 }
216                 if (!strcmp(arg, "--stdin")) {
217                         if (i != argc - 1)
218                                 die("--stdin must be at the end");
219                         read_from_stdin = 1;
220                         i++; /* do not consider arg as a file name */
221                         break;
222                 }
223                 if (!strcmp(arg, "--temp")) {
224                         to_tempfile = 1;
225                         continue;
226                 }
227                 if (!strncmp(arg, "--prefix=", 9)) {
228                         state.base_dir = arg+9;
229                         state.base_dir_len = strlen(state.base_dir);
230                         continue;
231                 }
232                 if (!strncmp(arg, "--stage=", 8)) {
233                         if (!strcmp(arg + 8, "all")) {
234                                 to_tempfile = 1;
235                                 checkout_stage = CHECKOUT_ALL;
236                         } else {
237                                 int ch = arg[8];
238                                 if ('1' <= ch && ch <= '3')
239                                         checkout_stage = arg[8] - '0';
240                                 else
241                                         die("stage should be between 1 and 3 or all");
242                         }
243                         continue;
244                 }
245                 if (arg[0] == '-')
246                         usage(checkout_cache_usage);
247                 break;
248         }
250         if (state.base_dir_len || to_tempfile) {
251                 /* when --prefix is specified we do not
252                  * want to update cache.
253                  */
254                 if (state.refresh_cache) {
255                         close(newfd); newfd = -1;
256                         rollback_lock_file(&lock_file);
257                 }
258                 state.refresh_cache = 0;
259         }
261         /* Check out named files first */
262         for ( ; i < argc; i++) {
263                 const char *arg = argv[i];
264                 const char *p;
266                 if (all)
267                         die("git-checkout-index: don't mix '--all' and explicit filenames");
268                 if (read_from_stdin)
269                         die("git-checkout-index: don't mix '--stdin' and explicit filenames");
270                 p = prefix_path(prefix, prefix_length, arg);
271                 checkout_file(p, prefix_length);
272                 if (p < arg || p > arg + strlen(arg))
273                         free((char*)p);
274         }
276         if (read_from_stdin) {
277                 struct strbuf buf;
278                 if (all)
279                         die("git-checkout-index: don't mix '--all' and '--stdin'");
280                 strbuf_init(&buf);
281                 while (1) {
282                         char *path_name;
283                         const char *p;
285                         read_line(&buf, stdin, line_termination);
286                         if (buf.eof)
287                                 break;
288                         if (line_termination && buf.buf[0] == '"')
289                                 path_name = unquote_c_style(buf.buf, NULL);
290                         else
291                                 path_name = buf.buf;
292                         p = prefix_path(prefix, prefix_length, path_name);
293                         checkout_file(p, prefix_length);
294                         if (p < path_name || p > path_name + strlen(path_name))
295                                 free((char *)p);
296                         if (path_name != buf.buf)
297                                 free(path_name);
298                 }
299         }
301         if (all)
302                 checkout_all(prefix, prefix_length);
304         if (0 <= newfd &&
305             (write_cache(newfd, active_cache, active_nr) ||
306              close(newfd) || commit_lock_file(&lock_file)))
307                 die("Unable to write new index file");
308         return 0;