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.

ReceiveCommand.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2008, 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.transport;
  44. import org.eclipse.jgit.lib.ObjectId;
  45. import org.eclipse.jgit.lib.Ref;
  46. /**
  47. * A command being processed by {@link ReceivePack}.
  48. * <p>
  49. * This command instance roughly translates to the server side representation of
  50. * the {@link RemoteRefUpdate} created by the client.
  51. */
  52. public class ReceiveCommand {
  53. /** Type of operation requested. */
  54. public static enum Type {
  55. /** Create a new ref; the ref must not already exist. */
  56. CREATE,
  57. /**
  58. * Update an existing ref with a fast-forward update.
  59. * <p>
  60. * During a fast-forward update no changes will be lost; only new
  61. * commits are inserted into the ref.
  62. */
  63. UPDATE,
  64. /**
  65. * Update an existing ref by potentially discarding objects.
  66. * <p>
  67. * The current value of the ref is not fully reachable from the new
  68. * value of the ref, so a successful command may result in one or more
  69. * objects becoming unreachable.
  70. */
  71. UPDATE_NONFASTFORWARD,
  72. /** Delete an existing ref; the ref should already exist. */
  73. DELETE;
  74. }
  75. /** Result of the update command. */
  76. public static enum Result {
  77. /** The command has not yet been attempted by the server. */
  78. NOT_ATTEMPTED,
  79. /** The server is configured to deny creation of this ref. */
  80. REJECTED_NOCREATE,
  81. /** The server is configured to deny deletion of this ref. */
  82. REJECTED_NODELETE,
  83. /** The update is a non-fast-forward update and isn't permitted. */
  84. REJECTED_NONFASTFORWARD,
  85. /** The update affects <code>HEAD</code> and cannot be permitted. */
  86. REJECTED_CURRENT_BRANCH,
  87. /**
  88. * One or more objects aren't in the repository.
  89. * <p>
  90. * This is severe indication of either repository corruption on the
  91. * server side, or a bug in the client wherein the client did not supply
  92. * all required objects during the pack transfer.
  93. */
  94. REJECTED_MISSING_OBJECT,
  95. /** Other failure; see {@link ReceiveCommand#getMessage()}. */
  96. REJECTED_OTHER_REASON,
  97. /** The ref could not be locked and updated atomically; try again. */
  98. LOCK_FAILURE,
  99. /** The change was completed successfully. */
  100. OK;
  101. }
  102. private final ObjectId oldId;
  103. private final ObjectId newId;
  104. private final String name;
  105. private Type type;
  106. private Ref ref;
  107. private Result status;
  108. private String message;
  109. /**
  110. * Create a new command for {@link ReceivePack}.
  111. *
  112. * @param oldId
  113. * the old object id; must not be null. Use
  114. * {@link ObjectId#zeroId()} to indicate a ref creation.
  115. * @param newId
  116. * the new object id; must not be null. Use
  117. * {@link ObjectId#zeroId()} to indicate a ref deletion.
  118. * @param name
  119. * name of the ref being affected.
  120. */
  121. public ReceiveCommand(final ObjectId oldId, final ObjectId newId,
  122. final String name) {
  123. this.oldId = oldId;
  124. this.newId = newId;
  125. this.name = name;
  126. type = Type.UPDATE;
  127. if (ObjectId.zeroId().equals(oldId))
  128. type = Type.CREATE;
  129. if (ObjectId.zeroId().equals(newId))
  130. type = Type.DELETE;
  131. status = Result.NOT_ATTEMPTED;
  132. }
  133. /** @return the old value the client thinks the ref has. */
  134. public ObjectId getOldId() {
  135. return oldId;
  136. }
  137. /** @return the requested new value for this ref. */
  138. public ObjectId getNewId() {
  139. return newId;
  140. }
  141. /** @return the name of the ref being updated. */
  142. public String getRefName() {
  143. return name;
  144. }
  145. /** @return the type of this command; see {@link Type}. */
  146. public Type getType() {
  147. return type;
  148. }
  149. /** @return the ref, if this was advertised by the connection. */
  150. public Ref getRef() {
  151. return ref;
  152. }
  153. /** @return the current status code of this command. */
  154. public Result getResult() {
  155. return status;
  156. }
  157. /** @return the message associated with a failure status. */
  158. public String getMessage() {
  159. return message;
  160. }
  161. /**
  162. * Set the status of this command.
  163. *
  164. * @param s
  165. * the new status code for this command.
  166. */
  167. public void setResult(final Result s) {
  168. setResult(s, null);
  169. }
  170. /**
  171. * Set the status of this command.
  172. *
  173. * @param s
  174. * new status code for this command.
  175. * @param m
  176. * optional message explaining the new status.
  177. */
  178. public void setResult(final Result s, final String m) {
  179. status = s;
  180. message = m;
  181. }
  182. void setRef(final Ref r) {
  183. ref = r;
  184. }
  185. void setType(final Type t) {
  186. type = t;
  187. }
  188. @Override
  189. public String toString() {
  190. return getType().name() + ": " + getOldId().name() + " "
  191. + getNewId().name() + " " + getRefName();
  192. }
  193. }