From e44c3e713902faaf3c831827915312666cd6ecd6 Mon Sep 17 00:00:00 2001 From: Tomasz Zarna Date: Wed, 5 Sep 2012 07:59:37 +0200 Subject: Output result of switching branch -- o.e.jgit.pgm.Checkout Change-Id: I9829950b686ce3b8c70b8f7a1774d5e2b55cd00a Signed-off-by: Matthias Sohn --- .../src/org/eclipse/jgit/pgm/CLIText.java | 5 +++ .../src/org/eclipse/jgit/pgm/Checkout.java | 40 ++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit.pgm/src') diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java index 954977e111..d748d504b1 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/CLIText.java @@ -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; diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java index 1b014f04ae..ee1ded09a6 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010, Chris Aniszczyk + * Copyright (C) 2010, 2012 Chris Aniszczyk * 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)); + } } } -- cgit v1.2.3