gitconfig.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import subprocess
  3. import infra
  4. GIT_HOST_DIR = "host"
  5. GIT_CONFIG_DIR = os.path.join(GIT_HOST_DIR, "home/br-user")
  6. GIT_CONFIG_FILE = os.path.join(GIT_CONFIG_DIR, "gitconfig")
  7. def run_git_config(logfile, cmd):
  8. logfile.write(
  9. "> running git config with '{}'\n".format(" ".join(cmd)))
  10. try:
  11. subprocess.check_call(cmd, stdout=logfile, stderr=logfile)
  12. except FileNotFoundError:
  13. logfile.write("> git config failed\n")
  14. raise SystemError("git config failed")
  15. def generate_gitconfig(builddir, logtofile, gitremotedir):
  16. logfile = infra.open_log_file(builddir, "gitconfig", logtofile)
  17. # The git repository used by this test is cloned locally from
  18. # gitremotedir using the "file://" protocol. Since it contains
  19. # several git submodules we need to allow this protocol to be used
  20. # with git submodules. Since we don't want to modify the user
  21. # (global) gitconfig, we use a local gitconfig file.
  22. localgitconfig = os.path.join(builddir, GIT_CONFIG_FILE)
  23. gitconfigdir = os.path.join(builddir, GIT_CONFIG_DIR)
  24. os.makedirs(gitconfigdir, exist_ok=True)
  25. # We are using the git repository from the Buildroot git tree
  26. # (gitremotedir). This repository is safe to use using "file://"
  27. # protocol with git submodules.
  28. cmd = ["git", "config", "--file", localgitconfig,
  29. "--add", "protocol.file.allow", "always"]
  30. run_git_config(logfile, cmd)
  31. # Disable ownership check of the git tree for Gitlab-CI
  32. # environment.
  33. # See: https://gitlab.com/buildroot.org/buildroot/-/commit/a016b693f7830f3c8ae815851d3204b8b6e99821
  34. for git_repo in os.scandir(gitremotedir):
  35. cmd = ["git", "config", "--file", localgitconfig,
  36. "--add", "safe.directory", git_repo.path]
  37. run_git_config(logfile, cmd)