Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RefTreeUpdate.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. @Override
  73. protected RefDatabase getRefDatabase() {
  74. return refdb;
  75. }
  76. @Override
  77. protected Repository getRepository() {
  78. return refdb.getRepository();
  79. }
  80. @Override
  81. protected boolean tryLock(boolean deref) throws IOException {
  82. rw = new RevWalk(getRepository());
  83. batch = new RefTreeBatch(refdb);
  84. batch.init(rw);
  85. oldRef = batch.exactRef(rw.getObjectReader(), getName());
  86. if (oldRef != null && oldRef.getObjectId() != null) {
  87. setOldObjectId(oldRef.getObjectId());
  88. } else if (oldRef == null && getExpectedOldObjectId() != null) {
  89. setOldObjectId(ObjectId.zeroId());
  90. }
  91. return true;
  92. }
  93. @Override
  94. protected void unlock() {
  95. batch = null;
  96. if (rw != null) {
  97. rw.close();
  98. rw = null;
  99. }
  100. }
  101. @Override
  102. protected Result doUpdate(Result desiredResult) throws IOException {
  103. return run(newRef(getName(), getNewObjectId()), desiredResult);
  104. }
  105. private Ref newRef(String name, ObjectId id)
  106. throws MissingObjectException, IOException {
  107. RevObject o = rw.parseAny(id);
  108. if (o instanceof RevTag) {
  109. RevObject p = rw.peel(o);
  110. return new ObjectIdRef.PeeledTag(LOOSE, name, id, p.copy());
  111. }
  112. return new ObjectIdRef.PeeledNonTag(LOOSE, name, id);
  113. }
  114. @Override
  115. protected Result doDelete(Result desiredResult) throws IOException {
  116. return run(null, desiredResult);
  117. }
  118. @Override
  119. protected Result doLink(String target) throws IOException {
  120. Ref dst = new ObjectIdRef.Unpeeled(NEW, target, null);
  121. SymbolicRef n = new SymbolicRef(getName(), dst);
  122. Result desiredResult = getRef().getStorage() == NEW
  123. ? Result.NEW
  124. : Result.FORCED;
  125. return run(n, desiredResult);
  126. }
  127. private Result run(@Nullable Ref newRef, Result desiredResult)
  128. throws IOException {
  129. Command c = new Command(oldRef, newRef);
  130. batch.setRefLogIdent(getRefLogIdent());
  131. batch.setRefLogMessage(getRefLogMessage(), isRefLogIncludingResult());
  132. batch.execute(rw, Collections.singletonList(c));
  133. return translate(c.getResult(), desiredResult);
  134. }
  135. static Result translate(ReceiveCommand.Result r, Result desiredResult) {
  136. switch (r) {
  137. case OK:
  138. return desiredResult;
  139. case LOCK_FAILURE:
  140. return Result.LOCK_FAILURE;
  141. case NOT_ATTEMPTED:
  142. return Result.NOT_ATTEMPTED;
  143. case REJECTED_MISSING_OBJECT:
  144. return Result.IO_FAILURE;
  145. case REJECTED_CURRENT_BRANCH:
  146. return Result.REJECTED_CURRENT_BRANCH;
  147. case REJECTED_OTHER_REASON:
  148. case REJECTED_NOCREATE:
  149. case REJECTED_NODELETE:
  150. case REJECTED_NONFASTFORWARD:
  151. default:
  152. return Result.REJECTED;
  153. }
  154. }
  155. }