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 16KB

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