Search in sources :

Example 1 with TransportBundleStream

use of org.eclipse.jgit.transport.TransportBundleStream in project gerrit by GerritCodeReview.

the class AbstractDaemonTest method fetchFromBundles.

/**
 * Fetches each bundle into a newly cloned repository, then it applies the bundle, and returns the
 * resulting tree id.
 *
 * <p>Omits NoteDb meta refs.
 */
protected Map<BranchNameKey, ObjectId> fetchFromBundles(BinaryResult bundles) throws Exception {
    assertThat(bundles.getContentType()).isEqualTo("application/x-zip");
    FileSystem fs = Jimfs.newFileSystem();
    Path previewPath = fs.getPath("preview.zip");
    try (OutputStream out = Files.newOutputStream(previewPath)) {
        bundles.writeTo(out);
    }
    Map<BranchNameKey, ObjectId> ret = new HashMap<>();
    try (FileSystem zipFs = FileSystems.newFileSystem(previewPath, (ClassLoader) null);
        DirectoryStream<Path> dirStream = Files.newDirectoryStream(Iterables.getOnlyElement(zipFs.getRootDirectories()))) {
        for (Path p : dirStream) {
            if (!Files.isRegularFile(p)) {
                continue;
            }
            String bundleName = p.getFileName().toString();
            int len = bundleName.length();
            assertThat(bundleName).endsWith(".git");
            String repoName = bundleName.substring(0, len - 4);
            Project.NameKey proj = Project.nameKey(repoName);
            TestRepository<?> localRepo = cloneProject(proj);
            try (InputStream bundleStream = Files.newInputStream(p);
                TransportBundleStream tbs = new TransportBundleStream(localRepo.getRepository(), new URIish(bundleName), bundleStream)) {
                FetchResult fr = tbs.fetch(NullProgressMonitor.INSTANCE, Arrays.asList(new RefSpec("refs/*:refs/preview/*")));
                for (Ref r : fr.getAdvertisedRefs()) {
                    String refName = r.getName();
                    if (RefNames.isNoteDbMetaRef(refName)) {
                        continue;
                    }
                    RevCommit c = localRepo.getRevWalk().parseCommit(r.getObjectId());
                    ret.put(BranchNameKey.create(proj, refName), c.getTree().copy());
                }
            }
        }
    }
    assertThat(ret).isNotEmpty();
    return ret;
}
Also used : Path(java.nio.file.Path) URIish(org.eclipse.jgit.transport.URIish) FetchResult(org.eclipse.jgit.transport.FetchResult) ObjectId(org.eclipse.jgit.lib.ObjectId) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IdString(com.google.gerrit.extensions.restapi.IdString) Project(com.google.gerrit.entities.Project) Ref(org.eclipse.jgit.lib.Ref) RefSpec(org.eclipse.jgit.transport.RefSpec) BranchNameKey(com.google.gerrit.entities.BranchNameKey) FileSystem(java.nio.file.FileSystem) TransportBundleStream(org.eclipse.jgit.transport.TransportBundleStream) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

BranchNameKey (com.google.gerrit.entities.BranchNameKey)1 Project (com.google.gerrit.entities.Project)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 FileSystem (java.nio.file.FileSystem)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Ref (org.eclipse.jgit.lib.Ref)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 FetchResult (org.eclipse.jgit.transport.FetchResult)1 RefSpec (org.eclipse.jgit.transport.RefSpec)1 TransportBundleStream (org.eclipse.jgit.transport.TransportBundleStream)1 URIish (org.eclipse.jgit.transport.URIish)1