]> source.dussan.org Git - jgit.git/commitdiff
Throw JGit exception when ResetCommand got wrong ref 79/3879/4
authorChristian Halstrick <christian.halstrick@sap.com>
Tue, 16 Aug 2011 13:15:01 +0000 (15:15 +0200)
committerChris Aniszczyk <zx@twitter.com>
Sun, 21 Aug 2011 21:11:00 +0000 (14:11 -0700)
If the ResetCommand should reset to a invalid ref (e.g. HEAD in a repo
whithout a single commit) it was throwing an NPE. This is fixed now by
throwing a JGitInternalExcpeption. It would be nicer if we could throw
a InvalidRefException, but this would modify our API.

Bug: 339610
Change-Id: Iffcb4f2cca9f702176471d93c3a71e5cb3e700b1
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

index e6d689a18aa9bdf3a1dca9e354d1d9b77cd6bf66..f10aaf52e1368a835ee8bce79c26814b07cc4e97 100644 (file)
@@ -46,6 +46,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
@@ -156,6 +157,22 @@ public class ResetCommandTest extends RepositoryTestCase {
                assertReflog(prevHead, head);
        }
 
+       @Test
+       public void testResetToNonexistingHEAD() throws JGitInternalException,
+                       AmbiguousObjectException, IOException {
+
+               // create a file in the working tree of a fresh repo
+               git = new Git(db);
+               writeTrashFile("f", "content");
+
+               try {
+                       git.reset().setRef(Constants.HEAD).call();
+                       fail("Expected JGitInternalException didn't occur");
+               } catch (JGitInternalException e) {
+                       // got the expected exception
+               }
+       }
+
        @Test
        public void testSoftReset() throws JGitInternalException,
                        AmbiguousObjectException, IOException, NoFilepatternException,
index 05f59950ffbc3a7b4642e9d3865db83ec007310c..cd50ba07dac8430a9d586c44cb0247ca864b8ccf 100644 (file)
@@ -151,6 +151,12 @@ public class ResetCommand extends GitCommand<Ref> {
                        final ObjectId commitId;
                        try {
                                commitId = repo.resolve(ref);
+                               if (commitId == null) {
+                                       // @TODO throw an InvalidRefNameException. We can't do that
+                                       // now because this would break the API
+                                       throw new JGitInternalException("Invalid ref " + ref
+                                                       + " specified");
+                               }
                        } catch (IOException e) {
                                throw new JGitInternalException(
                                                MessageFormat.format(JGitText.get().cannotRead, ref),