Browse Source

Output result of switching branch -- o.e.jgit.pgm.Checkout

Change-Id: I9829950b686ce3b8c70b8f7a1774d5e2b55cd00a
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
tags/v2.1.0.201209190230-r
Tomasz Zarna 11 years ago
parent
commit
e44c3e7139

+ 2
- 0
org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/pgm/CLIGitCommand.java View 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();

+ 6
- 0
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java View 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]);
}
}

+ 101
- 0
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CheckoutTest.java View File

@@ -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]);
}
}

+ 7
- 2
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties View 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

+ 5
- 0
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java View 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;

+ 38
- 2
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java View 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
@@ -43,8 +43,16 @@

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));
}
}
}

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/api/errors/RefAlreadyExistsException.java View 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 {

Loading…
Cancel
Save