LsRefsV2Request.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.List;

  15. import org.eclipse.jgit.annotations.NonNull;
  16. import org.eclipse.jgit.annotations.Nullable;

  17. /**
  18.  * ls-refs protocol v2 request.
  19.  *
  20.  * <p>
  21.  * This is used as an input to {@link ProtocolV2Hook}.
  22.  *
  23.  * @since 5.1
  24.  */
  25. public final class LsRefsV2Request {
  26.     private final List<String> refPrefixes;

  27.     private final boolean symrefs;

  28.     private final boolean peel;

  29.     @Nullable
  30.     private final String agent;

  31.     @NonNull
  32.     private final List<String> serverOptions;

  33.     private LsRefsV2Request(List<String> refPrefixes, boolean symrefs,
  34.             boolean peel, @Nullable String agent,
  35.             @NonNull List<String> serverOptions) {
  36.         this.refPrefixes = refPrefixes;
  37.         this.symrefs = symrefs;
  38.         this.peel = peel;
  39.         this.agent = agent;
  40.         this.serverOptions = requireNonNull(serverOptions);
  41.     }

  42.     /** @return ref prefixes that the client requested. */
  43.     public List<String> getRefPrefixes() {
  44.         return refPrefixes;
  45.     }

  46.     /** @return true if the client requests symbolic references. */
  47.     public boolean getSymrefs() {
  48.         return symrefs;
  49.     }

  50.     /** @return true if the client requests tags to be peeled. */
  51.     public boolean getPeel() {
  52.         return peel;
  53.     }

  54.     /**
  55.      * @return agent as reported by the client
  56.      *
  57.      * @since 5.2
  58.      */
  59.     @Nullable
  60.     public String getAgent() {
  61.         return agent;
  62.     }

  63.     /**
  64.      * Get application-specific options provided by the client using
  65.      * --server-option.
  66.      * <p>
  67.      * It returns just the content, without the "server-option=" prefix. E.g. a
  68.      * request with server-option=A and server-option=B lines returns the list
  69.      * [A, B].
  70.      *
  71.      * @return application-specific options from the client as an unmodifiable
  72.      *         list
  73.      *
  74.      * @since 5.2
  75.      */
  76.     @NonNull
  77.     public List<String> getServerOptions() {
  78.         return serverOptions;
  79.     }

  80.     /** @return A builder of {@link LsRefsV2Request}. */
  81.     public static Builder builder() {
  82.         return new Builder();
  83.     }

  84.     /** A builder for {@link LsRefsV2Request}. */
  85.     public static final class Builder {
  86.         private List<String> refPrefixes = Collections.emptyList();

  87.         private boolean symrefs;

  88.         private boolean peel;

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

  90.         private String agent;

  91.         private Builder() {
  92.         }

  93.         /**
  94.          * @param value
  95.          * @return the Builder
  96.          */
  97.         public Builder setRefPrefixes(List<String> value) {
  98.             refPrefixes = value;
  99.             return this;
  100.         }

  101.         /**
  102.          * @param value
  103.          * @return the Builder
  104.          */
  105.         public Builder setSymrefs(boolean value) {
  106.             symrefs = value;
  107.             return this;
  108.         }

  109.         /**
  110.          * @param value
  111.          * @return the Builder
  112.          */
  113.         public Builder setPeel(boolean value) {
  114.             peel = value;
  115.             return this;
  116.         }

  117.         /**
  118.          * Records an application-specific option supplied in a server-option
  119.          * line, for later retrieval with
  120.          * {@link LsRefsV2Request#getServerOptions}.
  121.          *
  122.          * @param value
  123.          *            the client-supplied server-option capability, without
  124.          *            leading "server-option=".
  125.          * @return this builder
  126.          * @since 5.2
  127.          */
  128.         public Builder addServerOption(@NonNull String value) {
  129.             serverOptions.add(value);
  130.             return this;
  131.         }

  132.         /**
  133.          * Value of an agent line received after the command and before the
  134.          * arguments. E.g. "agent=a.b.c/1.0" should set "a.b.c/1.0".
  135.          *
  136.          * @param value
  137.          *            the client-supplied agent capability, without leading
  138.          *            "agent="
  139.          * @return this builder
  140.          *
  141.          * @since 5.2
  142.          */
  143.         public Builder setAgent(@Nullable String value) {
  144.             agent = value;
  145.             return this;
  146.         }

  147.         /** @return LsRefsV2Request */
  148.         public LsRefsV2Request build() {
  149.             return new LsRefsV2Request(
  150.                     Collections.unmodifiableList(refPrefixes), symrefs, peel,
  151.                     agent, Collections.unmodifiableList(serverOptions));
  152.         }
  153.     }
  154. }