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

[JGIT PATCH 3/3] Verbose branch command

From
Charles O'Farrell <charleso@charleso.org>
Date
Aug 18, 2008, 11:01 UTC
Message-ID
<1219057305-9284-4-git-send-email-charleso@charleso.org>
In-Reply-To
<1219057305-9284-3-git-send-email-charleso@charleso.org>
Signed-off-by: Charles O'Farrell <charleso@charleso.org>
---
 .../src/org/spearce/jgit/pgm/Branch.java           |   34 +++++++++++++++----
 1 files changed, 27 insertions(+), 7 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 9141be0..ba8ffde 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
@@ -52,6 +52,7 @@
 import org.spearce.jgit.lib.RefComparator;
 import org.spearce.jgit.lib.RefUpdate;
 import org.spearce.jgit.lib.RefUpdate.Result;
+import org.spearce.jgit.revwalk.RevWalk;
 
 @Command(common = true, usage = "List, create, or delete branches")
 class Branch extends TextBuiltin {
@@ -76,15 +77,23 @@
 
 	private Map<String, Ref> printRefs = new LinkedHashMap<String, Ref>();
 
+	/** Only set for verbose branch listing at-the-moment */
+	private RevWalk rw;
+
+	private int maxNameLength;
+
 	@Override
 	protected void run() throws Exception {
 		if (delete || deleteForce)
 			delete(deleteForce);
-		else
+		else {
+			if (verbose)
+				rw = new RevWalk(db);
 			list();
+		}
 	}
 
-	private void list() {
+	private void list() throws Exception {
 		Map<String, Ref> refs = db.getAllRefs();
 		Ref head = refs.get(Constants.HEAD);
 		// This can happen if HEAD is stillborn
@@ -95,7 +104,8 @@ private void list() {
 			addRefs(refs, Constants.HEADS_PREFIX, !remote);
 			addRefs(refs, Constants.REMOTES_PREFIX, remote);
 			for (Entry<String, Ref> e : printRefs.entrySet()) {
-				printHead(e.getKey(), current.equals(e.getValue().getName()));
+				Ref ref = e.getValue();
+				printHead(e.getKey(), current.equals(ref.getName()), ref);
 			}
 		}
 	}
@@ -105,20 +115,30 @@ private void addRefs(Map<String, Ref> allRefs, String prefix, boolean add) {
 			for (Ref ref : RefComparator.sort(allRefs.values())) {
 				String name = ref.getName();
 				if (name.startsWith(prefix))
-					addRef(name, ref);
+					addRef(name.substring(name.indexOf('/', 5) + 1), ref);
 			}
 		}
 	}
 
 	private void addRef(String name, Ref ref) {
 		printRefs.put(name, ref);
+		maxNameLength = Math.max(maxNameLength, name.length());
 	}
 
-	private void printHead(String ref, boolean isCurrent) {
+	private void printHead(String ref, boolean isCurrent, Ref refObj)
+			throws Exception {
 		out.print(isCurrent ? '*' : ' ');
 		out.print(' ');
-		ref = ref.substring(ref.indexOf('/', 5) + 1);
-		out.println(ref);
+		out.print(ref);
+		if (verbose) {
+			int spaces = maxNameLength - ref.length() + 1;
+			out.print(String.format("%" + spaces + "s", ""));
+			ObjectId objectId = refObj.getObjectId();
+			out.print(objectId.toString().substring(0, 7));
+			out.print(' ');
+			out.print(rw.parseCommit(objectId).getShortMessage());
+		}
+		out.println();
 	}
 
 	private void delete(boolean force) throws IOException {
-- 
1.6.0.2.g2ebc0
Previous: Charles O'FarrellNext: Marek Zawirski
Message 4 of 6 in “jgit: Verbase branch command”
  1. 0/3 jgit: Verbase branch commandCharles O'Farrell, Aug 18, 2008
  2. 1/3 Extract RefComparator to sort collection of RefsCharles O'Farrell, Aug 18, 2008
  3. 2/3 Cleanup of Branch command ready for verbose modeCharles O'Farrell, Aug 18, 2008
  4. 3/3 Verbose branch commandCharles O'Farrell, Aug 18, 2008
  5. Marek ZawirskiAug 18, 2008
  6. Shawn O. PearceAug 18, 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.