import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import java.util.Set;
+
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.ObjectIdRef;
import org.eclipse.jgit.lib.Ref;
assertEquals(1000, peeledRef.getUpdateIndex());
}
}
+
+ @Test
+ public void sha1ToTip_ref() throws Exception {
+ InMemoryRepository repo = new InMemoryRepository(
+ new DfsRepositoryDescription());
+ try (TestRepository<InMemoryRepository> git = new TestRepository<>(
+ repo)) {
+ RevCommit commit = git.branch("master").commit()
+ .message("first commit").create();
+
+ Set<Ref> tipsWithSha1 = repo.getRefDatabase()
+ .getTipsWithSha1(commit.getId());
+ assertEquals(1, tipsWithSha1.size());
+ Ref ref = tipsWithSha1.iterator().next();
+ assertEquals(ref.getName(), "refs/heads/master");
+ assertEquals(commit.getId(), ref.getObjectId());
+ }
+ }
+
+ @Test
+ public void sha1ToTip_annotatedTag() throws Exception {
+ InMemoryRepository repo = new InMemoryRepository(
+ new DfsRepositoryDescription());
+ try (TestRepository<InMemoryRepository> git = new TestRepository<>(
+ repo)) {
+ RevCommit commit = git.commit()
+ .message("first commit").create();
+ RevTag tagObj = git.tag("v0.1", commit);
+ git.update("refs/tags/v0.1", tagObj);
+ Set<Ref> tipsWithSha1 = repo.getRefDatabase()
+ .getTipsWithSha1(commit.getId());
+ assertEquals(1, tipsWithSha1.size());
+ Ref ref = tipsWithSha1.iterator().next();
+ assertEquals(ref.getName(), "refs/tags/v0.1");
+ assertEquals(commit.getId(), ref.getPeeledObjectId());
+ }
+ }
+
+ @Test
+ public void sha1ToTip_tag() throws Exception {
+ InMemoryRepository repo = new InMemoryRepository(
+ new DfsRepositoryDescription());
+ try (TestRepository<InMemoryRepository> git = new TestRepository<>(
+ repo)) {
+ RevCommit commit = git.commit().message("first commit").create();
+ git.update("refs/tags/v0.2", commit);
+ Set<Ref> tipsWithSha1 = repo.getRefDatabase()
+ .getTipsWithSha1(commit.getId());
+ assertEquals(1, tipsWithSha1.size());
+ Ref ref = tipsWithSha1.iterator().next();
+ assertEquals(ref.getName(), "refs/tags/v0.2");
+ assertEquals(commit.getId(), ref.getObjectId());
+ }
+ }
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.util.Collection;
import java.util.List;
import java.util.Optional;
+import java.util.Set;
import java.util.TreeSet;
import org.eclipse.jgit.lib.Ref.Storage;
assertEquals(dst.isPeeled(), ref.isPeeled());
}
- private static void checkContainsRef(List<Ref> haystack, Ref needle) {
+ private static void checkContainsRef(Collection<Ref> haystack, Ref needle) {
for (Ref ref : haystack) {
if (ref.getName().equals(needle.getName()) &&
ref.getObjectId().equals(needle.getObjectId())) {
checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
checkContainsRef(refs, db.exactRef("refs/tags/A"));
}
+
+ @Test
+ public void testResolveTipSha1() throws IOException {
+ ObjectId masterId = db.resolve("refs/heads/master");
+ Set<Ref> resolved = db.getRefDatabase().getTipsWithSha1(masterId);
+
+ assertEquals(2, resolved.size());
+ checkContainsRef(resolved, db.exactRef("refs/heads/master"));
+ checkContainsRef(resolved, db.exactRef("HEAD"));
+
+ assertEquals(db.getRefDatabase()
+ .getTipsWithSha1(ObjectId.zeroId()).size(), 0);
+ }
}
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.jgit.annotations.Nullable;
return Collections.unmodifiableList(all);
}
+ /** {@inheritDoc} */
+ @Override
+ public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException {
+ if (!getReftableConfig().isIndexObjects()) {
+ return super.getTipsWithSha1(id);
+ }
+ lock.lock();
+ try {
+ RefCursor cursor = reader().byObjectId(id);
+ Set<Ref> refs = new HashSet<>();
+ while (cursor.next()) {
+ refs.add(cursor.getRef());
+ }
+ return refs;
+ } finally {
+ lock.unlock();
+ }
+ }
+
/** {@inheritDoc} */
@Override
public Ref peel(Ref ref) throws IOException {
package org.eclipse.jgit.lib;
import static java.util.stream.Collectors.toList;
+import static java.util.stream.Collectors.toSet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-
+import java.util.Set;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
return Collections.unmodifiableList(result);
}
+
+ /**
+ * Returns all refs that resolve directly to the given {@link ObjectId}.
+ * Includes peeled {@linkObjectId}s. This is the inverse lookup of
+ * {@link #exactRef(String...)}.
+ *
+ * <p>
+ * The default implementation uses a linear scan. Implementors of
+ * {@link RefDatabase} should override this method directly if a better
+ * implementation is possible.
+ *
+ * @param id
+ * {@link ObjectId} to resolve
+ * @return a {@link Set} of {@link Ref}s whose tips point to the provided
+ * id.
+ * @throws java.io.IOException
+ * the reference space cannot be accessed.
+ * @since 5.4
+ */
+ @NonNull
+ public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException {
+ return getRefs().stream().filter(r -> id.equals(r.getObjectId())
+ || id.equals(r.getPeeledObjectId())).collect(toSet());
+ }
+
/**
* Check if any refs exist in the ref database.
* <p>