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>
  5.  * and other copyright owners as documented in the project's IP log.
  6.  *
  7.  * This program and the accompanying materials are made available
  8.  * under the terms of the Eclipse Distribution License v1.0 which
  9.  * accompanies this distribution, is reproduced below, and is
  10.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  11.  *
  12.  * All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or
  15.  * without modification, are permitted provided that the following
  16.  * conditions are met:
  17.  *
  18.  * - Redistributions of source code must retain the above copyright
  19.  *   notice, this list of conditions and the following disclaimer.
  20.  *
  21.  * - Redistributions in binary form must reproduce the above
  22.  *   copyright notice, this list of conditions and the following
  23.  *   disclaimer in the documentation and/or other materials provided
  24.  *   with the distribution.
  25.  *
  26.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  27.  *   names of its contributors may be used to endorse or promote
  28.  *   products derived from this software without specific prior
  29.  *   written permission.
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  */

  45. package org.eclipse.jgit.transport;

  46. import org.eclipse.jgit.lib.AnyObjectId;
  47. import org.eclipse.jgit.lib.ObjectId;
  48. import org.eclipse.jgit.lib.RefUpdate;

  49. /**
  50.  * Update of a locally stored tracking branch.
  51.  */
  52. public class TrackingRefUpdate {
  53.     private final String remoteName;
  54.     final String localName;
  55.     boolean forceUpdate;
  56.     ObjectId oldObjectId;
  57.     ObjectId newObjectId;

  58.     private RefUpdate.Result result;
  59.     private ReceiveCommand cmd;

  60.     TrackingRefUpdate(
  61.             boolean canForceUpdate,
  62.             String remoteName,
  63.             String localName,
  64.             AnyObjectId oldValue,
  65.             AnyObjectId newValue) {
  66.         this.remoteName = remoteName;
  67.         this.localName = localName;
  68.         this.forceUpdate = canForceUpdate;
  69.         this.oldObjectId = oldValue.copy();
  70.         this.newObjectId = newValue.copy();
  71.     }

  72.     /**
  73.      * Get the name of the remote ref.
  74.      * <p>
  75.      * Usually this is of the form "refs/heads/master".
  76.      *
  77.      * @return the name used within the remote repository.
  78.      */
  79.     public String getRemoteName() {
  80.         return remoteName;
  81.     }

  82.     /**
  83.      * Get the name of the local tracking ref.
  84.      * <p>
  85.      * Usually this is of the form "refs/remotes/origin/master".
  86.      *
  87.      * @return the name used within this local repository.
  88.      */
  89.     public String getLocalName() {
  90.         return localName;
  91.     }

  92.     /**
  93.      * Get the new value the ref will be (or was) updated to.
  94.      *
  95.      * @return new value. Null if the caller has not configured it.
  96.      */
  97.     public ObjectId getNewObjectId() {
  98.         return newObjectId;
  99.     }

  100.     /**
  101.      * The old value of the ref, prior to the update being attempted.
  102.      * <p>
  103.      * This value may differ before and after the update method. Initially it is
  104.      * populated with the value of the ref before the lock is taken, but the old
  105.      * value may change if someone else modified the ref between the time we
  106.      * last read it and when the ref was locked for update.
  107.      *
  108.      * @return the value of the ref prior to the update being attempted.
  109.      */
  110.     public ObjectId getOldObjectId() {
  111.         return oldObjectId;
  112.     }

  113.     /**
  114.      * Get the status of this update.
  115.      *
  116.      * @return the status of the update.
  117.      */
  118.     public RefUpdate.Result getResult() {
  119.         return result;
  120.     }

  121.     void setResult(RefUpdate.Result result) {
  122.         this.result = result;
  123.     }

  124.     /**
  125.      * Get this update wrapped by a ReceiveCommand.
  126.      *
  127.      * @return this update wrapped by a ReceiveCommand.
  128.      * @since 3.4
  129.      */
  130.     public ReceiveCommand asReceiveCommand() {
  131.         if (cmd == null)
  132.             cmd = new Command();
  133.         return cmd;
  134.     }

  135.     final class Command extends ReceiveCommand {
  136.         Command() {
  137.             super(oldObjectId, newObjectId, localName);
  138.         }

  139.         boolean canForceUpdate() {
  140.             return forceUpdate;
  141.         }

  142.         @Override
  143.         public void setResult(RefUpdate.Result status) {
  144.             result = status;
  145.             super.setResult(status);
  146.         }

  147.         @Override
  148.         public void setResult(ReceiveCommand.Result status) {
  149.             result = decode(status);
  150.             super.setResult(status);
  151.         }

  152.         @Override
  153.         public void setResult(ReceiveCommand.Result status, String msg) {
  154.             result = decode(status);
  155.             super.setResult(status, msg);
  156.         }

  157.         private RefUpdate.Result decode(ReceiveCommand.Result status) {
  158.             switch (status) {
  159.             case OK:
  160.                 if (AnyObjectId.isEqual(oldObjectId, newObjectId))
  161.                     return RefUpdate.Result.NO_CHANGE;
  162.                 switch (getType()) {
  163.                 case CREATE:
  164.                     return RefUpdate.Result.NEW;
  165.                 case UPDATE:
  166.                     return RefUpdate.Result.FAST_FORWARD;
  167.                 case DELETE:
  168.                 case UPDATE_NONFASTFORWARD:
  169.                 default:
  170.                     return RefUpdate.Result.FORCED;
  171.                 }

  172.             case REJECTED_NOCREATE:
  173.             case REJECTED_NODELETE:
  174.             case REJECTED_NONFASTFORWARD:
  175.                 return RefUpdate.Result.REJECTED;
  176.             case REJECTED_CURRENT_BRANCH:
  177.                 return RefUpdate.Result.REJECTED_CURRENT_BRANCH;
  178.             case REJECTED_MISSING_OBJECT:
  179.                 return RefUpdate.Result.IO_FAILURE;
  180.             case LOCK_FAILURE:
  181.             case NOT_ATTEMPTED:
  182.             case REJECTED_OTHER_REASON:
  183.             default:
  184.                 return RefUpdate.Result.LOCK_FAILURE;
  185.             }
  186.         }
  187.     }

  188.     /** {@inheritDoc} */
  189.     @SuppressWarnings("nls")
  190.     @Override
  191.     public String toString() {
  192.         StringBuilder sb = new StringBuilder();
  193.         sb.append("TrackingRefUpdate[");
  194.         sb.append(remoteName);
  195.         sb.append(" -> ");
  196.         sb.append(localName);
  197.         if (forceUpdate)
  198.             sb.append(" (forced)");
  199.         sb.append(" ");
  200.         sb.append(oldObjectId == null ? "" : oldObjectId.abbreviate(7).name());
  201.         sb.append("..");
  202.         sb.append(newObjectId == null ? "" : newObjectId.abbreviate(7).name());
  203.         sb.append("]");
  204.         return sb.toString();
  205.     }
  206. }