summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2010-01-09 19:22:47 -0800
committerShawn O. Pearce <spearce@spearce.org>2010-01-23 11:10:57 -0800
commit57f6f6a6bb50bf4916a32723c4f32bac616a1da6 (patch)
tree4fe939ba015510aa8ed4aa4ccd3dcb8ddc1b23f2 /org.eclipse.jgit.pgm
parent73b6efc9289d6f7b6c147f4c2e2c62d2134fd7f3 (diff)
downloadjgit-57f6f6a6bb50bf4916a32723c4f32bac616a1da6.tar.gz
jgit-57f6f6a6bb50bf4916a32723c4f32bac616a1da6.zip
branch: Add -m option to rename a branch
Change-Id: I7cf8e43344eaf301592fba0c178e04daad930f9a Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit.pgm')
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java41
1 files changed, 37 insertions, 4 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java
index ffc28edc55..7a1dd16043 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java
@@ -50,18 +50,19 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import org.kohsuke.args4j.Argument;
-import org.kohsuke.args4j.ExampleMode;
-import org.kohsuke.args4j.Option;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefComparator;
+import org.eclipse.jgit.lib.RefRename;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.pgm.opt.CmdLineParser;
import org.eclipse.jgit.revwalk.RevWalk;
+import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.ExampleMode;
+import org.kohsuke.args4j.Option;
@Command(common = true, usage = "List, create, or delete branches")
class Branch extends TextBuiltin {
@@ -81,6 +82,9 @@ class Branch extends TextBuiltin {
@Option(name = "--create-force", aliases = { "-f" }, usage = "force create branch even exists")
private boolean createForce = false;
+ @Option(name = "-m", usage = "move/rename a branch")
+ private boolean rename = false;
+
@Option(name = "--verbose", aliases = { "-v" }, usage = "be verbose")
private boolean verbose = false;
@@ -102,7 +106,36 @@ class Branch extends TextBuiltin {
if (branches.size() > 2)
throw die("Too many refs given\n" + new CmdLineParser(this).printExample(ExampleMode.ALL));
- if (branches.size() > 0) {
+ if (rename) {
+ String src, dst;
+ if (branches.size() == 1) {
+ final Ref head = db.getRef(Constants.HEAD);
+ if (head != null && head.isSymbolic())
+ src = head.getLeaf().getName();
+ else
+ throw die("Cannot rename detached HEAD");
+ dst = branches.get(0);
+ } else {
+ src = branches.get(0);
+ final Ref old = db.getRef(src);
+ if (old == null)
+ throw die(String.format("%s does not exist", src));
+ if (!old.getName().startsWith(Constants.R_HEADS))
+ throw die(String.format("%s is not a branch", src));
+ src = old.getName();
+ dst = branches.get(1);
+ }
+
+ if (!dst.startsWith(Constants.R_HEADS))
+ dst = Constants.R_HEADS + dst;
+ if (!Repository.isValidRefName(dst))
+ throw die(String.format("%s is not a valid ref name", dst));
+
+ RefRename r = db.renameRef(src, dst);
+ if (r.rename() != Result.RENAMED)
+ throw die(String.format("%s cannot be renamed", src));
+
+ } else if (branches.size() > 0) {
String newHead = branches.get(0);
String startBranch;
if (branches.size() == 2)