|
| 1 | +package git_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "slices" |
| 6 | + "testing" |
| 7 | + |
| 8 | + gitv5 "github.com/go-git/go-git/v5" |
| 9 | + gitv5config "github.com/go-git/go-git/v5/config" |
| 10 | + "github.com/go-git/go-git/v5/plumbing" |
| 11 | + "github.com/openshift-knative/deviate/pkg/config" |
| 12 | + configgit "github.com/openshift-knative/deviate/pkg/config/git" |
| 13 | + "github.com/openshift-knative/deviate/pkg/files" |
| 14 | + "github.com/openshift-knative/deviate/pkg/git" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCheckout_OntoWorkspace(t *testing.T) { |
| 20 | + projectPath := t.TempDir() |
| 21 | + remote := configgit.Remote{ |
| 22 | + Name: "origin", |
| 23 | + URL: "https://github.com/cardil/ghet", |
| 24 | + } |
| 25 | + mainBranch := "main" |
| 26 | + gr, err := gitv5.PlainClone(projectPath, false, &gitv5.CloneOptions{ |
| 27 | + URL: remote.URL, |
| 28 | + RemoteName: "origin", |
| 29 | + Depth: 1, |
| 30 | + SingleBranch: true, |
| 31 | + ReferenceName: plumbing.NewBranchReferenceName(mainBranch), |
| 32 | + }) |
| 33 | + require.NoError(t, err) |
| 34 | + wt, gerr := gr.Worktree() |
| 35 | + require.NoError(t, gerr) |
| 36 | + initCommit := "9ab17a360b240c506cf98dfa83997563aa6d9a28" |
| 37 | + require.NoError(t, gr.Fetch(&gitv5.FetchOptions{ |
| 38 | + RemoteName: "origin", |
| 39 | + RefSpecs: []gitv5config.RefSpec{ |
| 40 | + gitv5config.RefSpec(initCommit + ":refs/heads/target"), |
| 41 | + }, |
| 42 | + Depth: 1, |
| 43 | + })) |
| 44 | + require.NoError(t, wt.Checkout(&gitv5.CheckoutOptions{ |
| 45 | + Branch: plumbing.NewBranchReferenceName("target"), |
| 46 | + })) |
| 47 | + repo := &git.Repository{ |
| 48 | + Context: context.TODO(), |
| 49 | + Project: config.Project{ |
| 50 | + Path: projectPath, |
| 51 | + }, |
| 52 | + Repository: gr, |
| 53 | + } |
| 54 | + filters := files.Filters{ |
| 55 | + Include: []string{ |
| 56 | + "**.go", |
| 57 | + }, |
| 58 | + Exclude: []string{ |
| 59 | + "**internal**", |
| 60 | + "**pkg**", |
| 61 | + "**Magefile**", |
| 62 | + "**_test.go", |
| 63 | + }, |
| 64 | + } |
| 65 | + err = repo.Checkout(remote, mainBranch).OntoWorkspace(filters) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + st, serr := wt.Status() |
| 69 | + require.NoError(t, serr) |
| 70 | + got := make([]string, 0, len(st)) |
| 71 | + for f := range st { |
| 72 | + got = append(got, f) |
| 73 | + } |
| 74 | + slices.Sort(got) |
| 75 | + assert.Equal(t, []string{"build/mage.go", "cmd/ght/main.go"}, got) |
| 76 | +} |
0 commit comments