View Javadoc
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  
12  import static java.util.Objects.requireNonNull;
13  
14  import java.util.ArrayList;
15  import java.util.Collections;
16  import java.util.List;
17  
18  import org.eclipse.jgit.annotations.NonNull;
19  import org.eclipse.jgit.annotations.Nullable;
20  
21  /**
22   * ls-refs protocol v2 request.
23   *
24   * <p>
25   * This is used as an input to {@link ProtocolV2Hook}.
26   *
27   * @since 5.1
28   */
29  public final class LsRefsV2Request {
30  	private final List<String> refPrefixes;
31  
32  	private final boolean symrefs;
33  
34  	private final boolean peel;
35  
36  	@Nullable
37  	private final String agent;
38  
39  	@NonNull
40  	private final List<String> serverOptions;
41  
42  	private LsRefsV2Request(List<String> refPrefixes, boolean symrefs,
43  			boolean peel, @Nullable String agent,
44  			@NonNull List<String> serverOptions) {
45  		this.refPrefixes = refPrefixes;
46  		this.symrefs = symrefs;
47  		this.peel = peel;
48  		this.agent = agent;
49  		this.serverOptions = requireNonNull(serverOptions);
50  	}
51  
52  	/** @return ref prefixes that the client requested. */
53  	public List<String> getRefPrefixes() {
54  		return refPrefixes;
55  	}
56  
57  	/** @return true if the client requests symbolic references. */
58  	public boolean getSymrefs() {
59  		return symrefs;
60  	}
61  
62  	/** @return true if the client requests tags to be peeled. */
63  	public boolean getPeel() {
64  		return peel;
65  	}
66  
67  	/**
68  	 * @return agent as reported by the client
69  	 *
70  	 * @since 5.2
71  	 */
72  	@Nullable
73  	public String getAgent() {
74  		return agent;
75  	}
76  
77  	/**
78  	 * Get application-specific options provided by the client using
79  	 * --server-option.
80  	 * <p>
81  	 * It returns just the content, without the "server-option=" prefix. E.g. a
82  	 * request with server-option=A and server-option=B lines returns the list
83  	 * [A, B].
84  	 *
85  	 * @return application-specific options from the client as an unmodifiable
86  	 *         list
87  	 *
88  	 * @since 5.2
89  	 */
90  	@NonNull
91  	public List<String> getServerOptions() {
92  		return serverOptions;
93  	}
94  
95  	/** @return A builder of {@link LsRefsV2Request}. */
96  	public static Builder builder() {
97  		return new Builder();
98  	}
99  
100 	/** A builder for {@link LsRefsV2Request}. */
101 	public static final class Builder {
102 		private List<String> refPrefixes = Collections.emptyList();
103 
104 		private boolean symrefs;
105 
106 		private boolean peel;
107 
108 		private final List<String> serverOptions = new ArrayList<>();
109 
110 		private String agent;
111 
112 		private Builder() {
113 		}
114 
115 		/**
116 		 * @param value
117 		 * @return the Builder
118 		 */
119 		public Builder setRefPrefixes(List<String> value) {
120 			refPrefixes = value;
121 			return this;
122 		}
123 
124 		/**
125 		 * @param value
126 		 * @return the Builder
127 		 */
128 		public Builder setSymrefs(boolean value) {
129 			symrefs = value;
130 			return this;
131 		}
132 
133 		/**
134 		 * @param value
135 		 * @return the Builder
136 		 */
137 		public Builder setPeel(boolean value) {
138 			peel = value;
139 			return this;
140 		}
141 
142 		/**
143 		 * Records an application-specific option supplied in a server-option
144 		 * line, for later retrieval with
145 		 * {@link LsRefsV2Request#getServerOptions}.
146 		 *
147 		 * @param value
148 		 *            the client-supplied server-option capability, without
149 		 *            leading "server-option=".
150 		 * @return this builder
151 		 * @since 5.2
152 		 */
153 		public Builder addServerOption(@NonNull String value) {
154 			serverOptions.add(value);
155 			return this;
156 		}
157 
158 		/**
159 		 * Value of an agent line received after the command and before the
160 		 * arguments. E.g. "agent=a.b.c/1.0" should set "a.b.c/1.0".
161 		 *
162 		 * @param value
163 		 *            the client-supplied agent capability, without leading
164 		 *            "agent="
165 		 * @return this builder
166 		 *
167 		 * @since 5.2
168 		 */
169 		public Builder setAgent(@Nullable String value) {
170 			agent = value;
171 			return this;
172 		}
173 
174 		/** @return LsRefsV2Request */
175 		public LsRefsV2Request build() {
176 			return new LsRefsV2Request(
177 					Collections.unmodifiableList(refPrefixes), symrefs, peel,
178 					agent, Collections.unmodifiableList(serverOptions));
179 		}
180 	}
181 }