git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Charles O'Farrell <charleso@charleso.org>
To: git@vger.kernel.org
Cc: Charles O'Farrell <charleso@charleso.org>
Subject: [PATCH 7/7] jgit: Added branch deletion to jgit command
Date: Thu, 14 Aug 2008 20:13:49 +1000	[thread overview]
Message-ID: <1218708829-8175-8-git-send-email-charleso@charleso.org> (raw)
In-Reply-To: <1218708829-8175-7-git-send-email-charleso@charleso.org>

Signed-off-by: Charles O'Farrell <charleso@charleso.org>
---
 .../src/org/spearce/jgit/pgm/Branch.java           |   57 +++++++++++++++++++-
 1 files changed, 55 insertions(+), 2 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 8415fe2..c89f510 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
@@ -37,24 +37,50 @@
 
 package org.spearce.jgit.pgm;
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.TreeSet;
 
+import org.kohsuke.args4j.Argument;
 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.RefUpdate;
+import org.spearce.jgit.lib.RefUpdate.Result;
 
 @Command(common = true, usage = "List, create, or delete branches")
 class Branch extends TextBuiltin {
 
 	@Option(name = "--remote", aliases = { "-r" }, usage = "act on remote-tracking branches")
-	boolean remote = false;
+	private boolean remote = false;
 
 	@Option(name = "--all", aliases = { "-a" }, usage = "list both remote-tracking and local branches")
-	boolean all = false;
+	private boolean all = false;
+
+	@Option(name = "--delete", aliases = { "-d" }, usage = "delete fully merged branch")
+	private boolean delete = false;
+
+	@Option(name = "--delete-force", aliases = { "-D" }, usage = "delete branch (even if not merged)")
+	private boolean deleteForce = false;
+
+	@Option(name = "--verbose", aliases = { "-v" }, usage = "be verbose")
+	private boolean verbose = false;
+
+	@Argument
+	private List<String> branches = new ArrayList<String>();
 
 	@Override
 	protected void run() throws Exception {
+		if (delete || deleteForce)
+			delete(deleteForce);
+		else
+			list();
+	}
+
+	private void list() {
 		Map<String, Ref> refs = db.getAllRefs();
 		Ref head = refs.get(Constants.HEAD);
 		// This can happen if HEAD is stillborn
@@ -80,4 +106,31 @@ class Branch extends TextBuiltin {
 		ref = ref.substring(ref.indexOf('/', 5) + 1);
 		out.println(ref);
 	}
+
+	private void delete(boolean force) throws IOException {
+		String current = db.getBranch();
+		ObjectId head = db.resolve(Constants.HEAD);
+		for (String branch : branches) {
+			if (current.equals(branch)) {
+				String err = "Cannot delete the branch '%s' which you are currently on.";
+				throw die(String.format(err, branch));
+			}
+			RefUpdate update = db.updateRef((remote ? Constants.REMOTES_PREFIX
+					: Constants.HEADS_PREFIX)
+					+ '/' + branch);
+			update.setNewObjectId(head);
+			update.setForceUpdate(force || remote);
+			Result result = update.delete();
+			if (result == Result.REJECTED) {
+				String err = "The branch '%s' is not an ancestor of your current HEAD.\n"
+						+ "If you are sure you want to delete it, run 'jgit branch -D %1$s'.";
+				throw die(String.format(err, branch));
+			} else if (result == Result.NEW)
+				throw die(String.format("branch '%s' not found.", branch));
+			if (remote)
+				out.println(String.format("Deleted remote branch %s", branch));
+			else if (verbose)
+				out.println(String.format("Deleted branch %s", branch));
+		}
+	}
 }
-- 
1.6.0.rc2.35.g04c6e

  reply	other threads:[~2008-08-14 10:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-14 10:13 [PATCH 0/7] jgit: Branch command now supports deletion Charles O'Farrell
2008-08-14 10:13 ` [PATCH 1/7] Refactor of WalkRemoteObjectDatabase ref writing into common class Charles O'Farrell
2008-08-14 10:13   ` [PATCH 2/7] Refactor of RefUpdate force to call common updateImpl instead of duplication Charles O'Farrell
2008-08-14 10:13     ` [PATCH 3/7] Minor refactor of constants, including log and ROOT_DIR Charles O'Farrell
2008-08-14 10:13       ` [PATCH 4/7] Extract lockAndWriteFile method in RefDatabase for reuse Charles O'Farrell
2008-08-14 10:13         ` [PATCH 5/7] Added removePackedRef method to RefDatabase for packed branch deletion Charles O'Farrell
2008-08-14 10:13           ` [PATCH 6/7] Added ref deletion to RefUpdate Charles O'Farrell
2008-08-14 10:13             ` Charles O'Farrell [this message]
2008-08-14 22:23     ` [PATCH 2/7] Refactor of RefUpdate force to call common updateImpl instead of duplication Shawn O. Pearce
2008-08-15  0:25 ` [JGIT PATCH v2 " Charles O'Farrell
2008-08-15  0:25   ` [JGIT PATCH v2 6/7] Added ref deletion to RefUpdate Charles O'Farrell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1218708829-8175-8-git-send-email-charleso@charleso.org \
    --to=charleso@charleso.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).