View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc. 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  
11  package org.eclipse.jgit.revwalk;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import java.lang.reflect.Field;
17  import java.util.Collections;
18  import java.util.HashMap;
19  
20  import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
21  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
22  import org.eclipse.jgit.treewalk.filter.TreeFilter;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  // Note: Much of this test case is broken as it depends upon
27  // the graph applying topological sorting *before* doing merge
28  // simplification.  It also depends upon a difference between
29  // full history and non-full history for a path, something we
30  // don't quite yet have a distiction for in JGit.
31  //
32  public class RevWalkPathFilter6012Test extends RevWalkTestCase {
33  	private static final String pA = "pA", pF = "pF", pE = "pE";
34  
35  	private RevCommit a, b, c, d, e, f, g, h, i;
36  
37  	private HashMap<RevCommit, String> byName;
38  
39  	@Override
40  	@Before
41  	public void setUp() throws Exception {
42  		super.setUp();
43  
44  		// Test graph was stolen from git-core t6012-rev-list-simplify
45  		// (by Junio C Hamano in 65347030590bcc251a9ff2ed96487a0f1b9e9fa8)
46  		//
47  		final RevBlob zF = blob("zF");
48  		final RevBlob zH = blob("zH");
49  		final RevBlob zI = blob("zI");
50  		final RevBlob zS = blob("zS");
51  		final RevBlob zY = blob("zY");
52  
53  		a = commit(tree(file(pF, zH)));
54  		b = commit(tree(file(pF, zI)), a);
55  		c = commit(tree(file(pF, zI)), a);
56  		d = commit(tree(file(pA, zS), file(pF, zI)), c);
57  		parseBody(d);
58  		e = commit(d.getTree(), d, b);
59  		f = commit(tree(file(pA, zS), file(pE, zY), file(pF, zI)), e);
60  		parseBody(f);
61  		g = commit(tree(file(pE, zY), file(pF, zI)), b);
62  		h = commit(f.getTree(), g, f);
63  		i = commit(tree(file(pA, zS), file(pE, zY), file(pF, zF)), h);
64  
65  		byName = new HashMap<>();
66  		for (Field z : RevWalkPathFilter6012Test.class.getDeclaredFields()) {
67  			if (z.getType() == RevCommit.class)
68  				byName.put((RevCommit) z.get(this), z.getName());
69  		}
70  	}
71  
72  	protected void check(RevCommit... order) throws Exception {
73  		markStart(i);
74  		final StringBuilder act = new StringBuilder();
75  		for (RevCommit z : rw) {
76  			final String name = byName.get(z);
77  			assertNotNull(name);
78  			act.append(name);
79  			act.append(' ');
80  		}
81  		final StringBuilder exp = new StringBuilder();
82  		for (RevCommit z : order) {
83  			final String name = byName.get(z);
84  			assertNotNull(name);
85  			exp.append(name);
86  			exp.append(' ');
87  		}
88  		assertEquals(exp.toString(), act.toString());
89  	}
90  
91  	protected void filter(String path) {
92  		rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup
93  				.createFromStrings(Collections.singleton(path)),
94  				TreeFilter.ANY_DIFF));
95  	}
96  
97  	@Test
98  	public void test1() throws Exception {
99  		// TODO --full-history
100 		check(i, h, g, f, e, d, c, b, a);
101 	}
102 
103 	@Test
104 	public void test2() throws Exception {
105 		// TODO --full-history
106 		filter(pF);
107 		// TODO fix broken test
108 		// check(i, h, e, c, b, a);
109 	}
110 
111 	@Test
112 	public void test3() throws Exception {
113 		// TODO --full-history
114 		rw.sort(RevSort.TOPO);
115 		filter(pF);
116 		// TODO fix broken test
117 		// check(i, h, e, c, b, a);
118 	}
119 
120 	@Test
121 	public void test4() throws Exception {
122 		// TODO --full-history
123 		rw.sort(RevSort.COMMIT_TIME_DESC);
124 		filter(pF);
125 		// TODO fix broken test
126 		// check(i, h, e, c, b, a);
127 	}
128 
129 	@Test
130 	public void test5() throws Exception {
131 		// TODO --simplify-merges
132 		filter(pF);
133 		// TODO fix broken test
134 		// check(i, e, c, b, a);
135 	}
136 
137 	@Test
138 	public void test6() throws Exception {
139 		filter(pF);
140 		check(i, b, a);
141 	}
142 
143 	@Test
144 	public void test7() throws Exception {
145 		rw.sort(RevSort.TOPO);
146 		filter(pF);
147 		check(i, b, a);
148 	}
149 }