Code

git-svn: documentation updates
[git.git] / unpack-trees.c
1 #include <signal.h>
2 #include <sys/time.h>
3 #include "cache.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
9 #define DBRT_DEBUG 1
11 struct tree_entry_list {
12         struct tree_entry_list *next;
13         unsigned directory : 1;
14         unsigned executable : 1;
15         unsigned symlink : 1;
16         unsigned int mode;
17         const char *name;
18         const unsigned char *sha1;
19 };
21 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
22 {
23         struct tree_desc desc;
24         struct name_entry one;
25         struct tree_entry_list *ret = NULL;
26         struct tree_entry_list **list_p = &ret;
28         if (!tree->object.parsed)
29                 parse_tree(tree);
31         desc.buf = tree->buffer;
32         desc.size = tree->size;
34         while (tree_entry(&desc, &one)) {
35                 struct tree_entry_list *entry;
37                 entry = xmalloc(sizeof(struct tree_entry_list));
38                 entry->name = one.path;
39                 entry->sha1 = one.sha1;
40                 entry->mode = one.mode;
41                 entry->directory = S_ISDIR(one.mode) != 0;
42                 entry->executable = (one.mode & S_IXUSR) != 0;
43                 entry->symlink = S_ISLNK(one.mode) != 0;
44                 entry->next = NULL;
46                 *list_p = entry;
47                 list_p = &entry->next;
48         }
49         return ret;
50 }
52 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
53 {
54         int len1 = strlen(name1);
55         int len2 = strlen(name2);
56         int len = len1 < len2 ? len1 : len2;
57         int ret = memcmp(name1, name2, len);
58         unsigned char c1, c2;
59         if (ret)
60                 return ret;
61         c1 = name1[len];
62         c2 = name2[len];
63         if (!c1 && dir1)
64                 c1 = '/';
65         if (!c2 && dir2)
66                 c2 = '/';
67         ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
68         if (c1 && c2 && !ret)
69                 ret = len1 - len2;
70         return ret;
71 }
73 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
74                             const char *base, struct unpack_trees_options *o,
75                             int *indpos,
76                             struct tree_entry_list *df_conflict_list)
77 {
78         int baselen = strlen(base);
79         int src_size = len + 1;
80         do {
81                 int i;
82                 const char *first;
83                 int firstdir = 0;
84                 int pathlen;
85                 unsigned ce_size;
86                 struct tree_entry_list **subposns;
87                 struct cache_entry **src;
88                 int any_files = 0;
89                 int any_dirs = 0;
90                 char *cache_name;
91                 int ce_stage;
93                 /* Find the first name in the input. */
95                 first = NULL;
96                 cache_name = NULL;
98                 /* Check the cache */
99                 if (o->merge && *indpos < active_nr) {
100                         /* This is a bit tricky: */
101                         /* If the index has a subdirectory (with
102                          * contents) as the first name, it'll get a
103                          * filename like "foo/bar". But that's after
104                          * "foo", so the entry in trees will get
105                          * handled first, at which point we'll go into
106                          * "foo", and deal with "bar" from the index,
107                          * because the base will be "foo/". The only
108                          * way we can actually have "foo/bar" first of
109                          * all the things is if the trees don't
110                          * contain "foo" at all, in which case we'll
111                          * handle "foo/bar" without going into the
112                          * directory, but that's fine (and will return
113                          * an error anyway, with the added unknown
114                          * file case.
115                          */
117                         cache_name = active_cache[*indpos]->name;
118                         if (strlen(cache_name) > baselen &&
119                             !memcmp(cache_name, base, baselen)) {
120                                 cache_name += baselen;
121                                 first = cache_name;
122                         } else {
123                                 cache_name = NULL;
124                         }
125                 }
127 #if DBRT_DEBUG > 1
128                 if (first)
129                         printf("index %s\n", first);
130 #endif
131                 for (i = 0; i < len; i++) {
132                         if (!posns[i] || posns[i] == df_conflict_list)
133                                 continue;
134 #if DBRT_DEBUG > 1
135                         printf("%d %s\n", i + 1, posns[i]->name);
136 #endif
137                         if (!first || entcmp(first, firstdir,
138                                              posns[i]->name,
139                                              posns[i]->directory) > 0) {
140                                 first = posns[i]->name;
141                                 firstdir = posns[i]->directory;
142                         }
143                 }
144                 /* No name means we're done */
145                 if (!first)
146                         return 0;
148                 pathlen = strlen(first);
149                 ce_size = cache_entry_size(baselen + pathlen);
151                 src = xcalloc(src_size, sizeof(struct cache_entry *));
153                 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
155                 if (cache_name && !strcmp(cache_name, first)) {
156                         any_files = 1;
157                         src[0] = active_cache[*indpos];
158                         remove_cache_entry_at(*indpos);
159                 }
161                 for (i = 0; i < len; i++) {
162                         struct cache_entry *ce;
164                         if (!posns[i] ||
165                             (posns[i] != df_conflict_list &&
166                              strcmp(first, posns[i]->name))) {
167                                 continue;
168                         }
170                         if (posns[i] == df_conflict_list) {
171                                 src[i + o->merge] = o->df_conflict_entry;
172                                 continue;
173                         }
175                         if (posns[i]->directory) {
176                                 struct tree *tree = lookup_tree(posns[i]->sha1);
177                                 any_dirs = 1;
178                                 parse_tree(tree);
179                                 subposns[i] = create_tree_entry_list(tree);
180                                 posns[i] = posns[i]->next;
181                                 src[i + o->merge] = o->df_conflict_entry;
182                                 continue;
183                         }
185                         if (!o->merge)
186                                 ce_stage = 0;
187                         else if (i + 1 < o->head_idx)
188                                 ce_stage = 1;
189                         else if (i + 1 > o->head_idx)
190                                 ce_stage = 3;
191                         else
192                                 ce_stage = 2;
194                         ce = xcalloc(1, ce_size);
195                         ce->ce_mode = create_ce_mode(posns[i]->mode);
196                         ce->ce_flags = create_ce_flags(baselen + pathlen,
197                                                        ce_stage);
198                         memcpy(ce->name, base, baselen);
199                         memcpy(ce->name + baselen, first, pathlen + 1);
201                         any_files = 1;
203                         hashcpy(ce->sha1, posns[i]->sha1);
204                         src[i + o->merge] = ce;
205                         subposns[i] = df_conflict_list;
206                         posns[i] = posns[i]->next;
207                 }
208                 if (any_files) {
209                         if (o->merge) {
210                                 int ret;
212 #if DBRT_DEBUG > 1
213                                 printf("%s:\n", first);
214                                 for (i = 0; i < src_size; i++) {
215                                         printf(" %d ", i);
216                                         if (src[i])
217                                                 printf("%s\n", sha1_to_hex(src[i]->sha1));
218                                         else
219                                                 printf("\n");
220                                 }
221 #endif
222                                 ret = o->fn(src, o);
224 #if DBRT_DEBUG > 1
225                                 printf("Added %d entries\n", ret);
226 #endif
227                                 *indpos += ret;
228                         } else {
229                                 for (i = 0; i < src_size; i++) {
230                                         if (src[i]) {
231                                                 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
232                                         }
233                                 }
234                         }
235                 }
236                 if (any_dirs) {
237                         char *newbase = xmalloc(baselen + 2 + pathlen);
238                         memcpy(newbase, base, baselen);
239                         memcpy(newbase + baselen, first, pathlen);
240                         newbase[baselen + pathlen] = '/';
241                         newbase[baselen + pathlen + 1] = '\0';
242                         if (unpack_trees_rec(subposns, len, newbase, o,
243                                              indpos, df_conflict_list))
244                                 return -1;
245                         free(newbase);
246                 }
247                 free(subposns);
248                 free(src);
249         } while (1);
252 /* Unlink the last component and attempt to remove leading
253  * directories, in case this unlink is the removal of the
254  * last entry in the directory -- empty directories are removed.
255  */
256 static void unlink_entry(char *name)
258         char *cp, *prev;
260         if (unlink(name))
261                 return;
262         prev = NULL;
263         while (1) {
264                 int status;
265                 cp = strrchr(name, '/');
266                 if (prev)
267                         *prev = '/';
268                 if (!cp)
269                         break;
271                 *cp = 0;
272                 status = rmdir(name);
273                 if (status) {
274                         *cp = '/';
275                         break;
276                 }
277                 prev = cp;
278         }
281 static volatile sig_atomic_t progress_update;
283 static void progress_interval(int signum)
285         progress_update = 1;
288 static void setup_progress_signal(void)
290         struct sigaction sa;
291         struct itimerval v;
293         memset(&sa, 0, sizeof(sa));
294         sa.sa_handler = progress_interval;
295         sigemptyset(&sa.sa_mask);
296         sa.sa_flags = SA_RESTART;
297         sigaction(SIGALRM, &sa, NULL);
299         v.it_interval.tv_sec = 1;
300         v.it_interval.tv_usec = 0;
301         v.it_value = v.it_interval;
302         setitimer(ITIMER_REAL, &v, NULL);
305 static struct checkout state;
306 static void check_updates(struct cache_entry **src, int nr,
307                 struct unpack_trees_options *o)
309         unsigned short mask = htons(CE_UPDATE);
310         unsigned last_percent = 200, cnt = 0, total = 0;
312         if (o->update && o->verbose_update) {
313                 for (total = cnt = 0; cnt < nr; cnt++) {
314                         struct cache_entry *ce = src[cnt];
315                         if (!ce->ce_mode || ce->ce_flags & mask)
316                                 total++;
317                 }
319                 /* Don't bother doing this for very small updates */
320                 if (total < 250)
321                         total = 0;
323                 if (total) {
324                         fprintf(stderr, "Checking files out...\n");
325                         setup_progress_signal();
326                         progress_update = 1;
327                 }
328                 cnt = 0;
329         }
331         while (nr--) {
332                 struct cache_entry *ce = *src++;
334                 if (total) {
335                         if (!ce->ce_mode || ce->ce_flags & mask) {
336                                 unsigned percent;
337                                 cnt++;
338                                 percent = (cnt * 100) / total;
339                                 if (percent != last_percent ||
340                                     progress_update) {
341                                         fprintf(stderr, "%4u%% (%u/%u) done\r",
342                                                 percent, cnt, total);
343                                         last_percent = percent;
344                                         progress_update = 0;
345                                 }
346                         }
347                 }
348                 if (!ce->ce_mode) {
349                         if (o->update)
350                                 unlink_entry(ce->name);
351                         continue;
352                 }
353                 if (ce->ce_flags & mask) {
354                         ce->ce_flags &= ~mask;
355                         if (o->update)
356                                 checkout_entry(ce, &state, NULL);
357                 }
358         }
359         if (total) {
360                 signal(SIGALRM, SIG_IGN);
361                 fputc('\n', stderr);
362         }
365 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
367         int indpos = 0;
368         unsigned len = object_list_length(trees);
369         struct tree_entry_list **posns;
370         int i;
371         struct object_list *posn = trees;
372         struct tree_entry_list df_conflict_list;
373         struct cache_entry df_conflict_entry;
375         memset(&df_conflict_list, 0, sizeof(df_conflict_list));
376         df_conflict_list.next = &df_conflict_list;
377         memset(&state, 0, sizeof(state));
378         state.base_dir = "";
379         state.force = 1;
380         state.quiet = 1;
381         state.refresh_cache = 1;
383         o->merge_size = len;
384         memset(&df_conflict_entry, 0, sizeof(df_conflict_entry));
385         o->df_conflict_entry = &df_conflict_entry;
387         if (len) {
388                 posns = xmalloc(len * sizeof(struct tree_entry_list *));
389                 for (i = 0; i < len; i++) {
390                         posns[i] = create_tree_entry_list((struct tree *) posn->item);
391                         posn = posn->next;
392                 }
393                 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
394                                      o, &indpos, &df_conflict_list))
395                         return -1;
396         }
398         if (o->trivial_merges_only && o->nontrivial_merge)
399                 die("Merge requires file-level merging");
401         check_updates(active_cache, active_nr, o);
402         return 0;
405 /* Here come the merge functions */
407 static void reject_merge(struct cache_entry *ce)
409         die("Entry '%s' would be overwritten by merge. Cannot merge.",
410             ce->name);
413 static int same(struct cache_entry *a, struct cache_entry *b)
415         if (!!a != !!b)
416                 return 0;
417         if (!a && !b)
418                 return 1;
419         return a->ce_mode == b->ce_mode &&
420                !hashcmp(a->sha1, b->sha1);
424 /*
425  * When a CE gets turned into an unmerged entry, we
426  * want it to be up-to-date
427  */
428 static void verify_uptodate(struct cache_entry *ce,
429                 struct unpack_trees_options *o)
431         struct stat st;
433         if (o->index_only || o->reset)
434                 return;
436         if (!lstat(ce->name, &st)) {
437                 unsigned changed = ce_match_stat(ce, &st, 1);
438                 if (!changed)
439                         return;
440                 errno = 0;
441         }
442         if (o->reset) {
443                 ce->ce_flags |= htons(CE_UPDATE);
444                 return;
445         }
446         if (errno == ENOENT)
447                 return;
448         die("Entry '%s' not uptodate. Cannot merge.", ce->name);
451 static void invalidate_ce_path(struct cache_entry *ce)
453         if (ce)
454                 cache_tree_invalidate_path(active_cache_tree, ce->name);
457 /*
458  * We do not want to remove or overwrite a working tree file that
459  * is not tracked.
460  */
461 static void verify_absent(const char *path, const char *action,
462                 struct unpack_trees_options *o)
464         struct stat st;
466         if (o->index_only || o->reset || !o->update)
467                 return;
468         if (!lstat(path, &st))
469                 die("Untracked working tree file '%s' "
470                     "would be %s by merge.", path, action);
473 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
474                 struct unpack_trees_options *o)
476         merge->ce_flags |= htons(CE_UPDATE);
477         if (old) {
478                 /*
479                  * See if we can re-use the old CE directly?
480                  * That way we get the uptodate stat info.
481                  *
482                  * This also removes the UPDATE flag on
483                  * a match.
484                  */
485                 if (same(old, merge)) {
486                         *merge = *old;
487                 } else {
488                         verify_uptodate(old, o);
489                         invalidate_ce_path(old);
490                 }
491         }
492         else {
493                 verify_absent(merge->name, "overwritten", o);
494                 invalidate_ce_path(merge);
495         }
497         merge->ce_flags &= ~htons(CE_STAGEMASK);
498         add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
499         return 1;
502 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
503                 struct unpack_trees_options *o)
505         if (old)
506                 verify_uptodate(old, o);
507         else
508                 verify_absent(ce->name, "removed", o);
509         ce->ce_mode = 0;
510         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
511         invalidate_ce_path(ce);
512         return 1;
515 static int keep_entry(struct cache_entry *ce)
517         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
518         return 1;
521 #if DBRT_DEBUG
522 static void show_stage_entry(FILE *o,
523                              const char *label, const struct cache_entry *ce)
525         if (!ce)
526                 fprintf(o, "%s (missing)\n", label);
527         else
528                 fprintf(o, "%s%06o %s %d\t%s\n",
529                         label,
530                         ntohl(ce->ce_mode),
531                         sha1_to_hex(ce->sha1),
532                         ce_stage(ce),
533                         ce->name);
535 #endif
537 int threeway_merge(struct cache_entry **stages,
538                 struct unpack_trees_options *o)
540         struct cache_entry *index;
541         struct cache_entry *head;
542         struct cache_entry *remote = stages[o->head_idx + 1];
543         int count;
544         int head_match = 0;
545         int remote_match = 0;
546         const char *path = NULL;
548         int df_conflict_head = 0;
549         int df_conflict_remote = 0;
551         int any_anc_missing = 0;
552         int no_anc_exists = 1;
553         int i;
555         for (i = 1; i < o->head_idx; i++) {
556                 if (!stages[i])
557                         any_anc_missing = 1;
558                 else {
559                         if (!path)
560                                 path = stages[i]->name;
561                         no_anc_exists = 0;
562                 }
563         }
565         index = stages[0];
566         head = stages[o->head_idx];
568         if (head == o->df_conflict_entry) {
569                 df_conflict_head = 1;
570                 head = NULL;
571         }
573         if (remote == o->df_conflict_entry) {
574                 df_conflict_remote = 1;
575                 remote = NULL;
576         }
578         if (!path && index)
579                 path = index->name;
580         if (!path && head)
581                 path = head->name;
582         if (!path && remote)
583                 path = remote->name;
585         /* First, if there's a #16 situation, note that to prevent #13
586          * and #14.
587          */
588         if (!same(remote, head)) {
589                 for (i = 1; i < o->head_idx; i++) {
590                         if (same(stages[i], head)) {
591                                 head_match = i;
592                         }
593                         if (same(stages[i], remote)) {
594                                 remote_match = i;
595                         }
596                 }
597         }
599         /* We start with cases where the index is allowed to match
600          * something other than the head: #14(ALT) and #2ALT, where it
601          * is permitted to match the result instead.
602          */
603         /* #14, #14ALT, #2ALT */
604         if (remote && !df_conflict_head && head_match && !remote_match) {
605                 if (index && !same(index, remote) && !same(index, head))
606                         reject_merge(index);
607                 return merged_entry(remote, index, o);
608         }
609         /*
610          * If we have an entry in the index cache, then we want to
611          * make sure that it matches head.
612          */
613         if (index && !same(index, head)) {
614                 reject_merge(index);
615         }
617         if (head) {
618                 /* #5ALT, #15 */
619                 if (same(head, remote))
620                         return merged_entry(head, index, o);
621                 /* #13, #3ALT */
622                 if (!df_conflict_remote && remote_match && !head_match)
623                         return merged_entry(head, index, o);
624         }
626         /* #1 */
627         if (!head && !remote && any_anc_missing)
628                 return 0;
630         /* Under the new "aggressive" rule, we resolve mostly trivial
631          * cases that we historically had git-merge-one-file resolve.
632          */
633         if (o->aggressive) {
634                 int head_deleted = !head && !df_conflict_head;
635                 int remote_deleted = !remote && !df_conflict_remote;
636                 /*
637                  * Deleted in both.
638                  * Deleted in one and unchanged in the other.
639                  */
640                 if ((head_deleted && remote_deleted) ||
641                     (head_deleted && remote && remote_match) ||
642                     (remote_deleted && head && head_match)) {
643                         if (index)
644                                 return deleted_entry(index, index, o);
645                         else if (path && !head_deleted)
646                                 verify_absent(path, "removed", o);
647                         return 0;
648                 }
649                 /*
650                  * Added in both, identically.
651                  */
652                 if (no_anc_exists && head && remote && same(head, remote))
653                         return merged_entry(head, index, o);
655         }
657         /* Below are "no merge" cases, which require that the index be
658          * up-to-date to avoid the files getting overwritten with
659          * conflict resolution files.
660          */
661         if (index) {
662                 verify_uptodate(index, o);
663         }
665         o->nontrivial_merge = 1;
667         /* #2, #3, #4, #6, #7, #9, #11. */
668         count = 0;
669         if (!head_match || !remote_match) {
670                 for (i = 1; i < o->head_idx; i++) {
671                         if (stages[i]) {
672                                 keep_entry(stages[i]);
673                                 count++;
674                                 break;
675                         }
676                 }
677         }
678 #if DBRT_DEBUG
679         else {
680                 fprintf(stderr, "read-tree: warning #16 detected\n");
681                 show_stage_entry(stderr, "head   ", stages[head_match]);
682                 show_stage_entry(stderr, "remote ", stages[remote_match]);
683         }
684 #endif
685         if (head) { count += keep_entry(head); }
686         if (remote) { count += keep_entry(remote); }
687         return count;
690 /*
691  * Two-way merge.
692  *
693  * The rule is to "carry forward" what is in the index without losing
694  * information across a "fast forward", favoring a successful merge
695  * over a merge failure when it makes sense.  For details of the
696  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
697  *
698  */
699 int twoway_merge(struct cache_entry **src,
700                 struct unpack_trees_options *o)
702         struct cache_entry *current = src[0];
703         struct cache_entry *oldtree = src[1], *newtree = src[2];
705         if (o->merge_size != 2)
706                 return error("Cannot do a twoway merge of %d trees",
707                              o->merge_size);
709         if (current) {
710                 if ((!oldtree && !newtree) || /* 4 and 5 */
711                     (!oldtree && newtree &&
712                      same(current, newtree)) || /* 6 and 7 */
713                     (oldtree && newtree &&
714                      same(oldtree, newtree)) || /* 14 and 15 */
715                     (oldtree && newtree &&
716                      !same(oldtree, newtree) && /* 18 and 19*/
717                      same(current, newtree))) {
718                         return keep_entry(current);
719                 }
720                 else if (oldtree && !newtree && same(current, oldtree)) {
721                         /* 10 or 11 */
722                         return deleted_entry(oldtree, current, o);
723                 }
724                 else if (oldtree && newtree &&
725                          same(current, oldtree) && !same(current, newtree)) {
726                         /* 20 or 21 */
727                         return merged_entry(newtree, current, o);
728                 }
729                 else {
730                         /* all other failures */
731                         if (oldtree)
732                                 reject_merge(oldtree);
733                         if (current)
734                                 reject_merge(current);
735                         if (newtree)
736                                 reject_merge(newtree);
737                         return -1;
738                 }
739         }
740         else if (newtree)
741                 return merged_entry(newtree, current, o);
742         else
743                 return deleted_entry(oldtree, current, o);
746 /*
747  * Bind merge.
748  *
749  * Keep the index entries at stage0, collapse stage1 but make sure
750  * stage0 does not have anything there.
751  */
752 int bind_merge(struct cache_entry **src,
753                 struct unpack_trees_options *o)
755         struct cache_entry *old = src[0];
756         struct cache_entry *a = src[1];
758         if (o->merge_size != 1)
759                 return error("Cannot do a bind merge of %d trees\n",
760                              o->merge_size);
761         if (a && old)
762                 die("Entry '%s' overlaps.  Cannot bind.", a->name);
763         if (!a)
764                 return keep_entry(old);
765         else
766                 return merged_entry(a, NULL, o);
769 /*
770  * One-way merge.
771  *
772  * The rule is:
773  * - take the stat information from stage0, take the data from stage1
774  */
775 int oneway_merge(struct cache_entry **src,
776                 struct unpack_trees_options *o)
778         struct cache_entry *old = src[0];
779         struct cache_entry *a = src[1];
781         if (o->merge_size != 1)
782                 return error("Cannot do a oneway merge of %d trees",
783                              o->merge_size);
785         if (!a)
786                 return deleted_entry(old, old, o);
787         if (old && same(old, a)) {
788                 if (o->reset) {
789                         struct stat st;
790                         if (lstat(old->name, &st) ||
791                             ce_match_stat(old, &st, 1))
792                                 old->ce_flags |= htons(CE_UPDATE);
793                 }
794                 return keep_entry(old);
795         }
796         return merged_entry(a, old, o);