1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19
20 import org.eclipse.jgit.junit.RepositoryTestCase;
21 import org.eclipse.jgit.lib.PersonIdent;
22 import org.eclipse.jgit.lib.Ref;
23 import org.eclipse.jgit.merge.MergeStrategy;
24 import org.eclipse.jgit.revwalk.RevCommit;
25 import org.eclipse.jgit.revwalk.filter.RevFilter;
26 import org.junit.Test;
27
28 public class LogCommandTest extends RepositoryTestCase {
29
30 @Test
31 public void logAllCommits() throws Exception {
32 List<RevCommit> commits = new ArrayList<>();
33 Git git = Git.wrap(db);
34
35 writeTrashFile("Test.txt", "Hello world");
36 git.add().addFilepattern("Test.txt").call();
37 commits.add(git.commit().setMessage("initial commit").call());
38
39 git.branchCreate().setName("branch1").call();
40 Ref checkedOut = git.checkout().setName("branch1").call();
41 assertEquals("refs/heads/branch1", checkedOut.getName());
42 writeTrashFile("Test1.txt", "Hello world!");
43 git.add().addFilepattern("Test1.txt").call();
44 commits.add(git.commit().setMessage("branch1 commit").call());
45
46 checkedOut = git.checkout().setName("master").call();
47 assertEquals("refs/heads/master", checkedOut.getName());
48 writeTrashFile("Test2.txt", "Hello world!!");
49 git.add().addFilepattern("Test2.txt").call();
50 commits.add(git.commit().setMessage("branch1 commit").call());
51
52 Iterator<RevCommit> log = git.log().all().call().iterator();
53 assertTrue(log.hasNext());
54 assertTrue(commits.contains(log.next()));
55 assertTrue(log.hasNext());
56 assertTrue(commits.contains(log.next()));
57 assertTrue(log.hasNext());
58 assertTrue(commits.contains(log.next()));
59 assertFalse(log.hasNext());
60 }
61
62 @Test
63 public void logAllCommitsWithTag() throws Exception {
64 List<RevCommit> commits = new ArrayList<>();
65 Git git = Git.wrap(db);
66
67 writeTrashFile("Test.txt", "Hello world");
68 git.add().addFilepattern("Test.txt").call();
69 commits.add(git.commit().setMessage("initial commit").call());
70
71 TagCommand tagCmd = git.tag();
72 tagCmd.setName("tagcommit");
73 tagCmd.setObjectId(commits.get(0));
74 tagCmd.setTagger(new PersonIdent(db));
75 Ref tag = tagCmd.call();
76
77 tagCmd = git.tag();
78 tagCmd.setName("tagtree");
79 tagCmd.setObjectId(commits.get(0).getTree());
80 tagCmd.setTagger(new PersonIdent(db));
81 tagCmd.call();
82
83 Iterator<RevCommit> log = git.log().all().call().iterator();
84 assertTrue(log.hasNext());
85 RevCommit commit = log.next();
86 tag = db.getRefDatabase().peel(tag);
87
88 assertEquals(commit.getName(), tag.getPeeledObjectId().getName());
89 assertTrue(commits.contains(commit));
90 }
91
92 private List<RevCommit> createCommits(Git git) throws Exception {
93 List<RevCommit> commits = new ArrayList<>();
94 writeTrashFile("Test.txt", "Hello world");
95 git.add().addFilepattern("Test.txt").call();
96 commits.add(git.commit().setMessage("commit#1").call());
97 writeTrashFile("Test.txt", "Hello world!");
98 git.add().addFilepattern("Test.txt").call();
99 commits.add(git.commit().setMessage("commit#2").call());
100 writeTrashFile("Test1.txt", "Hello world!!");
101 git.add().addFilepattern("Test1.txt").call();
102 commits.add(git.commit().setMessage("commit#3").call());
103 return commits;
104 }
105
106 @Test
107 public void logAllCommitsWithMaxCount() throws Exception {
108 Git git = Git.wrap(db);
109 List<RevCommit> commits = createCommits(git);
110
111 Iterator<RevCommit> log = git.log().all().setMaxCount(2).call()
112 .iterator();
113 assertTrue(log.hasNext());
114 RevCommit commit = log.next();
115 assertTrue(commits.contains(commit));
116 assertEquals("commit#3", commit.getShortMessage());
117 assertTrue(log.hasNext());
118 commit = log.next();
119 assertTrue(commits.contains(commit));
120 assertEquals("commit#2", commit.getShortMessage());
121 assertFalse(log.hasNext());
122 }
123
124 @Test
125 public void logPathWithMaxCount() throws Exception {
126 Git git = Git.wrap(db);
127 List<RevCommit> commits = createCommits(git);
128
129 Iterator<RevCommit> log = git.log().addPath("Test.txt").setMaxCount(1)
130 .call().iterator();
131 assertTrue(log.hasNext());
132 RevCommit commit = log.next();
133 assertTrue(commits.contains(commit));
134 assertEquals("commit#2", commit.getShortMessage());
135 assertFalse(log.hasNext());
136 }
137
138 @Test
139 public void logPathWithSkip() throws Exception {
140 Git git = Git.wrap(db);
141 List<RevCommit> commits = createCommits(git);
142
143 Iterator<RevCommit> log = git.log().addPath("Test.txt").setSkip(1)
144 .call().iterator();
145 assertTrue(log.hasNext());
146 RevCommit commit = log.next();
147 assertTrue(commits.contains(commit));
148 assertEquals("commit#1", commit.getShortMessage());
149 assertFalse(log.hasNext());
150 }
151
152 @Test
153 public void logAllCommitsWithSkip() throws Exception {
154 Git git = Git.wrap(db);
155 List<RevCommit> commits = createCommits(git);
156
157 Iterator<RevCommit> log = git.log().all().setSkip(1).call().iterator();
158 assertTrue(log.hasNext());
159 RevCommit commit = log.next();
160 assertTrue(commits.contains(commit));
161 assertEquals("commit#2", commit.getShortMessage());
162 assertTrue(log.hasNext());
163 commit = log.next();
164 assertTrue(commits.contains(commit));
165 assertEquals("commit#1", commit.getShortMessage());
166 assertFalse(log.hasNext());
167 }
168
169 @Test
170 public void logAllCommitsWithSkipAndMaxCount() throws Exception {
171 Git git = Git.wrap(db);
172 List<RevCommit> commits = createCommits(git);
173
174 Iterator<RevCommit> log = git.log().all().setSkip(1).setMaxCount(1).call()
175 .iterator();
176 assertTrue(log.hasNext());
177 RevCommit commit = log.next();
178 assertTrue(commits.contains(commit));
179 assertEquals("commit#2", commit.getShortMessage());
180 assertFalse(log.hasNext());
181 }
182
183 @Test
184 public void logOnlyMergeCommits() throws Exception {
185 setCommitsAndMerge();
186 Git git = Git.wrap(db);
187
188 Iterable<RevCommit> commits = git.log().all().call();
189 Iterator<RevCommit> i = commits.iterator();
190 RevCommit commit = i.next();
191 assertEquals("merge s0 with m1", commit.getFullMessage());
192 commit = i.next();
193 assertEquals("s0", commit.getFullMessage());
194 commit = i.next();
195 assertEquals("m1", commit.getFullMessage());
196 commit = i.next();
197 assertEquals("m0", commit.getFullMessage());
198 assertFalse(i.hasNext());
199
200 commits = git.log().setRevFilter(RevFilter.ONLY_MERGES).call();
201 i = commits.iterator();
202 commit = i.next();
203 assertEquals("merge s0 with m1", commit.getFullMessage());
204 assertFalse(i.hasNext());
205 }
206
207 @Test
208 public void logNoMergeCommits() throws Exception {
209 setCommitsAndMerge();
210 Git git = Git.wrap(db);
211
212 Iterable<RevCommit> commits = git.log().all().call();
213 Iterator<RevCommit> i = commits.iterator();
214 RevCommit commit = i.next();
215 assertEquals("merge s0 with m1", commit.getFullMessage());
216 commit = i.next();
217 assertEquals("s0", commit.getFullMessage());
218 commit = i.next();
219 assertEquals("m1", commit.getFullMessage());
220 commit = i.next();
221 assertEquals("m0", commit.getFullMessage());
222 assertFalse(i.hasNext());
223
224 commits = git.log().setRevFilter(RevFilter.NO_MERGES).call();
225 i = commits.iterator();
226 commit = i.next();
227 assertEquals("m1", commit.getFullMessage());
228 commit = i.next();
229 assertEquals("s0", commit.getFullMessage());
230 commit = i.next();
231 assertEquals("m0", commit.getFullMessage());
232 assertFalse(i.hasNext());
233 }
234
235 private void setCommitsAndMerge() throws Exception {
236 Git git = Git.wrap(db);
237 writeTrashFile("file1", "1\n2\n3\n4\n");
238 git.add().addFilepattern("file1").call();
239 RevCommit masterCommit0 = git.commit().setMessage("m0").call();
240
241 createBranch(masterCommit0, "refs/heads/side");
242 checkoutBranch("refs/heads/side");
243
244 writeTrashFile("file2", "1\n2\n3\n4\n5\n6\n7\n8\n");
245 git.add().addFilepattern("file2").call();
246 RevCommit c = git.commit().setMessage("s0").call();
247
248 checkoutBranch("refs/heads/master");
249
250 writeTrashFile("file3", "1\n2\n");
251 git.add().addFilepattern("file3").call();
252 git.commit().setMessage("m1").call();
253
254 git.merge().include(c.getId())
255 .setStrategy(MergeStrategy.RESOLVE)
256 .setMessage("merge s0 with m1").call();
257 }
258
259 }