選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RefWriter.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 org.eclipse.jgit.lib.Constants#INFO_REFS} and
  56. * {@link org.eclipse.jgit.lib.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. * <p>Constructor for RefWriter.</p>
  65. *
  66. * @param refs
  67. * the complete set of references. This should have been computed
  68. * by applying updates to the advertised refs already discovered.
  69. */
  70. public RefWriter(Collection<Ref> refs) {
  71. this.refs = RefComparator.sort(refs);
  72. }
  73. /**
  74. * <p>Constructor for RefWriter.</p>
  75. *
  76. * @param refs
  77. * the complete set of references. This should have been computed
  78. * by applying updates to the advertised refs already discovered.
  79. */
  80. public RefWriter(Map<String, Ref> refs) {
  81. if (refs instanceof RefMap)
  82. this.refs = refs.values();
  83. else
  84. this.refs = RefComparator.sort(refs.values());
  85. }
  86. /**
  87. * <p>Constructor for RefWriter.</p>
  88. *
  89. * @param refs
  90. * the complete set of references. This should have been computed
  91. * by applying updates to the advertised refs already discovered.
  92. */
  93. public RefWriter(RefList<Ref> refs) {
  94. this.refs = refs.asList();
  95. }
  96. /**
  97. * Rebuild the {@link org.eclipse.jgit.lib.Constants#INFO_REFS}.
  98. * <p>
  99. * This method rebuilds the contents of the
  100. * {@link org.eclipse.jgit.lib.Constants#INFO_REFS} file to match the passed
  101. * list of references.
  102. *
  103. * @throws java.io.IOException
  104. * writing is not supported, or attempting to write the file
  105. * failed, possibly due to permissions or remote disk full, etc.
  106. */
  107. public void writeInfoRefs() throws IOException {
  108. final StringWriter w = new StringWriter();
  109. final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
  110. for (Ref r : refs) {
  111. if (Constants.HEAD.equals(r.getName())) {
  112. // Historically HEAD has never been published through
  113. // the INFO_REFS file. This is a mistake, but its the
  114. // way things are.
  115. //
  116. continue;
  117. }
  118. ObjectId objectId = r.getObjectId();
  119. if (objectId == null) {
  120. // Symrefs to unborn branches aren't advertised in the info/refs
  121. // file.
  122. continue;
  123. }
  124. objectId.copyTo(tmp, w);
  125. w.write('\t');
  126. w.write(r.getName());
  127. w.write('\n');
  128. ObjectId peeledObjectId = r.getPeeledObjectId();
  129. if (peeledObjectId != null) {
  130. peeledObjectId.copyTo(tmp, w);
  131. w.write('\t');
  132. w.write(r.getName());
  133. w.write("^{}\n"); //$NON-NLS-1$
  134. }
  135. }
  136. writeFile(Constants.INFO_REFS, Constants.encode(w.toString()));
  137. }
  138. /**
  139. * Rebuild the {@link org.eclipse.jgit.lib.Constants#PACKED_REFS} file.
  140. * <p>
  141. * This method rebuilds the contents of the
  142. * {@link org.eclipse.jgit.lib.Constants#PACKED_REFS} file to match the
  143. * passed list of references, including only those refs that have a storage
  144. * type of {@link org.eclipse.jgit.lib.Ref.Storage#PACKED}.
  145. *
  146. * @throws java.io.IOException
  147. * writing is not supported, or attempting to write the file
  148. * failed, possibly due to permissions or remote disk full, etc.
  149. */
  150. public void writePackedRefs() throws IOException {
  151. boolean peeled = false;
  152. for (Ref r : refs) {
  153. if (r.getStorage().isPacked() && r.isPeeled()) {
  154. peeled = true;
  155. break;
  156. }
  157. }
  158. final StringWriter w = new StringWriter();
  159. if (peeled) {
  160. w.write(RefDirectory.PACKED_REFS_HEADER);
  161. if (peeled)
  162. w.write(RefDirectory.PACKED_REFS_PEELED);
  163. w.write('\n');
  164. }
  165. final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
  166. for (Ref r : refs) {
  167. if (r.getStorage() != Ref.Storage.PACKED)
  168. continue;
  169. ObjectId objectId = r.getObjectId();
  170. if (objectId == null) {
  171. // A packed ref cannot be a symref, let alone a symref
  172. // to an unborn branch.
  173. throw new NullPointerException();
  174. }
  175. objectId.copyTo(tmp, w);
  176. w.write(' ');
  177. w.write(r.getName());
  178. w.write('\n');
  179. ObjectId peeledObjectId = r.getPeeledObjectId();
  180. if (peeledObjectId != null) {
  181. w.write('^');
  182. peeledObjectId.copyTo(tmp, w);
  183. w.write('\n');
  184. }
  185. }
  186. writeFile(Constants.PACKED_REFS, Constants.encode(w.toString()));
  187. }
  188. /**
  189. * Handles actual writing of ref files to the git repository, which may
  190. * differ slightly depending on the destination and transport.
  191. *
  192. * @param file
  193. * path to ref file.
  194. * @param content
  195. * byte content of file to be written.
  196. * @throws java.io.IOException
  197. */
  198. protected abstract void writeFile(String file, byte[] content)
  199. throws IOException;
  200. }