1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.transport;
12
13 import java.text.MessageFormat;
14
15 import org.eclipse.jgit.annotations.Nullable;
16 import org.eclipse.jgit.errors.PackProtocolException;
17 import org.eclipse.jgit.internal.JGitText;
18
19
20
21
22
23
24
25 public final class FilterSpec {
26
27 private final long blobLimit;
28
29 private final long treeDepthLimit;
30
31 private FilterSpec(long blobLimit, long treeDepthLimit) {
32 this.blobLimit = blobLimit;
33 this.treeDepthLimit = treeDepthLimit;
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static FilterSpec fromFilterLine(String filterLine)
54 throws PackProtocolException {
55 if (filterLine.equals("blob:none")) {
56 return FilterSpec.withBlobLimit(0);
57 } else if (filterLine.startsWith("blob:limit=")) {
58 long blobLimit = -1;
59 try {
60 blobLimit = Long
61 .parseLong(filterLine.substring("blob:limit=".length()));
62 } catch (NumberFormatException e) {
63
64
65 }
66 if (blobLimit >= 0) {
67 return FilterSpec.withBlobLimit(blobLimit);
68 }
69 } else if (filterLine.startsWith("tree:")) {
70 long treeDepthLimit = -1;
71 try {
72 treeDepthLimit = Long
73 .parseLong(filterLine.substring("tree:".length()));
74 } catch (NumberFormatException e) {
75
76
77 }
78 if (treeDepthLimit >= 0) {
79 return FilterSpec.withTreeDepthLimit(treeDepthLimit);
80 }
81 }
82
83
84 throw new PackProtocolException(
85 MessageFormat.format(JGitText.get().invalidFilter, filterLine));
86 }
87
88
89
90
91
92
93 static FilterSpec withBlobLimit(long blobLimit) {
94 if (blobLimit < 0) {
95 throw new IllegalArgumentException(
96 "blobLimit cannot be negative: " + blobLimit);
97 }
98 return new FilterSpec(blobLimit, -1);
99 }
100
101
102
103
104
105
106
107 static FilterSpec withTreeDepthLimit(long treeDepthLimit) {
108 if (treeDepthLimit < 0) {
109 throw new IllegalArgumentException(
110 "treeDepthLimit cannot be negative: " + treeDepthLimit);
111 }
112 return new FilterSpec(-1, treeDepthLimit);
113 }
114
115
116
117
118 public static final FilterSpecec.html#FilterSpec">FilterSpec NO_FILTER = new FilterSpec(-1, -1);
119
120
121
122
123
124 public long getBlobLimit() {
125 return blobLimit;
126 }
127
128
129
130
131
132
133 public long getTreeDepthLimit() {
134 return treeDepthLimit;
135 }
136
137
138
139
140 public boolean isNoOp() {
141 return blobLimit == -1 && treeDepthLimit == -1;
142 }
143
144
145
146
147 @Nullable
148 public String filterLine() {
149 if (blobLimit == 0) {
150 return GitProtocolConstants.OPTION_FILTER + " blob:none";
151 }
152
153 if (blobLimit > 0) {
154 return GitProtocolConstants.OPTION_FILTER + " blob:limit=" + blobLimit;
155 }
156
157 return null;
158 }
159 }