move get_merge_bases() to core lib.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Jun 29, 2006 at 15:17 UTC
7c6f8aaf6d795f0c9a75671acceb9754ea06fd81
3 files changed
+242
-238
commit.c
+240
@@ -846,3 +846,243 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
846
}
847
free(nodes);
848
}
849
+
850
+/* merge-rebase stuff */
851
+
852
+#define PARENT1 1
853
+#define PARENT2 2
854
+#define UNINTERESTING 4
855
+
856
+static struct commit *interesting(struct commit_list *list)
857
+{
858
+ while (list) {
859
+ struct commit *commit = list->item;
860
+ list = list->next;
861
+ if (commit->object.flags & UNINTERESTING)
862
+ continue;
863
+ return commit;
864
+ }
865
+ return NULL;
866
+}
867
+
868
+/*
869
+ * A pathological example of how this thing works.
870
+ *
871
+ * Suppose we had this commit graph, where chronologically
872
+ * the timestamp on the commit are A <= B <= C <= D <= E <= F
873
+ * and we are trying to figure out the merge base for E and F
874
+ * commits.
875
+ *
876
+ * F
877
+ * / \
878
+ * E A D
879
+ * \ / /
880
+ * B /
881
+ * \ /
882
+ * C
883
+ *
884
+ * First we push E and F to list to be processed. E gets bit 1
885
+ * and F gets bit 2. The list becomes:
886
+ *
887
+ * list=F(2) E(1), result=empty
888
+ *
889
+ * Then we pop F, the newest commit, from the list. Its flag is 2.
890
+ * We scan its parents, mark them reachable from the side that F is
891
+ * reachable from, and push them to the list:
892
+ *
893
+ * list=E(1) D(2) A(2), result=empty
894
+ *
895
+ * Next pop E and do the same.
896
+ *
897
+ * list=D(2) B(1) A(2), result=empty
898
+ *
899
+ * Next pop D and do the same.
900
+ *
901
+ * list=C(2) B(1) A(2), result=empty
902
+ *
903
+ * Next pop C and do the same.
904
+ *
905
+ * list=B(1) A(2), result=empty
906
+ *
907
+ * Now it is B's turn. We mark its parent, C, reachable from B's side,
908
+ * and push it to the list:
909
+ *
910
+ * list=C(3) A(2), result=empty
911
+ *
912
+ * Now pop C and notice it has flags==3. It is placed on the result list,
913
+ * and the list now contains:
914
+ *
915
+ * list=A(2), result=C(3)
916
+ *
917
+ * We pop A and do the same.
918
+ *
919
+ * list=B(3), result=C(3)
920
+ *
921
+ * Next, we pop B and something very interesting happens. It has flags==3
922
+ * so it is also placed on the result list, and its parents are marked
923
+ * uninteresting, retroactively, and placed back on the list:
924
+ *
925
+ * list=C(7), result=C(7) B(3)
926
+ *
927
+ * Now, list does not have any interesting commit. So we find the newest
928
+ * commit from the result list that is not marked uninteresting. Which is
929
+ * commit B.
930
+ *
931
+ *
932
+ * Another pathological example how this thing used to fail to mark an
933
+ * ancestor of a merge base as UNINTERESTING before we introduced the
934
+ * postprocessing phase (mark_reachable_commits).
935
+ *
936
+ * 2
937
+ * H
938
+ * 1 / \
939
+ * G A \
940
+ * |\ / \
941
+ * | B \
942
+ * | \ \
943
+ * \ C F
944
+ * \ \ /
945
+ * \ D /
946
+ * \ | /
947
+ * \| /
948
+ * E
949
+ *
950
+ * list A B C D E F G H
951
+ * G1 H2 - - - - - - 1 2
952
+ * H2 E1 B1 - 1 - - 1 - 1 2
953
+ * F2 E1 B1 A2 2 1 - - 1 2 1 2
954
+ * E3 B1 A2 2 1 - - 3 2 1 2
955
+ * B1 A2 2 1 - - 3 2 1 2
956
+ * C1 A2 2 1 1 - 3 2 1 2
957
+ * D1 A2 2 1 1 1 3 2 1 2
958
+ * A2 2 1 1 1 3 2 1 2
959
+ * B3 2 3 1 1 3 2 1 2
960
+ * C7 2 3 7 1 3 2 1 2
961
+ *
962
+ * At this point, unfortunately, everybody in the list is
963
+ * uninteresting, so we fail to complete the following two
964
+ * steps to fully marking uninteresting commits.
965
+ *
966
+ * D7 2 3 7 7 3 2 1 2
967
+ * E7 2 3 7 7 7 2 1 2
968
+ *
969
+ * and we ended up showing E as an interesting merge base.
970
+ * The postprocessing phase re-injects C and continues traversal
971
+ * to contaminate D and E.
972
+ */
973
+
974
+static void mark_reachable_commits(struct commit_list *result,
975
+ struct commit_list *list)
976
+{
977
+ struct commit_list *tmp;
978
+
979
+ /*
980
+ * Postprocess to fully contaminate the well.
981
+ */
982
+ for (tmp = result; tmp; tmp = tmp->next) {
983
+ struct commit *c = tmp->item;
984
+ /* Reinject uninteresting ones to list,
985
+ * so we can scan their parents.
986
+ */
987
+ if (c->object.flags & UNINTERESTING)
988
+ commit_list_insert(c, &list);
989
+ }
990
+ while (list) {
991
+ struct commit *c = list->item;
992
+ struct commit_list *parents;
993
+
994
+ tmp = list;
995
+ list = list->next;
996
+ free(tmp);
997
+
998
+ /* Anything taken out of the list is uninteresting, so
999
+ * mark all its parents uninteresting. We do not
1000
+ * parse new ones (we already parsed all the relevant
1001
+ * ones).
1002
+ */
1003
+ parents = c->parents;
1004
+ while (parents) {
1005
+ struct commit *p = parents->item;
1006
+ parents = parents->next;
1007
+ if (!(p->object.flags & UNINTERESTING)) {
1008
+ p->object.flags |= UNINTERESTING;
1009
+ commit_list_insert(p, &list);
1010
+ }
1011
+ }
1012
+ }
1013
+}
1014
+
1015
+struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2)
1016
+{
1017
+ struct commit_list *list = NULL;
1018
+ struct commit_list *result = NULL;
1019
+ struct commit_list *tmp = NULL;
1020
+
1021
+ if (rev1 == rev2)
1022
+ return commit_list_insert(rev1, &result);
1023
+
1024
+ parse_commit(rev1);
1025
+ parse_commit(rev2);
1026
+
1027
+ rev1->object.flags |= PARENT1;
1028
+ rev2->object.flags |= PARENT2;
1029
+ insert_by_date(rev1, &list);
1030
+ insert_by_date(rev2, &list);
1031
+
1032
+ while (interesting(list)) {
1033
+ struct commit *commit = list->item;
1034
+ struct commit_list *parents;
1035
+ int flags = commit->object.flags
1036
+ & (PARENT1 | PARENT2 | UNINTERESTING);
1037
+
1038
+ tmp = list;
1039
+ list = list->next;
1040
+ free(tmp);
1041
+ if (flags == (PARENT1 | PARENT2)) {
1042
+ insert_by_date(commit, &result);
1043
+
1044
+ /* Mark parents of a found merge uninteresting */
1045
+ flags |= UNINTERESTING;
1046
+ }
1047
+ parents = commit->parents;
1048
+ while (parents) {
1049
+ struct commit *p = parents->item;
1050
+ parents = parents->next;
1051
+ if ((p->object.flags & flags) == flags)
1052
+ continue;
1053
+ parse_commit(p);
1054
+ p->object.flags |= flags;
1055
+ insert_by_date(p, &list);
1056
+ }
1057
+ }
1058
+
1059
+ if (!result)
1060
+ return NULL;
1061
+
1062
+ if (result->next && list)
1063
+ mark_reachable_commits(result, list);
1064
+
1065
+ /* cull duplicates */
1066
+ for (tmp = result, list = NULL; tmp; ) {
1067
+ struct commit *commit = tmp->item;
1068
+ struct commit_list *next = tmp->next;
1069
+ if (commit->object.flags & UNINTERESTING) {
1070
+ if (list != NULL)
1071
+ list->next = next;
1072
+ free(tmp);
1073
+ } else {
1074
+ if (list == NULL)
1075
+ result = tmp;
1076
+ list = tmp;
1077
+ commit->object.flags |= UNINTERESTING;
1078
+ }
1079
+
1080
+ tmp = next;
1081
+ }
1082
+
1083
+ /* reset flags */
1084
+ clear_commit_marks(rev1, PARENT1 | PARENT2 | UNINTERESTING);
1085
+ clear_commit_marks(rev2, PARENT1 | PARENT2 | UNINTERESTING);
1086
+
1087
+ return result;
1088
+}
commit.h
+2
@@ -105,4 +105,6 @@ struct commit_graft *read_graft_line(char *buf, int len);
105
int register_commit_graft(struct commit_graft *, int);
106
int read_graft_file(const char *graft_file);
107
108
+extern struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2);
109
+
110
#endif /* COMMIT_H */
merge-base.c
-238
@@ -2,244 +2,6 @@
2
#include "cache.h"
3
#include "commit.h"
4
5
-#define PARENT1 1
6
-#define PARENT2 2
7
-#define UNINTERESTING 4
8
-
9
-static struct commit *interesting(struct commit_list *list)
10
-{
11
- while (list) {
12
- struct commit *commit = list->item;
13
- list = list->next;
14
- if (commit->object.flags & UNINTERESTING)
15
- continue;
16
- return commit;
17
- }
18
- return NULL;
19
-}
20
-
21
-/*
22
- * A pathological example of how this thing works.
23
- *
24
- * Suppose we had this commit graph, where chronologically
25
- * the timestamp on the commit are A <= B <= C <= D <= E <= F
26
- * and we are trying to figure out the merge base for E and F
27
- * commits.
28
- *
29
- * F
30
- * / \
31
- * E A D
32
- * \ / /
33
- * B /
34
- * \ /
35
- * C
36
- *
37
- * First we push E and F to list to be processed. E gets bit 1
38
- * and F gets bit 2. The list becomes:
39
- *
40
- * list=F(2) E(1), result=empty
41
- *
42
- * Then we pop F, the newest commit, from the list. Its flag is 2.
43
- * We scan its parents, mark them reachable from the side that F is
44
- * reachable from, and push them to the list:
45
- *
46
- * list=E(1) D(2) A(2), result=empty
47
- *
48
- * Next pop E and do the same.
49
- *
50
- * list=D(2) B(1) A(2), result=empty
51
- *
52
- * Next pop D and do the same.
53
- *
54
- * list=C(2) B(1) A(2), result=empty
55
- *
56
- * Next pop C and do the same.
57
- *
58
- * list=B(1) A(2), result=empty
59
- *
60
- * Now it is B's turn. We mark its parent, C, reachable from B's side,
61
- * and push it to the list:
62
- *
63
- * list=C(3) A(2), result=empty
64
- *
65
- * Now pop C and notice it has flags==3. It is placed on the result list,
66
- * and the list now contains:
67
- *
68
- * list=A(2), result=C(3)
69
- *
70
- * We pop A and do the same.
71
- *
72
- * list=B(3), result=C(3)
73
- *
74
- * Next, we pop B and something very interesting happens. It has flags==3
75
- * so it is also placed on the result list, and its parents are marked
76
- * uninteresting, retroactively, and placed back on the list:
77
- *
78
- * list=C(7), result=C(7) B(3)
79
- *
80
- * Now, list does not have any interesting commit. So we find the newest
81
- * commit from the result list that is not marked uninteresting. Which is
82
- * commit B.
83
- *
84
- *
85
- * Another pathological example how this thing used to fail to mark an
86
- * ancestor of a merge base as UNINTERESTING before we introduced the
87
- * postprocessing phase (mark_reachable_commits).
88
- *
89
- * 2
90
- * H
91
- * 1 / \
92
- * G A \
93
- * |\ / \
94
- * | B \
95
- * | \ \
96
- * \ C F
97
- * \ \ /
98
- * \ D /
99
- * \ | /
100
- * \| /
101
- * E
102
- *
103
- * list A B C D E F G H
104
- * G1 H2 - - - - - - 1 2
105
- * H2 E1 B1 - 1 - - 1 - 1 2
106
- * F2 E1 B1 A2 2 1 - - 1 2 1 2
107
- * E3 B1 A2 2 1 - - 3 2 1 2
108
- * B1 A2 2 1 - - 3 2 1 2
109
- * C1 A2 2 1 1 - 3 2 1 2
110
- * D1 A2 2 1 1 1 3 2 1 2
111
- * A2 2 1 1 1 3 2 1 2
112
- * B3 2 3 1 1 3 2 1 2
113
- * C7 2 3 7 1 3 2 1 2
114
- *
115
- * At this point, unfortunately, everybody in the list is
116
- * uninteresting, so we fail to complete the following two
117
- * steps to fully marking uninteresting commits.
118
- *
119
- * D7 2 3 7 7 3 2 1 2
120
- * E7 2 3 7 7 7 2 1 2
121
- *
122
- * and we ended up showing E as an interesting merge base.
123
- * The postprocessing phase re-injects C and continues traversal
124
- * to contaminate D and E.
125
- */
126
-
127
-static void mark_reachable_commits(struct commit_list *result,
128
- struct commit_list *list)
129
-{
130
- struct commit_list *tmp;
131
-
132
- /*
133
- * Postprocess to fully contaminate the well.
134
- */
135
- for (tmp = result; tmp; tmp = tmp->next) {
136
- struct commit *c = tmp->item;
137
- /* Reinject uninteresting ones to list,
138
- * so we can scan their parents.
139
- */
140
- if (c->object.flags & UNINTERESTING)
141
- commit_list_insert(c, &list);
142
- }
143
- while (list) {
144
- struct commit *c = list->item;
145
- struct commit_list *parents;
146
-
147
- tmp = list;
148
- list = list->next;
149
- free(tmp);
150
-
151
- /* Anything taken out of the list is uninteresting, so
152
- * mark all its parents uninteresting. We do not
153
- * parse new ones (we already parsed all the relevant
154
- * ones).
155
- */
156
- parents = c->parents;
157
- while (parents) {
158
- struct commit *p = parents->item;
159
- parents = parents->next;
160
- if (!(p->object.flags & UNINTERESTING)) {
161
- p->object.flags |= UNINTERESTING;
162
- commit_list_insert(p, &list);
163
- }
164
- }
165
- }
166
-}
167
-
168
-struct commit_list *get_merge_bases(struct commit *rev1, struct commit *rev2)
169
-{
170
- struct commit_list *list = NULL;
171
- struct commit_list *result = NULL;
172
- struct commit_list *tmp = NULL;
173
-
174
- if (rev1 == rev2)
175
- return commit_list_insert(rev1, &result);
176
-
177
- parse_commit(rev1);
178
- parse_commit(rev2);
179
-
180
- rev1->object.flags |= PARENT1;
181
- rev2->object.flags |= PARENT2;
182
- insert_by_date(rev1, &list);
183
- insert_by_date(rev2, &list);
184
-
185
- while (interesting(list)) {
186
- struct commit *commit = list->item;
187
- struct commit_list *parents;
188
- int flags = commit->object.flags
189
- & (PARENT1 | PARENT2 | UNINTERESTING);
190
-
191
- tmp = list;
192
- list = list->next;
193
- free(tmp);
194
- if (flags == (PARENT1 | PARENT2)) {
195
- insert_by_date(commit, &result);
196
-
197
- /* Mark parents of a found merge uninteresting */
198
- flags |= UNINTERESTING;
199
- }
200
- parents = commit->parents;
201
- while (parents) {
202
- struct commit *p = parents->item;
203
- parents = parents->next;
204
- if ((p->object.flags & flags) == flags)
205
- continue;
206
- parse_commit(p);
207
- p->object.flags |= flags;
208
- insert_by_date(p, &list);
209
- }
210
- }
211
-
212
- if (!result)
213
- return NULL;
214
-
215
- if (result->next && list)
216
- mark_reachable_commits(result, list);
217
-
218
- /* cull duplicates */
219
- for (tmp = result, list = NULL; tmp; ) {
220
- struct commit *commit = tmp->item;
221
- struct commit_list *next = tmp->next;
222
- if (commit->object.flags & UNINTERESTING) {
223
- if (list != NULL)
224
- list->next = next;
225
- free(tmp);
226
- } else {
227
- if (list == NULL)
228
- result = tmp;
229
- list = tmp;
230
- commit->object.flags |= UNINTERESTING;
231
- }
232
-
233
- tmp = next;
234
- }
235
-
236
- /* reset flags */
237
- clear_commit_marks(rev1, PARENT1 | PARENT2 | UNINTERESTING);
238
- clear_commit_marks(rev2, PARENT1 | PARENT2 | UNINTERESTING);
239
-
240
- return result;
241
-}
242
-
5
static int show_all = 0;
6
7
static int merge_base(struct commit *rev1, struct commit *rev2)