You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilterSpec.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2019, Google LLC.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.text.MessageFormat;
  45. import org.eclipse.jgit.annotations.Nullable;
  46. import org.eclipse.jgit.errors.PackProtocolException;
  47. import org.eclipse.jgit.internal.JGitText;
  48. /**
  49. * Represents either a filter specified in a protocol "filter" line, or a
  50. * placeholder to indicate no filtering.
  51. *
  52. * @since 5.4
  53. */
  54. public final class FilterSpec {
  55. private final long blobLimit;
  56. private final long treeDepthLimit;
  57. private FilterSpec(long blobLimit, long treeDepthLimit) {
  58. this.blobLimit = blobLimit;
  59. this.treeDepthLimit = treeDepthLimit;
  60. }
  61. /**
  62. * Process the content of "filter" line from the protocol. It has a shape
  63. * like:
  64. *
  65. * <ul>
  66. * <li>"blob:none"
  67. * <li>"blob:limit=N", with N &gt;= 0
  68. * <li>"tree:DEPTH", with DEPTH &gt;= 0
  69. * </ul>
  70. *
  71. * @param filterLine
  72. * the content of the "filter" line in the protocol
  73. * @return a FilterSpec representing the given filter
  74. * @throws PackProtocolException
  75. * invalid filter because due to unrecognized format or
  76. * negative/non-numeric filter.
  77. */
  78. public static FilterSpec fromFilterLine(String filterLine)
  79. throws PackProtocolException {
  80. if (filterLine.equals("blob:none")) { //$NON-NLS-1$
  81. return FilterSpec.withBlobLimit(0);
  82. } else if (filterLine.startsWith("blob:limit=")) { //$NON-NLS-1$
  83. long blobLimit = -1;
  84. try {
  85. blobLimit = Long
  86. .parseLong(filterLine.substring("blob:limit=".length())); //$NON-NLS-1$
  87. } catch (NumberFormatException e) {
  88. // Do not change blobLimit so that we throw a
  89. // PackProtocolException later.
  90. }
  91. if (blobLimit >= 0) {
  92. return FilterSpec.withBlobLimit(blobLimit);
  93. }
  94. } else if (filterLine.startsWith("tree:")) { //$NON-NLS-1$
  95. long treeDepthLimit = -1;
  96. try {
  97. treeDepthLimit = Long
  98. .parseLong(filterLine.substring("tree:".length())); //$NON-NLS-1$
  99. } catch (NumberFormatException e) {
  100. // Do not change blobLimit so that we throw a
  101. // PackProtocolException later.
  102. }
  103. if (treeDepthLimit >= 0) {
  104. return FilterSpec.withTreeDepthLimit(treeDepthLimit);
  105. }
  106. }
  107. // Did not match any known filter format.
  108. throw new PackProtocolException(
  109. MessageFormat.format(JGitText.get().invalidFilter, filterLine));
  110. }
  111. /**
  112. * @param blobLimit
  113. * the blob limit in a "blob:[limit]" or "blob:none" filter line
  114. * @return a filter spec which filters blobs above a certain size
  115. */
  116. static FilterSpec withBlobLimit(long blobLimit) {
  117. if (blobLimit < 0) {
  118. throw new IllegalArgumentException(
  119. "blobLimit cannot be negative: " + blobLimit); //$NON-NLS-1$
  120. }
  121. return new FilterSpec(blobLimit, -1);
  122. }
  123. /**
  124. * @param treeDepthLimit
  125. * the tree depth limit in a "tree:[depth]" filter line
  126. * @return a filter spec which filters blobs and trees beyond a certain tree
  127. * depth
  128. */
  129. static FilterSpec withTreeDepthLimit(long treeDepthLimit) {
  130. if (treeDepthLimit < 0) {
  131. throw new IllegalArgumentException(
  132. "treeDepthLimit cannot be negative: " + treeDepthLimit); //$NON-NLS-1$
  133. }
  134. return new FilterSpec(-1, treeDepthLimit);
  135. }
  136. /**
  137. * A placeholder that indicates no filtering.
  138. */
  139. public static final FilterSpec NO_FILTER = new FilterSpec(-1, -1);
  140. /**
  141. * @return -1 if this filter does not filter blobs based on size, or a
  142. * non-negative integer representing the max size of blobs to allow
  143. */
  144. public long getBlobLimit() {
  145. return blobLimit;
  146. }
  147. /**
  148. * @return -1 if this filter does not filter blobs and trees based on depth,
  149. * or a non-negative integer representing the max tree depth of
  150. * blobs and trees to fetch
  151. */
  152. public long getTreeDepthLimit() {
  153. return treeDepthLimit;
  154. }
  155. /**
  156. * @return true if this filter doesn't filter out anything
  157. */
  158. public boolean isNoOp() {
  159. return blobLimit == -1 && treeDepthLimit == -1;
  160. }
  161. /**
  162. * @return the filter line which describes this spec, e.g. "filter blob:limit=42"
  163. */
  164. @Nullable
  165. public String filterLine() {
  166. if (blobLimit == 0) {
  167. return GitProtocolConstants.OPTION_FILTER + " blob:none"; //$NON-NLS-1$
  168. }
  169. if (blobLimit > 0) {
  170. return GitProtocolConstants.OPTION_FILTER + " blob:limit=" + blobLimit; //$NON-NLS-1$
  171. }
  172. return null;
  173. }
  174. }