* [EGIT PATCH 1/3] Include a print command line usage string utility
@ 2008-09-24 21:56 Robin Rosenberg
2008-09-24 21:56 ` [EGIT PATCH 2/3] Add create support to the branch command Robin Rosenberg
0 siblings, 1 reply; 3+ messages in thread
From: Robin Rosenberg @ 2008-09-24 21:56 UTC (permalink / raw)
To: spearce; +Cc: git, Robin Rosenberg
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
.../src/org/spearce/jgit/pgm/TextBuiltin.java | 40 +++++++++++++++-----
1 files changed, 30 insertions(+), 10 deletions(-)
diff --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
index 0746eb3..d0fe4af 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
@@ -127,22 +127,42 @@ protected void parseArguments(final String[] args) {
}
if (help) {
- System.err.print("jgit ");
- System.err.print(commandName);
- clp.printSingleLineUsage(System.err);
- System.err.println();
-
- System.err.println();
- clp.printUsage(System.err);
- System.err.println();
-
- System.exit(1);
+ printUsageAndExit(clp);
}
argWalk = clp.getRevWalkGently();
}
/**
+ * Print the usage line
+ *
+ * @param clp
+ */
+ public void printUsageAndExit(final CmdLineParser clp) {
+ printUsageAndExit("", clp);
+ }
+
+ /**
+ * Print an error message and the usage line
+ *
+ * @param message
+ * @param clp
+ */
+ public void printUsageAndExit(final String message, final CmdLineParser clp) {
+ System.err.println(message);
+ System.err.print("jgit ");
+ System.err.print(commandName);
+ clp.printSingleLineUsage(System.err);
+ System.err.println();
+
+ System.err.println();
+ clp.printUsage(System.err);
+ System.err.println();
+
+ System.exit(1);
+ }
+
+ /**
* Perform the actions of this command.
* <p>
* This method should only be invoked by {@link #execute(String[])}.
--
1.6.0.1.310.gf789d0.dirty
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [EGIT PATCH 2/3] Add create support to the branch command
2008-09-24 21:56 [EGIT PATCH 1/3] Include a print command line usage string utility Robin Rosenberg
@ 2008-09-24 21:56 ` Robin Rosenberg
2008-09-24 21:56 ` [EGIT PATCH 3/3] Tell the branch dialog how to create a new branch Robin Rosenberg
0 siblings, 1 reply; 3+ messages in thread
From: Robin Rosenberg @ 2008-09-24 21:56 UTC (permalink / raw)
To: spearce; +Cc: git, Robin Rosenberg
The help string hinted it could create branches, but it was not implemented.
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
.../src/org/spearce/jgit/pgm/Branch.java | 38 ++++++++++++++++++--
1 files changed, 35 insertions(+), 3 deletions(-)
diff --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
index a266244..db0aab8 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java
@@ -45,13 +45,16 @@
import java.util.Map.Entry;
import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.ExampleMode;
import org.kohsuke.args4j.Option;
import org.spearce.jgit.lib.Constants;
import org.spearce.jgit.lib.ObjectId;
import org.spearce.jgit.lib.Ref;
import org.spearce.jgit.lib.RefComparator;
import org.spearce.jgit.lib.RefUpdate;
+import org.spearce.jgit.lib.Repository;
import org.spearce.jgit.lib.RefUpdate.Result;
+import org.spearce.jgit.pgm.opt.CmdLineParser;
import org.spearce.jgit.revwalk.RevWalk;
@Command(common = true, usage = "List, create, or delete branches")
@@ -69,6 +72,9 @@
@Option(name = "--delete-force", aliases = { "-D" }, usage = "delete branch (even if not merged)")
private boolean deleteForce = false;
+ @Option(name = "--create-force", aliases = { "-f" }, usage = "force create branch even exists")
+ private boolean createForce = false;
+
@Option(name = "--verbose", aliases = { "-v" }, usage = "be verbose")
private boolean verbose = false;
@@ -87,9 +93,35 @@ protected void run() throws Exception {
if (delete || deleteForce)
delete(deleteForce);
else {
- if (verbose)
- rw = new RevWalk(db);
- list();
+ if (branches.size() > 2)
+ throw die("Too many refs given\n" + new CmdLineParser(this).printExample(ExampleMode.ALL));
+
+ if (branches.size() > 0) {
+ String newHead = branches.get(0);
+ ObjectId startAt;
+ if (branches.size() == 2)
+ startAt = db.resolve(branches.get(1) + "^0");
+ else
+ startAt = db.resolve(Constants.HEAD + "^0");
+
+ String newRefName = newHead;
+ if (!newRefName.startsWith(Constants.R_HEADS))
+ newRefName = Constants.R_HEADS + newRefName;
+ if (!Repository.isValidRefName(newRefName))
+ throw die(String.format("%s is not a valid ref name", newRefName));
+ if (!createForce && db.resolve(newRefName) != null)
+ throw die(String.format("branch %s already exists", newHead));
+ RefUpdate updateRef = db.updateRef(newRefName);
+ updateRef.setNewObjectId(startAt);
+ updateRef.setForceUpdate(createForce);
+ Result update = updateRef.update();
+ if (update == Result.REJECTED)
+ throw die(String.format("Could not create branch %s: %s", newHead, update.toString()));
+ } else {
+ if (verbose)
+ rw = new RevWalk(db);
+ list();
+ }
}
}
--
1.6.0.1.310.gf789d0.dirty
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [EGIT PATCH 3/3] Tell the branch dialog how to create a new branch.
2008-09-24 21:56 ` [EGIT PATCH 2/3] Add create support to the branch command Robin Rosenberg
@ 2008-09-24 21:56 ` Robin Rosenberg
0 siblings, 0 replies; 3+ messages in thread
From: Robin Rosenberg @ 2008-09-24 21:56 UTC (permalink / raw)
To: spearce; +Cc: git, Robin Rosenberg
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
.../ui/internal/dialogs/BranchSelectionDialog.java | 98 ++++++++++++++++++--
1 files changed, 88 insertions(+), 10 deletions(-)
diff --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
index 158738c..26d423a 100644
--- 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
@@ -17,14 +17,21 @@
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.IInputValidator;
+import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
+import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
@@ -35,6 +42,10 @@
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.spearce.egit.core.op.ResetOperation.ResetType;
+import org.spearce.egit.ui.Activator;
+import org.spearce.jgit.lib.Constants;
+import org.spearce.jgit.lib.ObjectId;
+import org.spearce.jgit.lib.RefUpdate;
import org.spearce.jgit.lib.Repository;
/**
@@ -81,9 +92,9 @@ protected Composite createDialogArea(Composite base) {
}
try {
- fillTreeWithBranches();
+ fillTreeWithBranches(null);
} catch (IOException e) {
- e.printStackTrace();
+ Activator.logError("Could not refresh list of branches", e);
}
return parent;
@@ -121,7 +132,7 @@ public void handleEvent(Event event) {
});
}
- private void fillTreeWithBranches() throws IOException {
+ private void fillTreeWithBranches(String select) throws IOException {
String branch = repo.getFullBranch();
List<String> branches = new ArrayList<String>(repo.getAllRefs()
.keySet());
@@ -197,6 +208,8 @@ public void widgetDisposed(DisposeEvent e) {
branchTree.showItem(item);
}
else item.setText(shortName);
+ if (ref.equals(select))
+ branchTree.select(item);
}
}
@@ -220,12 +233,7 @@ public ResetType getResetType() {
@Override
protected void okPressed() {
- TreeItem[] selection = branchTree.getSelection();
- refName = null;
- if (selection != null && selection.length > 0) {
- TreeItem item = selection[0];
- refName = (String) item.getData();
- }
+ refNameFromDialog();
if (refName == null) {
MessageDialog.openWarning(getShell(), "No branch/tag selected", "You must select a valid ref.");
return;
@@ -244,9 +252,79 @@ protected void okPressed() {
super.okPressed();
}
+ private void refNameFromDialog() {
+ TreeItem[] selection = branchTree.getSelection();
+ refName = null;
+ if (selection != null && selection.length > 0) {
+ TreeItem item = selection[0];
+ refName = (String) item.getData();
+ }
+ }
+
@Override
protected void createButtonsForButtonBar(Composite parent) {
- createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
+ if (!showResetType) {
+ Button newButton = new Button(parent, SWT.PUSH);
+ newButton.setFont(JFaceResources.getDialogFont());
+ newButton.setText("New branch");
+ ((GridLayout)parent.getLayout()).numColumns++;
+ newButton.addSelectionListener(new SelectionListener() {
+
+ public void widgetSelected(SelectionEvent e) {
+ // check what ref name the user selected, if any.
+ refNameFromDialog();
+
+ InputDialog labelDialog = new InputDialog(
+ getShell(),
+ "New branch",
+ "Enter name of new branch. It will branch from the selected branch. refs/heads/ will be prepended to the name you type",
+ null, new IInputValidator() {
+ public String isValid(String newText) {
+ String testFor = Constants.R_HEADS + newText;
+ try {
+ if (repo.resolve(testFor) != null)
+ return "Already exists";
+ } catch (IOException e1) {
+ Activator.logError(String.format(
+ "Could not attempt to resolve %s", testFor), e1);
+ }
+ if (!Repository.isValidRefName(testFor))
+ return "Invalid ref name";
+ return null;
+ }
+ });
+ labelDialog.setBlockOnOpen(true);
+ if (labelDialog.open() == Window.OK) {
+ String newRefName = Constants.R_HEADS + labelDialog.getValue();
+ RefUpdate updateRef;
+ try {
+ updateRef = repo.updateRef(newRefName);
+ ObjectId startAt;
+ if (refName == null)
+ startAt = repo.resolve(Constants.HEAD);
+ else
+ startAt = repo.resolve(refName);
+ updateRef.setNewObjectId(startAt);
+ updateRef.update();
+ } catch (IOException e1) {
+ Activator.logError(String.format(
+ "Could not create new ref %s", newRefName), e1);
+ }
+ try {
+ branchTree.removeAll();
+ fillTreeWithBranches(newRefName);
+ } catch (IOException e1) {
+ Activator.logError("Could not refresh list of branches",e1);
+ }
+ }
+ }
+
+ public void widgetDefaultSelected(SelectionEvent e) {
+ widgetSelected(e);
+ }
+ });
+ }
+ createButton(parent, IDialogConstants.OK_ID, showResetType ? "Reset" : "Checkout", true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
--
1.6.0.1.310.gf789d0.dirty
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-09-24 21:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-24 21:56 [EGIT PATCH 1/3] Include a print command line usage string utility Robin Rosenberg
2008-09-24 21:56 ` [EGIT PATCH 2/3] Add create support to the branch command Robin Rosenberg
2008-09-24 21:56 ` [EGIT PATCH 3/3] Tell the branch dialog how to create a new branch Robin Rosenberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).