From e526d57956221df3f3e14505631e369b546bf4a5 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Mon, 27 Feb 2023 13:40:56 +0100 Subject: [PATCH 1/2] Fix git archive for `.gitattributes` Signed-off-by: Cristian Le --- src/tito/builder/submodule_aware_builder.py | 2 +- src/tito/common.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tito/builder/submodule_aware_builder.py b/src/tito/builder/submodule_aware_builder.py index 9b1a2da8..8bd5b458 100644 --- a/src/tito/builder/submodule_aware_builder.py +++ b/src/tito/builder/submodule_aware_builder.py @@ -86,7 +86,7 @@ def _setup_sources(self): def run_git_archive(self, relative_git_dir, prefix, commit, dest_tar, subdir=None): # command to generate a git-archive - git_archive_cmd = "git archive --format=tar --prefix=%s/ %s:%s --output=%s" % ( + git_archive_cmd = "git archive --format=tar --prefix=%s/ %s %s --output=%s" % ( prefix, commit, relative_git_dir, diff --git a/src/tito/common.py b/src/tito/common.py index 8c9fe892..59d821bf 100644 --- a/src/tito/common.py +++ b/src/tito/common.py @@ -868,7 +868,7 @@ def create_tgz(git_root, prefix, commit, relative_dir, initial_tar = "%s.initial" % basename # command to generate a git-archive - git_archive_cmd = 'git archive --format=tar --prefix=%s/ %s:%s --output=%s' % ( + git_archive_cmd = 'git archive --format=tar --prefix=%s/ %s %s --output=%s' % ( prefix, commit, relative_git_dir, initial_tar) run_command(git_archive_cmd) From d008bd184e91b2545f1db4570ecc7adc6d61cac6 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Fri, 3 Mar 2023 10:11:24 +0100 Subject: [PATCH 2/2] Fix tar prefix Signed-off-by: Cristian Le --- src/tito/common.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/tito/common.py b/src/tito/common.py index 59d821bf..0b76c064 100644 --- a/src/tito/common.py +++ b/src/tito/common.py @@ -868,8 +868,8 @@ def create_tgz(git_root, prefix, commit, relative_dir, initial_tar = "%s.initial" % basename # command to generate a git-archive - git_archive_cmd = 'git archive --format=tar --prefix=%s/ %s %s --output=%s' % ( - prefix, commit, relative_git_dir, initial_tar) + git_archive_cmd = 'git archive --format=tar %s %s --output=%s' % ( + commit, relative_git_dir, initial_tar) run_command(git_archive_cmd) # Run git-archive separately if --debug was specified. @@ -878,14 +878,33 @@ def create_tgz(git_root, prefix, commit, relative_dir, debug('git-archive fails if relative dir is not in git tree', '%s > /dev/null' % git_archive_cmd) - fixed_tar = "%s.tar" % basename - fixed_tar_fh = open(fixed_tar, 'wb') + fixed_timestamp_tar = "%s.fixed_timestamp" % basename + fixed_tar_fh = open(fixed_timestamp_tar, 'wb') try: tarfixer = TarFixer(open(initial_tar, 'rb'), fixed_tar_fh, timestamp, commit) tarfixer.fix() finally: fixed_tar_fh.close() + # Fix tar archive locations + fixed_tar = "%s.tar" % basename + # First normalize paths + relative_git_dir = relative_git_dir.removeprefix("/").removeprefix("./") + if relative_git_dir and not relative_git_dir.endswith("/"): + # Note: if relative_git_dir is empty, it should remain empty + relative_git_dir += "/" + prefix = prefix.removeprefix("/").removeprefix("./") + if not prefix.endswith("/"): + prefix += "/" + # Extract archive to a temp folder + tar_extract_transform_cmd = 'tar -xf %s --transform="s|^%s|tmp_extract/%s|g"' % ( + fixed_timestamp_tar, relative_git_dir, prefix) + run_command(tar_extract_transform_cmd) + # Re-compress the archive + tar_create_cmd = 'tar -cf %s -C tmp_extract/ %s' % ( + fixed_tar, prefix) + run_command(tar_create_cmd) + # It's a pity we can't use Python's gzip, but it doesn't offer an equivalent of -n return run_command("gzip -n -c < %s > %s" % (fixed_tar, dest_tgz))