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.pgm;
44
45 import static org.junit.Assert.assertArrayEquals;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertTrue;
49
50 import org.eclipse.jgit.api.Git;
51 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
52 import org.eclipse.jgit.revwalk.RevCommit;
53 import org.junit.Before;
54 import org.junit.Ignore;
55 import org.junit.Test;
56
57 public class ResetTest extends CLIRepositoryTestCase {
58
59 private Git git;
60
61 @Override
62 @Before
63 public void setUp() throws Exception {
64 super.setUp();
65 git = new Git(db);
66 }
67
68 @Test
69 public void testPathOptionHelp() throws Exception {
70 String[] result = execute("git reset -h");
71 assertTrue("Unexpected argument: " + result[1],
72 result[1].endsWith("[-- path ...]"));
73 }
74
75 @Test
76 public void testZombieArgument_Bug484951() throws Exception {
77 String[] result = execute("git reset -h");
78 assertFalse("Unexpected argument: " + result[0],
79 result[0].contains("[VAL ...]"));
80 }
81
82 @Test
83 public void testResetSelf() throws Exception {
84 RevCommit commit = git.commit().setMessage("initial commit").call();
85 assertStringArrayEquals("",
86 execute("git reset --hard " + commit.getId().name()));
87 assertEquals(commit.getId(),
88 git.getRepository().exactRef("HEAD").getObjectId());
89 }
90
91 @Test
92 public void testResetPrevious() throws Exception {
93 RevCommit commit = git.commit().setMessage("initial commit").call();
94 git.commit().setMessage("second commit").call();
95 assertStringArrayEquals("",
96 execute("git reset --hard " + commit.getId().name()));
97 assertEquals(commit.getId(),
98 git.getRepository().exactRef("HEAD").getObjectId());
99 }
100
101 @Test
102 public void testResetEmptyPath() throws Exception {
103 RevCommit commit = git.commit().setMessage("initial commit").call();
104 assertStringArrayEquals("",
105 execute("git reset --hard " + commit.getId().name() + " --"));
106 assertEquals(commit.getId(),
107 git.getRepository().exactRef("HEAD").getObjectId());
108 }
109
110 @Test
111 public void testResetPathDoubleDash() throws Exception {
112 resetPath(true, true);
113 }
114
115 @Test
116 public void testResetPathNoDoubleDash() throws Exception {
117 resetPath(false, true);
118 }
119
120 @Test
121 public void testResetPathDoubleDashNoRef() throws Exception {
122 resetPath(true, false);
123 }
124
125 @Ignore("Currently we cannote recognize if a name is a commit-ish or a path, "
126 + "so 'git reset a' will not work if 'a' is not a branch name but a file path")
127 @Test
128 public void testResetPathNoDoubleDashNoRef() throws Exception {
129 resetPath(false, false);
130 }
131
132 private void resetPath(boolean useDoubleDash, boolean supplyCommit)
133 throws Exception {
134
135 writeTrashFile("a", "Hello world a");
136 writeTrashFile("b", "Hello world b");
137
138 git.add().addFilepattern(".").call();
139
140 RevCommit commit = git.commit().setMessage("files a & b").call();
141
142
143 writeTrashFile("a", "New Hello world a");
144 writeTrashFile("b", "New Hello world b");
145
146 git.add().addFilepattern(".").call();
147
148
149 String cmd = String.format("git reset %s%s a",
150 supplyCommit ? commit.getId().name() : "",
151 useDoubleDash ? " --" : "");
152 assertStringArrayEquals("", execute(cmd));
153 assertEquals(commit.getId(),
154 git.getRepository().exactRef("HEAD").getObjectId());
155
156 org.eclipse.jgit.api.Status status = git.status().call();
157
158 assertArrayEquals(new String[] { "a" },
159 status.getModified().toArray());
160
161 assertArrayEquals(new String[] { "b" },
162 status.getChanged().toArray());
163 }
164
165 }