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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. ObjectId objectId = r.getObjectId();
  113. if (objectId == null) {
  114. // Symrefs to unborn branches aren't advertised in the info/refs
  115. // file.
  116. continue;
  117. }
  118. objectId.copyTo(tmp, w);
  119. w.write('\t');
  120. w.write(r.getName());
  121. w.write('\n');
  122. ObjectId peeledObjectId = r.getPeeledObjectId();
  123. if (peeledObjectId != null) {
  124. peeledObjectId.copyTo(tmp, w);
  125. w.write('\t');
  126. w.write(r.getName());
  127. w.write("^{}\n"); //$NON-NLS-1$
  128. }
  129. }
  130. writeFile(Constants.INFO_REFS, Constants.encode(w.toString()));
  131. }
  132. /**
  133. * Rebuild the {@link Constants#PACKED_REFS} file.
  134. * <p>
  135. * This method rebuilds the contents of the {@link Constants#PACKED_REFS}
  136. * file to match the passed list of references, including only those refs
  137. * that have a storage type of {@link Ref.Storage#PACKED}.
  138. *
  139. * @throws IOException
  140. * writing is not supported, or attempting to write the file
  141. * failed, possibly due to permissions or remote disk full, etc.
  142. */
  143. public void writePackedRefs() throws IOException {
  144. boolean peeled = false;
  145. for (final Ref r : refs) {
  146. if (r.getStorage().isPacked() && r.isPeeled()) {
  147. peeled = true;
  148. break;
  149. }
  150. }
  151. final StringWriter w = new StringWriter();
  152. if (peeled) {
  153. w.write(RefDirectory.PACKED_REFS_HEADER);
  154. if (peeled)
  155. w.write(RefDirectory.PACKED_REFS_PEELED);
  156. w.write('\n');
  157. }
  158. final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
  159. for (final Ref r : refs) {
  160. if (r.getStorage() != Ref.Storage.PACKED)
  161. continue;
  162. ObjectId objectId = r.getObjectId();
  163. if (objectId == null) {
  164. // A packed ref cannot be a symref, let alone a symref
  165. // to an unborn branch.
  166. throw new NullPointerException();
  167. }
  168. objectId.copyTo(tmp, w);
  169. w.write(' ');
  170. w.write(r.getName());
  171. w.write('\n');
  172. ObjectId peeledObjectId = r.getPeeledObjectId();
  173. if (peeledObjectId != null) {
  174. w.write('^');
  175. peeledObjectId.copyTo(tmp, w);
  176. w.write('\n');
  177. }
  178. }
  179. writeFile(Constants.PACKED_REFS, Constants.encode(w.toString()));
  180. }
  181. /**
  182. * Handles actual writing of ref files to the git repository, which may
  183. * differ slightly depending on the destination and transport.
  184. *
  185. * @param file
  186. * path to ref file.
  187. * @param content
  188. * byte content of file to be written.
  189. * @throws IOException
  190. */
  191. protected abstract void writeFile(String file, byte[] content)
  192. throws IOException;
  193. }