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