FetchV2Request.java

  1. /*
  2.  * Copyright (C) 2018, 2022 Google LLC. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.transport;

  11. import static java.util.Objects.requireNonNull;

  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Set;

  17. import org.eclipse.jgit.annotations.NonNull;
  18. import org.eclipse.jgit.annotations.Nullable;
  19. import org.eclipse.jgit.lib.ObjectId;

  20. /**
  21.  * Fetch request from git protocol v2.
  22.  *
  23.  * <p>
  24.  * This is used as an input to {@link ProtocolV2Hook}.
  25.  *
  26.  * @since 5.1
  27.  */
  28. public final class FetchV2Request extends FetchRequest {
  29.     private final List<ObjectId> peerHas;

  30.     private final List<String> wantedRefs;

  31.     private final boolean doneReceived;

  32.     private final boolean waitForDone;

  33.     @NonNull
  34.     private final List<String> serverOptions;

  35.     private final boolean sidebandAll;

  36.     @NonNull
  37.     private final List<String> packfileUriProtocols;

  38.     FetchV2Request(@NonNull List<ObjectId> peerHas,
  39.             @NonNull List<String> wantedRefs,
  40.             @NonNull Set<ObjectId> wantIds,
  41.             @NonNull Set<ObjectId> clientShallowCommits, int deepenSince,
  42.             @NonNull List<String> deepenNots, int depth,
  43.             @NonNull FilterSpec filterSpec,
  44.             boolean doneReceived, boolean waitForDone,
  45.             @NonNull Set<String> clientCapabilities,
  46.             @Nullable String agent, @NonNull List<String> serverOptions,
  47.             boolean sidebandAll, @NonNull List<String> packfileUriProtocols,
  48.             @Nullable String clientSID) {
  49.         super(wantIds, depth, clientShallowCommits, filterSpec,
  50.                 clientCapabilities, deepenSince,
  51.                 deepenNots, agent, clientSID);
  52.         this.peerHas = requireNonNull(peerHas);
  53.         this.wantedRefs = requireNonNull(wantedRefs);
  54.         this.doneReceived = doneReceived;
  55.         this.waitForDone = waitForDone;
  56.         this.serverOptions = requireNonNull(serverOptions);
  57.         this.sidebandAll = sidebandAll;
  58.         this.packfileUriProtocols = packfileUriProtocols;
  59.     }

  60.     /**
  61.      * @return object ids received in the "have" lines
  62.      */
  63.     @NonNull
  64.     List<ObjectId> getPeerHas() {
  65.         return peerHas;
  66.     }

  67.     /**
  68.      * @return list of references received in "want-ref" lines
  69.      *
  70.      * @since 5.4
  71.      */
  72.     @NonNull
  73.     public List<String> getWantedRefs() {
  74.         return wantedRefs;
  75.     }

  76.     /**
  77.      * @return true if the request had a "done" line
  78.      */
  79.     boolean wasDoneReceived() {
  80.         return doneReceived;
  81.     }

  82.     /**
  83.      * @return true if the request had a "wait-for-done" line
  84.      */
  85.     boolean wasWaitForDoneReceived() {
  86.         return waitForDone;
  87.     }

  88.     /**
  89.      * Options received in server-option lines. The caller can choose to act on
  90.      * these in an application-specific way
  91.      *
  92.      * @return Immutable list of server options received in the request
  93.      *
  94.      * @since 5.2
  95.      */
  96.     @NonNull
  97.     public List<String> getServerOptions() {
  98.         return serverOptions;
  99.     }

  100.     /**
  101.      * @return true if "sideband-all" was received
  102.      */
  103.     boolean getSidebandAll() {
  104.         return sidebandAll;
  105.     }

  106.     @NonNull
  107.     List<String> getPackfileUriProtocols() {
  108.         return packfileUriProtocols;
  109.     }

  110.     /** @return A builder of {@link FetchV2Request}. */
  111.     static Builder builder() {
  112.         return new Builder();
  113.     }

  114.     /** A builder for {@link FetchV2Request}. */
  115.     static final class Builder {
  116.         final List<ObjectId> peerHas = new ArrayList<>();

  117.         final List<String> wantedRefs = new ArrayList<>();

  118.         final Set<ObjectId> wantIds = new HashSet<>();

  119.         final Set<ObjectId> clientShallowCommits = new HashSet<>();

  120.         final List<String> deepenNots = new ArrayList<>();

  121.         final Set<String> clientCapabilities = new HashSet<>();

  122.         int depth;

  123.         int deepenSince;

  124.         FilterSpec filterSpec = FilterSpec.NO_FILTER;

  125.         boolean doneReceived;

  126.         boolean waitForDone;

  127.         @Nullable
  128.         String agent;

  129.         @Nullable
  130.         String clientSID;

  131.         final List<String> serverOptions = new ArrayList<>();

  132.         boolean sidebandAll;

  133.         final List<String> packfileUriProtocols = new ArrayList<>();

  134.         private Builder() {
  135.         }

  136.         /**
  137.          * @param objectId
  138.          *            object id received in a "have" line
  139.          * @return this builder
  140.          */
  141.         Builder addPeerHas(ObjectId objectId) {
  142.             peerHas.add(objectId);
  143.             return this;
  144.         }

  145.         /**
  146.          * Ref received in "want-ref" line and the object-id it refers to
  147.          *
  148.          * @param refName
  149.          *            reference name
  150.          * @return this builder
  151.          */
  152.         Builder addWantedRef(String refName) {
  153.             wantedRefs.add(refName);
  154.             return this;
  155.         }

  156.         /**
  157.          * @param clientCapability
  158.          *            capability line sent by the client
  159.          * @return this builder
  160.          */
  161.         Builder addClientCapability(String clientCapability) {
  162.             clientCapabilities.add(clientCapability);
  163.             return this;
  164.         }

  165.         /**
  166.          * @param wantId
  167.          *            object id received in a "want" line
  168.          * @return this builder
  169.          */
  170.         Builder addWantId(ObjectId wantId) {
  171.             wantIds.add(wantId);
  172.             return this;
  173.         }

  174.         /**
  175.          * @param shallowOid
  176.          *            object id received in a "shallow" line
  177.          * @return this builder
  178.          */
  179.         Builder addClientShallowCommit(ObjectId shallowOid) {
  180.             clientShallowCommits.add(shallowOid);
  181.             return this;
  182.         }

  183.         /**
  184.          * @param d
  185.          *            Depth received in a "deepen" line
  186.          * @return this builder
  187.          */
  188.         Builder setDepth(int d) {
  189.             depth = d;
  190.             return this;
  191.         }

  192.         /**
  193.          * @return depth set in the request (via a "deepen" line). Defaulting to
  194.          *         0 if not set.
  195.          */
  196.         int getDepth() {
  197.             return depth;
  198.         }

  199.         /**
  200.          * @return true if there has been at least one "deepen not" line in the
  201.          *         request so far
  202.          */
  203.         boolean hasDeepenNots() {
  204.             return !deepenNots.isEmpty();
  205.         }

  206.         /**
  207.          * @param deepenNot
  208.          *            reference received in a "deepen not" line
  209.          * @return this builder
  210.          */
  211.         Builder addDeepenNot(String deepenNot) {
  212.             deepenNots.add(deepenNot);
  213.             return this;
  214.         }

  215.         /**
  216.          * @param value
  217.          *            Unix timestamp received in a "deepen since" line
  218.          * @return this builder
  219.          */
  220.         Builder setDeepenSince(int value) {
  221.             deepenSince = value;
  222.             return this;
  223.         }

  224.         /**
  225.          * @return shallow since value, sent before in a "deepen since" line. 0
  226.          *         by default.
  227.          */
  228.         int getDeepenSince() {
  229.             return deepenSince;
  230.         }

  231.         /**
  232.          * @param filter
  233.          *            spec set in a "filter" line
  234.          * @return this builder
  235.          */
  236.         Builder setFilterSpec(@NonNull FilterSpec filter) {
  237.             filterSpec = requireNonNull(filter);
  238.             return this;
  239.         }

  240.         /**
  241.          * Mark that the "done" line has been received.
  242.          *
  243.          * @return this builder
  244.          */
  245.         Builder setDoneReceived() {
  246.             doneReceived = true;
  247.             return this;
  248.         }

  249.         /**
  250.          * Mark that the "wait-for-done" line has been received.
  251.          *
  252.          * @return this builder
  253.          */
  254.         Builder setWaitForDone() {
  255.             waitForDone = true;
  256.             return this;
  257.         }

  258.         /**
  259.          * Value of an agent line received after the command and before the
  260.          * arguments. E.g. "agent=a.b.c/1.0" should set "a.b.c/1.0".
  261.          *
  262.          * @param agentValue
  263.          *            the client-supplied agent capability, without the leading
  264.          *            "agent="
  265.          * @return this builder
  266.          */
  267.         Builder setAgent(@Nullable String agentValue) {
  268.             agent = agentValue;
  269.             return this;
  270.         }

  271.         /**
  272.          * @param clientSIDValue
  273.          *            the client-supplied session capability, without the
  274.          *            leading "session-id="
  275.          * @return this builder
  276.          */
  277.         Builder setClientSID(@Nullable String clientSIDValue) {
  278.             clientSID = clientSIDValue;
  279.             return this;
  280.         }

  281.         /**
  282.          * Records an application-specific option supplied in a server-option
  283.          * line, for later retrieval with
  284.          * {@link FetchV2Request#getServerOptions}.
  285.          *
  286.          * @param value
  287.          *            the client-supplied server-option capability, without
  288.          *            leading "server-option=".
  289.          * @return this builder
  290.          */
  291.         Builder addServerOption(@NonNull String value) {
  292.             serverOptions.add(value);
  293.             return this;
  294.         }

  295.         /**
  296.          * @param value true if client sent "sideband-all"
  297.          * @return this builder
  298.          */
  299.         Builder setSidebandAll(boolean value) {
  300.             sidebandAll = value;
  301.             return this;
  302.         }

  303.         Builder addPackfileUriProtocol(@NonNull String value) {
  304.             packfileUriProtocols.add(value);
  305.             return this;
  306.         }

  307.         /**
  308.          * @return Initialized fetch request
  309.          */
  310.         FetchV2Request build() {
  311.             return new FetchV2Request(peerHas, wantedRefs, wantIds,
  312.                     clientShallowCommits, deepenSince, deepenNots,
  313.                     depth, filterSpec, doneReceived, waitForDone, clientCapabilities,
  314.                     agent, Collections.unmodifiableList(serverOptions),
  315.                     sidebandAll,
  316.                     Collections.unmodifiableList(packfileUriProtocols),
  317.                     clientSID);
  318.         }
  319.     }
  320. }