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 package org.eclipse.jgit.api;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48
49 import java.util.ArrayList;
50 import java.util.Iterator;
51 import java.util.List;
52
53 import org.eclipse.jgit.junit.RepositoryTestCase;
54 import org.eclipse.jgit.lib.PersonIdent;
55 import org.eclipse.jgit.lib.Ref;
56 import org.eclipse.jgit.revwalk.RevCommit;
57 import org.junit.Test;
58
59 public class LogCommandTest extends RepositoryTestCase {
60
61 @Test
62 public void logAllCommits() throws Exception {
63 List<RevCommit> commits = new ArrayList<RevCommit>();
64 Git git = Git.wrap(db);
65
66 writeTrashFile("Test.txt", "Hello world");
67 git.add().addFilepattern("Test.txt").call();
68 commits.add(git.commit().setMessage("initial commit").call());
69
70 git.branchCreate().setName("branch1").call();
71 Ref checkedOut = git.checkout().setName("branch1").call();
72 assertEquals("refs/heads/branch1", checkedOut.getName());
73 writeTrashFile("Test1.txt", "Hello world!");
74 git.add().addFilepattern("Test1.txt").call();
75 commits.add(git.commit().setMessage("branch1 commit").call());
76
77 checkedOut = git.checkout().setName("master").call();
78 assertEquals("refs/heads/master", checkedOut.getName());
79 writeTrashFile("Test2.txt", "Hello world!!");
80 git.add().addFilepattern("Test2.txt").call();
81 commits.add(git.commit().setMessage("branch1 commit").call());
82
83 Iterator<RevCommit> log = git.log().all().call().iterator();
84 assertTrue(log.hasNext());
85 assertTrue(commits.contains(log.next()));
86 assertTrue(log.hasNext());
87 assertTrue(commits.contains(log.next()));
88 assertTrue(log.hasNext());
89 assertTrue(commits.contains(log.next()));
90 assertFalse(log.hasNext());
91 }
92
93 @Test
94 public void logAllCommitsWithTag() throws Exception {
95 List<RevCommit> commits = new ArrayList<RevCommit>();
96 Git git = Git.wrap(db);
97
98 writeTrashFile("Test.txt", "Hello world");
99 git.add().addFilepattern("Test.txt").call();
100 commits.add(git.commit().setMessage("initial commit").call());
101
102 TagCommand tagCmd = git.tag();
103 tagCmd.setName("tagcommit");
104 tagCmd.setObjectId(commits.get(0));
105 tagCmd.setTagger(new PersonIdent(db));
106 Ref tag = tagCmd.call();
107
108 tagCmd = git.tag();
109 tagCmd.setName("tagtree");
110 tagCmd.setObjectId(commits.get(0).getTree());
111 tagCmd.setTagger(new PersonIdent(db));
112 tagCmd.call();
113
114 Iterator<RevCommit> log = git.log().all().call().iterator();
115 assertTrue(log.hasNext());
116 RevCommit commit = log.next();
117 tag = db.peel(tag);
118
119 assertEquals(commit.getName(), tag.getPeeledObjectId().getName());
120 assertTrue(commits.contains(commit));
121 }
122
123 private List<RevCommit> createCommits(Git git) throws Exception {
124 List<RevCommit> commits = new ArrayList<RevCommit>();
125 writeTrashFile("Test.txt", "Hello world");
126 git.add().addFilepattern("Test.txt").call();
127 commits.add(git.commit().setMessage("commit#1").call());
128 writeTrashFile("Test.txt", "Hello world!");
129 git.add().addFilepattern("Test.txt").call();
130 commits.add(git.commit().setMessage("commit#2").call());
131 writeTrashFile("Test1.txt", "Hello world!!");
132 git.add().addFilepattern("Test1.txt").call();
133 commits.add(git.commit().setMessage("commit#3").call());
134 return commits;
135 }
136
137 @Test
138 public void logAllCommitsWithMaxCount() throws Exception {
139 Git git = Git.wrap(db);
140 List<RevCommit> commits = createCommits(git);
141
142 Iterator<RevCommit> log = git.log().all().setMaxCount(2).call()
143 .iterator();
144 assertTrue(log.hasNext());
145 RevCommit commit = log.next();
146 assertTrue(commits.contains(commit));
147 assertEquals("commit#3", commit.getShortMessage());
148 assertTrue(log.hasNext());
149 commit = log.next();
150 assertTrue(commits.contains(commit));
151 assertEquals("commit#2", commit.getShortMessage());
152 assertFalse(log.hasNext());
153 }
154
155 @Test
156 public void logPathWithMaxCount() throws Exception {
157 Git git = Git.wrap(db);
158 List<RevCommit> commits = createCommits(git);
159
160 Iterator<RevCommit> log = git.log().addPath("Test.txt").setMaxCount(1)
161 .call().iterator();
162 assertTrue(log.hasNext());
163 RevCommit commit = log.next();
164 assertTrue(commits.contains(commit));
165 assertEquals("commit#2", commit.getShortMessage());
166 assertFalse(log.hasNext());
167 }
168
169 @Test
170 public void logPathWithSkip() throws Exception {
171 Git git = Git.wrap(db);
172 List<RevCommit> commits = createCommits(git);
173
174 Iterator<RevCommit> log = git.log().addPath("Test.txt").setSkip(1)
175 .call().iterator();
176 assertTrue(log.hasNext());
177 RevCommit commit = log.next();
178 assertTrue(commits.contains(commit));
179 assertEquals("commit#1", commit.getShortMessage());
180 assertFalse(log.hasNext());
181 }
182
183 @Test
184 public void logAllCommitsWithSkip() throws Exception {
185 Git git = Git.wrap(db);
186 List<RevCommit> commits = createCommits(git);
187
188 Iterator<RevCommit> log = git.log().all().setSkip(1).call().iterator();
189 assertTrue(log.hasNext());
190 RevCommit commit = log.next();
191 assertTrue(commits.contains(commit));
192 assertEquals("commit#2", commit.getShortMessage());
193 assertTrue(log.hasNext());
194 commit = log.next();
195 assertTrue(commits.contains(commit));
196 assertEquals("commit#1", commit.getShortMessage());
197 assertFalse(log.hasNext());
198 }
199
200 @Test
201 public void logAllCommitsWithSkipAndMaxCount() throws Exception {
202 Git git = Git.wrap(db);
203 List<RevCommit> commits = createCommits(git);
204
205 Iterator<RevCommit> log = git.log().all().setSkip(1).setMaxCount(1).call()
206 .iterator();
207 assertTrue(log.hasNext());
208 RevCommit commit = log.next();
209 assertTrue(commits.contains(commit));
210 assertEquals("commit#2", commit.getShortMessage());
211 assertFalse(log.hasNext());
212 }
213 }