1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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
93 assertEquals(c1, db.resolve("@{0}"));
94 try {
95 assertEquals(c1, db.resolve("@{1}"));
96 fail();
97 } catch (RevisionSyntaxException e) {
98 assertNotNull(e);
99 }
100
101
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
155 try {
156
157 db.resolve("@{-1}@{0}");
158 fail();
159 } catch (RevisionSyntaxException e) {
160
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 }