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

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