Skip to content

Commit 1a8442d

Browse files
committed
Merge remote-tracking branch 'jorio/submodule-fetch-depth'
2 parents 2538735 + 85bb84b commit 1a8442d

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
@@ -166,37 +167,54 @@ def test_update_instance(repo):
166167

167168

168169
@utils.requires_network
169-
def test_oneshot_update(repo):
170+
@pytest.mark.parametrize('depth', [0, 1])
171+
def test_oneshot_update(repo, depth):
170172
status = repo.submodules.status(SUBM_NAME)
171173
assert status == (SS.IN_HEAD | SS.IN_INDEX | SS.IN_CONFIG | SS.WD_UNINITIALIZED)
172174

173175
subrepo_file_path = Path(repo.workdir) / SUBM_PATH / 'master.txt'
174176
assert not subrepo_file_path.exists()
175-
repo.submodules.update(init=True)
177+
repo.submodules.update(init=True, depth=depth)
176178
assert subrepo_file_path.exists()
177179

178180
status = repo.submodules.status(SUBM_NAME)
179181
assert status == (SS.IN_HEAD | SS.IN_INDEX | SS.IN_CONFIG | SS.IN_WD)
180182

183+
sm_repo = repo.submodules[SUBM_NAME].open()
184+
if depth == 0:
185+
sm_repo[SUBM_BOTTOM_SHA] # full history must be available
186+
else:
187+
with pytest.raises(KeyError):
188+
sm_repo[SUBM_BOTTOM_SHA] # shallow clone
189+
181190

182191
@utils.requires_network
183-
def test_oneshot_update_instance(repo):
192+
@pytest.mark.parametrize('depth', [0, 1])
193+
def test_oneshot_update_instance(repo, depth):
184194
subrepo_file_path = Path(repo.workdir) / SUBM_PATH / 'master.txt'
185195
assert not subrepo_file_path.exists()
186196
sm = repo.submodules[SUBM_NAME]
187-
sm.update(init=True)
197+
sm.update(init=True, depth=depth)
188198
assert subrepo_file_path.exists()
189199

200+
sm_repo = sm.open()
201+
if depth == 0:
202+
sm_repo[SUBM_BOTTOM_SHA] # full history must be available
203+
else:
204+
with pytest.raises(KeyError):
205+
sm_repo[SUBM_BOTTOM_SHA] # shallow clone
206+
190207

191208
@utils.requires_network
192209
def test_head_id(repo):
193210
assert repo.submodules[SUBM_PATH].head_id == SUBM_HEAD_SHA
194211

195212

196213
@utils.requires_network
197-
def test_add_submodule(repo):
214+
@pytest.mark.parametrize('depth', [0, 1])
215+
def test_add_submodule(repo, depth):
198216
sm_repo_path = 'test/testrepo'
199-
sm = repo.submodules.add(SUBM_URL, sm_repo_path)
217+
sm = repo.submodules.add(SUBM_URL, sm_repo_path, depth=depth)
200218

201219
status = repo.submodules.status(sm_repo_path)
202220
assert status == (SS.IN_INDEX | SS.IN_CONFIG | SS.IN_WD | SS.INDEX_ADDED)
@@ -206,6 +224,12 @@ def test_add_submodule(repo):
206224
assert SUBM_URL == sm.url
207225
assert not sm_repo.is_empty
208226

227+
if depth == 0:
228+
sm_repo[SUBM_BOTTOM_SHA] # full history must be available
229+
else:
230+
with pytest.raises(KeyError):
231+
sm_repo[SUBM_BOTTOM_SHA] # shallow clone
232+
209233

210234
@utils.requires_network
211235
def test_submodule_status(repo):

0 commit comments

Comments
 (0)