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 org.eclipse.jgit.lib.AnyObjectId;
  14. import org.eclipse.jgit.lib.ObjectId;
  15. import org.eclipse.jgit.lib.RefUpdate;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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