| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"github.com/go-git/go-git/v5/plumbing" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
var ErrUnsupportedObjectFilterType = errors.New("unsupported object filter type") |
| 12 |
|
| 13 |
// Filter values enable the partial clone capability which causes |
| 14 |
// the server to omit objects that match the filter. |
| 15 |
// |
| 16 |
// See [Git's documentation] for more details. |
| 17 |
// |
| 18 |
// [Git's documentation]: https://github.com/git/git/blob/e02ecfcc534e2021aae29077a958dd11c3897e4c/Documentation/rev-list-options.txt#L948 |
| 19 |
type Filter string |
| 20 |
|
| 21 |
type BlobLimitPrefix string |
| 22 |
|
| 23 |
const ( |
| 24 |
BlobLimitPrefixNone BlobLimitPrefix = "" |
| 25 |
BlobLimitPrefixKibi BlobLimitPrefix = "k" |
| 26 |
BlobLimitPrefixMebi BlobLimitPrefix = "m" |
| 27 |
BlobLimitPrefixGibi BlobLimitPrefix = "g" |
| 28 |
) |
| 29 |
|
| 30 |
// FilterBlobNone omits all blobs. |
| 31 |
func FilterBlobNone() Filter { |
| 32 |
return "blob:none" |
| 33 |
} |
| 34 |
|
| 35 |
// FilterBlobLimit omits blobs of size at least n bytes (when prefix is |
| 36 |
// BlobLimitPrefixNone), n kibibytes (when prefix is BlobLimitPrefixKibi), |
| 37 |
// n mebibytes (when prefix is BlobLimitPrefixMebi) or n gibibytes (when |
| 38 |
// prefix is BlobLimitPrefixGibi). n can be zero, in which case all blobs |
| 39 |
// will be omitted. |
| 40 |
func FilterBlobLimit(n uint64, prefix BlobLimitPrefix) Filter { |
| 41 |
return Filter(fmt.Sprintf("blob:limit=%d%s", n, prefix)) |
| 42 |
} |
| 43 |
|
| 44 |
// FilterTreeDepth omits all blobs and trees whose depth from the root tree |
| 45 |
// is larger or equal to depth. |
| 46 |
func FilterTreeDepth(depth uint64) Filter { |
| 47 |
return Filter(fmt.Sprintf("tree:%d", depth)) |
| 48 |
} |
| 49 |
|
| 50 |
// FilterObjectType omits all objects which are not of the requested type t. |
| 51 |
// Supported types are TagObject, CommitObject, TreeObject and BlobObject. |
| 52 |
func FilterObjectType(t plumbing.ObjectType) (Filter, error) { |
| 53 |
switch t { |
| 54 |
case plumbing.TagObject: |
| 55 |
fallthrough |
| 56 |
case plumbing.CommitObject: |
| 57 |
fallthrough |
| 58 |
case plumbing.TreeObject: |
| 59 |
fallthrough |
| 60 |
case plumbing.BlobObject: |
| 61 |
return Filter(fmt.Sprintf("object:type=%s", t.String())), nil |
| 62 |
default: |
| 63 |
return "", fmt.Errorf("%w: %s", ErrUnsupportedObjectFilterType, t.String()) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// FilterCombine combines multiple Filter values together. |
| 68 |
func FilterCombine(filters ...Filter) Filter { |
| 69 |
var escapedFilters []string |
| 70 |
|
| 71 |
for _, filter := range filters { |
| 72 |
escapedFilters = append(escapedFilters, url.QueryEscape(string(filter))) |
| 73 |
} |
| 74 |
|
| 75 |
return Filter(fmt.Sprintf("combine:%s", strings.Join(escapedFilters, "+"))) |
| 76 |
} |