Merge branch 'jc/cache-tree' into jc/dirwalk-n-cache-tree
[git.git] / update-index.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7 #include "strbuf.h"
8 #include "quote.h"
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11
12 /*
13  * Default to not allowing changes to the list of files. The
14  * tool doesn't actually care, but this makes it harder to add
15  * files to the revision control by mistake by doing something
16  * like "git-update-index *" and suddenly having all the object
17  * files be revision controlled.
18  */
19 static int allow_add;
20 static int allow_remove;
21 static int allow_replace;
22 static int info_only;
23 static int force_remove;
24 static int verbose;
25 static int mark_valid_only = 0;
26 #define MARK_VALID 1
27 #define UNMARK_VALID 2
28
29 static void report(const char *fmt, ...)
30 {
31         va_list vp;
32
33         if (!verbose)
34                 return;
35
36         va_start(vp, fmt);
37         vprintf(fmt, vp);
38         putchar('\n');
39         va_end(vp);
40 }
41
42 static int mark_valid(const char *path)
43 {
44         int namelen = strlen(path);
45         int pos = cache_name_pos(path, namelen);
46         if (0 <= pos) {
47                 switch (mark_valid_only) {
48                 case MARK_VALID:
49                         active_cache[pos]->ce_flags |= htons(CE_VALID);
50                         break;
51                 case UNMARK_VALID:
52                         active_cache[pos]->ce_flags &= ~htons(CE_VALID);
53                         break;
54                 }
55                 cache_tree_invalidate_path(active_cache_tree, path);
56                 active_cache_changed = 1;
57                 return 0;
58         }
59         return -1;
60 }
61
62 static int add_file_to_cache(const char *path)
63 {
64         int size, namelen, option, status;
65         struct cache_entry *ce;
66         struct stat st;
67
68         status = lstat(path, &st);
69
70         /* We probably want to do this in remove_file_from_cache() and
71          * add_cache_entry() instead...
72          */
73         cache_tree_invalidate_path(active_cache_tree, path);
74
75         if (status < 0 || S_ISDIR(st.st_mode)) {
76                 /* When we used to have "path" and now we want to add
77                  * "path/file", we need a way to remove "path" before
78                  * being able to add "path/file".  However,
79                  * "git-update-index --remove path" would not work.
80                  * --force-remove can be used but this is more user
81                  * friendly, especially since we can do the opposite
82                  * case just fine without --force-remove.
83                  */
84                 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
85                         if (allow_remove) {
86                                 if (remove_file_from_cache(path))
87                                         return error("%s: cannot remove from the index",
88                                                      path);
89                                 else
90                                         return 0;
91                         } else if (status < 0) {
92                                 return error("%s: does not exist and --remove not passed",
93                                              path);
94                         }
95                 }
96                 if (0 == status)
97                         return error("%s: is a directory - add files inside instead",
98                                      path);
99                 else
100                         return error("lstat(\"%s\"): %s", path,
101                                      strerror(errno));
102         }
103
104         namelen = strlen(path);
105         size = cache_entry_size(namelen);
106         ce = xcalloc(1, size);
107         memcpy(ce->name, path, namelen);
108         ce->ce_flags = htons(namelen);
109         fill_stat_cache_info(ce, &st);
110
111         ce->ce_mode = create_ce_mode(st.st_mode);
112         if (!trust_executable_bit) {
113                 /* If there is an existing entry, pick the mode bits
114                  * from it.
115                  */
116                 int pos = cache_name_pos(path, namelen);
117                 if (0 <= pos)
118                         ce->ce_mode = active_cache[pos]->ce_mode;
119         }
120
121         if (index_path(ce->sha1, path, &st, !info_only))
122                 return -1;
123         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
124         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
125         if (add_cache_entry(ce, option))
126                 return error("%s: cannot add to the index - missing --add option?",
127                              path);
128         return 0;
129 }
130
131 /*
132  * We fundamentally don't like some paths: we don't want
133  * dot or dot-dot anywhere, and for obvious reasons don't
134  * want to recurse into ".git" either.
135  *
136  * Also, we don't want double slashes or slashes at the
137  * end that can make pathnames ambiguous.
138  */
139 static int verify_dotfile(const char *rest)
140 {
141         /*
142          * The first character was '.', but that
143          * has already been discarded, we now test
144          * the rest.
145          */
146         switch (*rest) {
147         /* "." is not allowed */
148         case '\0': case '/':
149                 return 0;
150
151         /*
152          * ".git" followed by  NUL or slash is bad. This
153          * shares the path end test with the ".." case.
154          */
155         case 'g':
156                 if (rest[1] != 'i')
157                         break;
158                 if (rest[2] != 't')
159                         break;
160                 rest += 2;
161         /* fallthrough */
162         case '.':
163                 if (rest[1] == '\0' || rest[1] == '/')
164                         return 0;
165         }
166         return 1;
167 }
168
169 static int verify_path(const char *path)
170 {
171         char c;
172
173         goto inside;
174         for (;;) {
175                 if (!c)
176                         return 1;
177                 if (c == '/') {
178 inside:
179                         c = *path++;
180                         switch (c) {
181                         default:
182                                 continue;
183                         case '/': case '\0':
184                                 break;
185                         case '.':
186                                 if (verify_dotfile(path))
187                                         continue;
188                         }
189                         return 0;
190                 }
191                 c = *path++;
192         }
193 }
194
195 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
196                          const char *path, int stage)
197 {
198         int size, len, option;
199         struct cache_entry *ce;
200
201         if (!verify_path(path))
202                 return -1;
203
204         len = strlen(path);
205         size = cache_entry_size(len);
206         ce = xcalloc(1, size);
207
208         memcpy(ce->sha1, sha1, 20);
209         memcpy(ce->name, path, len);
210         ce->ce_flags = create_ce_flags(len, stage);
211         ce->ce_mode = create_ce_mode(mode);
212         if (assume_unchanged)
213                 ce->ce_flags |= htons(CE_VALID);
214         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
215         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
216         if (add_cache_entry(ce, option))
217                 return error("%s: cannot add to the index - missing --add option?",
218                              path);
219         report("add '%s'", path);
220         cache_tree_invalidate_path(active_cache_tree, path);
221         return 0;
222 }
223
224 static void chmod_path(int flip, const char *path)
225 {
226         int pos;
227         struct cache_entry *ce;
228         unsigned int mode;
229
230         pos = cache_name_pos(path, strlen(path));
231         if (pos < 0)
232                 goto fail;
233         ce = active_cache[pos];
234         mode = ntohl(ce->ce_mode);
235         if (!S_ISREG(mode))
236                 goto fail;
237         switch (flip) {
238         case '+':
239                 ce->ce_mode |= htonl(0111); break;
240         case '-':
241                 ce->ce_mode &= htonl(~0111); break;
242         default:
243                 goto fail;
244         }
245         cache_tree_invalidate_path(active_cache_tree, path);
246         active_cache_changed = 1;
247         report("chmod %cx '%s'", flip, path);
248         return;
249  fail:
250         die("git-update-index: cannot chmod %cx '%s'", flip, path);
251 }
252
253 static struct cache_file cache_file;
254
255 static void update_one(const char *path, const char *prefix, int prefix_length)
256 {
257         const char *p = prefix_path(prefix, prefix_length, path);
258         if (!verify_path(p)) {
259                 fprintf(stderr, "Ignoring path %s\n", path);
260                 goto free_return;
261         }
262         if (mark_valid_only) {
263                 if (mark_valid(p))
264                         die("Unable to mark file %s", path);
265                 goto free_return;
266         }
267         cache_tree_invalidate_path(active_cache_tree, path);
268
269         if (force_remove) {
270                 if (remove_file_from_cache(p))
271                         die("git-update-index: unable to remove %s", path);
272                 report("remove '%s'", path);
273                 goto free_return;
274         }
275         if (add_file_to_cache(p))
276                 die("Unable to process file %s", path);
277         report("add '%s'", path);
278  free_return:
279         if (p < path || p > path + strlen(path))
280                 free((char*)p);
281 }
282
283 static void read_index_info(int line_termination)
284 {
285         struct strbuf buf;
286         strbuf_init(&buf);
287         while (1) {
288                 char *ptr, *tab;
289                 char *path_name;
290                 unsigned char sha1[20];
291                 unsigned int mode;
292                 int stage;
293
294                 /* This reads lines formatted in one of three formats:
295                  *
296                  * (1) mode         SP sha1          TAB path
297                  * The first format is what "git-apply --index-info"
298                  * reports, and used to reconstruct a partial tree
299                  * that is used for phony merge base tree when falling
300                  * back on 3-way merge.
301                  *
302                  * (2) mode SP type SP sha1          TAB path
303                  * The second format is to stuff git-ls-tree output
304                  * into the index file.
305                  * 
306                  * (3) mode         SP sha1 SP stage TAB path
307                  * This format is to put higher order stages into the
308                  * index file and matches git-ls-files --stage output.
309                  */
310                 read_line(&buf, stdin, line_termination);
311                 if (buf.eof)
312                         break;
313
314                 mode = strtoul(buf.buf, &ptr, 8);
315                 if (ptr == buf.buf || *ptr != ' ')
316                         goto bad_line;
317
318                 tab = strchr(ptr, '\t');
319                 if (!tab || tab - ptr < 41)
320                         goto bad_line;
321
322                 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
323                         stage = tab[-1] - '0';
324                         ptr = tab + 1; /* point at the head of path */
325                         tab = tab - 2; /* point at tail of sha1 */
326                 }
327                 else {
328                         stage = 0;
329                         ptr = tab + 1; /* point at the head of path */
330                 }
331
332                 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
333                         goto bad_line;
334
335                 if (line_termination && ptr[0] == '"')
336                         path_name = unquote_c_style(ptr, NULL);
337                 else
338                         path_name = ptr;
339
340                 if (!verify_path(path_name)) {
341                         fprintf(stderr, "Ignoring path %s\n", path_name);
342                         if (path_name != ptr)
343                                 free(path_name);
344                         continue;
345                 }
346                 cache_tree_invalidate_path(active_cache_tree, path_name);
347
348                 if (!mode) {
349                         /* mode == 0 means there is no such path -- remove */
350                         if (remove_file_from_cache(path_name))
351                                 die("git-update-index: unable to remove %s",
352                                     ptr);
353                 }
354                 else {
355                         /* mode ' ' sha1 '\t' name
356                          * ptr[-1] points at tab,
357                          * ptr[-41] is at the beginning of sha1
358                          */
359                         ptr[-42] = ptr[-1] = 0;
360                         if (add_cacheinfo(mode, sha1, path_name, stage))
361                                 die("git-update-index: unable to update %s",
362                                     path_name);
363                 }
364                 if (path_name != ptr)
365                         free(path_name);
366                 continue;
367
368         bad_line:
369                 die("malformed index info %s", buf.buf);
370         }
371 }
372
373 static const char update_index_usage[] =
374 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again] [--ignore-missing] [-z] [--verbose] [--] <file>...";
375
376 static unsigned char head_sha1[20];
377 static unsigned char merge_head_sha1[20];
378
379 static struct cache_entry *read_one_ent(const char *which,
380                                         unsigned char *ent, const char *path,
381                                         int namelen, int stage)
382 {
383         unsigned mode;
384         unsigned char sha1[20];
385         int size;
386         struct cache_entry *ce;
387
388         if (get_tree_entry(ent, path, sha1, &mode)) {
389                 if (which)
390                         error("%s: not in %s branch.", path, which);
391                 return NULL;
392         }
393         if (mode == S_IFDIR) {
394                 if (which)
395                         error("%s: not a blob in %s branch.", path, which);
396                 return NULL;
397         }
398         size = cache_entry_size(namelen);
399         ce = xcalloc(1, size);
400
401         memcpy(ce->sha1, sha1, 20);
402         memcpy(ce->name, path, namelen);
403         ce->ce_flags = create_ce_flags(namelen, stage);
404         ce->ce_mode = create_ce_mode(mode);
405         return ce;
406 }
407
408 static int unresolve_one(const char *path)
409 {
410         int namelen = strlen(path);
411         int pos;
412         int ret = 0;
413         struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
414
415         /* See if there is such entry in the index. */
416         pos = cache_name_pos(path, namelen);
417         if (pos < 0) {
418                 /* If there isn't, either it is unmerged, or
419                  * resolved as "removed" by mistake.  We do not
420                  * want to do anything in the former case.
421                  */
422                 pos = -pos-1;
423                 if (pos < active_nr) {
424                         struct cache_entry *ce = active_cache[pos];
425                         if (ce_namelen(ce) == namelen &&
426                             !memcmp(ce->name, path, namelen)) {
427                                 fprintf(stderr,
428                                         "%s: skipping still unmerged path.\n",
429                                         path);
430                                 goto free_return;
431                         }
432                 }
433         }
434
435         /* Grab blobs from given path from HEAD and MERGE_HEAD,
436          * stuff HEAD version in stage #2,
437          * stuff MERGE_HEAD version in stage #3.
438          */
439         ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
440         ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
441
442         if (!ce_2 || !ce_3) {
443                 ret = -1;
444                 goto free_return;
445         }
446         if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
447             ce_2->ce_mode == ce_3->ce_mode) {
448                 fprintf(stderr, "%s: identical in both, skipping.\n",
449                         path);
450                 goto free_return;
451         }
452
453         cache_tree_invalidate_path(active_cache_tree, path);
454         remove_file_from_cache(path);
455         if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
456                 error("%s: cannot add our version to the index.", path);
457                 ret = -1;
458                 goto free_return;
459         }
460         if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
461                 return 0;
462         error("%s: cannot add their version to the index.", path);
463         ret = -1;
464  free_return:
465         free(ce_2);
466         free(ce_3);
467         return ret;
468 }
469
470 static void read_head_pointers(void)
471 {
472         if (read_ref(git_path("HEAD"), head_sha1))
473                 die("No HEAD -- no initial commit yet?\n");
474         if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1)) {
475                 fprintf(stderr, "Not in the middle of a merge.\n");
476                 exit(0);
477         }
478 }
479
480 static int do_unresolve(int ac, const char **av,
481                         const char *prefix, int prefix_length)
482 {
483         int i;
484         int err = 0;
485
486         /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
487          * are not doing a merge, so exit with success status.
488          */
489         read_head_pointers();
490
491         for (i = 1; i < ac; i++) {
492                 const char *arg = av[i];
493                 const char *p = prefix_path(prefix, prefix_length, arg);
494                 err |= unresolve_one(p);
495                 if (p < arg || p > arg + strlen(arg))
496                         free((char*)p);
497         }
498         return err;
499 }
500
501 static int do_reupdate(int ac, const char **av,
502                        const char *prefix, int prefix_length)
503 {
504         /* Read HEAD and run update-index on paths that are
505          * merged and already different between index and HEAD.
506          */
507         int pos;
508         int has_head = 1;
509         const char **pathspec = get_pathspec(prefix, av + 1);
510
511         if (read_ref(git_path("HEAD"), head_sha1))
512                 /* If there is no HEAD, that means it is an initial
513                  * commit.  Update everything in the index.
514                  */
515                 has_head = 0;
516  redo:
517         for (pos = 0; pos < active_nr; pos++) {
518                 struct cache_entry *ce = active_cache[pos];
519                 struct cache_entry *old = NULL;
520                 int save_nr;
521
522                 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
523                         continue;
524                 if (has_head)
525                         old = read_one_ent(NULL, head_sha1,
526                                            ce->name, ce_namelen(ce), 0);
527                 if (old && ce->ce_mode == old->ce_mode &&
528                     !memcmp(ce->sha1, old->sha1, 20)) {
529                         free(old);
530                         continue; /* unchanged */
531                 }
532                 /* Be careful.  The working tree may not have the
533                  * path anymore, in which case, under 'allow_remove',
534                  * or worse yet 'allow_replace', active_nr may decrease.
535                  */
536                 save_nr = active_nr;
537                 update_one(ce->name + prefix_length, prefix, prefix_length);
538                 if (save_nr != active_nr)
539                         goto redo;
540         }
541         return 0;
542 }
543
544 int main(int argc, const char **argv)
545 {
546         int i, newfd, entries, has_errors = 0, line_termination = '\n';
547         int allow_options = 1;
548         int read_from_stdin = 0;
549         const char *prefix = setup_git_directory();
550         int prefix_length = prefix ? strlen(prefix) : 0;
551         char set_executable_bit = 0;
552         unsigned int refresh_flags = 0;
553
554         git_config(git_default_config);
555
556         newfd = hold_index_file_for_update(&cache_file, get_index_file());
557         if (newfd < 0)
558                 die("unable to create new cachefile");
559
560         entries = read_cache();
561         if (entries < 0)
562                 die("cache corrupted");
563
564         for (i = 1 ; i < argc; i++) {
565                 const char *path = argv[i];
566
567                 if (allow_options && *path == '-') {
568                         if (!strcmp(path, "--")) {
569                                 allow_options = 0;
570                                 continue;
571                         }
572                         if (!strcmp(path, "-q")) {
573                                 refresh_flags |= REFRESH_QUIET;
574                                 continue;
575                         }
576                         if (!strcmp(path, "--add")) {
577                                 allow_add = 1;
578                                 continue;
579                         }
580                         if (!strcmp(path, "--replace")) {
581                                 allow_replace = 1;
582                                 continue;
583                         }
584                         if (!strcmp(path, "--remove")) {
585                                 allow_remove = 1;
586                                 continue;
587                         }
588                         if (!strcmp(path, "--unmerged")) {
589                                 refresh_flags |= REFRESH_UNMERGED;
590                                 continue;
591                         }
592                         if (!strcmp(path, "--refresh")) {
593                                 has_errors |= refresh_cache(refresh_flags);
594                                 continue;
595                         }
596                         if (!strcmp(path, "--really-refresh")) {
597                                 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
598                                 continue;
599                         }
600                         if (!strcmp(path, "--cacheinfo")) {
601                                 unsigned char sha1[20];
602                                 unsigned int mode;
603
604                                 if (i+3 >= argc)
605                                         die("git-update-index: --cacheinfo <mode> <sha1> <path>");
606
607                                 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
608                                     get_sha1_hex(argv[i+2], sha1) ||
609                                     add_cacheinfo(mode, sha1, argv[i+3], 0))
610                                         die("git-update-index: --cacheinfo"
611                                             " cannot add %s", argv[i+3]);
612                                 i += 3;
613                                 continue;
614                         }
615                         if (!strcmp(path, "--chmod=-x") ||
616                             !strcmp(path, "--chmod=+x")) {
617                                 if (argc <= i+1)
618                                         die("git-update-index: %s <path>", path);
619                                 set_executable_bit = path[8];
620                                 continue;
621                         }
622                         if (!strcmp(path, "--assume-unchanged")) {
623                                 mark_valid_only = MARK_VALID;
624                                 continue;
625                         }
626                         if (!strcmp(path, "--no-assume-unchanged")) {
627                                 mark_valid_only = UNMARK_VALID;
628                                 continue;
629                         }
630                         if (!strcmp(path, "--info-only")) {
631                                 info_only = 1;
632                                 continue;
633                         }
634                         if (!strcmp(path, "--force-remove")) {
635                                 force_remove = 1;
636                                 continue;
637                         }
638                         if (!strcmp(path, "-z")) {
639                                 line_termination = 0;
640                                 continue;
641                         }
642                         if (!strcmp(path, "--stdin")) {
643                                 if (i != argc - 1)
644                                         die("--stdin must be at the end");
645                                 read_from_stdin = 1;
646                                 break;
647                         }
648                         if (!strcmp(path, "--index-info")) {
649                                 if (i != argc - 1)
650                                         die("--index-info must be at the end");
651                                 allow_add = allow_replace = allow_remove = 1;
652                                 read_index_info(line_termination);
653                                 break;
654                         }
655                         if (!strcmp(path, "--unresolve")) {
656                                 has_errors = do_unresolve(argc - i, argv + i,
657                                                           prefix, prefix_length);
658                                 if (has_errors)
659                                         active_cache_changed = 0;
660                                 goto finish;
661                         }
662                         if (!strcmp(path, "--again")) {
663                                 has_errors = do_reupdate(argc - i, argv + i,
664                                                          prefix, prefix_length);
665                                 if (has_errors)
666                                         active_cache_changed = 0;
667                                 goto finish;
668                         }
669                         if (!strcmp(path, "--ignore-missing")) {
670                                 refresh_flags |= REFRESH_IGNORE_MISSING;
671                                 continue;
672                         }
673                         if (!strcmp(path, "--verbose")) {
674                                 verbose = 1;
675                                 continue;
676                         }
677                         if (!strcmp(path, "-h") || !strcmp(path, "--help"))
678                                 usage(update_index_usage);
679                         die("unknown option %s", path);
680                 }
681                 update_one(path, prefix, prefix_length);
682                 if (set_executable_bit)
683                         chmod_path(set_executable_bit, path);
684         }
685         if (read_from_stdin) {
686                 struct strbuf buf;
687                 strbuf_init(&buf);
688                 while (1) {
689                         char *path_name;
690                         const char *p;
691                         read_line(&buf, stdin, line_termination);
692                         if (buf.eof)
693                                 break;
694                         if (line_termination && buf.buf[0] == '"')
695                                 path_name = unquote_c_style(buf.buf, NULL);
696                         else
697                                 path_name = buf.buf;
698                         p = prefix_path(prefix, prefix_length, path_name);
699                         update_one(p, NULL, 0);
700                         if (set_executable_bit)
701                                 chmod_path(set_executable_bit, p);
702                         if (p < path_name || p > path_name + strlen(path_name))
703                                 free((char*) p);
704                         if (path_name != buf.buf)
705                                 free(path_name);
706                 }
707         }
708
709  finish:
710         if (active_cache_changed) {
711                 if (write_cache(newfd, active_cache, active_nr) ||
712                     commit_index_file(&cache_file))
713                         die("Unable to write new cachefile");
714         }
715
716         return has_errors ? 1 : 0;
717 }