1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.transport;
11
12 import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER;
13
14 import java.io.EOFException;
15 import java.io.IOException;
16 import java.text.MessageFormat;
17
18 import org.eclipse.jgit.errors.PackProtocolException;
19 import org.eclipse.jgit.internal.JGitText;
20 import org.eclipse.jgit.internal.transport.parser.FirstWant;
21 import org.eclipse.jgit.lib.ObjectId;
22
23
24
25
26
27
28
29
30
31 final class ProtocolV0Parser {
32
33 private final TransferConfig transferConfig;
34
35 ProtocolV0Parser(TransferConfig transferConfig) {
36 this.transferConfig = transferConfig;
37 }
38
39
40
41
42
43
44
45
46
47
48
49
50
51 FetchV0Request recvWants(PacketLineIn pckIn)
52 throws PackProtocolException, IOException {
53 FetchV0Request.Builder reqBuilder = new FetchV0Request.Builder();
54
55 boolean isFirst = true;
56 boolean filterReceived = false;
57
58 for (;;) {
59 String line;
60 try {
61 line = pckIn.readString();
62 } catch (EOFException eof) {
63 if (isFirst) {
64 break;
65 }
66 throw eof;
67 }
68
69 if (PacketLineIn.isEnd(line)) {
70 break;
71 }
72
73 if (line.startsWith("deepen ")) {
74 int depth = Integer.parseInt(line.substring(7));
75 if (depth <= 0) {
76 throw new PackProtocolException(
77 MessageFormat.format(JGitText.get().invalidDepth,
78 Integer.valueOf(depth)));
79 }
80 reqBuilder.setDepth(depth);
81 continue;
82 }
83
84 if (line.startsWith("shallow ")) {
85 reqBuilder.addClientShallowCommit(
86 ObjectId.fromString(line.substring(8)));
87 continue;
88 }
89
90 if (transferConfig.isAllowFilter()
91 && line.startsWith(OPTION_FILTER + " ")) {
92 String arg = line.substring(OPTION_FILTER.length() + 1);
93
94 if (filterReceived) {
95 throw new PackProtocolException(
96 JGitText.get().tooManyFilters);
97 }
98 filterReceived = true;
99
100 reqBuilder.setFilterSpec(FilterSpec.fromFilterLine(arg));
101 continue;
102 }
103
104 if (!line.startsWith("want ") || line.length() < 45) {
105 throw new PackProtocolException(MessageFormat
106 .format(JGitText.get().expectedGot, "want", line));
107 }
108
109 if (isFirst) {
110 if (line.length() > 45) {
111 FirstWant firstLine = FirstWant.fromLine(line);
112 reqBuilder.addClientCapabilities(firstLine.getCapabilities());
113 reqBuilder.setAgent(firstLine.getAgent());
114 line = firstLine.getLine();
115 }
116 }
117
118 reqBuilder.addWantId(ObjectId.fromString(line.substring(5)));
119 isFirst = false;
120 }
121
122 return reqBuilder.build();
123 }
124
125 }