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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.util.RefList;
  52. import org.eclipse.jgit.util.RefMap;
  53. /**
  54. * Writes out refs to the {@link Constants#INFO_REFS} and
  55. * {@link Constants#PACKED_REFS} files.
  56. *
  57. * This class is abstract as the writing of the files must be handled by the
  58. * caller. This is because it is used by transport classes as well.
  59. */
  60. public abstract class RefWriter {
  61. private final Collection<Ref> refs;
  62. /**
  63. * @param refs
  64. * the complete set of references. This should have been computed
  65. * by applying updates to the advertised refs already discovered.
  66. */
  67. public RefWriter(Collection<Ref> refs) {
  68. this.refs = RefComparator.sort(refs);
  69. }
  70. /**
  71. * @param refs
  72. * the complete set of references. This should have been computed
  73. * by applying updates to the advertised refs already discovered.
  74. */
  75. public RefWriter(Map<String, Ref> refs) {
  76. if (refs instanceof RefMap)
  77. this.refs = refs.values();
  78. else
  79. this.refs = RefComparator.sort(refs.values());
  80. }
  81. RefWriter(RefList<Ref> list) {
  82. this.refs = list.asList();
  83. }
  84. /**
  85. * Rebuild the {@link Constants#INFO_REFS}.
  86. * <p>
  87. * This method rebuilds the contents of the {@link Constants#INFO_REFS} file
  88. * to match the passed list of references.
  89. *
  90. *
  91. * @throws IOException
  92. * writing is not supported, or attempting to write the file
  93. * failed, possibly due to permissions or remote disk full, etc.
  94. */
  95. public void writeInfoRefs() throws IOException {
  96. final StringWriter w = new StringWriter();
  97. final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
  98. for (final Ref r : refs) {
  99. if (Constants.HEAD.equals(r.getName())) {
  100. // Historically HEAD has never been published through
  101. // the INFO_REFS file. This is a mistake, but its the
  102. // way things are.
  103. //
  104. continue;
  105. }
  106. r.getObjectId().copyTo(tmp, w);
  107. w.write('\t');
  108. w.write(r.getName());
  109. w.write('\n');
  110. if (r.getPeeledObjectId() != null) {
  111. r.getPeeledObjectId().copyTo(tmp, w);
  112. w.write('\t');
  113. w.write(r.getName());
  114. w.write("^{}\n");
  115. }
  116. }
  117. writeFile(Constants.INFO_REFS, Constants.encode(w.toString()));
  118. }
  119. /**
  120. * Rebuild the {@link Constants#PACKED_REFS} file.
  121. * <p>
  122. * This method rebuilds the contents of the {@link Constants#PACKED_REFS}
  123. * file to match the passed list of references, including only those refs
  124. * that have a storage type of {@link Ref.Storage#PACKED}.
  125. *
  126. * @throws IOException
  127. * writing is not supported, or attempting to write the file
  128. * failed, possibly due to permissions or remote disk full, etc.
  129. */
  130. public void writePackedRefs() throws IOException {
  131. boolean peeled = false;
  132. for (final Ref r : refs) {
  133. if (r.getStorage().isPacked() && r.isPeeled()) {
  134. peeled = true;
  135. break;
  136. }
  137. }
  138. final StringWriter w = new StringWriter();
  139. if (peeled) {
  140. w.write(RefDirectory.PACKED_REFS_HEADER);
  141. if (peeled)
  142. w.write(RefDirectory.PACKED_REFS_PEELED);
  143. w.write('\n');
  144. }
  145. final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
  146. for (final Ref r : refs) {
  147. if (r.getStorage() != Ref.Storage.PACKED)
  148. continue;
  149. r.getObjectId().copyTo(tmp, w);
  150. w.write(' ');
  151. w.write(r.getName());
  152. w.write('\n');
  153. if (r.getPeeledObjectId() != null) {
  154. w.write('^');
  155. r.getPeeledObjectId().copyTo(tmp, w);
  156. w.write('\n');
  157. }
  158. }
  159. writeFile(Constants.PACKED_REFS, Constants.encode(w.toString()));
  160. }
  161. /**
  162. * Handles actual writing of ref files to the git repository, which may
  163. * differ slightly depending on the destination and transport.
  164. *
  165. * @param file
  166. * path to ref file.
  167. * @param content
  168. * byte content of file to be written.
  169. * @throws IOException
  170. */
  171. protected abstract void writeFile(String file, byte[] content)
  172. throws IOException;
  173. }