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.

RemoteRefUpdate.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  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 java.io.IOException;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.JGitText;
  47. import org.eclipse.jgit.lib.ObjectId;
  48. import org.eclipse.jgit.lib.Repository;
  49. import org.eclipse.jgit.revwalk.RevWalk;
  50. /**
  51. * Represent request and status of a remote ref update. Specification is
  52. * provided by client, while status is handled by {@link PushProcess} class,
  53. * being read-only for client.
  54. * <p>
  55. * Client can create instances of this class directly, basing on user
  56. * specification and advertised refs ({@link Connection} or through
  57. * {@link Transport} helper methods. Apply this specification on remote
  58. * repository using
  59. * {@link Transport#push(org.eclipse.jgit.lib.ProgressMonitor, java.util.Collection)}
  60. * method.
  61. * </p>
  62. *
  63. */
  64. public class RemoteRefUpdate {
  65. /**
  66. * Represent current status of a remote ref update.
  67. */
  68. public static enum Status {
  69. /**
  70. * Push process hasn't yet attempted to update this ref. This is the
  71. * default status, prior to push process execution.
  72. */
  73. NOT_ATTEMPTED,
  74. /**
  75. * Remote ref was up to date, there was no need to update anything.
  76. */
  77. UP_TO_DATE,
  78. /**
  79. * Remote ref update was rejected, as it would cause non fast-forward
  80. * update.
  81. */
  82. REJECTED_NONFASTFORWARD,
  83. /**
  84. * Remote ref update was rejected, because remote side doesn't
  85. * support/allow deleting refs.
  86. */
  87. REJECTED_NODELETE,
  88. /**
  89. * Remote ref update was rejected, because old object id on remote
  90. * repository wasn't the same as defined expected old object.
  91. */
  92. REJECTED_REMOTE_CHANGED,
  93. /**
  94. * Remote ref update was rejected for other reason, possibly described
  95. * in {@link RemoteRefUpdate#getMessage()}.
  96. */
  97. REJECTED_OTHER_REASON,
  98. /**
  99. * Remote ref didn't exist. Can occur on delete request of a non
  100. * existing ref.
  101. */
  102. NON_EXISTING,
  103. /**
  104. * Push process is awaiting update report from remote repository. This
  105. * is a temporary state or state after critical error in push process.
  106. */
  107. AWAITING_REPORT,
  108. /**
  109. * Remote ref was successfully updated.
  110. */
  111. OK;
  112. }
  113. private final ObjectId expectedOldObjectId;
  114. private final ObjectId newObjectId;
  115. private final String remoteName;
  116. private final TrackingRefUpdate trackingRefUpdate;
  117. private final String srcRef;
  118. private final boolean forceUpdate;
  119. private Status status;
  120. private boolean fastForward;
  121. private String message;
  122. private final Repository localDb;
  123. /**
  124. * Construct remote ref update request by providing an update specification.
  125. * Object is created with default {@link Status#NOT_ATTEMPTED} status and no
  126. * message.
  127. *
  128. * @param localDb
  129. * local repository to push from.
  130. * @param srcRef
  131. * source revision - any string resolvable by
  132. * {@link Repository#resolve(String)}. This resolves to the new
  133. * object that the caller want remote ref to be after update. Use
  134. * null or {@link ObjectId#zeroId()} string for delete request.
  135. * @param remoteName
  136. * full name of a remote ref to update, e.g. "refs/heads/master"
  137. * (no wildcard, no short name).
  138. * @param forceUpdate
  139. * true when caller want remote ref to be updated regardless
  140. * whether it is fast-forward update (old object is ancestor of
  141. * new object).
  142. * @param localName
  143. * optional full name of a local stored tracking branch, to
  144. * update after push, e.g. "refs/remotes/zawir/dirty" (no
  145. * wildcard, no short name); null if no local tracking branch
  146. * should be updated.
  147. * @param expectedOldObjectId
  148. * optional object id that caller is expecting, requiring to be
  149. * advertised by remote side before update; update will take
  150. * place ONLY if remote side advertise exactly this expected id;
  151. * null if caller doesn't care what object id remote side
  152. * advertise. Use {@link ObjectId#zeroId()} when expecting no
  153. * remote ref with this name.
  154. * @throws IOException
  155. * when I/O error occurred during creating
  156. * {@link TrackingRefUpdate} for local tracking branch or srcRef
  157. * can't be resolved to any object.
  158. * @throws IllegalArgumentException
  159. * if some required parameter was null
  160. */
  161. public RemoteRefUpdate(final Repository localDb, final String srcRef,
  162. final String remoteName, final boolean forceUpdate,
  163. final String localName, final ObjectId expectedOldObjectId)
  164. throws IOException {
  165. if (remoteName == null)
  166. throw new IllegalArgumentException(JGitText.get().remoteNameCantBeNull);
  167. this.srcRef = srcRef;
  168. this.newObjectId = (srcRef == null ? ObjectId.zeroId() : localDb
  169. .resolve(srcRef));
  170. if (newObjectId == null)
  171. throw new IOException(MessageFormat.format(
  172. JGitText.get().sourceRefDoesntResolveToAnyObject, srcRef));
  173. this.remoteName = remoteName;
  174. this.forceUpdate = forceUpdate;
  175. if (localName != null && localDb != null)
  176. trackingRefUpdate = new TrackingRefUpdate(localDb, localName,
  177. remoteName, true, newObjectId, "push");
  178. else
  179. trackingRefUpdate = null;
  180. this.localDb = localDb;
  181. this.expectedOldObjectId = expectedOldObjectId;
  182. this.status = Status.NOT_ATTEMPTED;
  183. }
  184. /**
  185. * Create a new instance of this object basing on existing instance for
  186. * configuration. State (like {@link #getMessage()}, {@link #getStatus()})
  187. * of base object is not shared. Expected old object id is set up from
  188. * scratch, as this constructor may be used for 2-stage push: first one
  189. * being dry run, second one being actual push.
  190. *
  191. * @param base
  192. * configuration base.
  193. * @param newExpectedOldObjectId
  194. * new expected object id value.
  195. * @throws IOException
  196. * when I/O error occurred during creating
  197. * {@link TrackingRefUpdate} for local tracking branch or srcRef
  198. * of base object no longer can be resolved to any object.
  199. */
  200. public RemoteRefUpdate(final RemoteRefUpdate base,
  201. final ObjectId newExpectedOldObjectId) throws IOException {
  202. this(base.localDb, base.srcRef, base.remoteName, base.forceUpdate,
  203. (base.trackingRefUpdate == null ? null : base.trackingRefUpdate
  204. .getLocalName()), newExpectedOldObjectId);
  205. }
  206. /**
  207. * @return expectedOldObjectId required to be advertised by remote side, as
  208. * set in constructor; may be null.
  209. */
  210. public ObjectId getExpectedOldObjectId() {
  211. return expectedOldObjectId;
  212. }
  213. /**
  214. * @return true if some object is required to be advertised by remote side,
  215. * as set in constructor; false otherwise.
  216. */
  217. public boolean isExpectingOldObjectId() {
  218. return expectedOldObjectId != null;
  219. }
  220. /**
  221. * @return newObjectId for remote ref, as set in constructor.
  222. */
  223. public ObjectId getNewObjectId() {
  224. return newObjectId;
  225. }
  226. /**
  227. * @return true if this update is deleting update; false otherwise.
  228. */
  229. public boolean isDelete() {
  230. return ObjectId.zeroId().equals(newObjectId);
  231. }
  232. /**
  233. * @return name of remote ref to update, as set in constructor.
  234. */
  235. public String getRemoteName() {
  236. return remoteName;
  237. }
  238. /**
  239. * @return local tracking branch update if localName was set in constructor.
  240. */
  241. public TrackingRefUpdate getTrackingRefUpdate() {
  242. return trackingRefUpdate;
  243. }
  244. /**
  245. * @return source revision as specified by user (in constructor), could be
  246. * any string parseable by {@link Repository#resolve(String)}; can
  247. * be null if specified that way in constructor - this stands for
  248. * delete request.
  249. */
  250. public String getSrcRef() {
  251. return srcRef;
  252. }
  253. /**
  254. * @return true if user specified a local tracking branch for remote update;
  255. * false otherwise.
  256. */
  257. public boolean hasTrackingRefUpdate() {
  258. return trackingRefUpdate != null;
  259. }
  260. /**
  261. * @return true if this update is forced regardless of old remote ref
  262. * object; false otherwise.
  263. */
  264. public boolean isForceUpdate() {
  265. return forceUpdate;
  266. }
  267. /**
  268. * @return status of remote ref update operation.
  269. */
  270. public Status getStatus() {
  271. return status;
  272. }
  273. /**
  274. * Check whether update was fast-forward. Note that this result is
  275. * meaningful only after successful update (when status is {@link Status#OK}).
  276. *
  277. * @return true if update was fast-forward; false otherwise.
  278. */
  279. public boolean isFastForward() {
  280. return fastForward;
  281. }
  282. /**
  283. * @return message describing reasons of status when needed/possible; may be
  284. * null.
  285. */
  286. public String getMessage() {
  287. return message;
  288. }
  289. void setStatus(final Status status) {
  290. this.status = status;
  291. }
  292. void setFastForward(boolean fastForward) {
  293. this.fastForward = fastForward;
  294. }
  295. void setMessage(final String message) {
  296. this.message = message;
  297. }
  298. /**
  299. * Update locally stored tracking branch with the new object.
  300. *
  301. * @param walk
  302. * walker used for checking update properties.
  303. * @throws IOException
  304. * when I/O error occurred during update
  305. */
  306. protected void updateTrackingRef(final RevWalk walk) throws IOException {
  307. if (isDelete())
  308. trackingRefUpdate.delete(walk);
  309. else
  310. trackingRefUpdate.update(walk);
  311. }
  312. @Override
  313. public String toString() {
  314. return "RemoteRefUpdate[remoteName=" + remoteName + ", " + status
  315. + ", " + (expectedOldObjectId!=null ? expectedOldObjectId.name() : "(null)")
  316. + "..." + (newObjectId != null ? newObjectId.name() : "(null)")
  317. + (fastForward ? ", fastForward" : "")
  318. + ", srcRef=" + srcRef + (forceUpdate ? ", forceUpdate" : "") + ", message=" + (message != null ? "\""
  319. + message + "\"" : "null") + "]";
  320. }
  321. }