{"thread":{"id":"15104","subject":"[JGIT PATCH v2 0/3] Verbase branch command","startedAt":"2008-08-20T11:00:38Z","lastAt":"2008-08-20T11:00:41Z","messageCount":4,"participants":["Charles O'Farrell"],"isPatch":true,"patchVersion":2,"patchTotal":3},"messages":[{"id":"87807","messageId":"1219230041-7998-1-git-send-email-charleso@charleso.org","threadId":"15104","inReplyTo":null,"subject":"[JGIT PATCH v2 0/3] Verbase branch command","fromName":"Charles O'Farrell","fromEmail":"charleso@charleso.org","sentAt":"2008-08-20T11:00:38Z","receivedAt":"2008-08-20T11:00:38Z","isPatch":true,"sender":{"key":"charleso@charleso.org","avatar":"https://gravatar.com/avatar/ce96d531daa37646a38a81e48113865af86a6d290ad92931f3b217896bdf86ea?d=mp&s=160"},"body":"Verbose branch listing added for the sake of completeness.\n\nThis probably doesn't need a cover letter. The only difference with the\nprevious patch is the '/' fix and to add 'final' where safe to do so.\nThat is to say I only added final for lines modified by this patch.\n\nCharles O'Farrell (3):\n  Extract RefComparator to sort collection of Refs\n  Cleanup of Branch command ready for verbose mode\n  Verbose branch command\n\n .../src/org/spearce/jgit/pgm/Branch.java           |   61 +++++++++++++----\n .../src/org/spearce/jgit/lib/RefComparator.java    |   72 ++++++++++++++++++++\n .../src/org/spearce/jgit/lib/RefWriter.java        |   14 +----\n 3 files changed, 121 insertions(+), 26 deletions(-)\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java\n"},{"id":"87808","messageId":"1219230041-7998-2-git-send-email-charleso@charleso.org","threadId":"15104","inReplyTo":"1219230041-7998-1-git-send-email-charleso@charleso.org","subject":"[JGIT PATCH v2 1/3] Extract RefComparator to sort collection of Refs","fromName":"Charles O'Farrell","fromEmail":"charleso@charleso.org","sentAt":"2008-08-20T11:00:39Z","receivedAt":"2008-08-20T11:00:39Z","isPatch":true,"sender":{"key":"charleso@charleso.org","avatar":"https://gravatar.com/avatar/ce96d531daa37646a38a81e48113865af86a6d290ad92931f3b217896bdf86ea?d=mp&s=160"},"body":"Signed-off-by: Charles O'Farrell <charleso@charleso.org>\n---\n .../src/org/spearce/jgit/lib/RefComparator.java    |   72 ++++++++++++++++++++\n .../src/org/spearce/jgit/lib/RefWriter.java        |   14 +----\n 2 files changed, 73 insertions(+), 13 deletions(-)\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java\nnew file mode 100644\nindex 0000000..95e3e0f\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java\n@@ -0,0 +1,72 @@\n+/**\n+ * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>\n+ *\n+ * All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or\n+ * without modification, are permitted provided that the following\n+ * conditions are met:\n+ *\n+ * - Redistributions of source code must retain the above copyright\n+ *   notice, this list of conditions and the following disclaimer.\n+ *\n+ * - Redistributions in binary form must reproduce the above\n+ *   copyright notice, this list of conditions and the following\n+ *   disclaimer in the documentation and/or other materials provided\n+ *   with the distribution.\n+ *\n+ * - Neither the name of the Git Development Community nor the\n+ *   names of its contributors may be used to endorse or promote\n+ *   products derived from this software without specific prior\n+ *   written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\n+ * CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+package org.spearce.jgit.lib;\n+\n+import java.util.ArrayList;\n+import java.util.Collection;\n+import java.util.Collections;\n+import java.util.Comparator;\n+import java.util.List;\n+\n+/**\n+ * Util for sorting (or comparing) Ref instances by name.\n+ * <p>\n+ * Useful for command line tools or writing out refs to file.\n+ */\n+public class RefComparator implements Comparator<Ref> {\n+\n+\t/** Singleton instance of RefComparator */\n+\tpublic static final RefComparator INSTANCE = new RefComparator();\n+\n+\tpublic int compare(final Ref o1, final Ref o2) {\n+\t\treturn o1.getName().compareTo(o2.getName());\n+\t}\n+\n+\t/**\n+\t * Sorts the collection of refs, returning a new collection.\n+\t * \n+\t * @param refs\n+\t *            collection to be sorted\n+\t * @return sorted collection of refs\n+\t */\n+\tpublic static Collection<Ref> sort(final Collection<Ref> refs) {\n+\t\tfinal List<Ref> r = new ArrayList<Ref>(refs);\n+\t\tCollections.sort(r, INSTANCE);\n+\t\treturn r;\n+\t}\n+}\n\\ No newline at end of file\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java\nindex 9c784d5..2d39713 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java\n@@ -41,8 +41,6 @@\n import java.io.IOException;\n import java.io.StringWriter;\n import java.util.Collection;\n-import java.util.Comparator;\n-import java.util.TreeSet;\n \n /**\n  * Writes out refs to the {@link Constants#INFO_REFS} and\n@@ -61,8 +59,7 @@\n \t *            by applying updates to the advertised refs already discovered.\n \t */\n \tpublic RefWriter(Collection<Ref> refs) {\n-\t\tthis.refs = new TreeSet<Ref>(RefComparator.INSTANCE);\n-\t\tthis.refs.addAll(refs);\n+\t\tthis.refs = RefComparator.sort(refs);\n \t}\n \n \t/**\n@@ -163,13 +160,4 @@ public void writePackedRefs() throws IOException {\n \t */\n \tprotected abstract void writeFile(String file, byte[] content)\n \t\t\tthrows IOException;\n-\n-\tprivate static class RefComparator implements Comparator<Ref> {\n-\n-\t\tprivate static final RefComparator INSTANCE = new RefComparator();\n-\n-\t\tpublic int compare(Ref o1, Ref o2) {\n-\t\t\treturn o1.getName().compareTo(o2.getName());\n-\t\t}\n-\t}\n }\n-- \n1.6.0.2.g2ebc0\n"},{"id":"87810","messageId":"1219230041-7998-3-git-send-email-charleso@charleso.org","threadId":"15104","inReplyTo":"1219230041-7998-2-git-send-email-charleso@charleso.org","subject":"[JGIT PATCH v2 2/3] Cleanup of Branch command ready for verbose mode","fromName":"Charles O'Farrell","fromEmail":"charleso@charleso.org","sentAt":"2008-08-20T11:00:40Z","receivedAt":"2008-08-20T11:00:40Z","isPatch":true,"sender":{"key":"charleso@charleso.org","avatar":"https://gravatar.com/avatar/ce96d531daa37646a38a81e48113865af86a6d290ad92931f3b217896bdf86ea?d=mp&s=160"},"body":"Signed-off-by: Charles O'Farrell <charleso@charleso.org>\n---\n .../src/org/spearce/jgit/pgm/Branch.java           |   31 ++++++++++++++-----\n 1 files changed, 23 insertions(+), 8 deletions(-)\n\ndiff --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\nindex c89f510..7958be4 100644\n--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n@@ -39,15 +39,17 @@\n \n import java.io.IOException;\n import java.util.ArrayList;\n+import java.util.LinkedHashMap;\n import java.util.List;\n import java.util.Map;\n-import java.util.TreeSet;\n+import java.util.Map.Entry;\n \n import org.kohsuke.args4j.Argument;\n import org.kohsuke.args4j.Option;\n import org.spearce.jgit.lib.Constants;\n import org.spearce.jgit.lib.ObjectId;\n import org.spearce.jgit.lib.Ref;\n+import org.spearce.jgit.lib.RefComparator;\n import org.spearce.jgit.lib.RefUpdate;\n import org.spearce.jgit.lib.RefUpdate.Result;\n \n@@ -72,6 +74,8 @@\n \t@Argument\n \tprivate List<String> branches = new ArrayList<String>();\n \n+\tprivate final Map<String, Ref> printRefs = new LinkedHashMap<String, Ref>();\n+\n \t@Override\n \tprotected void run() throws Exception {\n \t\tif (delete || deleteForce)\n@@ -87,17 +91,28 @@ private void list() {\n \t\tif (head != null) {\n \t\t\tString current = head.getName();\n \t\t\tif (current.equals(Constants.HEAD))\n-\t\t\t\tprintHead(\"(no branch)\", true);\n-\t\t\tfor (String ref : new TreeSet<String>(refs.keySet())) {\n-\t\t\t\tif (isHead(ref))\n-\t\t\t\t\tprintHead(ref, current.equals(ref));\n+\t\t\t\taddRef(\"(no branch)\", head);\n+\t\t\taddRefs(refs, Constants.HEADS_PREFIX + '/', !remote);\n+\t\t\taddRefs(refs, Constants.REMOTES_PREFIX + '/', remote);\n+\t\t\tfor (final Entry<String, Ref> e : printRefs.entrySet()) {\n+\t\t\t\tprintHead(e.getKey(), current.equals(e.getValue().getName()));\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tprivate void addRefs(final Map<String, Ref> allRefs, final String prefix,\n+\t\t\tfinal boolean add) {\n+\t\tif (all || add) {\n+\t\t\tfor (final Ref ref : RefComparator.sort(allRefs.values())) {\n+\t\t\t\tfinal String name = ref.getName();\n+\t\t\t\tif (name.startsWith(prefix))\n+\t\t\t\t\taddRef(name, ref);\n \t\t\t}\n \t\t}\n \t}\n \n-\tprivate boolean isHead(String key) {\n-\t\treturn (all || !remote) && key.startsWith(Constants.HEADS_PREFIX)\n-\t\t\t\t|| (all || remote) && key.startsWith(Constants.REMOTES_PREFIX);\n+\tprivate void addRef(final String name, final Ref ref) {\n+\t\tprintRefs.put(name, ref);\n \t}\n \n \tprivate void printHead(String ref, boolean isCurrent) {\n-- \n1.6.0.2.g2ebc0\n"},{"id":"87809","messageId":"1219230041-7998-4-git-send-email-charleso@charleso.org","threadId":"15104","inReplyTo":"1219230041-7998-3-git-send-email-charleso@charleso.org","subject":"[JGIT PATCH v2 3/3] Verbose branch command","fromName":"Charles O'Farrell","fromEmail":"charleso@charleso.org","sentAt":"2008-08-20T11:00:41Z","receivedAt":"2008-08-20T11:00:41Z","isPatch":true,"sender":{"key":"charleso@charleso.org","avatar":"https://gravatar.com/avatar/ce96d531daa37646a38a81e48113865af86a6d290ad92931f3b217896bdf86ea?d=mp&s=160"},"body":"Signed-off-by: Charles O'Farrell <charleso@charleso.org>\n---\n .../src/org/spearce/jgit/pgm/Branch.java           |   34 +++++++++++++++----\n 1 files changed, 27 insertions(+), 7 deletions(-)\n\ndiff --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\nindex 7958be4..8123f89 100644\n--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n@@ -52,6 +52,7 @@\n import org.spearce.jgit.lib.RefComparator;\n import org.spearce.jgit.lib.RefUpdate;\n import org.spearce.jgit.lib.RefUpdate.Result;\n+import org.spearce.jgit.revwalk.RevWalk;\n \n @Command(common = true, usage = \"List, create, or delete branches\")\n class Branch extends TextBuiltin {\n@@ -76,15 +77,23 @@\n \n \tprivate final Map<String, Ref> printRefs = new LinkedHashMap<String, Ref>();\n \n+\t/** Only set for verbose branch listing at-the-moment */\n+\tprivate RevWalk rw;\n+\n+\tprivate int maxNameLength;\n+\n \t@Override\n \tprotected void run() throws Exception {\n \t\tif (delete || deleteForce)\n \t\t\tdelete(deleteForce);\n-\t\telse\n+\t\telse {\n+\t\t\tif (verbose)\n+\t\t\t\trw = new RevWalk(db);\n \t\t\tlist();\n+\t\t}\n \t}\n \n-\tprivate void list() {\n+\tprivate void list() throws Exception {\n \t\tMap<String, Ref> refs = db.getAllRefs();\n \t\tRef head = refs.get(Constants.HEAD);\n \t\t// This can happen if HEAD is stillborn\n@@ -95,7 +104,8 @@ private void list() {\n \t\t\taddRefs(refs, Constants.HEADS_PREFIX + '/', !remote);\n \t\t\taddRefs(refs, Constants.REMOTES_PREFIX + '/', remote);\n \t\t\tfor (final Entry<String, Ref> e : printRefs.entrySet()) {\n-\t\t\t\tprintHead(e.getKey(), current.equals(e.getValue().getName()));\n+\t\t\t\tfinal Ref ref = e.getValue();\n+\t\t\t\tprintHead(e.getKey(), current.equals(ref.getName()), ref);\n \t\t\t}\n \t\t}\n \t}\n@@ -106,20 +116,30 @@ private void addRefs(final Map<String, Ref> allRefs, final String prefix,\n \t\t\tfor (final Ref ref : RefComparator.sort(allRefs.values())) {\n \t\t\t\tfinal String name = ref.getName();\n \t\t\t\tif (name.startsWith(prefix))\n-\t\t\t\t\taddRef(name, ref);\n+\t\t\t\t\taddRef(name.substring(name.indexOf('/', 5) + 1), ref);\n \t\t\t}\n \t\t}\n \t}\n \n \tprivate void addRef(final String name, final Ref ref) {\n \t\tprintRefs.put(name, ref);\n+\t\tmaxNameLength = Math.max(maxNameLength, name.length());\n \t}\n \n-\tprivate void printHead(String ref, boolean isCurrent) {\n+\tprivate void printHead(final String ref, final boolean isCurrent,\n+\t\t\tfinal Ref refObj) throws Exception {\n \t\tout.print(isCurrent ? '*' : ' ');\n \t\tout.print(' ');\n-\t\tref = ref.substring(ref.indexOf('/', 5) + 1);\n-\t\tout.println(ref);\n+\t\tout.print(ref);\n+\t\tif (verbose) {\n+\t\t\tfinal int spaces = maxNameLength - ref.length() + 1;\n+\t\t\tout.print(String.format(\"%\" + spaces + \"s\", \"\"));\n+\t\t\tfinal ObjectId objectId = refObj.getObjectId();\n+\t\t\tout.print(objectId.toString().substring(0, 7));\n+\t\t\tout.print(' ');\n+\t\t\tout.print(rw.parseCommit(objectId).getShortMessage());\n+\t\t}\n+\t\tout.println();\n \t}\n \n \tprivate void delete(boolean force) throws IOException {\n-- \n1.6.0.2.g2ebc0\n"}]}