DfsBundleWriter.java

  1. /*
  2.  * Copyright (c) 2020, Google LLC  and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * http://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.internal.storage.dfs;

  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.util.ArrayList;
  14. import java.util.List;

  15. import org.eclipse.jgit.internal.storage.pack.CachedPack;
  16. import org.eclipse.jgit.lib.ProgressMonitor;
  17. import org.eclipse.jgit.transport.BundleWriter;

  18. /** Writes {@link DfsRepository} to a Git bundle. */
  19. public class DfsBundleWriter {
  20.     /**
  21.      * Writes the entire {@link DfsRepository} to a Git bundle.
  22.      * <p>
  23.      * This method try to avoid traversing the pack files as much as possible
  24.      * and dumps all objects as-is to a Git bundle.
  25.      *
  26.      * @param pm
  27.      *            progress monitor
  28.      * @param os
  29.      *            Git bundle output
  30.      * @param db
  31.      *            repository
  32.      * @throws IOException
  33.      *             thrown if the output stream throws one.
  34.      */
  35.     public static void writeEntireRepositoryAsBundle(ProgressMonitor pm,
  36.             OutputStream os, DfsRepository db) throws IOException {
  37.         BundleWriter bw = new BundleWriter(db);
  38.         db.getRefDatabase().getRefs().forEach(bw::include);
  39.         List<CachedPack> packs = new ArrayList<>();
  40.         for (DfsPackFile p : db.getObjectDatabase().getPacks()) {
  41.             packs.add(new DfsCachedPack(p));
  42.         }
  43.         bw.addObjectsAsIs(packs);
  44.         bw.writeBundle(pm, os);
  45.     }

  46.     private DfsBundleWriter() {
  47.     }
  48. }