fsck-objects: work from subdirectory.
[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
12dccc16
LT
37static struct checkout state = {
38 .base_dir = "",
39 .base_dir_len = 0,
40 .force = 0,
41 .quiet = 0,
42 .not_new = 0,
43 .refresh_cache = 0,
44};
33db5f4d 45
d7f6ea3d 46static int checkout_file(const char *name)
33db5f4d
LT
47{
48 int pos = cache_name_pos(name, strlen(name));
49 if (pos < 0) {
12dccc16 50 if (!state.quiet) {
a38800fd
JH
51 pos = -pos - 1;
52 fprintf(stderr,
215a7ad1 53 "git-checkout-index: %s is %s.\n",
a38800fd
JH
54 name,
55 (pos < active_nr &&
56 !strcmp(active_cache[pos]->name, name)) ?
57 "unmerged" : "not in the cache");
58 }
33db5f4d
LT
59 return -1;
60 }
12dccc16 61 return checkout_entry(active_cache[pos], &state);
33db5f4d
LT
62}
63
d7f6ea3d 64static int checkout_all(void)
33db5f4d 65{
4b12dae6 66 int i, errs = 0;
33db5f4d
LT
67
68 for (i = 0; i < active_nr ; i++) {
69 struct cache_entry *ce = active_cache[i];
d9f98eeb
LT
70 if (ce_stage(ce))
71 continue;
12dccc16 72 if (checkout_entry(ce, &state) < 0)
4b12dae6 73 errs++;
33db5f4d 74 }
4b12dae6
JH
75 if (errs)
76 /* we have already done our error reporting.
77 * exit with the same code as die().
78 */
79 exit(128);
33db5f4d
LT
80 return 0;
81}
82
4d1f1190 83static const char checkout_cache_usage[] =
215a7ad1 84"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--prefix=<string>] [--] <file>...";
d46ad9c9 85
31f584c2
JB
86static struct cache_file cache_file;
87
33db5f4d
LT
88int main(int argc, char **argv)
89{
a65a686f 90 int i;
415e96c8 91 int newfd = -1;
a65a686f 92 int all = 0;
33db5f4d
LT
93
94 if (read_cache() < 0) {
2de381f9 95 die("invalid cache");
33db5f4d
LT
96 }
97
98 for (i = 1; i < argc; i++) {
99 const char *arg = argv[i];
a65a686f
LT
100
101 if (!strcmp(arg, "--")) {
102 i++;
103 break;
104 }
105 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
106 all = 1;
107 continue;
33db5f4d 108 }
a65a686f
LT
109 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
110 state.force = 1;
111 continue;
33db5f4d 112 }
a65a686f
LT
113 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
114 state.quiet = 1;
115 continue;
415e96c8 116 }
a65a686f
LT
117 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
118 state.not_new = 1;
119 continue;
120 }
121 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
122 state.refresh_cache = 1;
123 if (newfd < 0)
124 newfd = hold_index_file_for_update
125 (&cache_file,
126 get_index_file());
127 if (newfd < 0)
128 die("cannot open index.lock file.");
129 continue;
415e96c8 130 }
a65a686f
LT
131 if (!memcmp(arg, "--prefix=", 9)) {
132 state.base_dir = arg+9;
133 state.base_dir_len = strlen(state.base_dir);
134 continue;
135 }
136 if (arg[0] == '-')
137 usage(checkout_cache_usage);
138 break;
139 }
140
141 if (state.base_dir_len) {
142 /* when --prefix is specified we do not
143 * want to update cache.
144 */
145 if (state.refresh_cache) {
146 close(newfd); newfd = -1;
147 rollback_index_file(&cache_file);
148 }
149 state.refresh_cache = 0;
150 }
151
152 /* Check out named files first */
153 for ( ; i < argc; i++) {
154 const char *arg = argv[i];
155
156 if (all)
157 die("git-checkout-index: don't mix '--all' and explicit filenames");
d7f6ea3d 158 checkout_file(arg);
33db5f4d 159 }
415e96c8 160
a65a686f
LT
161 if (all)
162 checkout_all();
163
415e96c8
JH
164 if (0 <= newfd &&
165 (write_cache(newfd, active_cache, active_nr) ||
166 commit_index_file(&cache_file)))
167 die("Unable to write new cachefile");
33db5f4d
LT
168 return 0;
169}