You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BundleWriter.java 8.1KB

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