Skip to content

Commit 959b545

Browse files
hvoigtgitster
authored andcommitted
submodule: implement a config API for lookup of .gitmodules values
In a superproject some commands need to interact with submodules. They need to query values from the .gitmodules file either from the worktree of from certain revisions. At the moment this is quite hard since a caller would need to read the .gitmodules file from the history and then parse the values. We want to provide an API for this so we have one place to get values from .gitmodules from any revision (including the worktree). The API is realized as a cache which allows us to lazily read .gitmodules configurations by commit into a runtime cache which can then be used to easily lookup values from it. Currently only the values for path or name are stored but it can be extended for any value needed. It is expected that .gitmodules files do not change often between commits. Thats why we lookup the .gitmodules sha1 from a commit and then either lookup an already parsed configuration or parse and cache an unknown one for each sha1. The cache is lazily build on demand for each requested commit. This cache can be used for all purposes which need knowledge about submodule configurations. Example use cases are: * Recursive submodule checkout needs to lookup a submodule name from its path when a submodule first appears. This needs be done before this configuration exists in the worktree. * The implementation of submodule support for 'git archive' needs to lookup the submodule name to generate the archive when given a revision that is not checked out. * 'git fetch' when given the --recurse-submodules=on-demand option (or configuration) needs to lookup submodule names by path from the database rather than reading from the worktree. For new submodule it needs to lookup the name from its path to allow cloning new submodules into the .git folder so they can be checked out without any network interaction when the user does a checkout of that revision. Signed-off-by: Heiko Voigt <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f86f31a commit 959b545

File tree

9 files changed

+671
-0
lines changed

9 files changed

+671
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
/test-sha1-array
205205
/test-sigchain
206206
/test-string-list
207+
/test-submodule-config
207208
/test-subprocess
208209
/test-svn-fe
209210
/test-urlmatch-normalization
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
submodule config cache API
2+
==========================
3+
4+
The submodule config cache API allows to read submodule
5+
configurations/information from specified revisions. Internally
6+
information is lazily read into a cache that is used to avoid
7+
unnecessary parsing of the same .gitmodule files. Lookups can be done by
8+
submodule path or name.
9+
10+
Usage
11+
-----
12+
13+
The caller can look up information about submodules by using the
14+
`submodule_from_path()` or `submodule_from_name()` functions. They return
15+
a `struct submodule` which contains the values. The API automatically
16+
initializes and allocates the needed infrastructure on-demand.
17+
18+
If the internal cache might grow too big or when the caller is done with
19+
the API, all internally cached values can be freed with submodule_free().
20+
21+
Data Structures
22+
---------------
23+
24+
`struct submodule`::
25+
26+
This structure is used to return the information about one
27+
submodule for a certain revision. It is returned by the lookup
28+
functions.
29+
30+
Functions
31+
---------
32+
33+
`void submodule_free()`::
34+
35+
Use these to free the internally cached values.
36+
37+
`const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const char *path)`::
38+
39+
Lookup values for one submodule by its commit_sha1 and path.
40+
41+
`const struct submodule *submodule_from_name(const unsigned char *commit_sha1, const char *name)`::
42+
43+
The same as above but lookup by name.
44+
45+
For an example usage see test-submodule-config.c.

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ TEST_PROGRAMS_NEED_X += test-sha1
594594
TEST_PROGRAMS_NEED_X += test-sha1-array
595595
TEST_PROGRAMS_NEED_X += test-sigchain
596596
TEST_PROGRAMS_NEED_X += test-string-list
597+
TEST_PROGRAMS_NEED_X += test-submodule-config
597598
TEST_PROGRAMS_NEED_X += test-subprocess
598599
TEST_PROGRAMS_NEED_X += test-svn-fe
599600
TEST_PROGRAMS_NEED_X += test-urlmatch-normalization
@@ -784,6 +785,7 @@ LIB_OBJS += strbuf.o
784785
LIB_OBJS += streaming.o
785786
LIB_OBJS += string-list.o
786787
LIB_OBJS += submodule.o
788+
LIB_OBJS += submodule-config.o
787789
LIB_OBJS += symlinks.o
788790
LIB_OBJS += tag.o
789791
LIB_OBJS += trace.o

0 commit comments

Comments
 (0)