'git sparse checkout with multiple repositories
I am wanting to create a project that borrows from two repositories, sparsely checked out. However the format of "${GIT_REPO}/info/sparse-checkout" only references a path, not a tree-ish or repo centric path. Hence I cannot seem to reference a specific branch like within this sparse-checkout file like: repo::branch:~/path
hence the following does not work for me over multiple repos:
git config core.sparseCheckout true
git remote add -f repo1 git://...
git remote add -f repo2 git://...
echo "path/within_repo/to/desired_subdir/*" > .git/info/sparse-checkout
git checkout [<tree-ish>]
Solution 1:[1]
... the format of "${GIT_REPO}/info/sparse-checkout" only references a path
That's true.
git remote add -f repo1 git://...
Repositories (and remote names) are not relevant; all that matters are commits.
Hence I cannot seem to reference a specific branch ...
Branch names are irrelevant as well. Branch names move and change over time; commit hash IDs do not. Any time you want to be totally specific about some particular commit, use its hash ID.
It might be nice if the sparse checkout file could be based on the commit, but it can't—at least not easily. You could:
- put a file into the commit itself, or some dedicated commit-or-branch;
- extract the desired sparse-checkout file from the commit (e.g.,
git show commit-specifier:path); - write its contents into
$(git rev-parse --git-dir)/info/sparse-checkout; and - run the desired checkout
with, perhaps, a fall-back default sparse-checkout file if the file does not exist in the given commit.
Since commit hash IDs uniquely identify the correct commit, one approach to putting the file into a dedicated branch would be:
- choose a branch name, e.g.,
sparse-info - use the various plumbing tools (or, easier,
git worktree add) to create an area in which these files will live; - name the files by commit hash ID.
Then to add a new file, you'd maneuver to the right place, link or copy the sparse checkout data, git add the file, and git commit the result.
Or, you could do all this without a branch, with just a dedicated work-area outside your normal work-tree.
Finally, consider writing your own format for your sparse checkout file, and a filter. Then it's just a matter of running your filter-generator to create the desired $GIT_DIR/info/sparse-checkout contents.
Solution 2:[2]
From torek's answer:
write its contents into
$(git rev-parse --git-dir)/info/sparse-checkout
Make sure to use Git 2.37 (Q3 2022): "git show :<path>"(man) learned to work better with the sparse-index feature, because of rev-parse.
See commit 124b05b, commit 4925adb, commit 561287d, commit a37d144, commit a9e0a49 (26 Apr 2022) by Derrick Stolee (derrickstolee).
(Merged by Junio C Hamano -- gitster -- in commit acdeb10, 20 May 2022)
rev-parse: integrate with sparse indexSigned-off-by: Derrick Stolee
It is not obvious that the '
git rev-parse'(man) builtin would use the sparse index, but it is possible to parse paths out of the index using the ":<path>" syntax.
The 'git rev-parse' output is only the OID of the object found at that location, but otherwise behaves similarly to 'git show :<path>'(man).
This includes the failure conditions on directories and the error messages depending on whether a path is in the worktree or not.The only code change required is to change the
command_requires_full_indexsetting inbuiltin/rev-parse.c, and we can re-use many existing 'git show' tests for the rev-parse case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | torek |
| Solution 2 | VonC |
