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