|
| 1 | +# Partial Submodules for Git |
| 2 | + |
| 3 | +**git-partial-submodule** is a command-line script for setting up and working with submodules while |
| 4 | +enabling them to use git's [partial clone](https://github.blog/2020-12-21-get-up-to-speed-with-partial-clone-and-shallow-clone/) |
| 5 | +and [sparse checkout](https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/) |
| 6 | +features. |
| 7 | + |
| 8 | +In top-level repositories, git provides various partial-clone options such as "blobless" clone, which |
| 9 | +reduce the burden of downloading large repositories. For submodules, only "shallow" clones are |
| 10 | +supported by git, but shallow clones have usability issues that make the resulting repo difficult to |
| 11 | +work with. **git-partial-submodule** clones your submodules as blobless repos, leaving them fully |
| 12 | +functional while saving bandwidth and disk space compared to a full clone. |
| 13 | + |
| 14 | +Similarly, top-level repositories support sparse checkout, which lets you cut down the worktree to |
| 15 | +just the files you're interested in. This is particularly relevant for submodules, as their |
| 16 | +repositories often contain extra contents such as tests, examples, ancillary tools, and suchlike |
| 17 | +that we don't need if we just want to use the submodule as a library in our project. |
| 18 | +**git-partial-submodule** stores sparse checkout patterns in `.gitmodules`, so they can be managed |
| 19 | +under version control and automatically applied when the submodules are cloned. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +* git 2.27.0 or later |
| 24 | +* Python 3.8 or later |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +**git-partial-submodule** is a single-file Python script. [Download the script](https://github.com/Reedbeta/git-partial-submodule/blob/main/git-partial-submodule.py) |
| 29 | +and put it somewhere in your `PATH`, or add it to your repository. Or add this repository as a |
| 30 | +submodule. |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +``` |
| 35 | +git-partial-submodule.py add [-b BRANCH] [--name NAME] [--sparse] <repository> <path> |
| 36 | +git-partial-submodule.py clone [<path>...] |
| 37 | +git-partial-submodule.py save-sparse [<path>...] |
| 38 | +git-partial-submodule.py restore-sparse [<path>...] |
| 39 | +``` |
| 40 | + |
| 41 | +### Add |
| 42 | + |
| 43 | +Creates and clones a new submodule, much like `git submodule add`, but performs a blobless clone. |
| 44 | +If `--sparse` is specified, also enables sparse checkout on the new submodule, with the default |
| 45 | +pattern set of `/* !/*/`. |
| 46 | + |
| 47 | +### Clone |
| 48 | + |
| 49 | +Use this to initialize submodules after a fresh clone of the superproject. Performs blobless clones |
| 50 | +of any submodules that are not already cloned. Also applies any sparse checkout patterns specified |
| 51 | +in `.gitmodules`. |
| 52 | + |
| 53 | +### Save-Sparse |
| 54 | + |
| 55 | +After making changes to the sparse patterns in a submodule, use this to save them to `.gitmodules`. |
| 56 | +Patterns are stored space-delimited in the `sparse-checkout` property. |
| 57 | + |
| 58 | +### Restore-Sparse |
| 59 | + |
| 60 | +Reapplies the sparse patterns saved in `.gitmodules` to the actual submodules. Use this after |
| 61 | +pulling or switching branches, etc. |
| 62 | + |
| 63 | +## Limitations and Cautions |
| 64 | + |
| 65 | +Partial clone and sparse checkout are both still experimental git features that may have sharp |
| 66 | +edges. |
| 67 | + |
| 68 | +This tool works by fiddling with the internals of your repository in not-officially-supported ways, |
| 69 | +so it might fail or do the wrong thing in some edge cases I haven't considered (and might leave your |
| 70 | +repo in a bad state afterward). |
| 71 | + |
| 72 | +Not all of the various command-line options to the underlying `git clone`, `git submodule add`, etc. |
| 73 | +are supported. |
| 74 | + |
| 75 | +"Cone" mode for sparse checkout is not currently supported. |
| 76 | + |
| 77 | +Spaces in sparse checkout patterns are not currently handled correctly. |
0 commit comments