FetchV2Request.java

  1. /*
  2.  * Copyright (C) 2018, 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.     @NonNull
  33.     private final List<String> serverOptions;

  34.     private final boolean sidebandAll;

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

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

  56.     /**
  57.      * @return object ids received in the "have" lines
  58.      */
  59.     @NonNull
  60.     List<ObjectId> getPeerHas() {
  61.         return peerHas;
  62.     }

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

  72.     /**
  73.      * @return true if the request had a "done" line
  74.      */
  75.     boolean wasDoneReceived() {
  76.         return doneReceived;
  77.     }

  78.     /**
  79.      * Options received in server-option lines. The caller can choose to act on
  80.      * these in an application-specific way
  81.      *
  82.      * @return Immutable list of server options received in the request
  83.      *
  84.      * @since 5.2
  85.      */
  86.     @NonNull
  87.     public List<String> getServerOptions() {
  88.         return serverOptions;
  89.     }

  90.     /**
  91.      * @return true if "sideband-all" was received
  92.      */
  93.     boolean getSidebandAll() {
  94.         return sidebandAll;
  95.     }

  96.     @NonNull
  97.     List<String> getPackfileUriProtocols() {
  98.         return packfileUriProtocols;
  99.     }

  100.     /** @return A builder of {@link FetchV2Request}. */
  101.     static Builder builder() {
  102.         return new Builder();
  103.     }

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

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

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

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

  110.         final List<String> deepenNotRefs = new ArrayList<>();

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

  112.         int depth;

  113.         int deepenSince;

  114.         FilterSpec filterSpec = FilterSpec.NO_FILTER;

  115.         boolean doneReceived;

  116.         @Nullable
  117.         String agent;

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

  119.         boolean sidebandAll;

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

  121.         private Builder() {
  122.         }

  123.         /**
  124.          * @param objectId
  125.          *            object id received in a "have" line
  126.          * @return this builder
  127.          */
  128.         Builder addPeerHas(ObjectId objectId) {
  129.             peerHas.add(objectId);
  130.             return this;
  131.         }

  132.         /**
  133.          * Ref received in "want-ref" line and the object-id it refers to
  134.          *
  135.          * @param refName
  136.          *            reference name
  137.          * @return this builder
  138.          */
  139.         Builder addWantedRef(String refName) {
  140.             wantedRefs.add(refName);
  141.             return this;
  142.         }

  143.         /**
  144.          * @param clientCapability
  145.          *            capability line sent by the client
  146.          * @return this builder
  147.          */
  148.         Builder addClientCapability(String clientCapability) {
  149.             clientCapabilities.add(clientCapability);
  150.             return this;
  151.         }

  152.         /**
  153.          * @param wantId
  154.          *            object id received in a "want" line
  155.          * @return this builder
  156.          */
  157.         Builder addWantId(ObjectId wantId) {
  158.             wantIds.add(wantId);
  159.             return this;
  160.         }

  161.         /**
  162.          * @param shallowOid
  163.          *            object id received in a "shallow" line
  164.          * @return this builder
  165.          */
  166.         Builder addClientShallowCommit(ObjectId shallowOid) {
  167.             clientShallowCommits.add(shallowOid);
  168.             return this;
  169.         }

  170.         /**
  171.          * @param d
  172.          *            Depth received in a "deepen" line
  173.          * @return this builder
  174.          */
  175.         Builder setDepth(int d) {
  176.             depth = d;
  177.             return this;
  178.         }

  179.         /**
  180.          * @return depth set in the request (via a "deepen" line). Defaulting to
  181.          *         0 if not set.
  182.          */
  183.         int getDepth() {
  184.             return depth;
  185.         }

  186.         /**
  187.          * @return true if there has been at least one "deepen not" line in the
  188.          *         request so far
  189.          */
  190.         boolean hasDeepenNotRefs() {
  191.             return !deepenNotRefs.isEmpty();
  192.         }

  193.         /**
  194.          * @param deepenNotRef
  195.          *            reference received in a "deepen not" line
  196.          * @return this builder
  197.          */
  198.         Builder addDeepenNotRef(String deepenNotRef) {
  199.             deepenNotRefs.add(deepenNotRef);
  200.             return this;
  201.         }

  202.         /**
  203.          * @param value
  204.          *            Unix timestamp received in a "deepen since" line
  205.          * @return this builder
  206.          */
  207.         Builder setDeepenSince(int value) {
  208.             deepenSince = value;
  209.             return this;
  210.         }

  211.         /**
  212.          * @return shallow since value, sent before in a "deepen since" line. 0
  213.          *         by default.
  214.          */
  215.         int getDeepenSince() {
  216.             return deepenSince;
  217.         }

  218.         /**
  219.          * @param filter
  220.          *            spec set in a "filter" line
  221.          * @return this builder
  222.          */
  223.         Builder setFilterSpec(@NonNull FilterSpec filter) {
  224.             filterSpec = requireNonNull(filter);
  225.             return this;
  226.         }

  227.         /**
  228.          * Mark that the "done" line has been received.
  229.          *
  230.          * @return this builder
  231.          */
  232.         Builder setDoneReceived() {
  233.             doneReceived = true;
  234.             return this;
  235.         }

  236.         /**
  237.          * Value of an agent line received after the command and before the
  238.          * arguments. E.g. "agent=a.b.c/1.0" should set "a.b.c/1.0".
  239.          *
  240.          * @param agentValue
  241.          *            the client-supplied agent capability, without the leading
  242.          *            "agent="
  243.          * @return this builder
  244.          */
  245.         Builder setAgent(@Nullable String agentValue) {
  246.             agent = agentValue;
  247.             return this;
  248.         }

  249.         /**
  250.          * Records an application-specific option supplied in a server-option
  251.          * line, for later retrieval with
  252.          * {@link FetchV2Request#getServerOptions}.
  253.          *
  254.          * @param value
  255.          *            the client-supplied server-option capability, without
  256.          *            leading "server-option=".
  257.          * @return this builder
  258.          */
  259.         Builder addServerOption(@NonNull String value) {
  260.             serverOptions.add(value);
  261.             return this;
  262.         }

  263.         /**
  264.          * @param value true if client sent "sideband-all"
  265.          * @return this builder
  266.          */
  267.         Builder setSidebandAll(boolean value) {
  268.             sidebandAll = value;
  269.             return this;
  270.         }

  271.         Builder addPackfileUriProtocol(@NonNull String value) {
  272.             packfileUriProtocols.add(value);
  273.             return this;
  274.         }

  275.         /**
  276.          * @return Initialized fetch request
  277.          */
  278.         FetchV2Request build() {
  279.             return new FetchV2Request(peerHas, wantedRefs, wantIds,
  280.                     clientShallowCommits, deepenSince, deepenNotRefs,
  281.                     depth, filterSpec, doneReceived, clientCapabilities,
  282.                     agent, Collections.unmodifiableList(serverOptions),
  283.                     sidebandAll,
  284.                     Collections.unmodifiableList(packfileUriProtocols));
  285.         }
  286.     }
  287. }