Limit readiness scans to selected package inputs
This commit is contained in:
@@ -386,6 +386,30 @@ func ReadTree(ctx context.Context, repositoryDir, commit string) ([]TreeEntry, e
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// ReadTreeDirectory lists immediate entries of a resolved repository-relative
|
||||
// directory in a captured commit. Entry paths remain repository-relative.
|
||||
func ReadTreeDirectory(ctx context.Context, repositoryDir, commit, directory string) ([]TreeEntry, error) {
|
||||
if !validObjectID(commit) {
|
||||
return nil, fmt.Errorf("invalid commit object ID %q", commit)
|
||||
}
|
||||
if directory == "" || path.IsAbs(directory) || path.Clean(directory) != directory || directory == ".." || strings.HasPrefix(directory, "../") {
|
||||
return nil, fmt.Errorf("invalid committed directory %q", directory)
|
||||
}
|
||||
args := []string{"--literal-pathspecs", "ls-tree", "-z", "--full-tree", commit}
|
||||
if directory != "." {
|
||||
args = append(args, "--", directory+"/")
|
||||
}
|
||||
output, err := readLocalObjects(ctx, repositoryDir, nil, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("enumerate committed directory %q: %w", directory, err)
|
||||
}
|
||||
entries, err := parseTreeEntries(output)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse committed directory %q: %w", directory, err)
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// ReadBlob reads the bytes for a Git blob object ID without consulting the
|
||||
// worktree.
|
||||
func ReadBlob(ctx context.Context, repositoryDir, objectID string) ([]byte, error) {
|
||||
|
||||
@@ -6,10 +6,50 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadTreeDirectoryListsOnlyImmediateCapturedEntries(t *testing.T) {
|
||||
fixture := newRemoteFixture(t)
|
||||
for _, name := range []string{"pkg[literal]/main.go", "pkg[literal]/nested/deep.go", "unrelated/elsewhere.txt"} {
|
||||
filename := filepath.Join(fixture.work, filepath.FromSlash(name))
|
||||
if err := os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filename, []byte("captured"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
gitRun(t, fixture.work, "add", "--all")
|
||||
gitRun(t, fixture.work, "commit", "-m", "capture package inputs")
|
||||
commit := strings.TrimSpace(gitText(t, fixture.work, "rev-parse", "HEAD"))
|
||||
if err := os.Remove(filepath.Join(fixture.work, "pkg[literal]", "main.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
directory string
|
||||
want []string
|
||||
}{
|
||||
{directory: ".", want: []string{"pkg[literal]", "source.txt", "unrelated"}},
|
||||
{directory: "pkg[literal]", want: []string{"pkg[literal]/main.go", "pkg[literal]/nested"}},
|
||||
{directory: "pkg[literal]/nested", want: []string{"pkg[literal]/nested/deep.go"}},
|
||||
} {
|
||||
entries, err := ReadTreeDirectory(t.Context(), fixture.work, commit, test.directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var names []string
|
||||
for _, entry := range entries {
|
||||
names = append(names, entry.Path)
|
||||
}
|
||||
if !slices.Equal(names, test.want) {
|
||||
t.Fatalf("directory %q entries=%v want=%v", test.directory, names, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectTaggedSourceSupportsLightweightAndAnnotatedTags(t *testing.T) {
|
||||
fixture := newRemoteFixture(t)
|
||||
gitRun(t, fixture.work, "tag", "v1.2.3")
|
||||
|
||||
Reference in New Issue
Block a user