1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.lib;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.fail;
16
17 import org.eclipse.jgit.api.Git;
18 import org.eclipse.jgit.errors.RevisionSyntaxException;
19 import org.eclipse.jgit.junit.RepositoryTestCase;
20 import org.eclipse.jgit.revwalk.RevCommit;
21 import org.junit.Test;
22
23
24
25
26 public class ReflogResolveTest extends RepositoryTestCase {
27
28 @Test
29 public void resolveMasterCommits() throws Exception {
30 try (Git git = new Git(db)) {
31 writeTrashFile("file.txt", "content");
32 git.add().addFilepattern("file.txt").call();
33 RevCommit c1 = git.commit().setMessage("create file").call();
34 writeTrashFile("file.txt", "content2");
35 git.add().addFilepattern("file.txt").call();
36 RevCommit c2 = git.commit().setMessage("edit file").call();
37
38 assertEquals(c2, db.resolve("master@{0}"));
39 assertEquals(c1, db.resolve("master@{1}"));
40 }
41 }
42
43 @Test
44 public void resolveUnnamedCurrentBranchCommits() throws Exception {
45 try (Git git = new Git(db)) {
46 writeTrashFile("file.txt", "content");
47 git.add().addFilepattern("file.txt").call();
48 RevCommit c1 = git.commit().setMessage("create file").call();
49 writeTrashFile("file.txt", "content2");
50 git.add().addFilepattern("file.txt").call();
51 RevCommit c2 = git.commit().setMessage("edit file").call();
52
53 assertEquals(c2, db.resolve("master@{0}"));
54 assertEquals(c1, db.resolve("master@{1}"));
55
56 git.checkout().setCreateBranch(true).setName("newbranch")
57 .setStartPoint(c1).call();
58
59
60 assertEquals(c1, db.resolve("@{0}"));
61 try {
62 assertEquals(c1, db.resolve("@{1}"));
63 fail();
64 } catch (RevisionSyntaxException e) {
65 assertNotNull(e);
66 }
67
68
69 git.checkout().setName(c2.getName()).call();
70 assertEquals(c2, db.resolve("@{0}"));
71 assertEquals(c1, db.resolve("@{1}"));
72 assertEquals(c2, db.resolve("@{2}"));
73 }
74 }
75
76 @Test
77 public void resolveReflogParent() 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 git.commit().setMessage("edit file").call();
85
86 assertEquals(c1, db.resolve("master@{0}~1"));
87 }
88 }
89
90 @Test
91 public void resolveNonExistingBranch() throws Exception {
92 try (Git git = new Git(db)) {
93 writeTrashFile("file.txt", "content");
94 git.add().addFilepattern("file.txt").call();
95 git.commit().setMessage("create file").call();
96 assertNull(db.resolve("notabranch@{7}"));
97 }
98 }
99
100 @Test
101 public void resolvePreviousBranch() throws Exception {
102 try (Git git = new Git(db)) {
103 writeTrashFile("file.txt", "content");
104 git.add().addFilepattern("file.txt").call();
105 RevCommit c1 = git.commit().setMessage("create file").call();
106 writeTrashFile("file.txt", "content2");
107 git.add().addFilepattern("file.txt").call();
108 RevCommit c2 = git.commit().setMessage("edit file").call();
109
110 git.checkout().setCreateBranch(true).setName("newbranch")
111 .setStartPoint(c1).call();
112
113 git.checkout().setName(c1.getName()).call();
114
115 git.checkout().setName("master").call();
116
117 assertEquals(c1.getName(), db.simplify("@{-1}"));
118 assertEquals("newbranch", db.simplify("@{-2}"));
119 assertEquals("master", db.simplify("@{-3}"));
120
121
122 try {
123
124 db.resolve("@{-1}@{0}");
125 fail();
126 } catch (RevisionSyntaxException e) {
127
128 }
129 assertEquals(c1.getName(), db.resolve("@{-2}@{0}").getName());
130
131 assertEquals(c2.getName(), db.resolve("@{-3}@{0}").getName());
132 }
133 }
134
135 @Test
136 public void resolveDate() throws Exception {
137 try (Git git = new Git(db)) {
138 writeTrashFile("file.txt", "content");
139 git.add().addFilepattern("file.txt").call();
140 git.commit().setMessage("create file").call();
141 try {
142 db.resolve("master@{yesterday}");
143 fail("Exception not thrown");
144 } catch (RevisionSyntaxException e) {
145 assertNotNull(e);
146 }
147 }
148 }
149 }