@matheusbsbs / matheus-bagliano / commits / f324943816
Fork 0

merge-base: update the clean-up postprocessing

This removes the "contaminate the well even more" approach taken in the current merge-base postprosessing code. Instead, when there are more than one merge-base results, we compute the merge-base between them and see if one is a fast-forward of the other, in which case the ancestor is removed from the result. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jul 4, 2006 at 18:46 UTC f324943816789fa047441f66edbe11669dc8a9ab
1 file changed +90 -190
commit.c
+90 -190
@@ -854,6 +854,7 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
854 #define PARENT1 (1u<< 8)
855 #define PARENT2 (1u<< 9)
856 #define STALE (1u<<10)
857 +#define RESULT (1u<<11)
858
859 static struct commit *interesting(struct commit_list *list)
860 {
@@ -867,183 +868,42 @@ static struct commit *interesting(struct commit_list *list)
868 return NULL;
869 }
870
870 -/*
871 - * A pathological example of how this thing works.
872 - *
873 - * Suppose we had this commit graph, where chronologically
874 - * the timestamp on the commit are A <= B <= C <= D <= E <= F
875 - * and we are trying to figure out the merge base for E and F
876 - * commits.
877 - *
878 - * F
879 - * / \
880 - * E A D
881 - * \ / /
882 - * B /
883 - * \ /
884 - * C
885 - *
886 - * First we push E and F to list to be processed. E gets bit 1
887 - * and F gets bit 2. The list becomes:
888 - *
889 - * list=F(2) E(1), result=empty
890 - *
891 - * Then we pop F, the newest commit, from the list. Its flag is 2.
892 - * We scan its parents, mark them reachable from the side that F is
893 - * reachable from, and push them to the list:
894 - *
895 - * list=E(1) D(2) A(2), result=empty
896 - *
897 - * Next pop E and do the same.
898 - *
899 - * list=D(2) B(1) A(2), result=empty
900 - *
901 - * Next pop D and do the same.
902 - *
903 - * list=C(2) B(1) A(2), result=empty
904 - *
905 - * Next pop C and do the same.
906 - *
907 - * list=B(1) A(2), result=empty
908 - *
909 - * Now it is B's turn. We mark its parent, C, reachable from B's side,
910 - * and push it to the list:
911 - *
912 - * list=C(3) A(2), result=empty
913 - *
914 - * Now pop C and notice it has flags==3. It is placed on the result list,
915 - * and the list now contains:
916 - *
917 - * list=A(2), result=C(3)
918 - *
919 - * We pop A and do the same.
920 - *
921 - * list=B(3), result=C(3)
922 - *
923 - * Next, we pop B and something very interesting happens. It has flags==3
924 - * so it is also placed on the result list, and its parents are marked
925 - * stale, retroactively, and placed back on the list:
926 - *
927 - * list=C(7), result=C(7) B(3)
928 - *
929 - * Now, list does not have any interesting commit. So we find the newest
930 - * commit from the result list that is not marked stale. Which is
931 - * commit B.
932 - *
933 - *
934 - * Another pathological example how this thing used to fail to mark an
935 - * ancestor of a merge base as STALE before we introduced the
936 - * postprocessing phase (mark_reachable_commits).
937 - *
938 - * 2
939 - * H
940 - * 1 / \
941 - * G A \
942 - * |\ / \
943 - * | B \
944 - * | \ \
945 - * \ C F
946 - * \ \ /
947 - * \ D /
948 - * \ | /
949 - * \| /
950 - * E
951 - *
952 - * list A B C D E F G H
953 - * G1 H2 - - - - - - 1 2
954 - * H2 E1 B1 - 1 - - 1 - 1 2
955 - * F2 E1 B1 A2 2 1 - - 1 2 1 2
956 - * E3 B1 A2 2 1 - - 3 2 1 2
957 - * B1 A2 2 1 - - 3 2 1 2
958 - * C1 A2 2 1 1 - 3 2 1 2
959 - * D1 A2 2 1 1 1 3 2 1 2
960 - * A2 2 1 1 1 3 2 1 2
961 - * B3 2 3 1 1 3 2 1 2
962 - * C7 2 3 7 1 3 2 1 2
963 - *
964 - * At this point, unfortunately, everybody in the list is
965 - * stale, so we fail to complete the following two
966 - * steps to fully marking stale commits.
967 - *
968 - * D7 2 3 7 7 3 2 1 2
969 - * E7 2 3 7 7 7 2 1 2
970 - *
971 - * and we ended up showing E as an interesting merge base.
972 - * The postprocessing phase re-injects C and continues traversal
973 - * to contaminate D and E.
974 - */
975 -
976 -static void mark_reachable_commits(struct commit_list *result,
977 - struct commit_list *list)
978 -{
979 - struct commit_list *tmp;
980 -
981 - /*
982 - * Postprocess to fully contaminate the well.
983 - */
984 - for (tmp = result; tmp; tmp = tmp->next) {
985 - struct commit *c = tmp->item;
986 - /* Reinject stale ones to list,
987 - * so we can scan their parents.
988 - */
989 - if (c->object.flags & STALE)
990 - commit_list_insert(c, &list);
991 - }
992 - while (list) {
993 - struct commit *c = list->item;
994 - struct commit_list *parents;
995 -
996 - tmp = list;
997 - list = list->next;
998 - free(tmp);
999 -
1000 - /* Anything taken out of the list is stale, so
1001 - * mark all its parents stale. We do not
1002 - * parse new ones (we already parsed all the relevant
1003 - * ones).
1004 - */
1005 - parents = c->parents;
1006 - while (parents) {
1007 - struct commit *p = parents->item;
1008 - parents = parents->next;
1009 - if (!(p->object.flags & STALE)) {
1010 - p->object.flags |= STALE;
1011 - commit_list_insert(p, &list);
1012 - }
1013 - }
1014 - }
1015 -}
1016 -
1017 -struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2,
1018 - int cleanup)
871 +static struct commit_list *merge_bases(struct commit *one, struct commit *two)
872 {
873 struct commit_list *list = NULL;
874 struct commit_list *result = NULL;
1022 - struct commit_list *tmp = NULL;
875
1024 - if (rev1 == rev2)
1025 - return commit_list_insert(rev1, &result);
876 + if (one == two)
877 + /* We do not mark this even with RESULT so we do not
878 + * have to clean it up.
879 + */
880 + return commit_list_insert(one, &result);
881
1027 - parse_commit(rev1);
1028 - parse_commit(rev2);
882 + parse_commit(one);
883 + parse_commit(two);
884
1030 - rev1->object.flags |= PARENT1;
1031 - rev2->object.flags |= PARENT2;
1032 - insert_by_date(rev1, &list);
1033 - insert_by_date(rev2, &list);
885 + one->object.flags |= PARENT1;
886 + two->object.flags |= PARENT2;
887 + insert_by_date(one, &list);
888 + insert_by_date(two, &list);
889
890 while (interesting(list)) {
1036 - struct commit *commit = list->item;
891 + struct commit *commit;
892 struct commit_list *parents;
1038 - int flags = commit->object.flags
1039 - & (PARENT1 | PARENT2 | STALE);
893 + struct commit_list *n;
894 + int flags;
895
1041 - tmp = list;
1042 - list = list->next;
1043 - free(tmp);
1044 - if (flags == (PARENT1 | PARENT2)) {
1045 - insert_by_date(commit, &result);
896 + commit = list->item;
897 + n = list->next;
898 + free(list);
899 + list = n;
900
901 + flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
902 + if (flags == (PARENT1 | PARENT2)) {
903 + if (!(commit->object.flags & RESULT)) {
904 + commit->object.flags |= RESULT;
905 + insert_by_date(commit, &result);
906 + }
907 /* Mark parents of a found merge stale */
908 flags |= STALE;
909 }
@@ -1059,35 +919,75 @@ struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2,
919 }
920 }
921
1062 - if (!result)
1063 - goto finish;
1064 -
1065 - if (result->next && list)
1066 - mark_reachable_commits(result, list);
922 + /* Clean up the result to remove stale ones */
923 + list = result; result = NULL;
924 + while (list) {
925 + struct commit_list *n = list->next;
926 + if (!(list->item->object.flags & STALE))
927 + insert_by_date(list->item, &result);
928 + free(list);
929 + list = n;
930 + }
931 + return result;
932 +}
933
1068 - /* cull duplicates */
1069 - for (tmp = result, list = NULL; tmp; ) {
1070 - struct commit *commit = tmp->item;
1071 - struct commit_list *next = tmp->next;
1072 - if (commit->object.flags & STALE) {
1073 - if (list != NULL)
1074 - list->next = next;
1075 - free(tmp);
1076 - } else {
1077 - if (list == NULL)
1078 - result = tmp;
1079 - list = tmp;
1080 - commit->object.flags |= STALE;
934 +struct commit_list *get_merge_bases(struct commit *one,
935 + struct commit *two,
936 + int cleanup)
937 +{
938 + const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
939 + struct commit_list *list;
940 + struct commit **rslt;
941 + struct commit_list *result;
942 + int cnt, i, j;
943 +
944 + result = merge_bases(one, two);
945 + if (one == two)
946 + return result;
947 + if (!result || !result->next) {
948 + if (cleanup) {
949 + clear_commit_marks(one, all_flags);
950 + clear_commit_marks(two, all_flags);
951 }
1082 -
1083 - tmp = next;
952 + return result;
953 }
954
1086 - finish:
1087 - if (cleanup) {
1088 - clear_commit_marks(rev1, PARENT1 | PARENT2 | STALE);
1089 - clear_commit_marks(rev2, PARENT1 | PARENT2 | STALE);
955 + /* There are more than one */
956 + cnt = 0;
957 + list = result;
958 + while (list) {
959 + list = list->next;
960 + cnt++;
961 + }
962 + rslt = xcalloc(cnt, sizeof(*rslt));
963 + for (list = result, i = 0; list; list = list->next)
964 + rslt[i++] = list->item;
965 + free_commit_list(result);
966 +
967 + clear_commit_marks(one, all_flags);
968 + clear_commit_marks(two, all_flags);
969 + for (i = 0; i < cnt - 1; i++) {
970 + for (j = i+1; j < cnt; j++) {
971 + if (!rslt[i] || !rslt[j])
972 + continue;
973 + result = merge_bases(rslt[i], rslt[j]);
974 + clear_commit_marks(rslt[i], all_flags);
975 + clear_commit_marks(rslt[j], all_flags);
976 + for (list = result; list; list = list->next) {
977 + if (rslt[i] == list->item)
978 + rslt[i] = NULL;
979 + if (rslt[j] == list->item)
980 + rslt[j] = NULL;
981 + }
982 + }
983 }
984
985 + /* Surviving ones in rslt[] are the independent results */
986 + result = NULL;
987 + for (i = 0; i < cnt; i++) {
988 + if (rslt[i])
989 + insert_by_date(rslt[i], &result);
990 + }
991 + free(rslt);
992 return result;
993 }