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  		try (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  
76  	@Test
77  	public void resolveUnnamedCurrentBranchCommits() throws Exception {
78  		try (Git git = new Git(db)) {
79  			writeTrashFile("file.txt", "content");
80  			git.add().addFilepattern("file.txt").call();
81  			RevCommit c1 = git.commit().setMessage("create file").call();
82  			writeTrashFile("file.txt", "content2");
83  			git.add().addFilepattern("file.txt").call();
84  			RevCommit c2 = git.commit().setMessage("edit file").call();
85  
86  			assertEquals(c2, db.resolve("master@{0}"));
87  			assertEquals(c1, db.resolve("master@{1}"));
88  
89  			git.checkout().setCreateBranch(true).setName("newbranch")
90  					.setStartPoint(c1).call();
91  
92  			// same as current branch, e.g. master
93  			assertEquals(c1, db.resolve("@{0}"));
94  			try {
95  				assertEquals(c1, db.resolve("@{1}"));
96  				fail(); // Looking at wrong ref, e.g HEAD
97  			} catch (RevisionSyntaxException e) {
98  				assertNotNull(e);
99  			}
100 
101 			// detached head, read HEAD reflog
102 			git.checkout().setName(c2.getName()).call();
103 			assertEquals(c2, db.resolve("@{0}"));
104 			assertEquals(c1, db.resolve("@{1}"));
105 			assertEquals(c2, db.resolve("@{2}"));
106 		}
107 	}
108 
109 	@Test
110 	public void resolveReflogParent() throws Exception {
111 		try (Git git = new Git(db)) {
112 			writeTrashFile("file.txt", "content");
113 			git.add().addFilepattern("file.txt").call();
114 			RevCommit c1 = git.commit().setMessage("create file").call();
115 			writeTrashFile("file.txt", "content2");
116 			git.add().addFilepattern("file.txt").call();
117 			git.commit().setMessage("edit file").call();
118 
119 			assertEquals(c1, db.resolve("master@{0}~1"));
120 		}
121 	}
122 
123 	@Test
124 	public void resolveNonExistingBranch() throws Exception {
125 		try (Git git = new Git(db)) {
126 			writeTrashFile("file.txt", "content");
127 			git.add().addFilepattern("file.txt").call();
128 			git.commit().setMessage("create file").call();
129 			assertNull(db.resolve("notabranch@{7}"));
130 		}
131 	}
132 
133 	@Test
134 	public void resolvePreviousBranch() throws Exception {
135 		try (Git git = new Git(db)) {
136 			writeTrashFile("file.txt", "content");
137 			git.add().addFilepattern("file.txt").call();
138 			RevCommit c1 = git.commit().setMessage("create file").call();
139 			writeTrashFile("file.txt", "content2");
140 			git.add().addFilepattern("file.txt").call();
141 			RevCommit c2 = git.commit().setMessage("edit file").call();
142 
143 			git.checkout().setCreateBranch(true).setName("newbranch")
144 					.setStartPoint(c1).call();
145 
146 			git.checkout().setName(c1.getName()).call();
147 
148 			git.checkout().setName("master").call();
149 
150 			assertEquals(c1.getName(), db.simplify("@{-1}"));
151 			assertEquals("newbranch", db.simplify("@{-2}"));
152 			assertEquals("master", db.simplify("@{-3}"));
153 
154 			// chained expression
155 			try {
156 				// Cannot refer to reflog of detached head
157 				db.resolve("@{-1}@{0}");
158 				fail();
159 			} catch (RevisionSyntaxException e) {
160 				// good
161 			}
162 			assertEquals(c1.getName(), db.resolve("@{-2}@{0}").getName());
163 
164 			assertEquals(c2.getName(), db.resolve("@{-3}@{0}").getName());
165 		}
166 	}
167 
168 	@Test
169 	public void resolveDate() throws Exception {
170 		try (Git git = new Git(db)) {
171 			writeTrashFile("file.txt", "content");
172 			git.add().addFilepattern("file.txt").call();
173 			git.commit().setMessage("create file").call();
174 			try {
175 				db.resolve("master@{yesterday}");
176 				fail("Exception not thrown");
177 			} catch (RevisionSyntaxException e) {
178 				assertNotNull(e);
179 			}
180 		}
181 	}
182 }