Merge branch 'jc/diff'
[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"
973d6a20 8#include "quote.h"
e83c5163 9
121481ab
LT
10/*
11 * Default to not allowing changes to the list of files. The
12 * tool doesn't actually care, but this makes it harder to add
13 * files to the revision control by mistake by doing something
215a7ad1 14 * like "git-update-index *" and suddenly having all the object
121481ab
LT
15 * files be revision controlled.
16 */
caf4f582
JH
17static int allow_add;
18static int allow_remove;
19static int allow_replace;
20static int allow_unmerged; /* --refresh needing merge is not error */
21static int not_new; /* --refresh not having working tree files is not error */
22static int quiet; /* --refresh needing update is not error */
23static int info_only;
9b63f501 24static int force_remove;
caf4f582 25static int verbose;
5f73076c
JH
26static int mark_valid_only = 0;
27#define MARK_VALID 1
28#define UNMARK_VALID 2
29
c6e007b0
JB
30
31/* Three functions to allow overloaded pointer return; see linux/err.h */
32static inline void *ERR_PTR(long error)
33{
34 return (void *) error;
35}
36
37static inline long PTR_ERR(const void *ptr)
38{
39 return (long) ptr;
40}
41
42static inline long IS_ERR(const void *ptr)
43{
44 return (unsigned long)ptr > (unsigned long)-1000L;
45}
121481ab 46
caf4f582
JH
47static void report(const char *fmt, ...)
48{
49 va_list vp;
50
51 if (!verbose)
52 return;
53
54 va_start(vp, fmt);
55 vprintf(fmt, vp);
56 putchar('\n');
57 va_end(vp);
58}
59
5f73076c
JH
60static int mark_valid(const char *path)
61{
62 int namelen = strlen(path);
63 int pos = cache_name_pos(path, namelen);
64 if (0 <= pos) {
65 switch (mark_valid_only) {
66 case MARK_VALID:
67 active_cache[pos]->ce_flags |= htons(CE_VALID);
68 break;
69 case UNMARK_VALID:
70 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
71 break;
72 }
73 active_cache_changed = 1;
74 return 0;
75 }
76 return -1;
77}
78
6b5ee137 79static int add_file_to_cache(const char *path)
e83c5163 80{
4c5abf42 81 int size, namelen, option, status;
e83c5163
LT
82 struct cache_entry *ce;
83 struct stat st;
e83c5163 84
4c5abf42
JH
85 status = lstat(path, &st);
86 if (status < 0 || S_ISDIR(st.st_mode)) {
87 /* When we used to have "path" and now we want to add
88 * "path/file", we need a way to remove "path" before
89 * being able to add "path/file". However,
215a7ad1 90 * "git-update-index --remove path" would not work.
4c5abf42
JH
91 * --force-remove can be used but this is more user
92 * friendly, especially since we can do the opposite
93 * case just fine without --force-remove.
94 */
95 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
34143b26
PB
96 if (allow_remove) {
97 if (remove_file_from_cache(path))
98 return error("%s: cannot remove from the index",
99 path);
100 else
101 return 0;
102 } else if (status < 0) {
103 return error("%s: does not exist and --remove not passed",
104 path);
105 }
121481ab 106 }
89bc8c78 107 if (0 == status)
34143b26
PB
108 return error("%s: is a directory - add files inside instead",
109 path);
89bc8c78
AW
110 else
111 return error("lstat(\"%s\"): %s", path,
112 strerror(errno));
e83c5163 113 }
3e09cdfd 114
e83c5163
LT
115 namelen = strlen(path);
116 size = cache_entry_size(namelen);
90321c10 117 ce = xcalloc(1, size);
e83c5163 118 memcpy(ce->name, path, namelen);
5f73076c 119 ce->ce_flags = htons(namelen);
711cf3a0 120 fill_stat_cache_info(ce, &st);
3e09cdfd 121
e4479470 122 ce->ce_mode = create_ce_mode(st.st_mode);
3e09cdfd
JH
123 if (!trust_executable_bit) {
124 /* If there is an existing entry, pick the mode bits
125 * from it.
126 */
127 int pos = cache_name_pos(path, namelen);
128 if (0 <= pos)
129 ce->ce_mode = active_cache[pos]->ce_mode;
130 }
ec1fcc16
JH
131
132 if (index_path(ce->sha1, path, &st, !info_only))
133 return -1;
192268c1
JH
134 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
135 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
34143b26
PB
136 if (add_cache_entry(ce, option))
137 return error("%s: cannot add to the index - missing --add option?",
138 path);
139 return 0;
121481ab
LT
140}
141
711cf3a0
LT
142/*
143 * "refresh" does not calculate a new sha1 file or bring the
144 * cache up-to-date for mode/content changes. But what it
145 * _does_ do is to "re-match" the stat information of a file
146 * with the cache, so that you can refresh the cache for a
147 * file that hasn't been changed but where the stat entry is
148 * out of date.
149 *
667bb59b 150 * For example, you'd want to do this after doing a "git-read-tree",
711cf3a0
LT
151 * to link up the stat cache details with the proper files.
152 */
5f73076c 153static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
711cf3a0
LT
154{
155 struct stat st;
156 struct cache_entry *updated;
157 int changed, size;
158
8ae0a8c5 159 if (lstat(ce->name, &st) < 0)
c6e007b0 160 return ERR_PTR(-errno);
711cf3a0 161
5f73076c 162 changed = ce_match_stat(ce, &st, really);
b92b2ce9
JH
163 if (!changed) {
164 if (really && assume_unchanged &&
165 !(ce->ce_flags & htons(CE_VALID)))
166 ; /* mark this one VALID again */
167 else
168 return NULL;
169 }
711cf3a0 170
5f73076c 171 if (ce_modified(ce, &st, really))
c6e007b0 172 return ERR_PTR(-EINVAL);
711cf3a0
LT
173
174 size = ce_size(ce);
812666c8 175 updated = xmalloc(size);
711cf3a0
LT
176 memcpy(updated, ce, size);
177 fill_stat_cache_info(updated, &st);
5f73076c 178
8b9b0f3a
JH
179 /* In this case, if really is not set, we should leave
180 * CE_VALID bit alone. Otherwise, paths marked with
181 * --no-assume-unchanged (i.e. things to be edited) will
182 * reacquire CE_VALID bit automatically, which is not
183 * really what we want.
184 */
185 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
186 updated->ce_flags &= ~htons(CE_VALID);
187
711cf3a0 188 return updated;
121481ab
LT
189}
190
5f73076c 191static int refresh_cache(int really)
121481ab
LT
192{
193 int i;
90535218 194 int has_errors = 0;
121481ab 195
711cf3a0 196 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
197 struct cache_entry *ce, *new;
198 ce = active_cache[i];
199 if (ce_stage(ce)) {
1bc992ac
JH
200 while ((i < active_nr) &&
201 ! strcmp(active_cache[i]->name, ce->name))
202 i++;
203 i--;
5d1a5c02
LT
204 if (allow_unmerged)
205 continue;
206 printf("%s: needs merge\n", ce->name);
207 has_errors = 1;
1bc992ac
JH
208 continue;
209 }
711cf3a0 210
5f73076c 211 new = refresh_entry(ce, really);
5d1a5c02
LT
212 if (!new)
213 continue;
c6e007b0 214 if (IS_ERR(new)) {
0ed3715f
LT
215 if (not_new && PTR_ERR(new) == -ENOENT)
216 continue;
5f73076c
JH
217 if (really && PTR_ERR(new) == -EINVAL) {
218 /* If we are doing --really-refresh that
219 * means the index is not valid anymore.
220 */
221 ce->ce_flags &= ~htons(CE_VALID);
222 active_cache_changed = 1;
223 }
0ed3715f
LT
224 if (quiet)
225 continue;
226 printf("%s: needs update\n", ce->name);
227 has_errors = 1;
711cf3a0
LT
228 continue;
229 }
ee267527 230 active_cache_changed = 1;
62d046a0
PB
231 /* You can NOT just free active_cache[i] here, since it
232 * might not be necessarily malloc()ed but can also come
233 * from mmap(). */
711cf3a0
LT
234 active_cache[i] = new;
235 }
90535218 236 return has_errors;
e83c5163
LT
237}
238
e83c5163
LT
239/*
240 * We fundamentally don't like some paths: we don't want
320d3a1b
LT
241 * dot or dot-dot anywhere, and for obvious reasons don't
242 * want to recurse into ".git" either.
e83c5163
LT
243 *
244 * Also, we don't want double slashes or slashes at the
aebb2679 245 * end that can make pathnames ambiguous.
e83c5163 246 */
320d3a1b
LT
247static int verify_dotfile(const char *rest)
248{
249 /*
250 * The first character was '.', but that
251 * has already been discarded, we now test
252 * the rest.
253 */
254 switch (*rest) {
255 /* "." is not allowed */
256 case '\0': case '/':
257 return 0;
258
259 /*
260 * ".git" followed by NUL or slash is bad. This
261 * shares the path end test with the ".." case.
262 */
263 case 'g':
264 if (rest[1] != 'i')
265 break;
266 if (rest[2] != 't')
267 break;
268 rest += 2;
269 /* fallthrough */
270 case '.':
271 if (rest[1] == '\0' || rest[1] == '/')
272 return 0;
273 }
274 return 1;
275}
276
6b5ee137 277static int verify_path(const char *path)
e83c5163
LT
278{
279 char c;
280
281 goto inside;
282 for (;;) {
283 if (!c)
284 return 1;
285 if (c == '/') {
286inside:
287 c = *path++;
320d3a1b
LT
288 switch (c) {
289 default:
e83c5163 290 continue;
320d3a1b
LT
291 case '/': case '\0':
292 break;
293 case '.':
294 if (verify_dotfile(path))
295 continue;
296 }
e83c5163
LT
297 return 0;
298 }
299 c = *path++;
300 }
301}
302
d23748a6
JH
303static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
304 const char *path, int stage)
9945d980 305{
192268c1 306 int size, len, option;
9945d980
LT
307 struct cache_entry *ce;
308
d23748a6 309 if (!verify_path(path))
9945d980 310 return -1;
9945d980 311
d23748a6 312 len = strlen(path);
9945d980 313 size = cache_entry_size(len);
90321c10 314 ce = xcalloc(1, size);
9945d980
LT
315
316 memcpy(ce->sha1, sha1, 20);
d23748a6
JH
317 memcpy(ce->name, path, len);
318 ce->ce_flags = create_ce_flags(len, stage);
e4479470 319 ce->ce_mode = create_ce_mode(mode);
5f73076c
JH
320 if (assume_unchanged)
321 ce->ce_flags |= htons(CE_VALID);
192268c1
JH
322 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
323 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
caf4f582
JH
324 if (add_cache_entry(ce, option))
325 return error("%s: cannot add to the index - missing --add option?",
d23748a6
JH
326 path);
327 report("add '%s'", path);
caf4f582 328 return 0;
9945d980
LT
329}
330
3e09cdfd
JH
331static int chmod_path(int flip, const char *path)
332{
333 int pos;
334 struct cache_entry *ce;
335 unsigned int mode;
f2a19340 336
3e09cdfd
JH
337 pos = cache_name_pos(path, strlen(path));
338 if (pos < 0)
339 return -1;
340 ce = active_cache[pos];
341 mode = ntohl(ce->ce_mode);
342 if (!S_ISREG(mode))
343 return -1;
344 switch (flip) {
345 case '+':
346 ce->ce_mode |= htonl(0111); break;
347 case '-':
348 ce->ce_mode &= htonl(~0111); break;
349 default:
350 return -1;
351 }
352 active_cache_changed = 1;
353 return 0;
354}
355
356static struct cache_file cache_file;
ee1bec3d
JH
357
358static void update_one(const char *path, const char *prefix, int prefix_length)
359{
360 const char *p = prefix_path(prefix, prefix_length, path);
361 if (!verify_path(p)) {
362 fprintf(stderr, "Ignoring path %s\n", path);
363 return;
364 }
5f73076c
JH
365 if (mark_valid_only) {
366 if (mark_valid(p))
367 die("Unable to mark file %s", path);
368 return;
369 }
370
ee1bec3d
JH
371 if (force_remove) {
372 if (remove_file_from_cache(p))
373 die("git-update-index: unable to remove %s", path);
caf4f582 374 report("remove '%s'", path);
ee1bec3d
JH
375 return;
376 }
377 if (add_file_to_cache(p))
378 die("Unable to process file %s", path);
caf4f582 379 report("add '%s'", path);
ee1bec3d
JH
380}
381
d4dbf36d
JH
382static void read_index_info(int line_termination)
383{
384 struct strbuf buf;
385 strbuf_init(&buf);
386 while (1) {
9c20a470 387 char *ptr, *tab;
973d6a20 388 char *path_name;
d4dbf36d
JH
389 unsigned char sha1[20];
390 unsigned int mode;
d23748a6
JH
391 int stage;
392
393 /* This reads lines formatted in one of three formats:
394 *
395 * (1) mode SP sha1 TAB path
396 * The first format is what "git-apply --index-info"
397 * reports, and used to reconstruct a partial tree
398 * that is used for phony merge base tree when falling
399 * back on 3-way merge.
400 *
401 * (2) mode SP type SP sha1 TAB path
402 * The second format is to stuff git-ls-tree output
403 * into the index file.
404 *
405 * (3) mode SP sha1 SP stage TAB path
406 * This format is to put higher order stages into the
407 * index file and matches git-ls-files --stage output.
408 */
d4dbf36d
JH
409 read_line(&buf, stdin, line_termination);
410 if (buf.eof)
411 break;
412
413 mode = strtoul(buf.buf, &ptr, 8);
9c20a470 414 if (ptr == buf.buf || *ptr != ' ')
d4dbf36d
JH
415 goto bad_line;
416
9c20a470
JH
417 tab = strchr(ptr, '\t');
418 if (!tab || tab - ptr < 41)
419 goto bad_line;
d23748a6 420
2d497115 421 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
d23748a6
JH
422 stage = tab[-1] - '0';
423 ptr = tab + 1; /* point at the head of path */
424 tab = tab - 2; /* point at tail of sha1 */
425 }
426 else {
427 stage = 0;
428 ptr = tab + 1; /* point at the head of path */
429 }
430
9c20a470
JH
431 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
432 goto bad_line;
973d6a20
JH
433
434 if (line_termination && ptr[0] == '"')
435 path_name = unquote_c_style(ptr, NULL);
436 else
437 path_name = ptr;
438
439 if (!verify_path(path_name)) {
440 fprintf(stderr, "Ignoring path %s\n", path_name);
441 if (path_name != ptr)
442 free(path_name);
d4dbf36d
JH
443 continue;
444 }
445
446 if (!mode) {
447 /* mode == 0 means there is no such path -- remove */
973d6a20 448 if (remove_file_from_cache(path_name))
d4dbf36d
JH
449 die("git-update-index: unable to remove %s",
450 ptr);
451 }
452 else {
453 /* mode ' ' sha1 '\t' name
454 * ptr[-1] points at tab,
455 * ptr[-41] is at the beginning of sha1
456 */
457 ptr[-42] = ptr[-1] = 0;
d23748a6 458 if (add_cacheinfo(mode, sha1, path_name, stage))
d4dbf36d 459 die("git-update-index: unable to update %s",
973d6a20 460 path_name);
d4dbf36d 461 }
973d6a20
JH
462 if (path_name != ptr)
463 free(path_name);
d4dbf36d
JH
464 continue;
465
466 bad_line:
467 die("malformed index info %s", buf.buf);
468 }
469}
470
f6ab5bb2 471static const char update_index_usage[] =
14470c0d 472"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
f6ab5bb2 473
6b5ee137 474int main(int argc, const char **argv)
e83c5163 475{
ee1bec3d 476 int i, newfd, entries, has_errors = 0, line_termination = '\n';
121481ab 477 int allow_options = 1;
ee1bec3d 478 int read_from_stdin = 0;
cfb0af1d 479 const char *prefix = setup_git_directory();
ee1bec3d 480 int prefix_length = prefix ? strlen(prefix) : 0;
bb233d69 481
3e09cdfd
JH
482 git_config(git_default_config);
483
415e96c8 484 newfd = hold_index_file_for_update(&cache_file, get_index_file());
9614b8dc 485 if (newfd < 0)
2de381f9 486 die("unable to create new cachefile");
9614b8dc 487
e83c5163 488 entries = read_cache();
9614b8dc 489 if (entries < 0)
2de381f9 490 die("cache corrupted");
e83c5163 491
e83c5163 492 for (i = 1 ; i < argc; i++) {
6b5ee137 493 const char *path = argv[i];
121481ab
LT
494
495 if (allow_options && *path == '-') {
496 if (!strcmp(path, "--")) {
497 allow_options = 0;
498 continue;
499 }
0ed3715f
LT
500 if (!strcmp(path, "-q")) {
501 quiet = 1;
502 continue;
503 }
121481ab
LT
504 if (!strcmp(path, "--add")) {
505 allow_add = 1;
506 continue;
507 }
192268c1
JH
508 if (!strcmp(path, "--replace")) {
509 allow_replace = 1;
510 continue;
511 }
121481ab
LT
512 if (!strcmp(path, "--remove")) {
513 allow_remove = 1;
514 continue;
515 }
5d1a5c02
LT
516 if (!strcmp(path, "--unmerged")) {
517 allow_unmerged = 1;
518 continue;
519 }
121481ab 520 if (!strcmp(path, "--refresh")) {
5f73076c
JH
521 has_errors |= refresh_cache(0);
522 continue;
523 }
524 if (!strcmp(path, "--really-refresh")) {
525 has_errors |= refresh_cache(1);
121481ab
LT
526 continue;
527 }
9945d980 528 if (!strcmp(path, "--cacheinfo")) {
d23748a6
JH
529 unsigned char sha1[20];
530 unsigned int mode;
531
b3f94c4b 532 if (i+3 >= argc)
215a7ad1 533 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
d23748a6
JH
534
535 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
536 get_sha1_hex(argv[i+2], sha1) ||
537 add_cacheinfo(mode, sha1, argv[i+3], 0))
538 die("git-update-index: --cacheinfo"
539 " cannot add %s", argv[i+3]);
9945d980
LT
540 i += 3;
541 continue;
542 }
3e09cdfd
JH
543 if (!strcmp(path, "--chmod=-x") ||
544 !strcmp(path, "--chmod=+x")) {
545 if (argc <= i+1)
546 die("git-update-index: %s <path>", path);
547 if (chmod_path(path[8], argv[++i]))
548 die("git-update-index: %s cannot chmod %s", path, argv[i]);
549 continue;
550 }
5f73076c
JH
551 if (!strcmp(path, "--assume-unchanged")) {
552 mark_valid_only = MARK_VALID;
553 continue;
554 }
555 if (!strcmp(path, "--no-assume-unchanged")) {
556 mark_valid_only = UNMARK_VALID;
557 continue;
558 }
df6e1516
BL
559 if (!strcmp(path, "--info-only")) {
560 info_only = 1;
561 continue;
562 }
0ff5bf7c 563 if (!strcmp(path, "--force-remove")) {
9b63f501 564 force_remove = 1;
0ff5bf7c
JH
565 continue;
566 }
ee1bec3d
JH
567 if (!strcmp(path, "-z")) {
568 line_termination = 0;
569 continue;
570 }
571 if (!strcmp(path, "--stdin")) {
572 if (i != argc - 1)
573 die("--stdin must be at the end");
574 read_from_stdin = 1;
575 break;
576 }
d4dbf36d 577 if (!strcmp(path, "--index-info")) {
a41c175d
SP
578 if (i != argc - 1)
579 die("--index-info must be at the end");
d4dbf36d
JH
580 allow_add = allow_replace = allow_remove = 1;
581 read_index_info(line_termination);
a41c175d 582 break;
d4dbf36d 583 }
c6e007b0
JB
584 if (!strcmp(path, "--ignore-missing")) {
585 not_new = 1;
586 continue;
587 }
caf4f582
JH
588 if (!strcmp(path, "--verbose")) {
589 verbose = 1;
590 continue;
591 }
f6ab5bb2
PB
592 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
593 usage(update_index_usage);
2de381f9 594 die("unknown option %s", path);
121481ab 595 }
ee1bec3d
JH
596 update_one(path, prefix, prefix_length);
597 }
598 if (read_from_stdin) {
599 struct strbuf buf;
600 strbuf_init(&buf);
601 while (1) {
a94d9948 602 char *path_name;
ee1bec3d
JH
603 read_line(&buf, stdin, line_termination);
604 if (buf.eof)
605 break;
a94d9948
JH
606 if (line_termination && buf.buf[0] == '"')
607 path_name = unquote_c_style(buf.buf, NULL);
608 else
609 path_name = buf.buf;
610 update_one(path_name, prefix, prefix_length);
611 if (path_name != buf.buf)
612 free(path_name);
9b63f501 613 }
e83c5163 614 }
5cd5ace7
LT
615 if (active_cache_changed) {
616 if (write_cache(newfd, active_cache, active_nr) ||
617 commit_index_file(&cache_file))
618 die("Unable to write new cachefile");
619 }
9614b8dc 620
c4b83e61 621 return has_errors ? 1 : 0;
e83c5163 622}