Skip to content

Sparse checkout support #2263

Open
Open

Description

@jspahrsummers

A user can set core.sparsecheckout to true, then populate .git/info/sparse-checkout with the specific subtrees that should be checked out. See http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/ for a basic explanation.

I don't know whether it makes sense for libgit2 to support this in its own checkout operations, but sparse checkouts should be taken into consideration for diffs and status (in that omitted subtrees should not appear).

Activity

  1. calavera commented on Jun 23, 2016

    @calavera
    Contributor

    I understand for the lack of traffic here that this is not in any roadmap, but I thought I could ask again. @ethomson, @carlosmn is there any possibility that this can be implemented? I don't really understand the whole scope of this request, so "no" is a very valid answer.

  2. carlosmn commented on Jul 1, 2016

    @carlosmn
    Member

    It is highly unlikely that either of us will work on this. Maybe @johnhaley81 is interested in this since he works on a client-side app.

  3. johnhaley81 commented on Jul 1, 2016

    @johnhaley81
    Contributor

    It won't be anytime soon but that is something that I'm looking to tackle eventually :)

  4. jlindner commented on Jul 19, 2018

    @jlindner

    Is this any update on this functionality possibly making it into libgit2? This has pretty much become a show stopper for us to use git since we have massive mono repos, but only need to perform diffs of certain directories. Currently, this lack of support has kept us from moving everything from Subversion to git. Since this was talked about a couple of years ago, I was hoping somebody would maybe have an update on this.

    Thanks

  5. ethomson commented on Jul 19, 2018

    @ethomson
    Member

    @tiennou has done some work here, but this is an insanely complex topic and remains unimplemented.

  6. tiennou commented on Jul 19, 2018

    @tiennou
    Contributor

    I think you're mismatching with shallow support ? At least I don't remember going anywhere deep down checkout yet 😉.

    From a cursory look at it, sparse checkouts ought to be easier to implement than shallow is (there seem to be no transport-level/pkt-protocol changes needed, as that the repo is still downloaded in full).

    I wonder though, doesn't diff provide a pathspec limiter already ? Am I missing something for your use case @jlindner ?

  7. ethomson commented on Jul 19, 2018

    @ethomson
    Member

    Yes I was conflating the two; I read this issue too quickly and thought it was about shallow not sparse. Sorry!

  8. jlindner commented on Jul 19, 2018

    @jlindner

    Yeah. I definetly need the sparseCheckout ability I believe. Basically, I have about 1500 directories(each directory contains a c++ project) in one git repo. I have a need to extract one directory(one project), without the rest of the directories(projects), and be able to get data from it. I know I can use the git command line to get to this data, but I was hoping that I could do it from the libgit2 functionality, without shelling out to the command line.

  9. jlindner commented on Jul 19, 2018

    @jlindner

    With the diff, which I haven't looked into that much, will it allow me to extract from the git repo, a specific directory, and a specific commit? I am not needing to diff, but if that has the ability to get that out where I could access the files, that might work.

    Basically, what is happening is for a directory/commit, I extract the files in that location, and run some scanners on it making sure they don't contain security flaws and a few other checks. Since the user gives me the directory and commit, for performance reasons, I would like to just get that out, and nothing else. Cloning the whole git repository locally isn't great either, as it is pretty good sized.

  10. tiennou commented on Jul 19, 2018

    @tiennou
    Contributor

    Sorry, I'm still unclear on what you need, and your clarifications seems to imply you really need the files to exist in the filesystem at some point… If that's the case, that's sparse checkout. If that's not, you might be able to get to the "contents" of the directory/files by looking up the commit, traverse its tree to get at the "project" path, and you'll have another tree whose contents are accessible (in memory at least, if your scanner supports that).

    Another caveat is that all this will require a git repository "locally" (though what you meant eludes me), ie. libgit2 will need to open a "clone" of your repository, either the canonical one (because your application is "local" to the machine that has the repository), or a brand new one (ie. clone it).

  11. jlindner commented on Jul 20, 2018

    @jlindner

    Yes, what I am needing is the ability to get one directory out of github, without bringing the whole repository down locally first. I am not wanting to have a local copy of the repository, as that would require large amounts of disk space. I am basically looking for the the git equivalent of SVN export on a specific directory. Basically, I don't need the git repo stuff, I need the files in that location, and only that location, so I can run scanners on them. From my understanding, to do so in git, you would use the sparse checkout. If there is another way to get that specific directory out, I am all for it, but I haven't been able to find a way to do that.

    With the command line git.exe, I have been able to perform this exact task(but I think it creates repo locally, it just doesn't have anything in that repo except this one directory. That is acceptible to my use case.

    I am just looking for the best way, to get one directory out of a git repo, so I can perform actions on our files that we have committed.

  12. jlindner commented on Jul 23, 2018

    @jlindner

    Do you know a better way to accomplish what I am needing to do? Do you think the sparseCheckout is what I need, or do you know of a better way to basically export one directory from a larger github repository. I am open to any ideas as to the best way to get the files out. I don't need a checkout/clone, I just need the actual files that I have checked in.

  13. ethomson commented on Jul 23, 2018

    @ethomson
    Member

    With the command line git.exe, I have been able to perform this exact task(but I think it creates repo locally, it just doesn't have anything in that repo except this one directory. That is acceptible to my use case.

    If you're doing a sparse checkout, you are indeed downloading the entire repository, you're just limiting what's checked out. You can emulate the checkout part of this by specifying paths to the checkout command. (However, this is not true sparse checkout support, as we do not set up the sparse checkout file). It sounds like this is not burdensome for your workflows.

    But this does download more than what you're checking out. If you want individual files, this is not part of the git protocol (at the moment), so you will need to use the API for the hosting provider you are using, whether that's GitHub, Bitbucket, GitLab, VSTS, etc.

  14. jlindner commented on Jul 23, 2018

    @jlindner

    Using the sparsecheckout from git.exe, I have been able to get just one directory out, the size of the entire repo is about 6GB currently, with several thousand projects. The local repo created for the sparsecheckout to work, ends up being about 115k, so it is not bringing down the entire repo, it just brings down that one directory, checked out. It does create the .git folders, which I can live with, because I can just delete them, but it isn't bringing down the entire repo, and taking up 6GB of disk space. I was hoping for an API that would be able to perform this action, since I am actually doing this task from a rest web service that the user calls to perform the action. Shelling out the command line in a web service may not be the worlds smartest idea, but if there is no other way around it, that may be my only option since none of the APIs seem to support this task.

  15. 2 remaining items

  16. jlindner commented on Jul 24, 2018

    @jlindner

    After doing more testing with the sparseCheckout, I have figured out that it isn't going to work for what I need. I was testing the sparseCheckout on a little repository, with only a few directories in it. It seemed really fast, and I wasn't looking at the logs of the action close enough, to realize that is was in fact cloning the entire repo locally, before it ran the sparseCheckout functionality to give me just the files I was wanting. As soon as I pointed it at the real repository, you are correct, it clones the whole thing locally first, and then sets the directory to only what I wanted. Sorry for the confusion. I don't think this is going to work for what I was needing, and we are going to need to take a different approach on the repo.

    Thanks for all your time. Keep up the good work.

    James

  17. ethomson commented on Jul 24, 2018

    @ethomson
    Member

    Thanks for the confirmation, @jlindner. What you've experienced aligns with my expectations.

    Work on "narrow" or "partial" clone support in git is continuing but this is in-progress and not something that libgit2 supports yet. I would encourage you to check out the appropriate API for your hosting provider in the meantime.

    Thanks!

  18. mathomp4 commented on Jul 22, 2019

    @mathomp4

    Hello all. The linked issue above is from me. The project I'm working on uses sparse checkout and apparently powerlevel10k prompt doesn't handle sparse checkout because libgit2 doesn't handle it.

    Note if someone has time and wants to see what the repos I'm using look like, it should be fairly simple to get the code checked out.

  19. mathomp4 commented on Jun 22, 2020

    @mathomp4
  20. jairbubbles commented on May 11, 2021

    @jairbubbles
    Contributor

    Looks like there is a PR on the subject: #5833

  21. zentron commented on May 19, 2022

    @zentron

    PR #5833 and #6169 both look for provide a workable solution to sparse checkouts. Is there anything further waiting for these to progress and merge?

  22. Nikitae57 commented on Oct 21, 2022

    @Nikitae57

    There're 2 PR ready to fix this issue. C'mon, it's been 8 years

  23. ethomson commented on Oct 21, 2022

    @ethomson
    Member

    @Nikitae57 have you tested them? Do they work? What sort of problems do they still have? Where are the performance issues?

  24. mathomp4 commented on Oct 21, 2022

    @mathomp4

    I am thinking that #6394 by @YuKitsune is probably the PR to focus on. I think it's a successor to #5833 by @jochenhz?

  25. shaybenh7 commented on Apr 18, 2023

    @shaybenh7

    hey any news here? this thread is already 9 years old! do you plan on supporting sparse checkout?

  26. MikeTheSnowman commented on Sep 14, 2023

    @MikeTheSnowman

    Raising my hand to keep this issue alive. Sparse checkout is indeed a much needed capability.

  27. MadL1me commented on Jul 23, 2024

    @MadL1me

    Extremely relevant feature for me as well. I'm required to use plain git in cli instead of libgit2, because it does not supports sparse checkouts currently.

  28. lrm29 commented on Jun 13, 2025

    @lrm29
    Contributor

    There are possibly multiple issues being conflated into one here:

    1. There are complaints that libgit2 "does not work", but I think that may have been to do with config.worktreeConfig which is now supported: Separate config reader and writer backend priorities (for worktree configs) #6756. Indeed I can point libgit2 at a sparse checkout without a hard error due to an unsupported extension.
    2. The internals of libgit2 currently don't work with sparse checkouts. e.g. checkout functions won't skip files not in the sparse specification.
    3. There is no libgit2 API for creating and managing sparse checkouts.

    I think aiming for 2) is a good starting point, and more likely to get merged. We also have to show there's no performance degradation when not using sparse checkouts.

    @jochenhz @YuKitsune, I checked out your PRs and have a couple of questions if you're still around:

    1. For the diff/checkout API, why are GIT_DIFF_SKIP_SPARSE_FILES and GIT_CHECKOUT_REMOVE_SPARSE_FILES not the default and need to be passed in?
    2. From the doc (https://git-scm.com/docs/git-sparse-checkout/2.42.0) it seems that non-cone mode was deprecated in 2.37.0. Did you attempt implementing cone mode? I think it's probably best to not support non-cone mode at all (it's not compatible with sparse-index either).

    I've implemented (corrrectly I hope) cone-mode pattern matching here: mathworks#12. I'm not sure about making the .git/info/sparse-checkout file a "git_attr_file", all that seems to give us is reparsing when the file changes on disk. We might need finer-grained control, and we still need to cache the cone-mode hashmaps somewhere.

    @ethomson If you have time, would welcome advice on what your policy would be on deprecated Git features. non-cone mode is deprecated and I have no interest in implementing/supporting it. I don't know what "deprecated" means in Git itself (will they actually delete or error in future?). Would libgit2 still want to match exactly Git's behaviour, or is it fine for us to return GIT_EUNSUPPORTED for deprecated Git features? If the deprecated behaviour does need to be supported there's some horrible behaviour to implement as well, such as warning (not erroring!) and falling back to non-cone mode patterns if there is an error parsing cone-mode patterns.

  29. olive007 commented on Oct 2, 2026

    @olive007

    I’m seeing behavior on NixOS that may be related to this issue: Nix reports a sparse checkout as dirty while Git reports it as clean.
    I use sparse checkout to omit documentation and CI files from my system configuration repository:

    git -C /etc/nixos sparse-checkout set --no-cone \
      '/*' \
      '!/documentation/' \
      '!/README.md' \
      '!/AGENTS.md' \
      '!/hosts/*' \
      '/hosts/office/' \
      '!/.gitlab-ci.yml'
    

    Running this from /etc/nixos:
    nixos-rebuild switch --flake .#olive-office

    Produces this warning during evaluation:
    warning: Git tree '/etc/nixos' is dirty

    But checking the same working tree:

    git -C /etc/nixos status
    Reports:
    On branch master
    Your branch is up to date with 'origin/master'.
    
    You are in a sparse checkout with 73% of tracked files present.
    
    nothing to commit, working tree clean
    

    Nix’s working-tree status implementation uses libgit2’s git_status_foreach_ext (example in Nix 2.34.8). I suspect files intentionally omitted by sparse checkout are being interpreted as deletions, but I haven’t isolated this with a standalone libgit2 reproducer.
    The expected behavior is that intentionally omitted files do not make the working tree dirty, while actual modifications to included files still do. I need to build from local files, including uncommitted edits, so selecting a committed revision does not meet that requirement.

    Is this consistent with the missing sparse-checkout status support tracked here?

    nix version 2.34.8
    git version 2.54.0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions