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 6.9KB

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