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.internal.transport.parser;
11  
12  import static java.util.Arrays.asList;
13  import static java.util.Collections.emptySet;
14  import static java.util.Collections.unmodifiableSet;
15  import static java.util.stream.Collectors.toSet;
16  
17  import java.util.Set;
18  
19  import org.eclipse.jgit.annotations.NonNull;
20  
21  /**
22   * In a push, the client sends a list of commands. The first command
23   * is special, as it can include a list of capabilities at its end.
24   * <p>
25   * For example:
26   * "oid oid name\0cap1 cap cap3"
27   * <p>
28   * Not to be confused with {@link FirstWant}, nor with the first line
29   * of the reference advertisement parsed by
30   * {@code BasePackConnection.readAdvertisedRefs}.
31   * <p>
32   * This class parses the inputted command line and holds the results:
33   * the actual command line and the capabilities.
34   */
35  public final class FirstCommand {
36  	private final String line;
37  	private final Set<String> capabilities;
38  
39  	/**
40  	 * Parse the first line of a receive-pack request.
41  	 *
42  	 * @param line
43  	 *            line from the client.
44  	 * @return an instance of FirstCommand with capabilities parsed out
45  	 */
46  	@NonNull
47  	public static FirstCommand fromLine(String line) {
48  		int nul = line.indexOf('\0');
49  		if (nul < 0) {
50  			return new FirstCommand(line, emptySet());
51  		}
52  		Set<String> opts =
53  				asList(line.substring(nul + 1).split(" ")) //$NON-NLS-1$
54  					.stream()
55  					.collect(toSet());
56  		return new FirstCommand(line.substring(0, nul), unmodifiableSet(opts));
57  	}
58  
59  	private FirstCommand(String line, Set<String> capabilities) {
60  		this.line = line;
61  		this.capabilities = capabilities;
62  	}
63  
64  	/** @return non-capabilities part of the line. */
65  	@NonNull
66  	public String getLine() {
67  		return line;
68  	}
69  
70  	/** @return capabilities parsed from the line, as an immutable set. */
71  	@NonNull
72  	public Set<String> getCapabilities() {
73  		return capabilities;
74  	}
75  }