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