* [JGIT PATCH v2 0/3] Verbase branch command @ 2008-08-20 11:00 Charles O'Farrell 2008-08-20 11:00 ` [JGIT PATCH v2 1/3] Extract RefComparator to sort collection of Refs Charles O'Farrell 0 siblings, 1 reply; 4+ messages in thread From: Charles O'Farrell @ 2008-08-20 11:00 UTC (permalink / raw) To: git; +Cc: Charles O'Farrell Verbose branch listing added for the sake of completeness. This probably doesn't need a cover letter. The only difference with the previous patch is the '/' fix and to add 'final' where safe to do so. That is to say I only added final for lines modified by this patch. Charles O'Farrell (3): Extract RefComparator to sort collection of Refs Cleanup of Branch command ready for verbose mode Verbose branch command .../src/org/spearce/jgit/pgm/Branch.java | 61 +++++++++++++---- .../src/org/spearce/jgit/lib/RefComparator.java | 72 ++++++++++++++++++++ .../src/org/spearce/jgit/lib/RefWriter.java | 14 +---- 3 files changed, 121 insertions(+), 26 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java ^ permalink raw reply [flat|nested] 4+ messages in thread
* [JGIT PATCH v2 1/3] Extract RefComparator to sort collection of Refs 2008-08-20 11:00 [JGIT PATCH v2 0/3] Verbase branch command Charles O'Farrell @ 2008-08-20 11:00 ` Charles O'Farrell 2008-08-20 11:00 ` [JGIT PATCH v2 2/3] Cleanup of Branch command ready for verbose mode Charles O'Farrell 0 siblings, 1 reply; 4+ messages in thread From: Charles O'Farrell @ 2008-08-20 11:00 UTC (permalink / raw) To: git; +Cc: Charles O'Farrell Signed-off-by: Charles O'Farrell <charleso@charleso.org> --- .../src/org/spearce/jgit/lib/RefComparator.java | 72 ++++++++++++++++++++ .../src/org/spearce/jgit/lib/RefWriter.java | 14 +---- 2 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java new file mode 100644 index 0000000..95e3e0f --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefComparator.java @@ -0,0 +1,72 @@ +/** + * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org> + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.lib; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * Util for sorting (or comparing) Ref instances by name. + * <p> + * Useful for command line tools or writing out refs to file. + */ +public class RefComparator implements Comparator<Ref> { + + /** Singleton instance of RefComparator */ + public static final RefComparator INSTANCE = new RefComparator(); + + public int compare(final Ref o1, final Ref o2) { + return o1.getName().compareTo(o2.getName()); + } + + /** + * Sorts the collection of refs, returning a new collection. + * + * @param refs + * collection to be sorted + * @return sorted collection of refs + */ + public static Collection<Ref> sort(final Collection<Ref> refs) { + final List<Ref> r = new ArrayList<Ref>(refs); + Collections.sort(r, INSTANCE); + return r; + } +} \ No newline at end of file diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java index 9c784d5..2d39713 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefWriter.java @@ -41,8 +41,6 @@ import java.io.IOException; import java.io.StringWriter; import java.util.Collection; -import java.util.Comparator; -import java.util.TreeSet; /** * Writes out refs to the {@link Constants#INFO_REFS} and @@ -61,8 +59,7 @@ * by applying updates to the advertised refs already discovered. */ public RefWriter(Collection<Ref> refs) { - this.refs = new TreeSet<Ref>(RefComparator.INSTANCE); - this.refs.addAll(refs); + this.refs = RefComparator.sort(refs); } /** @@ -163,13 +160,4 @@ public void writePackedRefs() throws IOException { */ protected abstract void writeFile(String file, byte[] content) throws IOException; - - private static class RefComparator implements Comparator<Ref> { - - private static final RefComparator INSTANCE = new RefComparator(); - - public int compare(Ref o1, Ref o2) { - return o1.getName().compareTo(o2.getName()); - } - } } -- 1.6.0.2.g2ebc0 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [JGIT PATCH v2 2/3] Cleanup of Branch command ready for verbose mode 2008-08-20 11:00 ` [JGIT PATCH v2 1/3] Extract RefComparator to sort collection of Refs Charles O'Farrell @ 2008-08-20 11:00 ` Charles O'Farrell 2008-08-20 11:00 ` [JGIT PATCH v2 3/3] Verbose branch command Charles O'Farrell 0 siblings, 1 reply; 4+ messages in thread From: Charles O'Farrell @ 2008-08-20 11:00 UTC (permalink / raw) To: git; +Cc: Charles O'Farrell Signed-off-by: Charles O'Farrell <charleso@charleso.org> --- .../src/org/spearce/jgit/pgm/Branch.java | 31 ++++++++++++++----- 1 files changed, 23 insertions(+), 8 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 c89f510..7958be4 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 @@ -39,15 +39,17 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.TreeSet; +import java.util.Map.Entry; 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.RefComparator; import org.spearce.jgit.lib.RefUpdate; import org.spearce.jgit.lib.RefUpdate.Result; @@ -72,6 +74,8 @@ @Argument private List<String> branches = new ArrayList<String>(); + private final Map<String, Ref> printRefs = new LinkedHashMap<String, Ref>(); + @Override protected void run() throws Exception { if (delete || deleteForce) @@ -87,17 +91,28 @@ private void list() { if (head != null) { String current = head.getName(); if (current.equals(Constants.HEAD)) - printHead("(no branch)", true); - for (String ref : new TreeSet<String>(refs.keySet())) { - if (isHead(ref)) - printHead(ref, current.equals(ref)); + addRef("(no branch)", head); + addRefs(refs, Constants.HEADS_PREFIX + '/', !remote); + addRefs(refs, Constants.REMOTES_PREFIX + '/', remote); + for (final Entry<String, Ref> e : printRefs.entrySet()) { + printHead(e.getKey(), current.equals(e.getValue().getName())); + } + } + } + + private void addRefs(final Map<String, Ref> allRefs, final String prefix, + final boolean add) { + if (all || add) { + for (final Ref ref : RefComparator.sort(allRefs.values())) { + final String name = ref.getName(); + if (name.startsWith(prefix)) + addRef(name, ref); } } } - private boolean isHead(String key) { - return (all || !remote) && key.startsWith(Constants.HEADS_PREFIX) - || (all || remote) && key.startsWith(Constants.REMOTES_PREFIX); + private void addRef(final String name, final Ref ref) { + printRefs.put(name, ref); } private void printHead(String ref, boolean isCurrent) { -- 1.6.0.2.g2ebc0 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [JGIT PATCH v2 3/3] Verbose branch command 2008-08-20 11:00 ` [JGIT PATCH v2 2/3] Cleanup of Branch command ready for verbose mode Charles O'Farrell @ 2008-08-20 11:00 ` Charles O'Farrell 0 siblings, 0 replies; 4+ messages in thread From: Charles O'Farrell @ 2008-08-20 11:00 UTC (permalink / raw) To: git; +Cc: Charles O'Farrell 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 7958be4..8123f89 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 final 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 (final Entry<String, Ref> e : printRefs.entrySet()) { - printHead(e.getKey(), current.equals(e.getValue().getName())); + final Ref ref = e.getValue(); + printHead(e.getKey(), current.equals(ref.getName()), ref); } } } @@ -106,20 +116,30 @@ private void addRefs(final Map<String, Ref> allRefs, final String prefix, for (final Ref ref : RefComparator.sort(allRefs.values())) { final String name = ref.getName(); if (name.startsWith(prefix)) - addRef(name, ref); + addRef(name.substring(name.indexOf('/', 5) + 1), ref); } } } private void addRef(final String name, final Ref ref) { printRefs.put(name, ref); + maxNameLength = Math.max(maxNameLength, name.length()); } - private void printHead(String ref, boolean isCurrent) { + private void printHead(final String ref, final boolean isCurrent, + final Ref refObj) throws Exception { out.print(isCurrent ? '*' : ' '); out.print(' '); - ref = ref.substring(ref.indexOf('/', 5) + 1); - out.println(ref); + out.print(ref); + if (verbose) { + final int spaces = maxNameLength - ref.length() + 1; + out.print(String.format("%" + spaces + "s", "")); + final 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 ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2008-08-20 11:03 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2008-08-20 11:00 [JGIT PATCH v2 0/3] Verbase branch command Charles O'Farrell 2008-08-20 11:00 ` [JGIT PATCH v2 1/3] Extract RefComparator to sort collection of Refs Charles O'Farrell 2008-08-20 11:00 ` [JGIT PATCH v2 2/3] Cleanup of Branch command ready for verbose mode Charles O'Farrell 2008-08-20 11:00 ` [JGIT PATCH v2 3/3] Verbose branch command Charles O'Farrell
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).