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 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
92 assertEquals(c1, db.resolve("@{0}"));
93 try {
94 assertEquals(c1, db.resolve("@{1}"));
95 fail();
96 } catch (RevisionSyntaxException e) {
97 assertNotNull(e);
98 }
99
100
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
151 try {
152
153 db.resolve("@{-1}@{0}");
154 fail();
155 } catch (RevisionSyntaxException e) {
156
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 }