Skip to content

SparseCheckoutDirectories works only for 1st level directory. #1455

@exu

Description

@exu

Describe the bug

The SparseCheckoutDirectories works only for top level directories (here test).
It doesn't work for more than one directory (here test/k6).

To Reproduce

Run the test:

package server

import (
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
)

func TestGit(t *testing.T) {
	path := "test/k6"

	dir, err := os.MkdirTemp("", "discovery")
	if err != nil {
		t.Fatal("can't create temp dir")
	}

	r, err := git.PlainClone(dir, false, &git.CloneOptions{
		URL:               "https://github.com/kubeshop/testkube.git",
		NoCheckout:        true,
		SingleBranch:      true,
		Depth:             1,
		Tags:              git.NoTags,
		ShallowSubmodules: true,
		ReferenceName:     plumbing.ReferenceName("refs/heads/main"),
	})
	if err != nil {
		t.Fatal("can't clone repository")
	}

	w, err := r.Worktree()
	if err != nil {
		t.Fatal("can't get a worktree")
	}

	options := &git.CheckoutOptions{
		Branch: plumbing.ReferenceName("refs/remotes/origin/main"),
	}
	if path != "" {
		options.SparseCheckoutDirectories = []string{"test/k6"}
	}

	err = w.Checkout(options)
	if err != nil {
		t.Fatal("can't checkout repository")
	}

	pathCheckedOut := false
	filepath.Walk(dir, func(f string, info fs.FileInfo, err error) error {
		t.Logf("- %s", f)
		if err != nil {
			return err
		}

		if strings.Contains(f, "test/k6") {
			pathCheckedOut = true
		}

		return nil
	})

	if !pathCheckedOut {
		t.Fatal("path not checked out")
	}
}

Expected behavior
Sparse checkout should check out only path which contains passed parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions