Merge branches 'jc/nostat' and 'jc/empty-commit'
[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         /* In this case, if really is not set, we should leave
176          * CE_VALID bit alone.  Otherwise, paths marked with
177          * --no-assume-unchanged (i.e. things to be edited) will
178          * reacquire CE_VALID bit automatically, which is not
179          * really what we want.
180          */
181         if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
182                 updated->ce_flags &= ~htons(CE_VALID);
183
184         return updated;
185 }
186
187 static int refresh_cache(int really)
188 {
189         int i;
190         int has_errors = 0;
191
192         for (i = 0; i < active_nr; i++) {
193                 struct cache_entry *ce, *new;
194                 ce = active_cache[i];
195                 if (ce_stage(ce)) {
196                         while ((i < active_nr) &&
197                                ! strcmp(active_cache[i]->name, ce->name))
198                                 i++;
199                         i--;
200                         if (allow_unmerged)
201                                 continue;
202                         printf("%s: needs merge\n", ce->name);
203                         has_errors = 1;
204                         continue;
205                 }
206
207                 new = refresh_entry(ce, really);
208                 if (!new)
209                         continue;
210                 if (IS_ERR(new)) {
211                         if (not_new && PTR_ERR(new) == -ENOENT)
212                                 continue;
213                         if (really && PTR_ERR(new) == -EINVAL) {
214                                 /* If we are doing --really-refresh that
215                                  * means the index is not valid anymore.
216                                  */
217                                 ce->ce_flags &= ~htons(CE_VALID);
218                                 active_cache_changed = 1;
219                         }
220                         if (quiet)
221                                 continue;
222                         printf("%s: needs update\n", ce->name);
223                         has_errors = 1;
224                         continue;
225                 }
226                 active_cache_changed = 1;
227                 /* You can NOT just free active_cache[i] here, since it
228                  * might not be necessarily malloc()ed but can also come
229                  * from mmap(). */
230                 active_cache[i] = new;
231         }
232         return has_errors;
233 }
234
235 /*
236  * We fundamentally don't like some paths: we don't want
237  * dot or dot-dot anywhere, and for obvious reasons don't
238  * want to recurse into ".git" either.
239  *
240  * Also, we don't want double slashes or slashes at the
241  * end that can make pathnames ambiguous.
242  */
243 static int verify_dotfile(const char *rest)
244 {
245         /*
246          * The first character was '.', but that
247          * has already been discarded, we now test
248          * the rest.
249          */
250         switch (*rest) {
251         /* "." is not allowed */
252         case '\0': case '/':
253                 return 0;
254
255         /*
256          * ".git" followed by  NUL or slash is bad. This
257          * shares the path end test with the ".." case.
258          */
259         case 'g':
260                 if (rest[1] != 'i')
261                         break;
262                 if (rest[2] != 't')
263                         break;
264                 rest += 2;
265         /* fallthrough */
266         case '.':
267                 if (rest[1] == '\0' || rest[1] == '/')
268                         return 0;
269         }
270         return 1;
271 }
272
273 static int verify_path(const char *path)
274 {
275         char c;
276
277         goto inside;
278         for (;;) {
279                 if (!c)
280                         return 1;
281                 if (c == '/') {
282 inside:
283                         c = *path++;
284                         switch (c) {
285                         default:
286                                 continue;
287                         case '/': case '\0':
288                                 break;
289                         case '.':
290                                 if (verify_dotfile(path))
291                                         continue;
292                         }
293                         return 0;
294                 }
295                 c = *path++;
296         }
297 }
298
299 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
300                          const char *path, int stage)
301 {
302         int size, len, option;
303         struct cache_entry *ce;
304
305         if (!verify_path(path))
306                 return -1;
307
308         len = strlen(path);
309         size = cache_entry_size(len);
310         ce = xmalloc(size);
311         memset(ce, 0, size);
312
313         memcpy(ce->sha1, sha1, 20);
314         memcpy(ce->name, path, len);
315         ce->ce_flags = create_ce_flags(len, stage);
316         ce->ce_mode = create_ce_mode(mode);
317         if (assume_unchanged)
318                 ce->ce_flags |= htons(CE_VALID);
319         option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
320         option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
321         if (add_cache_entry(ce, option))
322                 return error("%s: cannot add to the index - missing --add option?",
323                              path);
324         report("add '%s'", path);
325         return 0;
326 }
327
328 static int chmod_path(int flip, const char *path)
329 {
330         int pos;
331         struct cache_entry *ce;
332         unsigned int mode;
333
334         pos = cache_name_pos(path, strlen(path));
335         if (pos < 0)
336                 return -1;
337         ce = active_cache[pos];
338         mode = ntohl(ce->ce_mode);
339         if (!S_ISREG(mode))
340                 return -1;
341         switch (flip) {
342         case '+':
343                 ce->ce_mode |= htonl(0111); break;
344         case '-':
345                 ce->ce_mode &= htonl(~0111); break;
346         default:
347                 return -1;
348         }
349         active_cache_changed = 1;
350         return 0;
351 }
352
353 static struct cache_file cache_file;
354
355 static void update_one(const char *path, const char *prefix, int prefix_length)
356 {
357         const char *p = prefix_path(prefix, prefix_length, path);
358         if (!verify_path(p)) {
359                 fprintf(stderr, "Ignoring path %s\n", path);
360                 return;
361         }
362         if (mark_valid_only) {
363                 if (mark_valid(p))
364                         die("Unable to mark file %s", path);
365                 return;
366         }
367
368         if (force_remove) {
369                 if (remove_file_from_cache(p))
370                         die("git-update-index: unable to remove %s", path);
371                 report("remove '%s'", path);
372                 return;
373         }
374         if (add_file_to_cache(p))
375                 die("Unable to process file %s", path);
376         report("add '%s'", path);
377 }
378
379 static void read_index_info(int line_termination)
380 {
381         struct strbuf buf;
382         strbuf_init(&buf);
383         while (1) {
384                 char *ptr, *tab;
385                 char *path_name;
386                 unsigned char sha1[20];
387                 unsigned int mode;
388                 int stage;
389
390                 /* This reads lines formatted in one of three formats:
391                  *
392                  * (1) mode         SP sha1          TAB path
393                  * The first format is what "git-apply --index-info"
394                  * reports, and used to reconstruct a partial tree
395                  * that is used for phony merge base tree when falling
396                  * back on 3-way merge.
397                  *
398                  * (2) mode SP type SP sha1          TAB path
399                  * The second format is to stuff git-ls-tree output
400                  * into the index file.
401                  * 
402                  * (3) mode         SP sha1 SP stage TAB path
403                  * This format is to put higher order stages into the
404                  * index file and matches git-ls-files --stage output.
405                  */
406                 read_line(&buf, stdin, line_termination);
407                 if (buf.eof)
408                         break;
409
410                 mode = strtoul(buf.buf, &ptr, 8);
411                 if (ptr == buf.buf || *ptr != ' ')
412                         goto bad_line;
413
414                 tab = strchr(ptr, '\t');
415                 if (!tab || tab - ptr < 41)
416                         goto bad_line;
417
418                 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
419                         stage = tab[-1] - '0';
420                         ptr = tab + 1; /* point at the head of path */
421                         tab = tab - 2; /* point at tail of sha1 */
422                 }
423                 else {
424                         stage = 0;
425                         ptr = tab + 1; /* point at the head of path */
426                 }
427
428                 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
429                         goto bad_line;
430
431                 if (line_termination && ptr[0] == '"')
432                         path_name = unquote_c_style(ptr, NULL);
433                 else
434                         path_name = ptr;
435
436                 if (!verify_path(path_name)) {
437                         fprintf(stderr, "Ignoring path %s\n", path_name);
438                         if (path_name != ptr)
439                                 free(path_name);
440                         continue;
441                 }
442
443                 if (!mode) {
444                         /* mode == 0 means there is no such path -- remove */
445                         if (remove_file_from_cache(path_name))
446                                 die("git-update-index: unable to remove %s",
447                                     ptr);
448                 }
449                 else {
450                         /* mode ' ' sha1 '\t' name
451                          * ptr[-1] points at tab,
452                          * ptr[-41] is at the beginning of sha1
453                          */
454                         ptr[-42] = ptr[-1] = 0;
455                         if (add_cacheinfo(mode, sha1, path_name, stage))
456                                 die("git-update-index: unable to update %s",
457                                     path_name);
458                 }
459                 if (path_name != ptr)
460                         free(path_name);
461                 continue;
462
463         bad_line:
464                 die("malformed index info %s", buf.buf);
465         }
466 }
467
468 static const char update_index_usage[] =
469 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
470
471 int main(int argc, const char **argv)
472 {
473         int i, newfd, entries, has_errors = 0, line_termination = '\n';
474         int allow_options = 1;
475         int read_from_stdin = 0;
476         const char *prefix = setup_git_directory();
477         int prefix_length = prefix ? strlen(prefix) : 0;
478
479         git_config(git_default_config);
480
481         newfd = hold_index_file_for_update(&cache_file, get_index_file());
482         if (newfd < 0)
483                 die("unable to create new cachefile");
484
485         entries = read_cache();
486         if (entries < 0)
487                 die("cache corrupted");
488
489         for (i = 1 ; i < argc; i++) {
490                 const char *path = argv[i];
491
492                 if (allow_options && *path == '-') {
493                         if (!strcmp(path, "--")) {
494                                 allow_options = 0;
495                                 continue;
496                         }
497                         if (!strcmp(path, "-q")) {
498                                 quiet = 1;
499                                 continue;
500                         }
501                         if (!strcmp(path, "--add")) {
502                                 allow_add = 1;
503                                 continue;
504                         }
505                         if (!strcmp(path, "--replace")) {
506                                 allow_replace = 1;
507                                 continue;
508                         }
509                         if (!strcmp(path, "--remove")) {
510                                 allow_remove = 1;
511                                 continue;
512                         }
513                         if (!strcmp(path, "--unmerged")) {
514                                 allow_unmerged = 1;
515                                 continue;
516                         }
517                         if (!strcmp(path, "--refresh")) {
518                                 has_errors |= refresh_cache(0);
519                                 continue;
520                         }
521                         if (!strcmp(path, "--really-refresh")) {
522                                 has_errors |= refresh_cache(1);
523                                 continue;
524                         }
525                         if (!strcmp(path, "--cacheinfo")) {
526                                 unsigned char sha1[20];
527                                 unsigned int mode;
528
529                                 if (i+3 >= argc)
530                                         die("git-update-index: --cacheinfo <mode> <sha1> <path>");
531
532                                 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
533                                     get_sha1_hex(argv[i+2], sha1) ||
534                                     add_cacheinfo(mode, sha1, argv[i+3], 0))
535                                         die("git-update-index: --cacheinfo"
536                                             " cannot add %s", argv[i+3]);
537                                 i += 3;
538                                 continue;
539                         }
540                         if (!strcmp(path, "--chmod=-x") ||
541                             !strcmp(path, "--chmod=+x")) {
542                                 if (argc <= i+1)
543                                         die("git-update-index: %s <path>", path);
544                                 if (chmod_path(path[8], argv[++i]))
545                                         die("git-update-index: %s cannot chmod %s", path, argv[i]);
546                                 continue;
547                         }
548                         if (!strcmp(path, "--assume-unchanged")) {
549                                 mark_valid_only = MARK_VALID;
550                                 continue;
551                         }
552                         if (!strcmp(path, "--no-assume-unchanged")) {
553                                 mark_valid_only = UNMARK_VALID;
554                                 continue;
555                         }
556                         if (!strcmp(path, "--info-only")) {
557                                 info_only = 1;
558                                 continue;
559                         }
560                         if (!strcmp(path, "--force-remove")) {
561                                 force_remove = 1;
562                                 continue;
563                         }
564                         if (!strcmp(path, "-z")) {
565                                 line_termination = 0;
566                                 continue;
567                         }
568                         if (!strcmp(path, "--stdin")) {
569                                 if (i != argc - 1)
570                                         die("--stdin must be at the end");
571                                 read_from_stdin = 1;
572                                 break;
573                         }
574                         if (!strcmp(path, "--index-info")) {
575                                 allow_add = allow_replace = allow_remove = 1;
576                                 read_index_info(line_termination);
577                                 continue;
578                         }
579                         if (!strcmp(path, "--ignore-missing")) {
580                                 not_new = 1;
581                                 continue;
582                         }
583                         if (!strcmp(path, "--verbose")) {
584                                 verbose = 1;
585                                 continue;
586                         }
587                         if (!strcmp(path, "-h") || !strcmp(path, "--help"))
588                                 usage(update_index_usage);
589                         die("unknown option %s", path);
590                 }
591                 update_one(path, prefix, prefix_length);
592         }
593         if (read_from_stdin) {
594                 struct strbuf buf;
595                 strbuf_init(&buf);
596                 while (1) {
597                         char *path_name;
598                         read_line(&buf, stdin, line_termination);
599                         if (buf.eof)
600                                 break;
601                         if (line_termination && buf.buf[0] == '"')
602                                 path_name = unquote_c_style(buf.buf, NULL);
603                         else
604                                 path_name = buf.buf;
605                         update_one(path_name, prefix, prefix_length);
606                         if (path_name != buf.buf)
607                                 free(path_name);
608                 }
609         }
610         if (active_cache_changed) {
611                 if (write_cache(newfd, active_cache, active_nr) ||
612                     commit_index_file(&cache_file))
613                         die("Unable to write new cachefile");
614         }
615
616         return has_errors ? 1 : 0;
617 }