]> source.dussan.org Git - jgit.git/commitdiff
Output result of switching branch -- o.e.jgit.pgm.Checkout 64/5764/8
authorTomasz Zarna <Tomasz.Zarna@pl.ibm.com>
Wed, 5 Sep 2012 05:59:37 +0000 (07:59 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 5 Sep 2012 05:59:37 +0000 (07:59 +0200)
Change-Id: I9829950b686ce3b8c70b8f7a1774d5e2b55cd00a
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java [new file with mode: 0644]
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java
org.eclipse.jgit/src/org/eclipse/jgit/api/errors/RefAlreadyExistsException.java

index 18abc826184c627163167f5071c980d69dcfb31c..28339c71e3f90c73b4f0cb86f42c3cfaef601b22 100644 (file)
@@ -94,6 +94,8 @@ public class CLIGitCommand {
                try {
                        cmd.execute(bean.getArguments().toArray(
                                        new String[bean.getArguments().size()]));
+               } catch (Die e) {
+                       return IO.readLines(e.getMessage());
                } finally {
                        if (cmd.out != null)
                                cmd.out.flush();
index ab7c5342b65db1bc6355aea8624ae8a33315ccfa..900164e98fbc9461e3a33c414e6a8553b94a2563 100644 (file)
@@ -62,4 +62,10 @@ public class BranchTest extends CLIRepositoryTestCase {
                assertEquals("* master 6fd41be initial commit",
                                execute("git branch -v")[0]);
        }
+
+       @Test
+       public void testExistingBranch() throws Exception {
+               assertEquals("A branch named 'master' already exists.",
+                               execute("git branch master")[0]);
+       }
 }
diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java
new file mode 100644 (file)
index 0000000..33cfb9a
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2012, IBM Corporation and others.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.eclipse.jgit.pgm;
+
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.CLIRepositoryTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CheckoutTest extends CLIRepositoryTestCase {
+
+       @Test
+       public void testCheckoutSelf() throws Exception {
+               new Git(db).commit().setMessage("initial commit").call();
+
+               assertEquals("Already on 'master'", execute("git checkout master"));
+       }
+
+       @Test
+       public void testCheckoutBranch() throws Exception {
+               new Git(db).commit().setMessage("initial commit").call();
+               new Git(db).branchCreate().setName("side").call();
+
+               assertEquals("Switched to branch 'side'", execute("git checkout side"));
+       }
+
+       @Test
+       public void testCheckoutNewBranch() throws Exception {
+               new Git(db).commit().setMessage("initial commit").call();
+
+               assertEquals("Switched to a new branch 'side'",
+                               execute("git checkout -b side"));
+       }
+
+       @Test
+       public void testCheckoutNonExistingBranch() throws Exception {
+               assertEquals(
+                               "error: pathspec 'side' did not match any file(s) known to git.",
+                               execute("git checkout side"));
+       }
+
+       @Test
+       public void testCheckoutNewBranchThatAlreadyExists() throws Exception {
+               new Git(db).commit().setMessage("initial commit").call();
+
+               assertEquals("A branch named 'master' already exists.",
+                               execute("git checkout -b master"));
+       }
+
+       @Test
+       public void testCheckoutNewBranchOnBranchToBeBorn() throws Exception {
+               assertEquals("You are on a branch yet to be born",
+                               execute("git checkout -b side"));
+       }
+
+       static private void assertEquals(String expected, String[] actual) {
+               Assert.assertEquals(actual[actual.length - 1].equals("") ? 2 : 1,
+                               actual.length); // ignore last line if empty
+               Assert.assertEquals(expected, actual[0]);
+       }
+}
index a14f374f94df7f28d797c54fc94a5771d1c42433..824ee80b2ce79748b1796fc8468185173747eda3 100644 (file)
@@ -6,10 +6,11 @@ ARGUMENTS=ARGUMENTS
 N=N
 
 IPZillaPasswordPrompt=IPZilla Password
+alreadyOnBranch=Already on ''{0}''
 authorInfo=Author: {0} <{1}>
 averageMSPerRead=average {0} ms/read
-branchAlreadyExists=branch {0} already exists
-branchCreatedFrom =branch: Created from {0}
+branchAlreadyExists=A branch named ''{0}'' already exists.
+branchCreatedFrom=branch: Created from {0}
 branchDetachedHEAD=detached HEAD
 branchIsNotAnAncestorOfYourCurrentHEAD=The branch '{0}' is not an ancestor of your current HEAD.\nIf you are sure you want to delete it, run 'jgit branch -D {0}'.
 branchNotFound=branch '{0}' not found.
@@ -122,8 +123,10 @@ notAnIndexFile={0} is not an index file
 notAnObject={0} is not an object
 notFound=!! NOT FOUND !!
 noteObjectTooLargeToPrint=Note object {0} too large to print
+onBranchToBeBorn=You are on a branch yet to be born
 onlyOneMetaVarExpectedIn=Only one {0} expected in {1}.
 onlyOneOfIncludeOnlyAllInteractiveCanBeUsed=Only one of --include/--only/--all/--interactive can be used.
+pathspecDidNotMatch=error: pathspec ''{0}'' did not match any file(s) known to git.
 pushTo=To {0}
 pathsRequired=at least one path has to be specified when using --only
 refDoesNotExistOrNoCommit={0} does not exist or is not referring to a commit
@@ -133,6 +136,8 @@ remoteSideDoesNotSupportDeletingRefs=remote side does not support deleting refs
 repaint=Repaint
 serviceNotSupported=Service '{0}' not supported
 skippingObject=skipping {0} {1}
+switchedToNewBranch=Switched to a new branch ''{0}''
+switchedToBranch=Switched to branch ''{0}''
 tagLabel=tag
 taggerInfo=Tagger: {0} <{1}>
 timeInMilliSeconds={0} ms
index 954977e1110595a3dde7becf4c20823c4f8e4f88..d748d504b1cd2c65674fefbcdcfdca64fa8cf775 100644 (file)
@@ -59,6 +59,7 @@ public class CLIText extends TranslationBundle {
        }
 
        /***/ public String IPZillaPasswordPrompt;
+       /***/ public String alreadyOnBranch;
        /***/ public String authorInfo;
        /***/ public String averageMSPerRead;
        /***/ public String branchAlreadyExists;
@@ -169,8 +170,10 @@ public class CLIText extends TranslationBundle {
        /***/ public String notAnObject;
        /***/ public String notFound;
        /***/ public String noteObjectTooLargeToPrint;
+       /***/ public String onBranchToBeBorn;
        /***/ public String onlyOneMetaVarExpectedIn;
        /***/ public String onlyOneOfIncludeOnlyAllInteractiveCanBeUsed;
+       /***/ public String pathspecDidNotMatch;
        /***/ public String pushTo;
        /***/ public String pathsRequired;
        /***/ public String refDoesNotExistOrNoCommit;
@@ -180,6 +183,8 @@ public class CLIText extends TranslationBundle {
        /***/ public String repaint;
        /***/ public String serviceNotSupported;
        /***/ public String skippingObject;
+       /***/ public String switchedToNewBranch;
+       /***/ public String switchedToBranch;
        /***/ public String tagLabel;
        /***/ public String taggerInfo;
        /***/ public String timeInMilliSeconds;
index 1b014f04ae748200803803c1ae9536ff0dd4e06b..ee1ded09a641e11adf33e6db954d073ee3ae4128 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
+ * Copyright (C) 2010, 2012 Chris Aniszczyk <caniszczyk@gmail.com>
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
 
 package org.eclipse.jgit.pgm;
 
+import java.text.MessageFormat;
+
 import org.eclipse.jgit.api.CheckoutCommand;
 import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
+import org.eclipse.jgit.api.errors.RefNotFoundException;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
 import org.kohsuke.args4j.Argument;
 import org.kohsuke.args4j.Option;
 
@@ -62,10 +70,38 @@ class Checkout extends TextBuiltin {
 
        @Override
        protected void run() throws Exception {
+               if (createBranch) {
+                       final ObjectId head = db.resolve(Constants.HEAD);
+                       if (head == null)
+                               throw die(CLIText.get().onBranchToBeBorn);
+               }
+
                CheckoutCommand command = new Git(db).checkout();
                command.setCreateBranch(createBranch);
                command.setName(name);
                command.setForce(force);
-               command.call();
+               try {
+                       String oldBranch = db.getBranch();
+                       Ref ref = command.call();
+                       if (Repository.shortenRefName(ref.getName()).equals(oldBranch)) {
+                               out.println(MessageFormat.format(CLIText.get().alreadyOnBranch,
+                                               name));
+                               return;
+                       }
+                       if (createBranch)
+                               out.println(MessageFormat.format(
+                                               CLIText.get().switchedToNewBranch,
+                                               Repository.shortenRefName(ref.getName())));
+                       else
+                               out.println(MessageFormat.format(
+                                               CLIText.get().switchedToBranch,
+                                               Repository.shortenRefName(ref.getName())));
+               } catch (RefNotFoundException e) {
+                       out.println(MessageFormat.format(CLIText.get().pathspecDidNotMatch,
+                                       name));
+               } catch (RefAlreadyExistsException e) {
+                       throw die(MessageFormat.format(CLIText.get().branchAlreadyExists,
+                                       name));
+               }
        }
 }
index 5e3ebf8d820484f3be24eff79abb21bc074f1909..837028ce32e699cf577dd6d2644d4d8c6441f87c 100644 (file)
@@ -40,7 +40,7 @@ package org.eclipse.jgit.api.errors;
 import org.eclipse.jgit.lib.Ref;
 
 /**
- * Thrown when trying to create a {@link Ref} with the same name as an exsiting
+ * Thrown when trying to create a {@link Ref} with the same name as an existing
  * one
  */
 public class RefAlreadyExistsException extends GitAPIException {