View Javadoc
1   /*
2    * Copyright (C) 2013, Google 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  
44  package org.eclipse.jgit.api;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.util.Map;
50  
51  import org.eclipse.jgit.junit.RepositoryTestCase;
52  import org.eclipse.jgit.junit.TestRepository;
53  import org.eclipse.jgit.lib.ObjectId;
54  import org.eclipse.jgit.lib.Repository;
55  import org.eclipse.jgit.revwalk.RevCommit;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  public class NameRevCommandTest extends RepositoryTestCase {
60  	private TestRepository<Repository> tr;
61  	private Git git;
62  
63  	@Override
64  	@Before
65  	public void setUp() throws Exception {
66  		super.setUp();
67  		tr = new TestRepository<>(db);
68  		git = new Git(db);
69  	}
70  
71  	@Test
72  	public void nameExact() throws Exception {
73  		RevCommit c = tr.commit().create();
74  		tr.update("master", c);
75  		assertOneResult("master", c);
76  	}
77  
78  	@Test
79  	public void prefix() throws Exception {
80  		RevCommit c = tr.commit().create();
81  		tr.update("refs/heads/master", c);
82  		tr.update("refs/tags/tag", c);
83  		assertOneResult("master", c);
84  		assertOneResult("master",
85  				git.nameRev().addPrefix("refs/heads/").addPrefix("refs/tags/"),
86  				c);
87  		assertOneResult("tag",
88  				git.nameRev().addPrefix("refs/tags/").addPrefix("refs/heads/"),
89  				c);
90  	}
91  
92  	@Test
93  	public void ref() throws Exception {
94  		RevCommit c = tr.commit().create();
95  		tr.update("refs/heads/master", c);
96  		tr.update("refs/tags/tag", c);
97  		assertOneResult("master",
98  				git.nameRev().addRef(db.exactRef("refs/heads/master")), c);
99  		assertOneResult("tag",
100 				git.nameRev().addRef(db.exactRef("refs/tags/tag")), c);
101 	}
102 
103 	@Test
104 	public void annotatedTags() throws Exception {
105 		RevCommit c = tr.commit().create();
106 		tr.update("refs/heads/master", c);
107 		tr.update("refs/tags/tag1", c);
108 		tr.update("refs/tags/tag2", tr.tag("tag2", c));
109 		assertOneResult("tag2", git.nameRev().addAnnotatedTags(), c);
110 	}
111 
112 	@Test
113 	public void annotatedTagsNoResult() throws Exception {
114 		RevCommit c = tr.commit().create();
115 		tr.update("refs/heads/master", c);
116 		tr.update("refs/tags/tag1", c);
117 		tr.update("refs/tags/tag2", c);
118 		Map<ObjectId, String> result = git.nameRev()
119 				.add(c)
120 				.addAnnotatedTags()
121 				.call();
122 		assertTrue(result.toString(), result.isEmpty());
123 	}
124 
125 	@Test
126 	public void simpleAncestor() throws Exception {
127 		// 0--1--2
128 		RevCommit c0 = tr.commit().create();
129 		RevCommit c1 = tr.commit().parent(c0).create();
130 		RevCommit c2 = tr.commit().parent(c1).create();
131 		tr.update("master", c2);
132 		Map<ObjectId, String> result = git.nameRev().add(c0).add(c1).add(c2).call();
133 		assertEquals(3, result.size());
134 		assertEquals("master~2", result.get(c0));
135 		assertEquals("master~1", result.get(c1));
136 		assertEquals("master", result.get(c2));
137 	}
138 
139 	@Test
140 	public void multiplePathsNoMerge() throws Exception {
141 		// 0--1    <- master
142 		//  \-2--3 <- branch
143 		RevCommit c0 = tr.commit().create();
144 		RevCommit c1 = tr.commit().parent(c0).create();
145 		RevCommit c2 = tr.commit().parent(c0).create();
146 		RevCommit c3 = tr.commit().parent(c2).create();
147 		tr.update("master", c1);
148 		tr.update("branch", c3);
149 		assertOneResult("master~1", c0);
150 	}
151 
152 	@Test
153 	public void onePathMerge() throws Exception {
154 		// 0--1--3
155 		//  \-2-/
156 		RevCommit c0 = tr.commit().create();
157 		RevCommit c1 = tr.commit().parent(c0).create();
158 		RevCommit c2 = tr.commit().parent(c0).create();
159 		RevCommit c3 = tr.commit().parent(c1).parent(c2).create();
160 		tr.update("master", c3);
161 		assertOneResult("master~2", c0);
162 	}
163 
164 	@Test
165 	public void onePathMergeSecondParent() throws Exception {
166 		// 0--1-----4
167 		//  \-2--3-/
168 		RevCommit c0 = tr.commit().create();
169 		RevCommit c1 = tr.commit().parent(c0).create();
170 		RevCommit c2 = tr.commit().parent(c0).create();
171 		RevCommit c3 = tr.commit().parent(c2).create();
172 		RevCommit c4 = tr.commit().parent(c1).parent(c3).create();
173 		tr.update("master", c4);
174 		assertOneResult("master^2", c3);
175 		assertOneResult("master^2~1", c2);
176 	}
177 
178 	@Test
179 	public void onePathMergeLongerFirstParentPath() throws Exception {
180 		// 0--1--2--4
181 		//  \--3---/
182 		RevCommit c0 = tr.commit().create();
183 		RevCommit c1 = tr.commit().parent(c0).create();
184 		RevCommit c2 = tr.commit().parent(c1).create();
185 		RevCommit c3 = tr.commit().parent(c0).create();
186 		RevCommit c4 = tr.commit().parent(c2).parent(c3).create();
187 		tr.update("master", c4);
188 		assertOneResult("master^2", c3);
189 		assertOneResult("master~3", c0);
190 	}
191 
192 	@Test
193 	public void multiplePathsSecondParent() throws Exception {
194 		// 0--...--2
195 		//  \--1--/
196 		RevCommit c0 = tr.commit().create();
197 		RevCommit c1 = tr.commit().parent(c0).create();
198 		RevCommit c = c0;
199 		int mergeCost = 5;
200 		for (int i = 0; i < mergeCost; i++) {
201 			c = tr.commit().parent(c).create();
202 		}
203 		RevCommit c2 = tr.commit().parent(c).parent(c1).create();
204 		tr.update("master", c2);
205 		assertOneResult("master^2~1", git.nameRev().setMergeCost(mergeCost), c0);
206 	}
207 
208 	private static void assertOneResult(String expected, NameRevCommand nameRev,
209 			ObjectId id) throws Exception {
210 		Map<ObjectId, String> result = nameRev.add(id).call();
211 		assertEquals(1, result.size());
212 		assertEquals(expected, result.get(id));
213 	}
214 
215 	private void assertOneResult(String expected, ObjectId id) throws Exception {
216 		assertOneResult(expected, git.nameRev(), id);
217 	}
218 }