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

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