Skip to content

Commit 85bb84b

Browse files
committed
Submodules: Add fetch depth options
1 parent 7bed9d2 commit 85bb84b

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

pygit2/submodules.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def init(self, overwrite: bool = False):
7373
err = C.git_submodule_init(self._subm, int(overwrite))
7474
check_error(err)
7575

76-
def update(self, init: bool = False, callbacks: RemoteCallbacks = None):
76+
def update(
77+
self, init: bool = False, callbacks: RemoteCallbacks = None, depth: int = 0
78+
):
7779
"""
7880
Update a submodule. This will clone a missing submodule and checkout
7981
the subrepository to the commit specified in the index of the
@@ -90,12 +92,17 @@ def update(self, init: bool = False, callbacks: RemoteCallbacks = None):
9092
9193
callbacks
9294
Optional RemoteCallbacks to clone or fetch the submodule.
95+
96+
depth
97+
Number of commits to fetch.
98+
The default is 0 (full commit history).
9399
"""
94100

95101
opts = ffi.new('git_submodule_update_options *')
96102
C.git_submodule_update_options_init(
97103
opts, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION
98104
)
105+
opts.fetch_opts.depth = depth
99106

100107
with git_fetch_options(callbacks, opts=opts.fetch_opts) as payload:
101108
err = C.git_submodule_update(self._subm, int(init), opts)
@@ -188,6 +195,7 @@ def add(
188195
path: str,
189196
link: bool = True,
190197
callbacks: Optional[RemoteCallbacks] = None,
198+
depth: int = 0,
191199
) -> Submodule:
192200
"""
193201
Add a submodule to the index.
@@ -208,6 +216,10 @@ def add(
208216
209217
callbacks
210218
Optional RemoteCallbacks to clone the submodule.
219+
220+
depth
221+
Number of commits to fetch.
222+
The default is 0 (full commit history).
211223
"""
212224
csub = ffi.new('git_submodule **')
213225
curl = ffi.new('char[]', to_bytes(url))
@@ -226,6 +238,7 @@ def add(
226238
C.git_submodule_update_options_init(
227239
opts, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION
228240
)
241+
opts.fetch_opts.depth = depth
229242

230243
with git_fetch_options(callbacks, opts=opts.fetch_opts) as payload:
231244
crepo = ffi.new('git_repository **')
@@ -268,6 +281,7 @@ def update(
268281
submodules: Optional[Iterable[str]] = None,
269282
init: bool = False,
270283
callbacks: Optional[RemoteCallbacks] = None,
284+
depth: int = 0,
271285
):
272286
"""
273287
Update submodules. This will clone a missing submodule and checkout
@@ -289,14 +303,18 @@ def update(
289303
290304
callbacks
291305
Optional RemoteCallbacks to clone or fetch the submodule.
306+
307+
depth
308+
Number of commits to fetch.
309+
The default is 0 (full commit history).
292310
"""
293311
if submodules is None:
294312
submodules = self._repository.listall_submodules()
295313

296314
instances = [self[s] for s in submodules]
297315

298316
for submodule in instances:
299-
submodule.update(init, callbacks)
317+
submodule.update(init, callbacks, depth)
300318

301319
def status(
302320
self, name: str, ignore: SubmoduleIgnore = SubmoduleIgnore.UNSPECIFIED

test/test_submodule.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
SUBM_PATH = 'TestGitRepository'
3939
SUBM_URL = 'https://github.com/libgit2/TestGitRepository'
4040
SUBM_HEAD_SHA = '49322bb17d3acc9146f98c97d078513228bbf3c0'
41+
SUBM_BOTTOM_SHA = '6c8b137b1c652731597c89668f417b8695f28dd7'
4142

4243

4344
@pytest.fixture
@@ -143,37 +144,54 @@ def test_update_instance(repo):
143144

144145

145146
@utils.requires_network
146-
def test_oneshot_update(repo):
147+
@pytest.mark.parametrize('depth', [0, 1])
148+
def test_oneshot_update(repo, depth):
147149
status = repo.submodules.status(SUBM_NAME)
148150
assert status == (SS.IN_HEAD | SS.IN_INDEX | SS.IN_CONFIG | SS.WD_UNINITIALIZED)
149151

150152
subrepo_file_path = Path(repo.workdir) / SUBM_PATH / 'master.txt'
151153
assert not subrepo_file_path.exists()
152-
repo.submodules.update(init=True)
154+
repo.submodules.update(init=True, depth=depth)
153155
assert subrepo_file_path.exists()
154156

155157
status = repo.submodules.status(SUBM_NAME)
156158
assert status == (SS.IN_HEAD | SS.IN_INDEX | SS.IN_CONFIG | SS.IN_WD)
157159

160+
sm_repo = repo.submodules[SUBM_NAME].open()
161+
if depth == 0:
162+
sm_repo[SUBM_BOTTOM_SHA] # full history must be available
163+
else:
164+
with pytest.raises(KeyError):
165+
sm_repo[SUBM_BOTTOM_SHA] # shallow clone
166+
158167

159168
@utils.requires_network
160-
def test_oneshot_update_instance(repo):
169+
@pytest.mark.parametrize('depth', [0, 1])
170+
def test_oneshot_update_instance(repo, depth):
161171
subrepo_file_path = Path(repo.workdir) / SUBM_PATH / 'master.txt'
162172
assert not subrepo_file_path.exists()
163173
sm = repo.submodules[SUBM_NAME]
164-
sm.update(init=True)
174+
sm.update(init=True, depth=depth)
165175
assert subrepo_file_path.exists()
166176

177+
sm_repo = sm.open()
178+
if depth == 0:
179+
sm_repo[SUBM_BOTTOM_SHA] # full history must be available
180+
else:
181+
with pytest.raises(KeyError):
182+
sm_repo[SUBM_BOTTOM_SHA] # shallow clone
183+
167184

168185
@utils.requires_network
169186
def test_head_id(repo):
170187
assert repo.submodules[SUBM_PATH].head_id == SUBM_HEAD_SHA
171188

172189

173190
@utils.requires_network
174-
def test_add_submodule(repo):
191+
@pytest.mark.parametrize('depth', [0, 1])
192+
def test_add_submodule(repo, depth):
175193
sm_repo_path = 'test/testrepo'
176-
sm = repo.submodules.add(SUBM_URL, sm_repo_path)
194+
sm = repo.submodules.add(SUBM_URL, sm_repo_path, depth=depth)
177195

178196
status = repo.submodules.status(sm_repo_path)
179197
assert status == (SS.IN_INDEX | SS.IN_CONFIG | SS.IN_WD | SS.INDEX_ADDED)
@@ -183,6 +201,12 @@ def test_add_submodule(repo):
183201
assert SUBM_URL == sm.url
184202
assert not sm_repo.is_empty
185203

204+
if depth == 0:
205+
sm_repo[SUBM_BOTTOM_SHA] # full history must be available
206+
else:
207+
with pytest.raises(KeyError):
208+
sm_repo[SUBM_BOTTOM_SHA] # shallow clone
209+
186210

187211
@utils.requires_network
188212
def test_submodule_status(repo):

0 commit comments

Comments
 (0)