Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SymbolicRef.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 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.lib;
  44. import org.eclipse.jgit.annotations.NonNull;
  45. import org.eclipse.jgit.annotations.Nullable;
  46. /**
  47. * A reference that indirectly points at another
  48. * {@link org.eclipse.jgit.lib.Ref}.
  49. * <p>
  50. * A symbolic reference always derives its current value from the target
  51. * reference.
  52. */
  53. public class SymbolicRef implements Ref {
  54. private final String name;
  55. private final Ref target;
  56. private final long updateIndex;
  57. /**
  58. * Create a new ref pairing.
  59. *
  60. * @param refName
  61. * name of this ref.
  62. * @param target
  63. * the ref we reference and derive our value from.
  64. */
  65. public SymbolicRef(@NonNull String refName, @NonNull Ref target) {
  66. this.name = refName;
  67. this.target = target;
  68. this.updateIndex = -1;
  69. }
  70. /**
  71. * Create a new ref pairing.
  72. *
  73. * @param refName
  74. * name of this ref.
  75. * @param target
  76. * the ref we reference and derive our value from.
  77. * @param updateIndex
  78. * index that increases with each update of the reference
  79. * @since 5.3
  80. */
  81. public SymbolicRef(@NonNull String refName, @NonNull Ref target,
  82. long updateIndex) {
  83. this.name = refName;
  84. this.target = target;
  85. this.updateIndex = updateIndex;
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. @NonNull
  90. public String getName() {
  91. return name;
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public boolean isSymbolic() {
  96. return true;
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. @NonNull
  101. public Ref getLeaf() {
  102. Ref dst = getTarget();
  103. while (dst.isSymbolic())
  104. dst = dst.getTarget();
  105. return dst;
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. @NonNull
  110. public Ref getTarget() {
  111. return target;
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. @Nullable
  116. public ObjectId getObjectId() {
  117. return getLeaf().getObjectId();
  118. }
  119. /** {@inheritDoc} */
  120. @Override
  121. @NonNull
  122. public Storage getStorage() {
  123. return Storage.LOOSE;
  124. }
  125. /** {@inheritDoc} */
  126. @Override
  127. @Nullable
  128. public ObjectId getPeeledObjectId() {
  129. return getLeaf().getPeeledObjectId();
  130. }
  131. /** {@inheritDoc} */
  132. @Override
  133. public boolean isPeeled() {
  134. return getLeaf().isPeeled();
  135. }
  136. /** {@inheritDoc} */
  137. @Override
  138. public long getUpdateIndex() {
  139. if (updateIndex == -1) {
  140. throw new UnsupportedOperationException();
  141. }
  142. return updateIndex;
  143. }
  144. /** {@inheritDoc} */
  145. @SuppressWarnings("nls")
  146. @Override
  147. public String toString() {
  148. StringBuilder r = new StringBuilder();
  149. r.append("SymbolicRef[");
  150. Ref cur = this;
  151. while (cur.isSymbolic()) {
  152. r.append(cur.getName());
  153. r.append(" -> ");
  154. cur = cur.getTarget();
  155. }
  156. r.append(cur.getName());
  157. r.append('=');
  158. r.append(ObjectId.toString(cur.getObjectId()));
  159. r.append("(");
  160. r.append(updateIndex); // Print value, even if -1
  161. r.append(")]");
  162. return r.toString();
  163. }
  164. }