Search code examples
gitgit-checkoutsparse-checkout

Sparse checkout for a specific file on empty repo


I have two repos: ci-test and ci-test2. The ci-test repo contains a CI.yaml file and the ci-test2 repo is empty. Using mgit, I am performing a sparse checkout of only the CI.yaml file on both repos and I get the error, "Sparse checkout leaves no entry on working directory". I am almost sure that this happens because I am trying to checkout the file in the ci-test2 repo which does not contain it. I was wondering if there is a way to gracefully handle this error, perhaps by looking into git read-tree. This is just an example, I have many more repos than this and my mgit checkout class does not know anything about the contents of my repos. Does anybody have an idea?


Solution

  • Even before the sparse checkout itself, you could check if the remote repository has the CI.yaml file: see "How do I check if a file exists in a remote?".

    But if you already have fetched the repository (and only need to checkout/restore one file in it), a git cat-file -e could work:

    -e
    

    Exit with zero status if <object> exists and is a valid object.
    If <object> is of an invalid format, exit with non-zero and emits an error on stderr.