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.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 }