{"thread":{"id":"14902","subject":"[PATCH] jgit: create a branch command","startedAt":"2008-08-09T06:04:28Z","lastAt":"2008-08-10T02:27:13Z","messageCount":2,"participants":["Charles O'Farrell","Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"86574","messageId":"1218261868-6508-1-git-send-email-charleso@charleso.org","threadId":"14902","inReplyTo":null,"subject":"[PATCH] jgit: create a branch command","fromName":"Charles O'Farrell","fromEmail":"charleso@charleso.org","sentAt":"2008-08-09T06:04:28Z","receivedAt":"2008-08-09T06:04:28Z","isPatch":true,"sender":{"key":"charleso@charleso.org","avatar":"https://gravatar.com/avatar/ce96d531daa37646a38a81e48113865af86a6d290ad92931f3b217896bdf86ea?d=mp&s=160"},"body":"This command lists the existing branches, highlighting the current branch with an asterisk. Currently only the -r and -a options are supported.\n\nSigned-off-by: Charles O'Farrell <charleso@charleso.org>\n---\n .../services/org.spearce.jgit.pgm.TextBuiltin      |    1 +\n .../src/org/spearce/jgit/pgm/Branch.java           |   83 ++++++++++++++++++++\n 2 files changed, 84 insertions(+), 0 deletions(-)\n create mode 100644 org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n\ndiff --git a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin\nindex 1ff5a30..734de3d 100644\n--- a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin\n+++ b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin\n@@ -1,3 +1,4 @@\n+org.spearce.jgit.pgm.Branch\n org.spearce.jgit.pgm.DiffTree\n org.spearce.jgit.pgm.Fetch\n org.spearce.jgit.pgm.Glog\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\nnew file mode 100644\nindex 0000000..8415fe2\n--- /dev/null\n+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n@@ -0,0 +1,83 @@\n+/*\n+ * Copyright (C) 2007, 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.pgm;\n+\n+import java.util.Map;\n+import java.util.TreeSet;\n+\n+import org.kohsuke.args4j.Option;\n+import org.spearce.jgit.lib.Constants;\n+import org.spearce.jgit.lib.Ref;\n+\n+@Command(common = true, usage = \"List, create, or delete branches\")\n+class Branch extends TextBuiltin {\n+\n+\t@Option(name = \"--remote\", aliases = { \"-r\" }, usage = \"act on remote-tracking branches\")\n+\tboolean remote = false;\n+\n+\t@Option(name = \"--all\", aliases = { \"-a\" }, usage = \"list both remote-tracking and local branches\")\n+\tboolean all = false;\n+\n+\t@Override\n+\tprotected void run() 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+\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}\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+\t}\n+\n+\tprivate void printHead(String ref, boolean isCurrent) {\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}\n+}\n-- \n1.5.6\n"},{"id":"86632","messageId":"20080810022713.GB22137@spearce.org","threadId":"14902","inReplyTo":"1218261868-6508-1-git-send-email-charleso@charleso.org","subject":"Re: [PATCH] jgit: create a branch command","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-08-10T02:27:13Z","receivedAt":"2008-08-10T02:27:13Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Charles O'Farrell <charleso@charleso.org> wrote:\n> This command lists the existing branches, highlighting the current branch with an asterisk. Currently only the -r and -a options are supported.\n> \n> Signed-off-by: Charles O'Farrell <charleso@charleso.org>\n\nLooks good, thanks.\n\n> diff --git a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin\n> index 1ff5a30..734de3d 100644\n> --- a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin\n> +++ b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin\n> @@ -1,3 +1,4 @@\n> +org.spearce.jgit.pgm.Branch\n>  org.spearce.jgit.pgm.DiffTree\n>  org.spearce.jgit.pgm.Fetch\n>  org.spearce.jgit.pgm.Glog\n> 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\n> new file mode 100644\n> index 0000000..8415fe2\n> --- /dev/null\n> +++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Branch.java\n> @@ -0,0 +1,83 @@\n> +/*\n> + * Copyright (C) 2007, 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.pgm;\n> +\n> +import java.util.Map;\n> +import java.util.TreeSet;\n> +\n> +import org.kohsuke.args4j.Option;\n> +import org.spearce.jgit.lib.Constants;\n> +import org.spearce.jgit.lib.Ref;\n> +\n> +@Command(common = true, usage = \"List, create, or delete branches\")\n> +class Branch extends TextBuiltin {\n> +\n> +\t@Option(name = \"--remote\", aliases = { \"-r\" }, usage = \"act on remote-tracking branches\")\n> +\tboolean remote = false;\n> +\n> +\t@Option(name = \"--all\", aliases = { \"-a\" }, usage = \"list both remote-tracking and local branches\")\n> +\tboolean all = false;\n> +\n> +\t@Override\n> +\tprotected void run() 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> +\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}\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> +\t}\n> +\n> +\tprivate void printHead(String ref, boolean isCurrent) {\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}\n> +}\n\n-- \nShawn.\n"}]}