]> source.dussan.org Git - jgit.git/commitdiff
Implement "git branch --contains" in pgm 20/20620/3
authorRobin Stocker <robin@nibor.org>
Tue, 14 Jan 2014 16:46:26 +0000 (17:46 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Fri, 7 Mar 2014 23:24:42 +0000 (18:24 -0500)
Bug: 425678
Change-Id: Ib59e05a0bde58562cc61e6e3000df761660b468e
Signed-off-by: Robin Stocker <robin@nibor.org>
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BranchTest.java
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java

index 5193aaa42d1cf9d8891cce0ab2c1d7712c86c284..4200cd05cf0b2d166e48779768bcb3f3babf9560 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012, IBM Corporation and others.
+ * Copyright (C) 2012, 2014 IBM Corporation and others.
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -46,6 +46,9 @@ import static org.junit.Assert.assertEquals;
 
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.RefUpdate;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -63,6 +66,26 @@ public class BranchTest extends CLIRepositoryTestCase {
                                execute("git branch -v")[0]);
        }
 
+       @Test
+       public void testListDetached() throws Exception {
+               RefUpdate updateRef = db.updateRef(Constants.HEAD, true);
+               updateRef.setNewObjectId(db.resolve("6fd41be"));
+               updateRef.update();
+               assertEquals("* (no branch) 6fd41be initial commit",
+                               execute("git branch -v")[0]);
+       }
+
+       @Test
+       public void testListContains() throws Exception {
+               new Git(db).branchCreate().setName("initial").call();
+               RevCommit second = new Git(db).commit().setMessage("second commit")
+                               .call();
+               assertArrayOfLinesEquals(new String[] { "  initial", "* master", "" },
+                               execute("git branch --contains 6fd41be"));
+               assertArrayOfLinesEquals(new String[] { "* master", "" },
+                               execute("git branch --contains " + second.name()));
+       }
+
        @Test
        public void testExistingBranch() throws Exception {
                assertEquals("fatal: A branch named 'master' already exists.",
index 53c1b352075240761d5f7d11ca8e24bc6cd589a0..372f1c1bf85f71f0f7d9d8234d821ffb39afcdfc 100644 (file)
@@ -303,6 +303,7 @@ usage_outputFile=Output file
 usage_path=path
 usage_performFsckStyleChecksOnReceive=perform fsck style checks on receive
 usage_portNumberToListenOn=port number to listen on
+usage_printOnlyBranchesThatContainTheCommit=print only branches that contain the commit
 usage_pruneStaleTrackingRefs=prune stale tracking refs
 usage_quiet=don't show progress messages
 usage_recordChangesToRepository=Record changes to the repository
index 0307a6073e2666d1c6ca63859c85a5cdc5141f00..7147544d1b31c57d71896aade96682fc851d7c82 100644 (file)
 
 package org.eclipse.jgit.pgm;
 
-import static org.eclipse.jgit.lib.RefDatabase.ALL;
-
 import java.io.IOException;
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.ListBranchCommand;
+import org.eclipse.jgit.api.ListBranchCommand.ListMode;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectReader;
@@ -60,8 +62,8 @@ import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.RefComparator;
 import org.eclipse.jgit.lib.RefRename;
 import org.eclipse.jgit.lib.RefUpdate;
-import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.lib.RefUpdate.Result;
+import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.pgm.internal.CLIText;
 import org.eclipse.jgit.pgm.opt.CmdLineParser;
 import org.eclipse.jgit.revwalk.RevWalk;
@@ -78,6 +80,9 @@ class Branch extends TextBuiltin {
        @Option(name = "--all", aliases = { "-a" }, usage = "usage_listBothRemoteTrackingAndLocalBranches")
        private boolean all = false;
 
+       @Option(name = "--contains", metaVar = "metaVar_commitish", usage = "usage_printOnlyBranchesThatContainTheCommit")
+       private String containsCommitish;
+
        @Option(name = "--delete", aliases = { "-d" }, usage = "usage_deleteFullyMergedBranch")
        private boolean delete = false;
 
@@ -177,15 +182,27 @@ class Branch extends TextBuiltin {
        }
 
        private void list() throws Exception {
-               Map<String, Ref> refs = db.getRefDatabase().getRefs(ALL);
-               Ref head = refs.get(Constants.HEAD);
+               Ref head = db.getRef(Constants.HEAD);
                // This can happen if HEAD is stillborn
                if (head != null) {
                        String current = head.getLeaf().getName();
-                       if (current.equals(Constants.HEAD))
-                               addRef("(no branch)", head); //$NON-NLS-1$
-                       addRefs(refs, Constants.R_HEADS, !remote);
-                       addRefs(refs, Constants.R_REMOTES, remote);
+                       ListBranchCommand command = new Git(db).branchList();
+                       if (all)
+                               command.setListMode(ListMode.ALL);
+                       else if (remote)
+                               command.setListMode(ListMode.REMOTE);
+
+                       if (containsCommitish != null)
+                               command.setContains(containsCommitish);
+
+                       List<Ref> refs = command.call();
+                       for (Ref ref : refs) {
+                               if (ref.getName().equals(Constants.HEAD))
+                                       addRef("(no branch)", head); //$NON-NLS-1$
+                       }
+
+                       addRefs(refs, Constants.R_HEADS);
+                       addRefs(refs, Constants.R_REMOTES);
 
                        ObjectReader reader = db.newObjectReader();
                        try {
@@ -200,14 +217,11 @@ class Branch extends TextBuiltin {
                }
        }
 
-       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.substring(name.indexOf('/', 5) + 1), ref);
-                       }
+       private void addRefs(final Collection<Ref> refs, final String prefix) {
+               for (final Ref ref : RefComparator.sort(refs)) {
+                       final String name = ref.getName();
+                       if (name.startsWith(prefix))
+                               addRef(name.substring(name.indexOf('/', 5) + 1), ref);
                }
        }