]> source.dussan.org Git - jgit.git/commitdiff
Do not fail when checking out HEAD 12/8512/3
authorTomasz Zarna <tomasz.zarna@tasktop.com>
Sun, 4 Nov 2012 20:57:12 +0000 (21:57 +0100)
committerChris Aniszczyk <zx@twitter.com>
Fri, 16 Nov 2012 00:05:09 +0000 (16:05 -0800)
Change-Id: I99f5467477ed53101121a5a5d8a0910c55758401
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java

index d7b91cc51a6c32cea53204a789a74ed83ef3db60..48cb5c54ca85750d5c9cf13f9cf8b32c9b52f231 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012, IBM Corporation and others.
+ * Copyright (C) 2012, IBM Corporation
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -93,9 +93,26 @@ public class CheckoutTest extends CLIRepositoryTestCase {
                                execute("git checkout -b side"));
        }
 
+       @Test
+       public void testCheckoutUnresolvedHead() throws Exception {
+               assertEquals(
+                               "error: pathspec 'HEAD' did not match any file(s) known to git.",
+                               execute("git checkout HEAD"));
+       }
+
+       @Test
+       public void testCheckoutHead() throws Exception {
+               new Git(db).commit().setMessage("initial commit").call();
+
+               assertEquals("", execute("git checkout HEAD"));
+       }
+
        static private void assertEquals(String expected, String[] actual) {
-               Assert.assertEquals(actual[actual.length - 1].equals("") ? 2 : 1,
-                               actual.length); // ignore last line if empty
+               // if there is more than one line, ignore last one if empty
+               Assert.assertEquals(
+                               1,
+                               actual.length > 1 && actual[actual.length - 1].equals("") ? actual.length - 1
+                                               : actual.length);
                Assert.assertEquals(expected, actual[0]);
        }
 }
index c356e184a48e3efbf4908a9fde99bb1a38307e65..953a5e02d594adab3371294c4cae8348b98fee8a 100644 (file)
@@ -83,6 +83,8 @@ class Checkout extends TextBuiltin {
                try {
                        String oldBranch = db.getBranch();
                        Ref ref = command.call();
+                       if (ref == null)
+                               return;
                        if (Repository.shortenRefName(ref.getName()).equals(oldBranch)) {
                                outw.println(MessageFormat.format(
                                                CLIText.get().alreadyOnBranch,