git-apply: parse index information
[git/git.git] / update-index.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
ee1bec3d 7#include "strbuf.h"
e83c5163 8
121481ab
LT
9/*
10 * Default to not allowing changes to the list of files. The
11 * tool doesn't actually care, but this makes it harder to add
12 * files to the revision control by mistake by doing something
215a7ad1 13 * like "git-update-index *" and suddenly having all the object
121481ab
LT
14 * files be revision controlled.
15 */
5d1a5c02 16static int allow_add = 0, allow_remove = 0, allow_replace = 0, allow_unmerged = 0, not_new = 0, quiet = 0, info_only = 0;
9b63f501 17static int force_remove;
c6e007b0
JB
18
19/* Three functions to allow overloaded pointer return; see linux/err.h */
20static inline void *ERR_PTR(long error)
21{
22 return (void *) error;
23}
24
25static inline long PTR_ERR(const void *ptr)
26{
27 return (long) ptr;
28}
29
30static inline long IS_ERR(const void *ptr)
31{
32 return (unsigned long)ptr > (unsigned long)-1000L;
33}
121481ab 34
6b5ee137 35static int add_file_to_cache(const char *path)
e83c5163 36{
4c5abf42 37 int size, namelen, option, status;
e83c5163
LT
38 struct cache_entry *ce;
39 struct stat st;
e83c5163 40
4c5abf42
JH
41 status = lstat(path, &st);
42 if (status < 0 || S_ISDIR(st.st_mode)) {
43 /* When we used to have "path" and now we want to add
44 * "path/file", we need a way to remove "path" before
45 * being able to add "path/file". However,
215a7ad1 46 * "git-update-index --remove path" would not work.
4c5abf42
JH
47 * --force-remove can be used but this is more user
48 * friendly, especially since we can do the opposite
49 * case just fine without --force-remove.
50 */
51 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
34143b26
PB
52 if (allow_remove) {
53 if (remove_file_from_cache(path))
54 return error("%s: cannot remove from the index",
55 path);
56 else
57 return 0;
58 } else if (status < 0) {
59 return error("%s: does not exist and --remove not passed",
60 path);
61 }
121481ab 62 }
89bc8c78 63 if (0 == status)
34143b26
PB
64 return error("%s: is a directory - add files inside instead",
65 path);
89bc8c78
AW
66 else
67 return error("lstat(\"%s\"): %s", path,
68 strerror(errno));
e83c5163 69 }
e83c5163
LT
70 namelen = strlen(path);
71 size = cache_entry_size(namelen);
812666c8 72 ce = xmalloc(size);
e83c5163
LT
73 memset(ce, 0, size);
74 memcpy(ce->name, path, namelen);
711cf3a0 75 fill_stat_cache_info(ce, &st);
e4479470 76 ce->ce_mode = create_ce_mode(st.st_mode);
f5cabd13 77 ce->ce_flags = htons(namelen);
ec1fcc16
JH
78
79 if (index_path(ce->sha1, path, &st, !info_only))
80 return -1;
192268c1
JH
81 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
82 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
34143b26
PB
83 if (add_cache_entry(ce, option))
84 return error("%s: cannot add to the index - missing --add option?",
85 path);
86 return 0;
121481ab
LT
87}
88
711cf3a0
LT
89/*
90 * "refresh" does not calculate a new sha1 file or bring the
91 * cache up-to-date for mode/content changes. But what it
92 * _does_ do is to "re-match" the stat information of a file
93 * with the cache, so that you can refresh the cache for a
94 * file that hasn't been changed but where the stat entry is
95 * out of date.
96 *
667bb59b 97 * For example, you'd want to do this after doing a "git-read-tree",
711cf3a0
LT
98 * to link up the stat cache details with the proper files.
99 */
100static struct cache_entry *refresh_entry(struct cache_entry *ce)
101{
102 struct stat st;
103 struct cache_entry *updated;
104 int changed, size;
105
8ae0a8c5 106 if (lstat(ce->name, &st) < 0)
c6e007b0 107 return ERR_PTR(-errno);
711cf3a0 108
5d728c84 109 changed = ce_match_stat(ce, &st);
711cf3a0 110 if (!changed)
5d1a5c02 111 return NULL;
711cf3a0 112
b0391890 113 if (ce_modified(ce, &st))
c6e007b0 114 return ERR_PTR(-EINVAL);
711cf3a0
LT
115
116 size = ce_size(ce);
812666c8 117 updated = xmalloc(size);
711cf3a0
LT
118 memcpy(updated, ce, size);
119 fill_stat_cache_info(updated, &st);
120 return updated;
121481ab
LT
121}
122
90535218 123static int refresh_cache(void)
121481ab
LT
124{
125 int i;
90535218 126 int has_errors = 0;
121481ab 127
711cf3a0 128 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
129 struct cache_entry *ce, *new;
130 ce = active_cache[i];
131 if (ce_stage(ce)) {
1bc992ac
JH
132 while ((i < active_nr) &&
133 ! strcmp(active_cache[i]->name, ce->name))
134 i++;
135 i--;
5d1a5c02
LT
136 if (allow_unmerged)
137 continue;
138 printf("%s: needs merge\n", ce->name);
139 has_errors = 1;
1bc992ac
JH
140 continue;
141 }
711cf3a0 142
1bc992ac 143 new = refresh_entry(ce);
5d1a5c02
LT
144 if (!new)
145 continue;
c6e007b0 146 if (IS_ERR(new)) {
0ed3715f
LT
147 if (not_new && PTR_ERR(new) == -ENOENT)
148 continue;
149 if (quiet)
150 continue;
151 printf("%s: needs update\n", ce->name);
152 has_errors = 1;
711cf3a0
LT
153 continue;
154 }
ee267527 155 active_cache_changed = 1;
62d046a0
PB
156 /* You can NOT just free active_cache[i] here, since it
157 * might not be necessarily malloc()ed but can also come
158 * from mmap(). */
711cf3a0
LT
159 active_cache[i] = new;
160 }
90535218 161 return has_errors;
e83c5163
LT
162}
163
e83c5163
LT
164/*
165 * We fundamentally don't like some paths: we don't want
320d3a1b
LT
166 * dot or dot-dot anywhere, and for obvious reasons don't
167 * want to recurse into ".git" either.
e83c5163
LT
168 *
169 * Also, we don't want double slashes or slashes at the
aebb2679 170 * end that can make pathnames ambiguous.
e83c5163 171 */
320d3a1b
LT
172static int verify_dotfile(const char *rest)
173{
174 /*
175 * The first character was '.', but that
176 * has already been discarded, we now test
177 * the rest.
178 */
179 switch (*rest) {
180 /* "." is not allowed */
181 case '\0': case '/':
182 return 0;
183
184 /*
185 * ".git" followed by NUL or slash is bad. This
186 * shares the path end test with the ".." case.
187 */
188 case 'g':
189 if (rest[1] != 'i')
190 break;
191 if (rest[2] != 't')
192 break;
193 rest += 2;
194 /* fallthrough */
195 case '.':
196 if (rest[1] == '\0' || rest[1] == '/')
197 return 0;
198 }
199 return 1;
200}
201
6b5ee137 202static int verify_path(const char *path)
e83c5163
LT
203{
204 char c;
205
206 goto inside;
207 for (;;) {
208 if (!c)
209 return 1;
210 if (c == '/') {
211inside:
212 c = *path++;
320d3a1b
LT
213 switch (c) {
214 default:
e83c5163 215 continue;
320d3a1b
LT
216 case '/': case '\0':
217 break;
218 case '.':
219 if (verify_dotfile(path))
220 continue;
221 }
e83c5163
LT
222 return 0;
223 }
224 c = *path++;
225 }
226}
227
6b5ee137 228static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
9945d980 229{
192268c1 230 int size, len, option;
9945d980
LT
231 unsigned int mode;
232 unsigned char sha1[20];
233 struct cache_entry *ce;
234
235 if (sscanf(arg1, "%o", &mode) != 1)
236 return -1;
9945d980
LT
237 if (get_sha1_hex(arg2, sha1))
238 return -1;
9945d980
LT
239 if (!verify_path(arg3))
240 return -1;
9945d980
LT
241
242 len = strlen(arg3);
243 size = cache_entry_size(len);
812666c8 244 ce = xmalloc(size);
9945d980
LT
245 memset(ce, 0, size);
246
247 memcpy(ce->sha1, sha1, 20);
248 memcpy(ce->name, arg3, len);
f5cabd13 249 ce->ce_flags = htons(len);
e4479470 250 ce->ce_mode = create_ce_mode(mode);
192268c1
JH
251 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
252 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
253 return add_cache_entry(ce, option);
9945d980
LT
254}
255
e99d59ff 256static struct cache_file cache_file;
f2a19340 257
ee1bec3d
JH
258
259static void update_one(const char *path, const char *prefix, int prefix_length)
260{
261 const char *p = prefix_path(prefix, prefix_length, path);
262 if (!verify_path(p)) {
263 fprintf(stderr, "Ignoring path %s\n", path);
264 return;
265 }
266 if (force_remove) {
267 if (remove_file_from_cache(p))
268 die("git-update-index: unable to remove %s", path);
269 return;
270 }
271 if (add_file_to_cache(p))
272 die("Unable to process file %s", path);
273}
274
6b5ee137 275int main(int argc, const char **argv)
e83c5163 276{
ee1bec3d 277 int i, newfd, entries, has_errors = 0, line_termination = '\n';
121481ab 278 int allow_options = 1;
ee1bec3d 279 int read_from_stdin = 0;
cfb0af1d 280 const char *prefix = setup_git_directory();
ee1bec3d 281 int prefix_length = prefix ? strlen(prefix) : 0;
bb233d69 282
415e96c8 283 newfd = hold_index_file_for_update(&cache_file, get_index_file());
9614b8dc 284 if (newfd < 0)
2de381f9 285 die("unable to create new cachefile");
9614b8dc 286
e83c5163 287 entries = read_cache();
9614b8dc 288 if (entries < 0)
2de381f9 289 die("cache corrupted");
e83c5163 290
e83c5163 291 for (i = 1 ; i < argc; i++) {
6b5ee137 292 const char *path = argv[i];
121481ab
LT
293
294 if (allow_options && *path == '-') {
295 if (!strcmp(path, "--")) {
296 allow_options = 0;
297 continue;
298 }
0ed3715f
LT
299 if (!strcmp(path, "-q")) {
300 quiet = 1;
301 continue;
302 }
121481ab
LT
303 if (!strcmp(path, "--add")) {
304 allow_add = 1;
305 continue;
306 }
192268c1
JH
307 if (!strcmp(path, "--replace")) {
308 allow_replace = 1;
309 continue;
310 }
121481ab
LT
311 if (!strcmp(path, "--remove")) {
312 allow_remove = 1;
313 continue;
314 }
5d1a5c02
LT
315 if (!strcmp(path, "--unmerged")) {
316 allow_unmerged = 1;
317 continue;
318 }
121481ab 319 if (!strcmp(path, "--refresh")) {
90535218 320 has_errors |= refresh_cache();
121481ab
LT
321 continue;
322 }
9945d980 323 if (!strcmp(path, "--cacheinfo")) {
b3f94c4b 324 if (i+3 >= argc)
215a7ad1 325 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
b3f94c4b 326 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
215a7ad1 327 die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
9945d980
LT
328 i += 3;
329 continue;
330 }
df6e1516
BL
331 if (!strcmp(path, "--info-only")) {
332 info_only = 1;
333 continue;
334 }
0ff5bf7c 335 if (!strcmp(path, "--force-remove")) {
9b63f501 336 force_remove = 1;
0ff5bf7c
JH
337 continue;
338 }
ee1bec3d
JH
339 if (!strcmp(path, "-z")) {
340 line_termination = 0;
341 continue;
342 }
343 if (!strcmp(path, "--stdin")) {
344 if (i != argc - 1)
345 die("--stdin must be at the end");
346 read_from_stdin = 1;
347 break;
348 }
c6e007b0
JB
349 if (!strcmp(path, "--ignore-missing")) {
350 not_new = 1;
351 continue;
352 }
2de381f9 353 die("unknown option %s", path);
121481ab 354 }
ee1bec3d
JH
355 update_one(path, prefix, prefix_length);
356 }
357 if (read_from_stdin) {
358 struct strbuf buf;
359 strbuf_init(&buf);
360 while (1) {
361 read_line(&buf, stdin, line_termination);
362 if (buf.eof)
363 break;
364 update_one(buf.buf, prefix, prefix_length);
9b63f501 365 }
e83c5163 366 }
5cd5ace7
LT
367 if (active_cache_changed) {
368 if (write_cache(newfd, active_cache, active_nr) ||
369 commit_index_file(&cache_file))
370 die("Unable to write new cachefile");
371 }
9614b8dc 372
c4b83e61 373 return has_errors ? 1 : 0;
e83c5163 374}