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