Skip to content

Commit 0cc3d1d

Browse files
authored
Properly support multiple submodules in git packager (#154)
Signed-off-by: Hemil Desai <[email protected]>
1 parent 3426adf commit 0cc3d1d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/nemo_run/core/packaging/git.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,14 @@ def package(self, path: Path, job_dir: str, name: str) -> str:
120120
git_archive_cmd = (
121121
f"git archive --format=tar --output={output_file}.tmp {self.ref}:{git_sub_path}"
122122
)
123+
if os.uname().sysname == "Linux":
124+
tar_submodule_cmd = f"tar Af {output_file}.tmp $sha1.tmp && rm $sha1.tmp"
125+
else:
126+
tar_submodule_cmd = f"cat $sha1.tmp >> {output_file}.tmp && rm $sha1.tmp"
127+
123128
git_submodule_cmd = f"""git submodule foreach --recursive \
124-
'git archive --format=tar --prefix=$sm_path/ --output=$sha1.tmp HEAD && cat $sha1.tmp >> {output_file}.tmp && rm $sha1.tmp'"""
129+
'git archive --format=tar --prefix=$sm_path/ --output=$sha1.tmp HEAD && {tar_submodule_cmd}'"""
130+
125131
with ctx.cd(git_base_path):
126132
ctx.run(git_archive_cmd)
127133
if self.include_submodules:

test/core/packaging/test_git.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,17 +342,28 @@ def test_untracked_files_raises_exception(temp_repo):
342342
@patch("nemo_run.core.packaging.git.Context", MockContext)
343343
def test_package_with_include_submodules(packager, temp_repo):
344344
temp_repo = Path(temp_repo)
345-
# Create a submodule
345+
# Create first submodule
346346
submodule_path = temp_repo / "submodule"
347347
submodule_path.mkdir()
348348
os.chdir(str(submodule_path))
349349
subprocess.check_call(["git", "init", "--initial-branch=main"])
350350
open("submodule_file.txt", "w").write("Submodule file")
351351
subprocess.check_call(["git", "add", "."])
352352
subprocess.check_call(["git", "commit", "-m", "Initial submodule commit"])
353+
354+
# Create second submodule
355+
submodule2_path = temp_repo / "submodule2"
356+
submodule2_path.mkdir()
357+
os.chdir(str(submodule2_path))
358+
subprocess.check_call(["git", "init", "--initial-branch=main"])
359+
open("submodule2_file.txt", "w").write("Second submodule file")
360+
subprocess.check_call(["git", "add", "."])
361+
subprocess.check_call(["git", "commit", "-m", "Initial submodule2 commit"])
362+
353363
os.chdir(str(temp_repo))
354364
subprocess.check_call(["git", "submodule", "add", str(submodule_path)])
355-
subprocess.check_call(["git", "commit", "-m", "Add submodule"])
365+
subprocess.check_call(["git", "submodule", "add", str(submodule2_path)])
366+
subprocess.check_call(["git", "commit", "-m", "Add submodules"])
356367

357368
packager = GitArchivePackager(ref="HEAD", include_submodules=True)
358369
with tempfile.TemporaryDirectory() as job_dir:
@@ -364,13 +375,22 @@ def test_package_with_include_submodules(packager, temp_repo):
364375
f"tar -xvzf {output_file} -C {os.path.join(job_dir, 'extracted_output')} --ignore-zeros"
365376
),
366377
)
378+
# Check first submodule
367379
cmp = filecmp.dircmp(
368380
os.path.join(temp_repo, "submodule"),
369381
os.path.join(job_dir, "extracted_output", "submodule"),
370382
)
371383
assert cmp.left_list == cmp.right_list
372384
assert not cmp.diff_files
373385

386+
# Check second submodule
387+
cmp2 = filecmp.dircmp(
388+
os.path.join(temp_repo, "submodule2"),
389+
os.path.join(job_dir, "extracted_output", "submodule2"),
390+
)
391+
assert cmp2.left_list == cmp2.right_list
392+
assert not cmp2.diff_files
393+
374394

375395
@patch("nemo_run.core.packaging.git.Context", MockContext)
376396
def test_package_without_include_submodules(packager, temp_repo):

0 commit comments

Comments
 (0)