TrackingRefUpdate.java

  1. /*
  2.  * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  3.  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  5.  *
  6.  * This program and the accompanying materials are made available under the
  7.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  8.  * https://www.eclipse.org/org/documents/edl-v10.php.
  9.  *
  10.  * SPDX-License-Identifier: BSD-3-Clause
  11.  */

  12. package org.eclipse.jgit.transport;

  13. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;

  14. import org.eclipse.jgit.lib.AnyObjectId;
  15. import org.eclipse.jgit.lib.ObjectId;
  16. import org.eclipse.jgit.lib.RefUpdate;

  17. /**
  18.  * Update of a locally stored tracking branch.
  19.  */
  20. public class TrackingRefUpdate {
  21.     private final String remoteName;
  22.     final String localName;
  23.     boolean forceUpdate;
  24.     ObjectId oldObjectId;
  25.     ObjectId newObjectId;

  26.     private RefUpdate.Result result;
  27.     private ReceiveCommand cmd;

  28.     TrackingRefUpdate(
  29.             boolean canForceUpdate,
  30.             String remoteName,
  31.             String localName,
  32.             AnyObjectId oldValue,
  33.             AnyObjectId newValue) {
  34.         this.remoteName = remoteName;
  35.         this.localName = localName;
  36.         this.forceUpdate = canForceUpdate;
  37.         this.oldObjectId = oldValue.copy();
  38.         this.newObjectId = newValue.copy();
  39.     }

  40.     /**
  41.      * Get the name of the remote ref.
  42.      * <p>
  43.      * Usually this is of the form "refs/heads/master".
  44.      *
  45.      * @return the name used within the remote repository.
  46.      */
  47.     public String getRemoteName() {
  48.         return remoteName;
  49.     }

  50.     /**
  51.      * Get the name of the local tracking ref.
  52.      * <p>
  53.      * Usually this is of the form "refs/remotes/origin/master".
  54.      *
  55.      * @return the name used within this local repository.
  56.      */
  57.     public String getLocalName() {
  58.         return localName;
  59.     }

  60.     /**
  61.      * Get the new value the ref will be (or was) updated to.
  62.      *
  63.      * @return new value. Null if the caller has not configured it.
  64.      */
  65.     public ObjectId getNewObjectId() {
  66.         return newObjectId;
  67.     }

  68.     /**
  69.      * The old value of the ref, prior to the update being attempted.
  70.      * <p>
  71.      * This value may differ before and after the update method. Initially it is
  72.      * populated with the value of the ref before the lock is taken, but the old
  73.      * value may change if someone else modified the ref between the time we
  74.      * last read it and when the ref was locked for update.
  75.      *
  76.      * @return the value of the ref prior to the update being attempted.
  77.      */
  78.     public ObjectId getOldObjectId() {
  79.         return oldObjectId;
  80.     }

  81.     /**
  82.      * Get the status of this update.
  83.      *
  84.      * @return the status of the update.
  85.      */
  86.     public RefUpdate.Result getResult() {
  87.         return result;
  88.     }

  89.     void setResult(RefUpdate.Result result) {
  90.         this.result = result;
  91.     }

  92.     /**
  93.      * Get this update wrapped by a ReceiveCommand.
  94.      *
  95.      * @return this update wrapped by a ReceiveCommand.
  96.      * @since 3.4
  97.      */
  98.     public ReceiveCommand asReceiveCommand() {
  99.         if (cmd == null)
  100.             cmd = new Command();
  101.         return cmd;
  102.     }

  103.     final class Command extends ReceiveCommand {
  104.         Command() {
  105.             super(oldObjectId, newObjectId, localName);
  106.         }

  107.         boolean canForceUpdate() {
  108.             return forceUpdate;
  109.         }

  110.         @Override
  111.         public void setResult(RefUpdate.Result status) {
  112.             result = status;
  113.             super.setResult(status);
  114.         }

  115.         @Override
  116.         public void setResult(ReceiveCommand.Result status) {
  117.             result = decode(status);
  118.             super.setResult(status);
  119.         }

  120.         @Override
  121.         public void setResult(ReceiveCommand.Result status, String msg) {
  122.             result = decode(status);
  123.             super.setResult(status, msg);
  124.         }

  125.         private RefUpdate.Result decode(ReceiveCommand.Result status) {
  126.             switch (status) {
  127.             case OK:
  128.                 if (AnyObjectId.isEqual(oldObjectId, newObjectId))
  129.                     return RefUpdate.Result.NO_CHANGE;
  130.                 switch (getType()) {
  131.                 case CREATE:
  132.                     return RefUpdate.Result.NEW;
  133.                 case UPDATE:
  134.                     return RefUpdate.Result.FAST_FORWARD;
  135.                 case DELETE:
  136.                 case UPDATE_NONFASTFORWARD:
  137.                 default:
  138.                     return RefUpdate.Result.FORCED;
  139.                 }

  140.             case REJECTED_NOCREATE:
  141.             case REJECTED_NODELETE:
  142.             case REJECTED_NONFASTFORWARD:
  143.                 return RefUpdate.Result.REJECTED;
  144.             case REJECTED_CURRENT_BRANCH:
  145.                 return RefUpdate.Result.REJECTED_CURRENT_BRANCH;
  146.             case REJECTED_MISSING_OBJECT:
  147.                 return RefUpdate.Result.IO_FAILURE;
  148.             case LOCK_FAILURE:
  149.             case NOT_ATTEMPTED:
  150.             case REJECTED_OTHER_REASON:
  151.             default:
  152.                 return RefUpdate.Result.LOCK_FAILURE;
  153.             }
  154.         }
  155.     }

  156.     /** {@inheritDoc} */
  157.     @SuppressWarnings("nls")
  158.     @Override
  159.     public String toString() {
  160.         StringBuilder sb = new StringBuilder();
  161.         sb.append("TrackingRefUpdate[");
  162.         sb.append(remoteName);
  163.         sb.append(" -> ");
  164.         sb.append(localName);
  165.         if (forceUpdate)
  166.             sb.append(" (forced)");
  167.         sb.append(" ");
  168.         sb.append(oldObjectId == null ? ""
  169.                 : oldObjectId.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
  170.                         .name());
  171.         sb.append("..");
  172.         sb.append(newObjectId == null ? ""
  173.                 : newObjectId.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
  174.                         .name());
  175.         sb.append("]");
  176.         return sb.toString();
  177.     }
  178. }