View Javadoc
1   /*
2    * Copyright (C) 2022, 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 java.nio.charset.StandardCharsets.UTF_8;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertNull;
16  import static org.junit.Assert.assertSame;
17  
18  import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
19  import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
20  import org.eclipse.jgit.junit.TestRepository;
21  import org.eclipse.jgit.lib.AnyObjectId;
22  import org.eclipse.jgit.lib.ObjectLoader;
23  import org.eclipse.jgit.revwalk.filter.RevFilter;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  public class RevCommitWithOverriddenParentTest {
28  	private TestRepository<InMemoryRepository> tr;
29  
30  	private RevWalk rw;
31  
32  	@Before
33  	public void setUp() throws Exception {
34  		tr = new TestRepository<>(
35  				new InMemoryRepository(new DfsRepositoryDescription("test")));
36  		rw = tr.getRevWalk();
37  	}
38  
39  	@Test
40  	public void testParseBody() throws Exception {
41  		RevCommit a = tr.commit().add("a", "foo").create();
42  		RevCommit b = tr.commit().parent(a).add("b", "bar").create();
43  		RevCommit c = tr.commit().parent(b).message("commit3").add("a", "foo'")
44  				.create();
45  
46  		RevCommit cBar = new RevCommit(c.getId()) {
47  			@Override
48  			public int getParentCount() {
49  				return 1;
50  			}
51  
52  			@Override
53  			public RevCommit getParent(int nth) {
54  				return a;
55  			}
56  
57  			@Override
58  			public RevCommit[] getParents() {
59  				return new RevCommit[] { a };
60  			}
61  		};
62  
63  		rw.parseBody(cBar);
64  		assertEquals(a, cBar.getParents()[0]);
65  		assertEquals("commit3", cBar.getFullMessage());
66  		assertEquals("foo'", blobAsString(cBar, "a"));
67  	}
68  
69  	@Test
70  	public void testParseHeader() throws Exception {
71  		RevCommit a = tr.commit().add("a", "foo").create();
72  		RevCommit b = tr.commit().parent(a).add("b", "bar").create();
73  		RevCommit c = tr.commit().parent(b).message("commit3").add("a", "foo'")
74  				.create();
75  
76  		RevCommit cBar = new RevCommit(c.getId()) {
77  			@Override
78  			public int getParentCount() {
79  				return 1;
80  			}
81  
82  			@Override
83  			public RevCommit getParent(int nth) {
84  				return a;
85  			}
86  
87  			@Override
88  			public RevCommit[] getParents() {
89  				return new RevCommit[] { a };
90  			}
91  		};
92  
93  		RevCommit parsed = rw.parseCommit(cBar.getId());
94  		rw.parseHeaders(cBar);
95  
96  		assertEquals(c.getId(), parsed.getId());
97  		assertEquals(parsed.getTree(), cBar.getTree());
98  		assertEquals(parsed.getCommitTime(), cBar.getCommitTime());
99  		assertEquals(parsed.getAuthorIdent(), cBar.getAuthorIdent());
100 	}
101 
102 	@Test
103 	public void testFilter() throws Exception {
104 		RevCommit a = tr.commit().add("a", "foo").create();
105 		RevCommit b = tr.commit().parent(a).add("b", "bar").create();
106 		RevCommit c = tr.commit().parent(b).message("commit3").add("a", "foo'")
107 				.create();
108 
109 		RevCommit cBar = new RevCommit(c.getId()) {
110 			@Override
111 			public int getParentCount() {
112 				return 1;
113 			}
114 
115 			@Override
116 			public RevCommit getParent(int nth) {
117 				return a;
118 			}
119 
120 			@Override
121 			public RevCommit[] getParents() {
122 				return new RevCommit[] { a };
123 			}
124 		};
125 
126 		rw.setRevFilter(RevFilter.ALL);
127 		rw.markStart(cBar);
128 		assertSame(cBar, rw.next());
129 		assertSame(a, rw.next());
130 		assertNull(rw.next());
131 	}
132 
133 	@Test
134 	public void testFlag() throws Exception {
135 		RevCommit root = tr.commit().add("todelete", "to be deleted").create();
136 		RevCommit orig = tr.commit().parent(root).rm("todelete")
137 				.add("foo", "foo contents").add("bar", "bar contents")
138 				.add("dir/baz", "baz contents").create();
139 
140 		RevCommit commitOrigBar = new RevCommit(orig.getId()) {
141 			@Override
142 			public int getParentCount() {
143 				return 1;
144 			}
145 
146 			@Override
147 			public RevCommit getParent(int nth) {
148 				return root;
149 			}
150 
151 			@Override
152 			public RevCommit[] getParents() {
153 				return new RevCommit[] { root };
154 			}
155 		};
156 
157 		assertEquals(RevObject.PARSED, orig.flags);
158 		assertEquals(0, commitOrigBar.flags);
159 		commitOrigBar.parseBody(rw);
160 		assertEquals(RevObject.PARSED, commitOrigBar.flags);
161 	}
162 
163 	private String blobAsString(AnyObjectId treeish, String path)
164 			throws Exception {
165 		RevObject obj = tr.get(rw.parseTree(treeish), path);
166 		assertSame(RevBlob.class, obj.getClass());
167 		ObjectLoader loader = rw.getObjectReader().open(obj);
168 		return new String(loader.getCachedBytes(), UTF_8);
169 	}
170 }