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.

RefWriter.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>
  3. * Copyright (C) 2009-2010, Google Inc.
  4. * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.io.IOException;
  48. import java.io.StringWriter;
  49. import java.util.Collection;
  50. import java.util.Map;
  51. import org.eclipse.jgit.internal.storage.file.RefDirectory;
  52. import org.eclipse.jgit.util.RefList;
  53. import org.eclipse.jgit.util.RefMap;
  54. /**
  55. * Writes out refs to the {@link Constants#INFO_REFS} and
  56. * {@link Constants#PACKED_REFS} files.
  57. *
  58. * This class is abstract as the writing of the files must be handled by the
  59. * caller. This is because it is used by transport classes as well.
  60. */
  61. public abstract class RefWriter {
  62. private final Collection<Ref> refs;
  63. /**
  64. * @param refs
  65. * the complete set of references. This should have been computed
  66. * by applying updates to the advertised refs already discovered.
  67. */
  68. public RefWriter(Collection<Ref> refs) {
  69. this.refs = RefComparator.sort(refs);
  70. }
  71. /**
  72. * @param refs
  73. * the complete set of references. This should have been computed
  74. * by applying updates to the advertised refs already discovered.
  75. */
  76. public RefWriter(Map<String, Ref> refs) {
  77. if (refs instanceof RefMap)
  78. this.refs = refs.values();
  79. else
  80. this.refs = RefComparator.sort(refs.values());
  81. }
  82. /**
  83. * @param refs
  84. * the complete set of references. This should have been computed
  85. * by applying updates to the advertised refs already discovered.
  86. */
  87. public RefWriter(RefList<Ref> refs) {
  88. this.refs = refs.asList();
  89. }
  90. /**
  91. * Rebuild the {@link Constants#INFO_REFS}.
  92. * <p>
  93. * This method rebuilds the contents of the {@link Constants#INFO_REFS} file
  94. * to match the passed list of references.
  95. *
  96. *
  97. * @throws IOException
  98. * writing is not supported, or attempting to write the file
  99. * failed, possibly due to permissions or remote disk full, etc.
  100. */
  101. public void writeInfoRefs() throws IOException {
  102. final StringWriter w = new StringWriter();
  103. final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
  104. for (final Ref r : refs) {
  105. if (Constants.HEAD.equals(r.getName())) {
  106. // Historically HEAD has never been published through
  107. // the INFO_REFS file. This is a mistake, but its the
  108. // way things are.
  109. //
  110. continue;
  111. }
  112. r.getObjectId().copyTo(tmp, w);
  113. w.write('\t');
  114. w.write(r.getName());
  115. w.write('\n');
  116. if (r.getPeeledObjectId() != null) {
  117. r.getPeeledObjectId().copyTo(tmp, w);
  118. w.write('\t');
  119. w.write(r.getName());
  120. w.write("^{}\n"); //$NON-NLS-1$
  121. }
  122. }
  123. writeFile(Constants.INFO_REFS, Constants.encode(w.toString()));
  124. }
  125. /**
  126. * Rebuild the {@link Constants#PACKED_REFS} file.
  127. * <p>
  128. * This method rebuilds the contents of the {@link Constants#PACKED_REFS}
  129. * file to match the passed list of references, including only those refs
  130. * that have a storage type of {@link Ref.Storage#PACKED}.
  131. *
  132. * @throws IOException
  133. * writing is not supported, or attempting to write the file
  134. * failed, possibly due to permissions or remote disk full, etc.
  135. */
  136. public void writePackedRefs() throws IOException {
  137. boolean peeled = false;
  138. for (final Ref r : refs) {
  139. if (r.getStorage().isPacked() && r.isPeeled()) {
  140. peeled = true;
  141. break;
  142. }
  143. }
  144. final StringWriter w = new StringWriter();
  145. if (peeled) {
  146. w.write(RefDirectory.PACKED_REFS_HEADER);
  147. if (peeled)
  148. w.write(RefDirectory.PACKED_REFS_PEELED);
  149. w.write('\n');
  150. }
  151. final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
  152. for (final Ref r : refs) {
  153. if (r.getStorage() != Ref.Storage.PACKED)
  154. continue;
  155. r.getObjectId().copyTo(tmp, w);
  156. w.write(' ');
  157. w.write(r.getName());
  158. w.write('\n');
  159. if (r.getPeeledObjectId() != null) {
  160. w.write('^');
  161. r.getPeeledObjectId().copyTo(tmp, w);
  162. w.write('\n');
  163. }
  164. }
  165. writeFile(Constants.PACKED_REFS, Constants.encode(w.toString()));
  166. }
  167. /**
  168. * Handles actual writing of ref files to the git repository, which may
  169. * differ slightly depending on the destination and transport.
  170. *
  171. * @param file
  172. * path to ref file.
  173. * @param content
  174. * byte content of file to be written.
  175. * @throws IOException
  176. */
  177. protected abstract void writeFile(String file, byte[] content)
  178. throws IOException;
  179. }