{"thread":{"id":"15658","subject":"[EGIT PATCH 2/3] Add create support to the branch command","startedAt":"2008-09-24T21:56:21Z","lastAt":"2008-09-24T21:56:23Z","messageCount":3,"participants":["Robin Rosenberg"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"91527","messageId":"1222293383-26016-1-git-send-email-robin.rosenberg@dewire.com","threadId":"15658","inReplyTo":null,"subject":"[EGIT PATCH 1/3] Include a print command line usage string utility","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2008-09-24T21:56:21Z","receivedAt":"2008-09-24T21:56:21Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>\n---\n .../src/org/spearce/jgit/pgm/TextBuiltin.java      |   40 +++++++++++++++-----\n 1 files changed, 30 insertions(+), 10 deletions(-)\n\ndiff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java\nindex 0746eb3..d0fe4af 100644\n--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java\n+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java\n@@ -127,22 +127,42 @@ protected void parseArguments(final String[] args) {\n \t\t}\n \n \t\tif (help) {\n-\t\t\tSystem.err.print(\"jgit \");\n-\t\t\tSystem.err.print(commandName);\n-\t\t\tclp.printSingleLineUsage(System.err);\n-\t\t\tSystem.err.println();\n-\n-\t\t\tSystem.err.println();\n-\t\t\tclp.printUsage(System.err);\n-\t\t\tSystem.err.println();\n-\n-\t\t\tSystem.exit(1);\n+\t\t\tprintUsageAndExit(clp);\n \t\t}\n \n \t\targWalk = clp.getRevWalkGently();\n \t}\n \n \t/**\n+\t * Print the usage line\n+\t *\n+\t * @param clp\n+\t */\n+\tpublic void printUsageAndExit(final CmdLineParser clp) {\n+\t\tprintUsageAndExit(\"\", clp);\n+\t}\n+\n+\t/**\n+\t * Print an error message and the usage line\n+\t *\n+\t * @param message\n+\t * @param clp\n+\t */\n+\tpublic void printUsageAndExit(final String message, final CmdLineParser clp) {\n+\t\tSystem.err.println(message);\n+\t\tSystem.err.print(\"jgit \");\n+\t\tSystem.err.print(commandName);\n+\t\tclp.printSingleLineUsage(System.err);\n+\t\tSystem.err.println();\n+\n+\t\tSystem.err.println();\n+\t\tclp.printUsage(System.err);\n+\t\tSystem.err.println();\n+\n+\t\tSystem.exit(1);\n+\t}\n+\n+\t/**\n \t * Perform the actions of this command.\n \t * <p>\n \t * This method should only be invoked by {@link #execute(String[])}.\n-- \n1.6.0.1.310.gf789d0.dirty\n"},{"id":"91525","messageId":"1222293383-26016-2-git-send-email-robin.rosenberg@dewire.com","threadId":"15658","inReplyTo":"1222293383-26016-1-git-send-email-robin.rosenberg@dewire.com","subject":"[EGIT PATCH 2/3] Add create support to the branch command","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2008-09-24T21:56:22Z","receivedAt":"2008-09-24T21:56:22Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"The help string hinted it could create branches, but it was not implemented.\n\nSigned-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>\n---\n .../src/org/spearce/jgit/pgm/Branch.java           |   38 ++++++++++++++++++--\n 1 files changed, 35 insertions(+), 3 deletions(-)\n\ndiff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\nindex a266244..db0aab8 100644\n--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n@@ -45,13 +45,16 @@\n import java.util.Map.Entry;\n \n import org.kohsuke.args4j.Argument;\n+import org.kohsuke.args4j.ExampleMode;\n import org.kohsuke.args4j.Option;\n import org.spearce.jgit.lib.Constants;\n import org.spearce.jgit.lib.ObjectId;\n import org.spearce.jgit.lib.Ref;\n import org.spearce.jgit.lib.RefComparator;\n import org.spearce.jgit.lib.RefUpdate;\n+import org.spearce.jgit.lib.Repository;\n import org.spearce.jgit.lib.RefUpdate.Result;\n+import org.spearce.jgit.pgm.opt.CmdLineParser;\n import org.spearce.jgit.revwalk.RevWalk;\n \n @Command(common = true, usage = \"List, create, or delete branches\")\n@@ -69,6 +72,9 @@\n \t@Option(name = \"--delete-force\", aliases = { \"-D\" }, usage = \"delete branch (even if not merged)\")\n \tprivate boolean deleteForce = false;\n \n+\t@Option(name = \"--create-force\", aliases = { \"-f\" }, usage = \"force create branch even exists\")\n+\tprivate boolean createForce = false;\n+\n \t@Option(name = \"--verbose\", aliases = { \"-v\" }, usage = \"be verbose\")\n \tprivate boolean verbose = false;\n \n@@ -87,9 +93,35 @@ protected void run() throws Exception {\n \t\tif (delete || deleteForce)\n \t\t\tdelete(deleteForce);\n \t\telse {\n-\t\t\tif (verbose)\n-\t\t\t\trw = new RevWalk(db);\n-\t\t\tlist();\n+\t\t\tif (branches.size() > 2)\n+\t\t\t\tthrow die(\"Too many refs given\\n\" + new CmdLineParser(this).printExample(ExampleMode.ALL));\n+\n+\t\t\tif (branches.size() > 0) {\n+\t\t\t\tString newHead = branches.get(0);\n+\t\t\t\tObjectId startAt;\n+\t\t\t\tif (branches.size() == 2)\n+\t\t\t\t\tstartAt = db.resolve(branches.get(1) + \"^0\");\n+\t\t\t\telse\n+\t\t\t\t\tstartAt = db.resolve(Constants.HEAD + \"^0\");\n+\n+\t\t\t\tString newRefName = newHead;\n+\t\t\t\tif (!newRefName.startsWith(Constants.R_HEADS))\n+\t\t\t\t\tnewRefName = Constants.R_HEADS + newRefName;\n+\t\t\t\tif (!Repository.isValidRefName(newRefName))\n+\t\t\t\t\tthrow die(String.format(\"%s is not a valid ref name\", newRefName));\n+\t\t\t\tif (!createForce && db.resolve(newRefName) != null)\n+\t\t\t\t\tthrow die(String.format(\"branch %s already exists\", newHead));\n+\t\t\t\tRefUpdate updateRef = db.updateRef(newRefName);\n+\t\t\t\tupdateRef.setNewObjectId(startAt);\n+\t\t\t\tupdateRef.setForceUpdate(createForce);\n+\t\t\t\tResult update = updateRef.update();\n+\t\t\t\tif (update == Result.REJECTED)\n+\t\t\t\t\tthrow die(String.format(\"Could not create branch %s: %s\", newHead, update.toString()));\n+\t\t\t} else {\n+\t\t\t\tif (verbose)\n+\t\t\t\t\trw = new RevWalk(db);\n+\t\t\t\tlist();\n+\t\t\t}\n \t\t}\n \t}\n \n-- \n1.6.0.1.310.gf789d0.dirty\n"},{"id":"91526","messageId":"1222293383-26016-3-git-send-email-robin.rosenberg@dewire.com","threadId":"15658","inReplyTo":"1222293383-26016-2-git-send-email-robin.rosenberg@dewire.com","subject":"[EGIT PATCH 3/3] Tell the branch dialog how to create a new branch.","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2008-09-24T21:56:23Z","receivedAt":"2008-09-24T21:56:23Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>\n---\n .../ui/internal/dialogs/BranchSelectionDialog.java |   98 ++++++++++++++++++--\n 1 files changed, 88 insertions(+), 10 deletions(-)\n\ndiff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/BranchSelectionDialog.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/BranchSelectionDialog.java\nindex 158738c..26d423a 100644\n--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/BranchSelectionDialog.java\n+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/internal/dialogs/BranchSelectionDialog.java\n@@ -17,14 +17,21 @@\n \n import org.eclipse.jface.dialogs.Dialog;\n import org.eclipse.jface.dialogs.IDialogConstants;\n+import org.eclipse.jface.dialogs.IInputValidator;\n+import org.eclipse.jface.dialogs.InputDialog;\n import org.eclipse.jface.dialogs.MessageDialog;\n import org.eclipse.jface.layout.GridDataFactory;\n import org.eclipse.jface.layout.GridLayoutFactory;\n+import org.eclipse.jface.resource.JFaceResources;\n+import org.eclipse.jface.window.Window;\n import org.eclipse.swt.SWT;\n import org.eclipse.swt.events.DisposeEvent;\n import org.eclipse.swt.events.DisposeListener;\n+import org.eclipse.swt.events.SelectionEvent;\n+import org.eclipse.swt.events.SelectionListener;\n import org.eclipse.swt.graphics.Font;\n import org.eclipse.swt.graphics.FontData;\n+import org.eclipse.swt.layout.GridLayout;\n import org.eclipse.swt.layout.RowLayout;\n import org.eclipse.swt.widgets.Button;\n import org.eclipse.swt.widgets.Composite;\n@@ -35,6 +42,10 @@\n import org.eclipse.swt.widgets.Tree;\n import org.eclipse.swt.widgets.TreeItem;\n import org.spearce.egit.core.op.ResetOperation.ResetType;\n+import org.spearce.egit.ui.Activator;\n+import org.spearce.jgit.lib.Constants;\n+import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.RefUpdate;\n import org.spearce.jgit.lib.Repository;\n \n /**\n@@ -81,9 +92,9 @@ protected Composite createDialogArea(Composite base) {\n \t\t}\n \t\t\n \t\ttry {\n-\t\t\tfillTreeWithBranches();\n+\t\t\tfillTreeWithBranches(null);\n \t\t} catch (IOException e) {\n-\t\t\te.printStackTrace();\n+\t\t\tActivator.logError(\"Could not refresh list of branches\", e);\n \t\t}\n \t\t\n \t\treturn parent;\n@@ -121,7 +132,7 @@ public void handleEvent(Event event) {\n \t\t});\n \t}\n \n-\tprivate void fillTreeWithBranches() throws IOException {\n+\tprivate void fillTreeWithBranches(String select) throws IOException {\n \t\tString branch = repo.getFullBranch();\n \t\tList<String> branches = new ArrayList<String>(repo.getAllRefs()\n \t\t\t\t.keySet());\n@@ -197,6 +208,8 @@ public void widgetDisposed(DisposeEvent e) {\n \t\t\t\tbranchTree.showItem(item);\n \t\t\t}\n \t\t\telse item.setText(shortName);\n+\t\t\tif (ref.equals(select))\n+\t\t\t\tbranchTree.select(item);\n \t\t}\n \t}\n \t\n@@ -220,12 +233,7 @@ public ResetType getResetType() {\n \t\n \t@Override\n \tprotected void okPressed() {\n-\t\tTreeItem[] selection = branchTree.getSelection();\n-\t\trefName = null;\n-\t\tif (selection != null && selection.length > 0) {\n-\t\t\tTreeItem item = selection[0];\n-\t\t\trefName = (String) item.getData();\n-\t\t}\n+\t\trefNameFromDialog();\n \t\tif (refName == null) {\n \t\t\tMessageDialog.openWarning(getShell(), \"No branch/tag selected\", \"You must select a valid ref.\");\n \t\t\treturn;\n@@ -244,9 +252,79 @@ protected void okPressed() {\n \t\tsuper.okPressed();\n \t}\n \n+\tprivate void refNameFromDialog() {\n+\t\tTreeItem[] selection = branchTree.getSelection();\n+\t\trefName = null;\n+\t\tif (selection != null && selection.length > 0) {\n+\t\t\tTreeItem item = selection[0];\n+\t\t\trefName = (String) item.getData();\n+\t\t}\n+\t}\n+\n \t@Override\n \tprotected void createButtonsForButtonBar(Composite parent) {\n-\t\tcreateButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);\n+\t\tif (!showResetType) {\n+\t\t\tButton newButton = new Button(parent, SWT.PUSH);\n+\t\t\tnewButton.setFont(JFaceResources.getDialogFont());\n+\t\t\tnewButton.setText(\"New branch\");\n+\t\t\t((GridLayout)parent.getLayout()).numColumns++;\n+\t\t\tnewButton.addSelectionListener(new SelectionListener() {\n+\n+\t\t\t\tpublic void widgetSelected(SelectionEvent e) {\n+\t\t\t\t\t// check what ref name the user selected, if any.\n+\t\t\t\t\trefNameFromDialog();\n+\n+\t\t\t\t\tInputDialog labelDialog = new InputDialog(\n+\t\t\t\t\t\t\tgetShell(),\n+\t\t\t\t\t\t\t\"New branch\",\n+\t\t\t\t\t\t\t\"Enter name of new branch. It will branch from the selected branch. refs/heads/ will be prepended to the name you type\",\n+\t\t\t\t\t\t\tnull, new IInputValidator() {\n+\t\t\t\t\t\t\t\tpublic String isValid(String newText) {\n+\t\t\t\t\t\t\t\t\tString testFor = Constants.R_HEADS + newText;\n+\t\t\t\t\t\t\t\t\ttry {\n+\t\t\t\t\t\t\t\t\t\tif (repo.resolve(testFor) != null)\n+\t\t\t\t\t\t\t\t\t\t\treturn \"Already exists\";\n+\t\t\t\t\t\t\t\t\t} catch (IOException e1) {\n+\t\t\t\t\t\t\t\t\t\tActivator.logError(String.format(\n+\t\t\t\t\t\t\t\t\t\t\t\t\"Could not attempt to resolve %s\", testFor), e1);\n+\t\t\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\t\t\tif (!Repository.isValidRefName(testFor))\n+\t\t\t\t\t\t\t\t\t\treturn \"Invalid ref name\";\n+\t\t\t\t\t\t\t\t\treturn null;\n+\t\t\t\t\t\t\t\t}\n+\t\t\t\t\t\t\t});\n+\t\t\t\t\tlabelDialog.setBlockOnOpen(true);\n+\t\t\t\t\tif (labelDialog.open() == Window.OK) {\n+\t\t\t\t\t\tString newRefName = Constants.R_HEADS + labelDialog.getValue();\n+\t\t\t\t\t\tRefUpdate updateRef;\n+\t\t\t\t\t\ttry {\n+\t\t\t\t\t\t\tupdateRef = repo.updateRef(newRefName);\n+\t\t\t\t\t\t\tObjectId startAt;\n+\t\t\t\t\t\t\tif (refName == null)\n+\t\t\t\t\t\t\t\tstartAt = repo.resolve(Constants.HEAD);\n+\t\t\t\t\t\t\telse\n+\t\t\t\t\t\t\t\tstartAt = repo.resolve(refName);\n+\t\t\t\t\t\t\tupdateRef.setNewObjectId(startAt);\n+\t\t\t\t\t\t\tupdateRef.update();\n+\t\t\t\t\t\t} catch (IOException e1) {\n+\t\t\t\t\t\t\tActivator.logError(String.format(\n+\t\t\t\t\t\t\t\t\t\"Could not create new ref %s\", newRefName), e1);\n+\t\t\t\t\t\t}\n+\t\t\t\t\t\ttry {\n+\t\t\t\t\t\t\tbranchTree.removeAll();\n+\t\t\t\t\t\t\tfillTreeWithBranches(newRefName);\n+\t\t\t\t\t\t} catch (IOException e1) {\n+\t\t\t\t\t\t\tActivator.logError(\"Could not refresh list of branches\",e1);\n+\t\t\t\t\t\t}\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\n+\t\t\t\tpublic void widgetDefaultSelected(SelectionEvent e) {\n+\t\t\t\t\twidgetSelected(e);\n+\t\t\t\t}\n+\t\t\t});\n+\t\t}\n+\t\tcreateButton(parent, IDialogConstants.OK_ID, showResetType ? \"Reset\" : \"Checkout\", true);\n \t\tcreateButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);\n \t}\n \n-- \n1.6.0.1.310.gf789d0.dirty\n"}]}