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