From f8d186e7763cefde8b7f146c40aff08e5489d352 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Mon, 31 Oct 2022 12:16:13 +0100 Subject: [PATCH] pmbootstrap ci: fix error with deleted files Check if each file listed by the two git ls-files commands still exists, and only add the existing ones to the tarball. Otherwise it would crash when a file has been deleted without having the change commited yet. Link: https://lists.sr.ht/~postmarketos/pmbootstrap-devel/%3C20221031111614.1377-1-ollieparanoid@postmarketos.org%3E --- pmb/ci/__init__.py | 11 ++++++++--- pmb/helpers/git.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pmb/ci/__init__.py b/pmb/ci/__init__.py index 98b6022a..879bf661 100644 --- a/pmb/ci/__init__.py +++ b/pmb/ci/__init__.py @@ -111,10 +111,15 @@ def copy_git_repo_to_chroot(args, topdir): pmb.helpers.git.get_topdir() """ pmb.chroot.init(args) tarball_path = f"{args.work}/chroot_native/tmp/git.tar.gz" + files = pmb.helpers.git.get_files(args, topdir) - cmd = "(git ls-files && git ls-files --exclude-standard --other)" - cmd += f" | tar -cf {shlex.quote(tarball_path)} -T -" - pmb.helpers.run.user(args, ["sh", "-c", cmd], topdir) + with open(f"{tarball_path}.files", "w") as handle: + for file in files: + handle.write(file) + handle.write("\n") + + pmb.helpers.run.user(args, ["tar", "-cf", tarball_path, "-T", + f"{tarball_path}.files"], topdir) ci_dir = "/home/pmos/ci" pmb.chroot.user(args, ["rm", "-rf", ci_dir]) diff --git a/pmb/helpers/git.py b/pmb/helpers/git.py index d33ecf1b..50597c91 100644 --- a/pmb/helpers/git.py +++ b/pmb/helpers/git.py @@ -253,3 +253,22 @@ def get_topdir(args, path): empty string if it's not a git repository. """ return pmb.helpers.run.user(args, ["git", "rev-parse", "--show-toplevel"], path, output_return=True, check=False).rstrip() + + +def get_files(args, path): + """ Get all files inside a git repository, that are either already in the + git tree or are not in gitignore. Do not list deleted files. To be used + for creating a tarball of the git repository. + :param path: top dir of the git repository + :returns: all files in a git repository as list, relative to path """ + ret = [] + files = pmb.helpers.run.user(args, ["git", "ls-files"], path, + output_return=True).split("\n") + files += pmb.helpers.run.user(args, ["git", "ls-files", + "--exclude-standard", "--other"], path, + output_return=True).split("\n") + for file in files: + if os.path.exists(f"{path}/{file}"): + ret += [file] + + return ret