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.

RefTreeUpdate.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2016, 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.internal.storage.reftree;
  44. import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
  45. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  46. import java.io.IOException;
  47. import java.util.Collections;
  48. import org.eclipse.jgit.annotations.Nullable;
  49. import org.eclipse.jgit.errors.MissingObjectException;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.lib.ObjectIdRef;
  52. import org.eclipse.jgit.lib.Ref;
  53. import org.eclipse.jgit.lib.RefDatabase;
  54. import org.eclipse.jgit.lib.RefUpdate;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.eclipse.jgit.lib.SymbolicRef;
  57. import org.eclipse.jgit.revwalk.RevObject;
  58. import org.eclipse.jgit.revwalk.RevTag;
  59. import org.eclipse.jgit.revwalk.RevWalk;
  60. import org.eclipse.jgit.transport.ReceiveCommand;
  61. /** Single reference update to {@link RefTreeDatabase}. */
  62. class RefTreeUpdate extends RefUpdate {
  63. private final RefTreeDatabase refdb;
  64. private RevWalk rw;
  65. private RefTreeBatch batch;
  66. private Ref oldRef;
  67. RefTreeUpdate(RefTreeDatabase refdb, Ref ref) {
  68. super(ref);
  69. this.refdb = refdb;
  70. setCheckConflicting(false); // Done automatically by doUpdate.
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. protected RefDatabase getRefDatabase() {
  75. return refdb;
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. protected Repository getRepository() {
  80. return refdb.getRepository();
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. protected boolean tryLock(boolean deref) throws IOException {
  85. rw = new RevWalk(getRepository());
  86. batch = new RefTreeBatch(refdb);
  87. batch.init(rw);
  88. oldRef = batch.exactRef(rw.getObjectReader(), getName());
  89. if (oldRef != null && oldRef.getObjectId() != null) {
  90. setOldObjectId(oldRef.getObjectId());
  91. } else if (oldRef == null && getExpectedOldObjectId() != null) {
  92. setOldObjectId(ObjectId.zeroId());
  93. }
  94. return true;
  95. }
  96. /** {@inheritDoc} */
  97. @Override
  98. protected void unlock() {
  99. batch = null;
  100. if (rw != null) {
  101. rw.close();
  102. rw = null;
  103. }
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. protected Result doUpdate(Result desiredResult) throws IOException {
  108. return run(newRef(getName(), getNewObjectId()), desiredResult);
  109. }
  110. private Ref newRef(String name, ObjectId id)
  111. throws MissingObjectException, IOException {
  112. RevObject o = rw.parseAny(id);
  113. if (o instanceof RevTag) {
  114. RevObject p = rw.peel(o);
  115. return new ObjectIdRef.PeeledTag(LOOSE, name, id, p.copy());
  116. }
  117. return new ObjectIdRef.PeeledNonTag(LOOSE, name, id);
  118. }
  119. /** {@inheritDoc} */
  120. @Override
  121. protected Result doDelete(Result desiredResult) throws IOException {
  122. return run(null, desiredResult);
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. protected Result doLink(String target) throws IOException {
  127. Ref dst = new ObjectIdRef.Unpeeled(NEW, target, null);
  128. SymbolicRef n = new SymbolicRef(getName(), dst);
  129. Result desiredResult = getRef().getStorage() == NEW
  130. ? Result.NEW
  131. : Result.FORCED;
  132. return run(n, desiredResult);
  133. }
  134. private Result run(@Nullable Ref newRef, Result desiredResult)
  135. throws IOException {
  136. Command c = new Command(oldRef, newRef);
  137. batch.setRefLogIdent(getRefLogIdent());
  138. batch.setRefLogMessage(getRefLogMessage(), isRefLogIncludingResult());
  139. batch.execute(rw, Collections.singletonList(c));
  140. return translate(c.getResult(), desiredResult);
  141. }
  142. static Result translate(ReceiveCommand.Result r, Result desiredResult) {
  143. switch (r) {
  144. case OK:
  145. return desiredResult;
  146. case LOCK_FAILURE:
  147. return Result.LOCK_FAILURE;
  148. case NOT_ATTEMPTED:
  149. return Result.NOT_ATTEMPTED;
  150. case REJECTED_MISSING_OBJECT:
  151. return Result.IO_FAILURE;
  152. case REJECTED_CURRENT_BRANCH:
  153. return Result.REJECTED_CURRENT_BRANCH;
  154. case REJECTED_OTHER_REASON:
  155. case REJECTED_NOCREATE:
  156. case REJECTED_NODELETE:
  157. case REJECTED_NONFASTFORWARD:
  158. default:
  159. return Result.REJECTED;
  160. }
  161. }
  162. }