View Javadoc
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 java.io.IOException;
47  import java.io.OutputStream;
48  import java.io.OutputStreamWriter;
49  import java.io.Writer;
50  import java.text.MessageFormat;
51  import java.util.HashSet;
52  import java.util.Map;
53  import java.util.Set;
54  import java.util.TreeMap;
55  
56  import org.eclipse.jgit.internal.JGitText;
57  import org.eclipse.jgit.internal.storage.pack.PackWriter;
58  import org.eclipse.jgit.lib.AnyObjectId;
59  import org.eclipse.jgit.lib.Constants;
60  import org.eclipse.jgit.lib.ObjectId;
61  import org.eclipse.jgit.lib.ProgressMonitor;
62  import org.eclipse.jgit.lib.Ref;
63  import org.eclipse.jgit.lib.Repository;
64  import org.eclipse.jgit.revwalk.RevCommit;
65  import org.eclipse.jgit.storage.pack.PackConfig;
66  
67  /**
68   * Creates a Git bundle file, for sneaker-net transport to another system.
69   * <p>
70   * Bundles generated by this class can be later read in from a file URI using
71   * the bundle transport, or from an application controlled buffer by the more
72   * generic {@link TransportBundleStream}.
73   * <p>
74   * Applications creating bundles need to call one or more <code>include</code>
75   * calls to reflect which objects should be available as refs in the bundle for
76   * the other side to fetch. At least one include is required to create a valid
77   * bundle file, and duplicate names are not permitted.
78   * <p>
79   * Optional <code>assume</code> calls can be made to declare commits which the
80   * recipient must have in order to fetch from the bundle file. Objects reachable
81   * from these assumed commits can be used as delta bases in order to reduce the
82   * overall bundle size.
83   */
84  public class BundleWriter {
85  	private final Repository db;
86  
87  	private final Map<String, ObjectId> include;
88  
89  	private final Set<RevCommit> assume;
90  
91  	private final Set<ObjectId> tagTargets;
92  
93  	private PackConfig packConfig;
94  
95  	/**
96  	 * Create a writer for a bundle.
97  	 *
98  	 * @param repo
99  	 *            repository where objects are stored.
100 	 */
101 	public BundleWriter(final Repository repo) {
102 		db = repo;
103 		include = new TreeMap<String, ObjectId>();
104 		assume = new HashSet<RevCommit>();
105 		tagTargets = new HashSet<ObjectId>();
106 	}
107 
108 	/**
109 	 * Set the configuration used by the pack generator.
110 	 *
111 	 * @param pc
112 	 *            configuration controlling packing parameters. If null the
113 	 *            source repository's settings will be used.
114 	 */
115 	public void setPackConfig(PackConfig pc) {
116 		this.packConfig = pc;
117 	}
118 
119 	/**
120 	 * Include an object (and everything reachable from it) in the bundle.
121 	 *
122 	 * @param name
123 	 *            name the recipient can discover this object as from the
124 	 *            bundle's list of advertised refs . The name must be a valid
125 	 *            ref format and must not have already been included in this
126 	 *            bundle writer.
127 	 * @param id
128 	 *            object to pack. Multiple refs may point to the same object.
129 	 */
130 	public void include(final String name, final AnyObjectId id) {
131 		boolean validRefName = Repository.isValidRefName(name) || Constants.HEAD.equals(name);
132 		if (!validRefName)
133 			throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidRefName, name));
134 		if (include.containsKey(name))
135 			throw new IllegalStateException(JGitText.get().duplicateRef + name);
136 		include.put(name, id.toObjectId());
137 	}
138 
139 	/**
140 	 * Include a single ref (a name/object pair) in the bundle.
141 	 * <p>
142 	 * This is a utility function for:
143 	 * <code>include(r.getName(), r.getObjectId())</code>.
144 	 *
145 	 * @param r
146 	 *            the ref to include.
147 	 */
148 	public void include(final Ref r) {
149 		include(r.getName(), r.getObjectId());
150 
151 		if (r.getPeeledObjectId() != null)
152 			tagTargets.add(r.getPeeledObjectId());
153 
154 		else if (r.getObjectId() != null
155 				&& r.getName().startsWith(Constants.R_HEADS))
156 			tagTargets.add(r.getObjectId());
157 	}
158 
159 	/**
160 	 * Assume a commit is available on the recipient's side.
161 	 * <p>
162 	 * In order to fetch from a bundle the recipient must have any assumed
163 	 * commit. Each assumed commit is explicitly recorded in the bundle header
164 	 * to permit the recipient to validate it has these objects.
165 	 *
166 	 * @param c
167 	 *            the commit to assume being available. This commit should be
168 	 *            parsed and not disposed in order to maximize the amount of
169 	 *            debugging information available in the bundle stream.
170 	 */
171 	public void assume(final RevCommit c) {
172 		if (c != null)
173 			assume.add(c);
174 	}
175 
176 	/**
177 	 * Generate and write the bundle to the output stream.
178 	 * <p>
179 	 * This method can only be called once per BundleWriter instance.
180 	 *
181 	 * @param monitor
182 	 *            progress monitor to report bundle writing status to.
183 	 * @param os
184 	 *            the stream the bundle is written to. The stream should be
185 	 *            buffered by the caller. The caller is responsible for closing
186 	 *            the stream.
187 	 * @throws IOException
188 	 *             an error occurred reading a local object's data to include in
189 	 *             the bundle, or writing compressed object data to the output
190 	 *             stream.
191 	 */
192 	public void writeBundle(ProgressMonitor monitor, OutputStream os)
193 			throws IOException {
194 		PackConfig pc = packConfig;
195 		if (pc == null)
196 			pc = new PackConfig(db);
197 		try (PackWriter packWriter = new PackWriter(pc, db.newObjectReader())) {
198 			final HashSet<ObjectId> inc = new HashSet<ObjectId>();
199 			final HashSet<ObjectId> exc = new HashSet<ObjectId>();
200 			inc.addAll(include.values());
201 			for (final RevCommit r : assume)
202 				exc.add(r.getId());
203 			packWriter.setIndexDisabled(true);
204 			packWriter.setDeltaBaseAsOffset(true);
205 			packWriter.setThin(exc.size() > 0);
206 			packWriter.setReuseValidatingObjects(false);
207 			if (exc.size() == 0)
208 				packWriter.setTagTargets(tagTargets);
209 			packWriter.preparePack(monitor, inc, exc);
210 
211 			final Writer w = new OutputStreamWriter(os, Constants.CHARSET);
212 			w.write(TransportBundle.V2_BUNDLE_SIGNATURE);
213 			w.write('\n');
214 
215 			final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
216 			for (final RevCommit a : assume) {
217 				w.write('-');
218 				a.copyTo(tmp, w);
219 				if (a.getRawBuffer() != null) {
220 					w.write(' ');
221 					w.write(a.getShortMessage());
222 				}
223 				w.write('\n');
224 			}
225 			for (final Map.Entry<String, ObjectId> e : include.entrySet()) {
226 				e.getValue().copyTo(tmp, w);
227 				w.write(' ');
228 				w.write(e.getKey());
229 				w.write('\n');
230 			}
231 
232 			w.write('\n');
233 			w.flush();
234 			packWriter.writePack(monitor, monitor, os);
235 		}
236 	}
237 }