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.lib;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.assertNull;
48  import static org.junit.Assert.fail;
49  
50  import org.eclipse.jgit.api.Git;
51  import org.eclipse.jgit.errors.RevisionSyntaxException;
52  import org.eclipse.jgit.junit.RepositoryTestCase;
53  import org.eclipse.jgit.revwalk.RevCommit;
54  import org.junit.Test;
55  
56  /**
57   * Unit tests for resolving reflog-based revisions
58   */
59  public class ReflogResolveTest extends RepositoryTestCase {
60  
61  	@Test
62  	public void resolveMasterCommits() throws Exception {
63  		Git git = new Git(db);
64  		writeTrashFile("file.txt", "content");
65  		git.add().addFilepattern("file.txt").call();
66  		RevCommit c1 = git.commit().setMessage("create file").call();
67  		writeTrashFile("file.txt", "content2");
68  		git.add().addFilepattern("file.txt").call();
69  		RevCommit c2 = git.commit().setMessage("edit file").call();
70  
71  		assertEquals(c2, db.resolve("master@{0}"));
72  		assertEquals(c1, db.resolve("master@{1}"));
73  	}
74  
75  	@Test
76  	public void resolveUnnamedCurrentBranchCommits() throws Exception {
77  		Git git = new Git(db);
78  		writeTrashFile("file.txt", "content");
79  		git.add().addFilepattern("file.txt").call();
80  		RevCommit c1 = git.commit().setMessage("create file").call();
81  		writeTrashFile("file.txt", "content2");
82  		git.add().addFilepattern("file.txt").call();
83  		RevCommit c2 = git.commit().setMessage("edit file").call();
84  
85  		assertEquals(c2, db.resolve("master@{0}"));
86  		assertEquals(c1, db.resolve("master@{1}"));
87  
88  		git.checkout().setCreateBranch(true).setName("newbranch")
89  				.setStartPoint(c1).call();
90  
91  		// same as current branch, e.g. master
92  		assertEquals(c1, db.resolve("@{0}"));
93  		try {
94  			assertEquals(c1, db.resolve("@{1}"));
95  			fail(); // Looking at wrong ref, e.g HEAD
96  		} catch (RevisionSyntaxException e) {
97  			assertNotNull(e);
98  		}
99  
100 		// detached head, read HEAD reflog
101 		git.checkout().setName(c2.getName()).call();
102 		assertEquals(c2, db.resolve("@{0}"));
103 		assertEquals(c1, db.resolve("@{1}"));
104 		assertEquals(c2, db.resolve("@{2}"));
105 	}
106 
107 	@Test
108 	public void resolveReflogParent() throws Exception {
109 		Git git = new Git(db);
110 		writeTrashFile("file.txt", "content");
111 		git.add().addFilepattern("file.txt").call();
112 		RevCommit c1 = git.commit().setMessage("create file").call();
113 		writeTrashFile("file.txt", "content2");
114 		git.add().addFilepattern("file.txt").call();
115 		git.commit().setMessage("edit file").call();
116 
117 		assertEquals(c1, db.resolve("master@{0}~1"));
118 	}
119 
120 	@Test
121 	public void resolveNonExistingBranch() throws Exception {
122 		Git git = new Git(db);
123 		writeTrashFile("file.txt", "content");
124 		git.add().addFilepattern("file.txt").call();
125 		git.commit().setMessage("create file").call();
126 		assertNull(db.resolve("notabranch@{7}"));
127 	}
128 
129 	@Test
130 	public void resolvePreviousBranch() throws Exception {
131 		Git git = new Git(db);
132 		writeTrashFile("file.txt", "content");
133 		git.add().addFilepattern("file.txt").call();
134 		RevCommit c1 = git.commit().setMessage("create file").call();
135 		writeTrashFile("file.txt", "content2");
136 		git.add().addFilepattern("file.txt").call();
137 		RevCommit c2 = git.commit().setMessage("edit file").call();
138 
139 		git.checkout().setCreateBranch(true).setName("newbranch")
140 				.setStartPoint(c1).call();
141 
142 		git.checkout().setName(c1.getName()).call();
143 
144 		git.checkout().setName("master").call();
145 
146 		assertEquals(c1.getName(), db.simplify("@{-1}"));
147 		assertEquals("newbranch", db.simplify("@{-2}"));
148 		assertEquals("master", db.simplify("@{-3}"));
149 
150 		// chained expression
151 		try {
152 			// Cannot refer to reflog of detached head
153 			db.resolve("@{-1}@{0}");
154 			fail();
155 		} catch (RevisionSyntaxException e) {
156 			// good
157 		}
158 		assertEquals(c1.getName(), db.resolve("@{-2}@{0}").getName());
159 
160 		assertEquals(c2.getName(), db.resolve("@{-3}@{0}").getName());
161 	}
162 
163 	@Test
164 	public void resolveDate() throws Exception {
165 		Git git = new Git(db);
166 		writeTrashFile("file.txt", "content");
167 		git.add().addFilepattern("file.txt").call();
168 		git.commit().setMessage("create file").call();
169 		try {
170 			db.resolve("master@{yesterday}");
171 			fail("Exception not thrown");
172 		} catch (RevisionSyntaxException e) {
173 			assertNotNull(e);
174 		}
175 	}
176 }