1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.pgm;
11
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertTrue;
16
17 import org.eclipse.jgit.api.Git;
18 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
19 import org.eclipse.jgit.revwalk.RevCommit;
20 import org.junit.Before;
21 import org.junit.Ignore;
22 import org.junit.Test;
23
24 public class ResetTest extends CLIRepositoryTestCase {
25
26 private Git git;
27
28 @Override
29 @Before
30 public void setUp() throws Exception {
31 super.setUp();
32 git = new Git(db);
33 }
34
35 @Test
36 public void testPathOptionHelp() throws Exception {
37 String[] result = execute("git reset -h");
38 assertTrue("Unexpected argument: " + result[1],
39 result[1].endsWith("[-- path ...]"));
40 }
41
42 @Test
43 public void testZombieArgument_Bug484951() throws Exception {
44 String[] result = execute("git reset -h");
45 assertFalse("Unexpected argument: " + result[0],
46 result[0].contains("[VAL ...]"));
47 }
48
49 @Test
50 public void testResetSelf() throws Exception {
51 RevCommit commit = git.commit().setMessage("initial commit").call();
52 assertStringArrayEquals("",
53 execute("git reset --hard " + commit.getId().name()));
54 assertEquals(commit.getId(),
55 git.getRepository().exactRef("HEAD").getObjectId());
56 }
57
58 @Test
59 public void testResetPrevious() throws Exception {
60 RevCommit commit = git.commit().setMessage("initial commit").call();
61 git.commit().setMessage("second commit").call();
62 assertStringArrayEquals("",
63 execute("git reset --hard " + commit.getId().name()));
64 assertEquals(commit.getId(),
65 git.getRepository().exactRef("HEAD").getObjectId());
66 }
67
68 @Test
69 public void testResetEmptyPath() throws Exception {
70 RevCommit commit = git.commit().setMessage("initial commit").call();
71 assertStringArrayEquals("",
72 execute("git reset --hard " + commit.getId().name() + " --"));
73 assertEquals(commit.getId(),
74 git.getRepository().exactRef("HEAD").getObjectId());
75 }
76
77 @Test
78 public void testResetPathDoubleDash() throws Exception {
79 resetPath(true, true);
80 }
81
82 @Test
83 public void testResetPathNoDoubleDash() throws Exception {
84 resetPath(false, true);
85 }
86
87 @Test
88 public void testResetPathDoubleDashNoRef() throws Exception {
89 resetPath(true, false);
90 }
91
92 @Ignore("Currently we cannote recognize if a name is a commit-ish or a path, "
93 + "so 'git reset a' will not work if 'a' is not a branch name but a file path")
94 @Test
95 public void testResetPathNoDoubleDashNoRef() throws Exception {
96 resetPath(false, false);
97 }
98
99 private void resetPath(boolean useDoubleDash, boolean supplyCommit)
100 throws Exception {
101
102 writeTrashFile("a", "Hello world a");
103 writeTrashFile("b", "Hello world b");
104
105 git.add().addFilepattern(".").call();
106
107 RevCommit commit = git.commit().setMessage("files a & b").call();
108
109
110 writeTrashFile("a", "New Hello world a");
111 writeTrashFile("b", "New Hello world b");
112
113 git.add().addFilepattern(".").call();
114
115
116 String cmd = String.format("git reset %s%s a",
117 supplyCommit ? commit.getId().name() : "",
118 useDoubleDash ? " --" : "");
119 assertStringArrayEquals("", execute(cmd));
120 assertEquals(commit.getId(),
121 git.getRepository().exactRef("HEAD").getObjectId());
122
123 org.eclipse.jgit.api.Status status = git.status().call();
124
125 assertArrayEquals(new String[] { "a" },
126 status.getModified().toArray());
127
128 assertArrayEquals(new String[] { "b" },
129 status.getChanged().toArray());
130 }
131
132 }