View Javadoc
1   /*
2    * Copyright (C) 2014, Sven Selberg <sven.selberg@sonymobile.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.revwalk;
11  
12  import static org.junit.Assert.assertTrue;
13  
14  import java.util.List;
15  import java.util.stream.Collectors;
16  import org.eclipse.jgit.lib.Ref;
17  import org.junit.Test;
18  
19  public class RevWalkMergedIntoTest extends RevWalkTestCase {
20  
21  	@Test
22  	public void testOldCommitWalk() throws Exception {
23  		/*
24  		 * Sometimes a merge is performed on a machine with faulty time.
25  		 * This makes the traversal of the graph, when trying to find out if B
26  		 * is merged into T, complex since the algorithm uses the time stamps
27  		 * of commits to find the best route.
28  		 * When for example O(ld) has a very old time stamp compared to one of the
29  		 * commits (N(ew)) on the upper route between T and F(alse base), the route
30  		 * to False is deemed the better option even though the alternate route leeds
31  		 * to B(ase) which was the commit we were after.
32  		 *
33  		 *             o---o---o---o---N
34  		 *            /                 \
35  		 *           /   o---o---o---O---T
36  		 *          /   /
37  		 *      ---F---B
38  		 *
39  		 * This test is asserting that isMergedInto(B, T) returns true even
40  		 * under those circumstances.
41  		 */
42  		final int threeDaysInSecs = 3 * 24 * 60 * 60;
43  		final RevCommit f = commit();
44  		final RevCommit b = commit(f);
45  		final RevCommit o = commit(-threeDaysInSecs, commit(commit(commit(b))));
46  		final RevCommit n = commit(commit(commit(commit(commit(f)))));
47  		final RevCommit t = commit(n, o);
48  		assertTrue(rw.isMergedInto(b, t));
49  	}
50  
51  	@Test
52  	public void testGetMergedInto() throws Exception {
53  		/*
54  		 *          i
55  		 *         / \
56  		 *        A   o
57  		 *       / \   \
58  		 *      o1  o2  E
59  		 *     / \ / \
60  		 *    B   C   D
61  		 */
62  		String b = "refs/heads/b";
63  		String c = "refs/heads/c";
64  		String d = "refs/heads/d";
65  		String e = "refs/heads/e";
66  		final RevCommit i = commit();
67  		final RevCommit a = commit(i);
68  		final RevCommit o1 = commit(a);
69  		final RevCommit o2 = commit(a);
70  		createBranch(commit(o1), b);
71  		createBranch(commit(o1, o2), c);
72  		createBranch(commit(o2), d);
73  		createBranch(commit(commit(i)), e);
74  
75  		List<String>  modifiedResult = rw.getMergedInto(a, getRefs())
76  				.stream().map(Ref::getName).collect(Collectors.toList());
77  
78  		assertTrue(modifiedResult.size() == 3);
79  		assertTrue(modifiedResult.contains(b));
80  		assertTrue(modifiedResult.contains(c));
81  		assertTrue(modifiedResult.contains(d));
82  	}
83  
84  	@Test
85  	public void testIsMergedIntoAny() throws Exception {
86  		/*
87  		 *          i
88  		 *         / \
89  		 *        A   o
90  		 *       /     \
91  		 *      o       C
92  		 *     /
93  		 *    B
94  		 */
95  		String b = "refs/heads/b";
96  		String c = "refs/heads/c";
97  		final RevCommit i = commit();
98  		final RevCommit a = commit(i);
99  		createBranch(commit(commit(a)), b);
100 		createBranch(commit(commit(i)), c);
101 
102 		assertTrue(rw.isMergedIntoAny(a, getRefs()));
103 	}
104 
105 	@Test
106 	public void testIsMergedIntoAll() throws Exception {
107 		/*
108 		 *
109 		 *        A
110 		 *       / \
111 		 *      o1  o2
112 		 *     / \ / \
113 		 *    B   C   D
114 		 */
115 
116 		String b = "refs/heads/b";
117 		String c = "refs/heads/c";
118 		String d = "refs/heads/c";
119 		final RevCommit a = commit();
120 		final RevCommit o1 = commit(a);
121 		final RevCommit o2 = commit(a);
122 		createBranch(commit(o1), b);
123 		createBranch(commit(o1, o2), c);
124 		createBranch(commit(o2), d);
125 
126 		assertTrue(rw.isMergedIntoAll(a, getRefs()));
127 	}
128 
129 	@Test
130 	public void testMergeIntoAnnotatedTag() throws Exception {
131 		/*
132 		 *        a
133 		 *        |
134 		 *        b
135 		 *       / \
136 		 *      c  v1 (annotated tag)
137 		 */
138 		String c = "refs/heads/c";
139 		String v1 = "refs/tags/v1";
140 		final RevCommit a = commit();
141 		final RevCommit b = commit(a);
142 		createBranch(commit(b), c);
143 		createBranch(tag("v1", b), v1);
144 
145 		assertTrue(rw.isMergedIntoAll(a, getRefs()));
146 	}
147 }