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