git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 7/7] jgit: Added branch deletion to jgit command

From
Charles O'Farrell <charleso@charleso.org>
Date
Aug 14, 2008, 10:13 UTC
Message-ID
<1218708829-8175-8-git-send-email-charleso@charleso.org>
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
Previous: Charles O'FarrellNext: Shawn O. Pearce
Message 8 of 11 in “jgit: Branch command now supports deletion”
  1. 0/7 jgit: Branch command now supports deletionCharles O'Farrell, Aug 14, 2008
  2. 1/7 Refactor of WalkRemoteObjectDatabase ref writing into common classCharles O'Farrell, Aug 14, 2008
  3. 2/7 Refactor of RefUpdate force to call common updateImpl instead of duplicationCharles O'Farrell, Aug 14, 2008
  4. 3/7 Minor refactor of constants, including log and ROOT_DIRCharles O'Farrell, Aug 14, 2008
  5. 4/7 Extract lockAndWriteFile method in RefDatabase for reuseCharles O'Farrell, Aug 14, 2008
  6. 5/7 Added removePackedRef method to RefDatabase for packed branch deletionCharles O'Farrell, Aug 14, 2008
  7. 6/7 Added ref deletion to RefUpdateCharles O'Farrell, Aug 14, 2008
  8. 7/7 jgit: Added branch deletion to jgit commandCharles O'Farrell, Aug 14, 2008
  9. Shawn O. PearceAug 14, 2008
  10. 2/7 Refactor of RefUpdate force to call common updateImpl instead of duplicationCharles O'Farrell, Aug 15, 2008
  11. 6/7 Added ref deletion to RefUpdateCharles O'Farrell, Aug 15, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.