Skip to content

Commit

Permalink
Add input parameter filter for git commands (#3595)
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane authored Jan 17, 2025
1 parent 91467d2 commit 73a8c77
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 100 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [Unreleased]

- No changes yet.
- Add input parameter `filter` for use with git inputs. This sets the filter
flag argument for the git fetch command.

## [v1.49.0] - 2025-01-07

Expand Down
9 changes: 9 additions & 0 deletions private/buf/buffetch/internal/git_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type gitRef struct {
depth uint32
recurseSubmodules bool
subDirPath string
filter string
}

func newGitRef(
Expand All @@ -51,6 +52,7 @@ func newGitRef(
depth uint32,
recurseSubmodules bool,
subDirPath string,
filter string,
) (*gitRef, error) {
gitScheme, path, err := getGitSchemeAndPath(format, path)
if err != nil {
Expand All @@ -74,6 +76,7 @@ func newGitRef(
recurseSubmodules,
depth,
subDirPath,
filter,
), nil
}

Expand All @@ -85,6 +88,7 @@ func newDirectGitRef(
recurseSubmodules bool,
depth uint32,
subDirPath string,
filter string,
) *gitRef {
return &gitRef{
format: format,
Expand All @@ -94,6 +98,7 @@ func newDirectGitRef(
depth: depth,
recurseSubmodules: recurseSubmodules,
subDirPath: subDirPath,
filter: filter,
}
}

Expand Down Expand Up @@ -125,6 +130,10 @@ func (r *gitRef) SubDirPath() string {
return r.subDirPath
}

func (r *gitRef) Filter() string {
return r.filter
}

func (*gitRef) ref() {}
func (*gitRef) bucketRef() {}
func (*gitRef) gitRef() {}
Expand Down
10 changes: 9 additions & 1 deletion private/buf/buffetch/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ type GitRef interface {
RecurseSubmodules() bool
// Will be empty instead of "." for root directory
SubDirPath() string
// Filter spec to use, see the --filter option in git rev-list.
Filter() string
gitRef()
}

Expand All @@ -217,8 +219,9 @@ func NewGitRef(
depth uint32,
recurseSubmodules bool,
subDirPath string,
filter string,
) (GitRef, error) {
return newGitRef("", path, gitName, depth, recurseSubmodules, subDirPath)
return newGitRef("", path, gitName, depth, recurseSubmodules, subDirPath, filter)
}

// ModuleRef is a module reference.
Expand Down Expand Up @@ -353,6 +356,7 @@ func NewDirectParsedGitRef(
recurseSubmodules bool,
depth uint32,
subDirPath string,
filter string,
) ParsedGitRef {
return newDirectGitRef(
format,
Expand All @@ -362,6 +366,7 @@ func NewDirectParsedGitRef(
recurseSubmodules,
depth,
subDirPath,
filter,
)
}

Expand Down Expand Up @@ -557,6 +562,9 @@ type RawRef struct {
// requested GitRef will be included when cloning the requested branch
// (or the repo's default branch if GitBranch is empty).
GitDepth uint32
// Only set for git formats.
// The filter spec to use, see the --filter option in git rev-list.
GitFilter string
// Only set for archive formats.
ArchiveStripComponents uint32
// Only set for proto file ref format.
Expand Down
2 changes: 2 additions & 0 deletions private/buf/buffetch/internal/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ func (r *reader) getGitBucket(
git.CloneToBucketOptions{
Name: gitRef.GitName(),
RecurseSubmodules: gitRef.RecurseSubmodules(),
SubDir: gitRef.SubDirPath(),
Filter: gitRef.Filter(),
},
); err != nil {
return nil, nil, fmt.Errorf("could not clone %s: %v", gitURL, err)
Expand Down
3 changes: 3 additions & 0 deletions private/buf/buffetch/internal/ref_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func (a *refParser) getRawRef(
rawRef.GitCommitOrTag = value
case "ref":
rawRef.GitRef = value
case "filter":
rawRef.GitFilter = value
case "depth":
depth, err := parseGitDepth(value)
if err != nil {
Expand Down Expand Up @@ -519,6 +521,7 @@ func getGitRef(
rawRef.GitDepth,
rawRef.GitRecurseSubmodules,
rawRef.SubDirPath,
rawRef.GitFilter,
)
}

Expand Down
39 changes: 39 additions & 0 deletions private/buf/buffetch/ref_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir.git",
)
Expand All @@ -270,6 +271,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
40,
"",
"",
),
"path/to/dir.git#depth=40",
)
Expand All @@ -283,6 +285,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir.git#branch=main",
)
Expand All @@ -296,6 +299,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"file:///path/to/dir.git#branch=main",
)
Expand All @@ -309,6 +313,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir.git#tag=v1.0.0",
)
Expand All @@ -322,6 +327,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"http://hello.com/path/to/dir.git#branch=main",
)
Expand All @@ -335,6 +341,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"https://hello.com/path/to/dir.git#branch=main",
)
Expand All @@ -348,6 +355,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"ssh://user@hello.com:path/to/dir.git#branch=main",
)
Expand All @@ -361,6 +369,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
50,
"",
"",
),
"ssh://user@hello.com:path/to/dir.git#ref=refs/remotes/origin/HEAD",
)
Expand All @@ -374,6 +383,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
50,
"",
"",
),
"ssh://user@hello.com:path/to/dir.git#ref=refs/remotes/origin/HEAD,branch=main",
)
Expand All @@ -387,6 +397,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
10,
"",
"",
),
"ssh://user@hello.com:path/to/dir.git#ref=refs/remotes/origin/HEAD,depth=10",
)
Expand All @@ -400,6 +411,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
10,
"",
"",
),
"ssh://user@hello.com:path/to/dir.git#ref=refs/remotes/origin/HEAD,branch=main,depth=10",
)
Expand All @@ -413,6 +425,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"foo/bar",
"",
),
"path/to/dir.git#subdir=foo/bar",
)
Expand All @@ -426,6 +439,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir.git#subdir=.",
)
Expand All @@ -439,6 +453,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir.git#subdir=foo/..",
)
Expand All @@ -452,6 +467,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"git://user@hello.com:path/to/dir.git#branch=main",
)
Expand All @@ -465,9 +481,24 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"git://path/to/dir.git#branch=main",
)
testGetParsedRefSuccess(
t,
internal.NewDirectParsedGitRef(
formatGit,
"path/to/dir.git",
internal.GitSchemeGit,
git.NewBranchName("main"),
false,
1,
"subdir",
"tree:0",
),
"git://path/to/dir.git#branch=main,filter=tree:0,subdir=subdir",
)
testGetParsedRefSuccess(
t,
internal.NewDirectParsedSingleRef(
Expand Down Expand Up @@ -811,6 +842,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"/path/to/dir#branch=main,format=git",
)
Expand All @@ -824,6 +856,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"/path/to/dir#format=git,branch=main/foo",
)
Expand All @@ -837,6 +870,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir#tag=main/foo,format=git",
)
Expand All @@ -850,6 +884,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir#format=git,tag=main/foo",
)
Expand All @@ -863,6 +898,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
true,
1,
"",
"",
),
"path/to/dir#format=git,tag=main/foo,recurse_submodules=true",
)
Expand All @@ -876,6 +912,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
1,
"",
"",
),
"path/to/dir#format=git,tag=main/foo,recurse_submodules=false",
)
Expand All @@ -889,6 +926,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
50,
"",
"",
),
"path/to/dir#format=git,ref=refs/remotes/origin/HEAD",
)
Expand All @@ -902,6 +940,7 @@ func TestGetParsedRefSuccess(t *testing.T) {
false,
10,
"",
"",
),
"path/to/dir#format=git,ref=refs/remotes/origin/HEAD,depth=10",
)
Expand Down
Loading

0 comments on commit 73a8c77

Please sign in to comment.