1 /* 2 * Copyright (C) 2008-2010, Google Inc. 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 package org.eclipse.jgit.transport; 45 46 import static java.nio.charset.StandardCharsets.UTF_8; 47 48 import java.io.IOException; 49 import java.io.OutputStream; 50 import java.io.OutputStreamWriter; 51 import java.io.Writer; 52 import java.text.MessageFormat; 53 import java.util.HashSet; 54 import java.util.Map; 55 import java.util.Set; 56 import java.util.TreeMap; 57 58 import org.eclipse.jgit.internal.JGitText; 59 import org.eclipse.jgit.internal.storage.pack.PackWriter; 60 import org.eclipse.jgit.lib.AnyObjectId; 61 import org.eclipse.jgit.lib.Constants; 62 import org.eclipse.jgit.lib.ObjectId; 63 import org.eclipse.jgit.lib.ObjectReader; 64 import org.eclipse.jgit.lib.ProgressMonitor; 65 import org.eclipse.jgit.lib.Ref; 66 import org.eclipse.jgit.lib.Repository; 67 import org.eclipse.jgit.revwalk.RevCommit; 68 import org.eclipse.jgit.storage.pack.PackConfig; 69 70 /** 71 * Creates a Git bundle file, for sneaker-net transport to another system. 72 * <p> 73 * Bundles generated by this class can be later read in from a file URI using 74 * the bundle transport, or from an application controlled buffer by the more 75 * generic {@link org.eclipse.jgit.transport.TransportBundleStream}. 76 * <p> 77 * Applications creating bundles need to call one or more <code>include</code> 78 * calls to reflect which objects should be available as refs in the bundle for 79 * the other side to fetch. At least one include is required to create a valid 80 * bundle file, and duplicate names are not permitted. 81 * <p> 82 * Optional <code>assume</code> calls can be made to declare commits which the 83 * recipient must have in order to fetch from the bundle file. Objects reachable 84 * from these assumed commits can be used as delta bases in order to reduce the 85 * overall bundle size. 86 */ 87 public class BundleWriter { 88 private final Repository db; 89 90 private final ObjectReader reader; 91 92 private final Map<String, ObjectId> include; 93 94 private final Set<RevCommit> assume; 95 96 private final Set<ObjectId> tagTargets; 97 98 private PackConfig packConfig; 99 100 private ObjectCountCallback callback; 101 102 /** 103 * Create a writer for a bundle. 104 * 105 * @param repo 106 * repository where objects are stored. 107 */ 108 public BundleWriter(Repository repo) { 109 db = repo; 110 reader = null; 111 include = new TreeMap<>(); 112 assume = new HashSet<>(); 113 tagTargets = new HashSet<>(); 114 } 115 116 /** 117 * Create a writer for a bundle. 118 * 119 * @param or 120 * reader for reading objects. Will be closed at the end of {@link 121 * #writeBundle(ProgressMonitor, OutputStream)}, but readers may be 122 * reused after closing. 123 * @since 4.8 124 */ 125 public BundleWriter(ObjectReader or) { 126 db = null; 127 reader = or; 128 include = new TreeMap<>(); 129 assume = new HashSet<>(); 130 tagTargets = new HashSet<>(); 131 } 132 133 /** 134 * Set the configuration used by the pack generator. 135 * 136 * @param pc 137 * configuration controlling packing parameters. If null the 138 * source repository's settings will be used, or the default 139 * settings if constructed without a repo. 140 */ 141 public void setPackConfig(PackConfig pc) { 142 this.packConfig = pc; 143 } 144 145 /** 146 * Include an object (and everything reachable from it) in the bundle. 147 * 148 * @param name 149 * name the recipient can discover this object as from the 150 * bundle's list of advertised refs . The name must be a valid 151 * ref format and must not have already been included in this 152 * bundle writer. 153 * @param id 154 * object to pack. Multiple refs may point to the same object. 155 */ 156 public void include(String name, AnyObjectId id) { 157 boolean validRefName = Repository.isValidRefName(name) || Constants.HEAD.equals(name); 158 if (!validRefName) 159 throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidRefName, name)); 160 if (include.containsKey(name)) 161 throw new IllegalStateException(JGitText.get().duplicateRef + name); 162 include.put(name, id.toObjectId()); 163 } 164 165 /** 166 * Include a single ref (a name/object pair) in the bundle. 167 * <p> 168 * This is a utility function for: 169 * <code>include(r.getName(), r.getObjectId())</code>. 170 * 171 * @param r 172 * the ref to include. 173 */ 174 public void include(Ref r) { 175 include(r.getName(), r.getObjectId()); 176 177 if (r.getPeeledObjectId() != null) 178 tagTargets.add(r.getPeeledObjectId()); 179 180 else if (r.getObjectId() != null 181 && r.getName().startsWith(Constants.R_HEADS)) 182 tagTargets.add(r.getObjectId()); 183 } 184 185 /** 186 * Assume a commit is available on the recipient's side. 187 * <p> 188 * In order to fetch from a bundle the recipient must have any assumed 189 * commit. Each assumed commit is explicitly recorded in the bundle header 190 * to permit the recipient to validate it has these objects. 191 * 192 * @param c 193 * the commit to assume being available. This commit should be 194 * parsed and not disposed in order to maximize the amount of 195 * debugging information available in the bundle stream. 196 */ 197 public void assume(RevCommit c) { 198 if (c != null) 199 assume.add(c); 200 } 201 202 /** 203 * Generate and write the bundle to the output stream. 204 * <p> 205 * This method can only be called once per BundleWriter instance. 206 * 207 * @param monitor 208 * progress monitor to report bundle writing status to. 209 * @param os 210 * the stream the bundle is written to. The stream should be 211 * buffered by the caller. The caller is responsible for closing 212 * the stream. 213 * @throws java.io.IOException 214 * an error occurred reading a local object's data to include in 215 * the bundle, or writing compressed object data to the output 216 * stream. 217 */ 218 public void writeBundle(ProgressMonitor monitor, OutputStream os) 219 throws IOException { 220 try (PackWriter packWriter = newPackWriter()) { 221 packWriter.setObjectCountCallback(callback); 222 223 final HashSet<ObjectId> inc = new HashSet<>(); 224 final HashSet<ObjectId> exc = new HashSet<>(); 225 inc.addAll(include.values()); 226 for (RevCommit r : assume) 227 exc.add(r.getId()); 228 packWriter.setIndexDisabled(true); 229 packWriter.setDeltaBaseAsOffset(true); 230 packWriter.setThin(!exc.isEmpty()); 231 packWriter.setReuseValidatingObjects(false); 232 if (exc.isEmpty()) { 233 packWriter.setTagTargets(tagTargets); 234 } 235 packWriter.preparePack(monitor, inc, exc); 236 237 final Writer w = new OutputStreamWriter(os, UTF_8); 238 w.write(TransportBundle.V2_BUNDLE_SIGNATURE); 239 w.write('\n'); 240 241 final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH]; 242 for (RevCommit a : assume) { 243 w.write('-'); 244 a.copyTo(tmp, w); 245 if (a.getRawBuffer() != null) { 246 w.write(' '); 247 w.write(a.getShortMessage()); 248 } 249 w.write('\n'); 250 } 251 for (Map.Entry<String, ObjectId> e : include.entrySet()) { 252 e.getValue().copyTo(tmp, w); 253 w.write(' '); 254 w.write(e.getKey()); 255 w.write('\n'); 256 } 257 258 w.write('\n'); 259 w.flush(); 260 packWriter.writePack(monitor, monitor, os); 261 } 262 } 263 264 private PackWriter newPackWriter() { 265 PackConfig pc = packConfig; 266 if (pc == null) { 267 pc = db != null ? new PackConfig/PackConfig.html#PackConfig">PackConfig(db) : new PackConfig(); 268 } 269 return new PackWriter(pc, reader != null ? reader : db.newObjectReader()); 270 } 271 272 /** 273 * Set the {@link org.eclipse.jgit.transport.ObjectCountCallback}. 274 * <p> 275 * It should be set before calling 276 * {@link #writeBundle(ProgressMonitor, OutputStream)}. 277 * <p> 278 * This callback will be passed on to 279 * {@link org.eclipse.jgit.internal.storage.pack.PackWriter#setObjectCountCallback}. 280 * 281 * @param callback 282 * the callback to set 283 * @return this object for chaining. 284 * @since 4.1 285 */ 286 public BundleWriter setObjectCountCallback(ObjectCountCallback callback) { 287 this.callback = callback; 288 return this; 289 } 290 }