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();
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]);
+ }
}
--- /dev/null
+/*
+ * 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]);
+ }
+}
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.
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
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
}
/***/ public String IPZillaPasswordPrompt;
+ /***/ public String alreadyOnBranch;
/***/ public String authorInfo;
/***/ public String averageMSPerRead;
/***/ public String branchAlreadyExists;
/***/ 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;
/***/ public String repaint;
/***/ public String serviceNotSupported;
/***/ public String skippingObject;
+ /***/ public String switchedToNewBranch;
+ /***/ public String switchedToBranch;
/***/ public String tagLabel;
/***/ public String taggerInfo;
/***/ public String timeInMilliSeconds;
/*
- * 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;
@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));
+ }
}
}
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 {