View Javadoc
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  
12  import java.io.IOException;
13  import java.io.OutputStream;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.eclipse.jgit.internal.storage.pack.CachedPack;
18  import org.eclipse.jgit.lib.ProgressMonitor;
19  import org.eclipse.jgit.transport.BundleWriter;
20  
21  /** Writes {@link DfsRepository} to a Git bundle. */
22  public class DfsBundleWriter {
23  	/**
24  	 * Writes the entire {@link DfsRepository} to a Git bundle.
25  	 * <p>
26  	 * This method try to avoid traversing the pack files as much as possible
27  	 * and dumps all objects as-is to a Git bundle.
28  	 *
29  	 * @param pm
30  	 *            progress monitor
31  	 * @param os
32  	 *            Git bundle output
33  	 * @param db
34  	 *            repository
35  	 * @throws IOException
36  	 *             thrown if the output stream throws one.
37  	 */
38  	public static void writeEntireRepositoryAsBundle(ProgressMonitor pm,
39  			OutputStream os, DfsRepository db) throws IOException {
40  		BundleWriter bw = new BundleWriter(db);
41  		db.getRefDatabase().getRefs().forEach(bw::include);
42  		List<CachedPack> packs = new ArrayList<>();
43  		for (DfsPackFile p : db.getObjectDatabase().getPacks()) {
44  			packs.add(new DfsCachedPack(p));
45  		}
46  		bw.addObjectsAsIs(packs);
47  		bw.writeBundle(pm, os);
48  	}
49  
50  	private DfsBundleWriter() {
51  	}
52  }