go-git icon indicating copy to clipboard operation
go-git copied to clipboard

feature request: support sparse checkout

Open missedone opened this issue 5 years ago • 11 comments

https://github.blog/2020-01-13-highlights-from-git-2-25/ git supports sparse checkout. in my case, i'd like to clone (checkout) a subfolder out of a large git repo for now it seems can not be done with go-git.

thanks

missedone avatar Jun 10 '20 16:06 missedone

this will be a great addition 🙌

c4milo avatar Jun 24 '20 16:06 c4milo

We really need this feature, is there a roadmap or something we can do for helping you ? We could pay in ETHW :smile:

GridexX avatar Sep 22 '22 14:09 GridexX

Hey folks, I am trying to contribute to ArgoCD (and potentially FluxCD) by adding sparse checkout: https://github.com/argoproj/argo-cd/pull/14272

But it seems it is not supported. Any opportunity to make the issue a priority?

yordis avatar Aug 25 '23 05:08 yordis

@yordis AFAICT, sparse checkout is supported. You can see it in this test: https://github.com/go-git/go-git/blob/809f9df1b76258a311a20c76d346e86aca0a08f8/repository_test.go#L291-L320

That being said, I'm trying to use this and it doesn't appear to working. After the checkout, I seem to have all dirs in the worktree, not just the sparse dirs. If you get it working, let me know! Thanks!

antonosmond avatar Sep 18 '23 13:09 antonosmond

I removed the usage of the package btw!

yordis avatar Sep 18 '23 20:09 yordis

@yordis @antonosmond

What did not work?

It looks to be working in my simple test: https://github.com/emirot/test-go-git/tree/main

emirot avatar Oct 14 '23 01:10 emirot

To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

Thanks for your understanding and cooperation!

github-actions[bot] avatar Jan 18 '24 07:01 github-actions[bot]

We would appreciate this feature as well!

jberger-dynatrace avatar Feb 01 '24 09:02 jberger-dynatrace

I also am having trouble using SparseCheckoutDirectories (with go-git v5.11.0). My case may be slightly strange, though. I want to get a sparse checkout from a particular commit. My code, in brief:

        # sha -> 40 character SHA
        # path -> subfolder in my repo, ala "src/app/config"
        # repo -> git form of my repo, ala "[email protected]:myorg/myrepo.git"

	tempDir, _ := os.MkdirTemp("", "source-*")
	fs := osfs.New(tempDir)
	dot, _ := fs.Chroot(".git")
	s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())

	refspec := config.RefSpec(fmt.Sprintf("+%s:refs/remotes/origin/%s", sha, sha))
	r, _ := gogit.Init(s, fs)
	r.CreateRemote(&config.RemoteConfig{
		Fetch: []config.RefSpec{refspec},
		Name:  "origin",
		URLs:  []string{repo},
	})
	w, _ := r.Worktree()

	r.Fetch(&gogit.FetchOptions{
		Depth:           1,
		InsecureSkipTLS: false,
		Tags:            gogit.NoTags,
	})

	w.Checkout(&gogit.CheckoutOptions{
		Hash:                      plumbing.NewHash(sha),
		SparseCheckoutDirectories: []string{path},
	})

While the sample code above does not show error checking, it is present in my actual code, and no errors are coming back.

I have also tried setting path as just "src" (instead of "src/app/cfg") to match the test mentioned above, but same behavior. Also same if I add a Force: true to the CheckoutOptions.

object88 avatar Apr 10 '24 19:04 object88

@yordis @antonosmond

What did not work?

It looks to be working in my simple test: https://github.com/emirot/test-go-git/tree/main

@emirot for you this works, because your example doesn't have NoCheckout: true, in git.CloneOptions of git.PlainClone

wilstdu avatar May 08 '24 15:05 wilstdu

Would also love this feature!

wandergeek avatar May 28 '24 01:05 wandergeek