View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.merge.MergeStrategy;
57  import org.eclipse.jgit.revwalk.RevCommit;
58  import org.eclipse.jgit.revwalk.filter.RevFilter;
59  import org.junit.Test;
60  
61  public class LogCommandTest extends RepositoryTestCase {
62  
63  	@Test
64  	public void logAllCommits() throws Exception {
65  		List<RevCommit> commits = new ArrayList<>();
66  		Git git = Git.wrap(db);
67  
68  		writeTrashFile("Test.txt", "Hello world");
69  		git.add().addFilepattern("Test.txt").call();
70  		commits.add(git.commit().setMessage("initial commit").call());
71  
72  		git.branchCreate().setName("branch1").call();
73  		Ref checkedOut = git.checkout().setName("branch1").call();
74  		assertEquals("refs/heads/branch1", checkedOut.getName());
75  		writeTrashFile("Test1.txt", "Hello world!");
76  		git.add().addFilepattern("Test1.txt").call();
77  		commits.add(git.commit().setMessage("branch1 commit").call());
78  
79  		checkedOut = git.checkout().setName("master").call();
80  		assertEquals("refs/heads/master", checkedOut.getName());
81  		writeTrashFile("Test2.txt", "Hello world!!");
82  		git.add().addFilepattern("Test2.txt").call();
83  		commits.add(git.commit().setMessage("branch1 commit").call());
84  
85  		Iterator<RevCommit> log = git.log().all().call().iterator();
86  		assertTrue(log.hasNext());
87  		assertTrue(commits.contains(log.next()));
88  		assertTrue(log.hasNext());
89  		assertTrue(commits.contains(log.next()));
90  		assertTrue(log.hasNext());
91  		assertTrue(commits.contains(log.next()));
92  		assertFalse(log.hasNext());
93  	}
94  
95      @Test
96      public void logAllCommitsWithTag() throws Exception {
97  		List<RevCommit> commits = new ArrayList<>();
98  		Git git = Git.wrap(db);
99  
100 		writeTrashFile("Test.txt", "Hello world");
101 		git.add().addFilepattern("Test.txt").call();
102 		commits.add(git.commit().setMessage("initial commit").call());
103 
104 		TagCommand tagCmd = git.tag();
105 		tagCmd.setName("tagcommit");
106 		tagCmd.setObjectId(commits.get(0));
107 		tagCmd.setTagger(new PersonIdent(db));
108 		Ref tag = tagCmd.call();
109 
110 		tagCmd = git.tag();
111 		tagCmd.setName("tagtree");
112 		tagCmd.setObjectId(commits.get(0).getTree());
113 		tagCmd.setTagger(new PersonIdent(db));
114 		tagCmd.call();
115 
116 		Iterator<RevCommit> log = git.log().all().call().iterator();
117 		assertTrue(log.hasNext());
118 		RevCommit commit = log.next();
119 		tag = db.peel(tag);
120 
121 		assertEquals(commit.getName(), tag.getPeeledObjectId().getName());
122 		assertTrue(commits.contains(commit));
123 	}
124 
125 	private List<RevCommit> createCommits(Git git) throws Exception {
126 		List<RevCommit> commits = new ArrayList<>();
127 		writeTrashFile("Test.txt", "Hello world");
128 		git.add().addFilepattern("Test.txt").call();
129 		commits.add(git.commit().setMessage("commit#1").call());
130 		writeTrashFile("Test.txt", "Hello world!");
131 		git.add().addFilepattern("Test.txt").call();
132 		commits.add(git.commit().setMessage("commit#2").call());
133 		writeTrashFile("Test1.txt", "Hello world!!");
134 		git.add().addFilepattern("Test1.txt").call();
135 		commits.add(git.commit().setMessage("commit#3").call());
136 		return commits;
137 	}
138 
139 	@Test
140 	public void logAllCommitsWithMaxCount() throws Exception {
141 		Git git = Git.wrap(db);
142 		List<RevCommit> commits = createCommits(git);
143 
144 		Iterator<RevCommit> log = git.log().all().setMaxCount(2).call()
145 				.iterator();
146 		assertTrue(log.hasNext());
147 		RevCommit commit = log.next();
148 		assertTrue(commits.contains(commit));
149 		assertEquals("commit#3", commit.getShortMessage());
150 		assertTrue(log.hasNext());
151 		commit = log.next();
152 		assertTrue(commits.contains(commit));
153 		assertEquals("commit#2", commit.getShortMessage());
154 		assertFalse(log.hasNext());
155 	}
156 
157 	@Test
158 	public void logPathWithMaxCount() throws Exception {
159 		Git git = Git.wrap(db);
160 		List<RevCommit> commits = createCommits(git);
161 
162 		Iterator<RevCommit> log = git.log().addPath("Test.txt").setMaxCount(1)
163 				.call().iterator();
164 		assertTrue(log.hasNext());
165 		RevCommit commit = log.next();
166 		assertTrue(commits.contains(commit));
167 		assertEquals("commit#2", commit.getShortMessage());
168 		assertFalse(log.hasNext());
169 	}
170 
171 	@Test
172 	public void logPathWithSkip() throws Exception {
173 		Git git = Git.wrap(db);
174 		List<RevCommit> commits = createCommits(git);
175 
176 		Iterator<RevCommit> log = git.log().addPath("Test.txt").setSkip(1)
177 				.call().iterator();
178 		assertTrue(log.hasNext());
179 		RevCommit commit = log.next();
180 		assertTrue(commits.contains(commit));
181 		assertEquals("commit#1", commit.getShortMessage());
182 		assertFalse(log.hasNext());
183 	}
184 
185 	@Test
186 	public void logAllCommitsWithSkip() throws Exception {
187 		Git git = Git.wrap(db);
188 		List<RevCommit> commits = createCommits(git);
189 
190 		Iterator<RevCommit> log = git.log().all().setSkip(1).call().iterator();
191 		assertTrue(log.hasNext());
192 		RevCommit commit = log.next();
193 		assertTrue(commits.contains(commit));
194 		assertEquals("commit#2", commit.getShortMessage());
195 		assertTrue(log.hasNext());
196 		commit = log.next();
197 		assertTrue(commits.contains(commit));
198 		assertEquals("commit#1", commit.getShortMessage());
199 		assertFalse(log.hasNext());
200 	}
201 
202 	@Test
203 	public void logAllCommitsWithSkipAndMaxCount() throws Exception {
204 		Git git = Git.wrap(db);
205 		List<RevCommit> commits = createCommits(git);
206 
207 		Iterator<RevCommit> log = git.log().all().setSkip(1).setMaxCount(1).call()
208 				.iterator();
209 		assertTrue(log.hasNext());
210 		RevCommit commit = log.next();
211 		assertTrue(commits.contains(commit));
212 		assertEquals("commit#2", commit.getShortMessage());
213 		assertFalse(log.hasNext());
214 	}
215 
216 	@Test
217 	public void logOnlyMergeCommits() throws Exception {
218 		setCommitsAndMerge();
219 		Git git = Git.wrap(db);
220 
221 		Iterable<RevCommit> commits = git.log().all().call();
222 		Iterator<RevCommit> i = commits.iterator();
223 		RevCommit commit = i.next();
224 		assertEquals("merge s0 with m1", commit.getFullMessage());
225 		commit = i.next();
226 		assertEquals("s0", commit.getFullMessage());
227 		commit = i.next();
228 		assertEquals("m1", commit.getFullMessage());
229 		commit = i.next();
230 		assertEquals("m0", commit.getFullMessage());
231 		assertFalse(i.hasNext());
232 
233 		commits = git.log().setRevFilter(RevFilter.ONLY_MERGES).call();
234 		i = commits.iterator();
235 		commit = i.next();
236 		assertEquals("merge s0 with m1", commit.getFullMessage());
237 		assertFalse(i.hasNext());
238 	}
239 
240 	@Test
241 	public void logNoMergeCommits() throws Exception {
242 		setCommitsAndMerge();
243 		Git git = Git.wrap(db);
244 
245 		Iterable<RevCommit> commits = git.log().all().call();
246 		Iterator<RevCommit> i = commits.iterator();
247 		RevCommit commit = i.next();
248 		assertEquals("merge s0 with m1", commit.getFullMessage());
249 		commit = i.next();
250 		assertEquals("s0", commit.getFullMessage());
251 		commit = i.next();
252 		assertEquals("m1", commit.getFullMessage());
253 		commit = i.next();
254 		assertEquals("m0", commit.getFullMessage());
255 		assertFalse(i.hasNext());
256 
257 		commits = git.log().setRevFilter(RevFilter.NO_MERGES).call();
258 		i = commits.iterator();
259 		commit = i.next();
260 		assertEquals("m1", commit.getFullMessage());
261 		commit = i.next();
262 		assertEquals("s0", commit.getFullMessage());
263 		commit = i.next();
264 		assertEquals("m0", commit.getFullMessage());
265 		assertFalse(i.hasNext());
266 	}
267 
268 	private void setCommitsAndMerge() throws Exception {
269 		Git git = Git.wrap(db);
270 		writeTrashFile("file1", "1\n2\n3\n4\n");
271 		git.add().addFilepattern("file1").call();
272 		RevCommit masterCommit0 = git.commit().setMessage("m0").call();
273 
274 		createBranch(masterCommit0, "refs/heads/side");
275 		checkoutBranch("refs/heads/side");
276 
277 		writeTrashFile("file2", "1\n2\n3\n4\n5\n6\n7\n8\n");
278 		git.add().addFilepattern("file2").call();
279 		RevCommit c = git.commit().setMessage("s0").call();
280 
281 		checkoutBranch("refs/heads/master");
282 
283 		writeTrashFile("file3", "1\n2\n");
284 		git.add().addFilepattern("file3").call();
285 		git.commit().setMessage("m1").call();
286 
287 		git.merge().include(c.getId())
288 				.setStrategy(MergeStrategy.RESOLVE)
289 				.setMessage("merge s0 with m1").call();
290 	}
291 
292 }