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

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