def git_prepare(lab: linux.Lab) -> str: """Prepare a test git repo.""" global _GIT if _GIT is None: # Git committer and author information in case the user's git # environment is not set up yet lab.env("GIT_AUTHOR_NAME", "tbot selftest") lab.env("GIT_AUTHOR_EMAIL", "*****@*****.**") lab.env("GIT_COMMITTER_NAME", "tbot selftest") lab.env("GIT_COMMITTER_EMAIL", "*****@*****.**") p = lab.workdir / "selftest-git-remote" if p.exists(): lab.exec0("rm", "-rf", p) tbot.log.message("Setting up test repo ...") lab.exec0("mkdir", "-p", p) lab.exec0("git", "-C", p, "init") repo = git.GitRepository(p, clean=False) lab.exec0( "echo", """\ # tbot Selftest This repo exists to test tbot's git testcase. You can safely remove it, but please **do not** modify it as that might break the tests.""", linux.RedirStdout(repo / "README.md"), ) repo.add(repo / "README.md") repo.commit("Initial", author="tbot Selftest <none@none>") tbot.log.message("Creating test patch ...") lab.exec0( "echo", """\ # File 2 A second file that will have been added by patching.""", linux.RedirStdout(repo / "file2.md"), ) repo.add(repo / "file2.md") repo.commit("Add file2", author="tbot Selftest <none@none>") patch_name = repo.git0("format-patch", "HEAD~1").strip() lab.exec0("mv", repo / patch_name, lab.workdir / "selftest-git.patch") tbot.log.message("Resetting repo ...") repo.reset("HEAD~1", git.ResetMode.HARD) _GIT = p._local_str() return _GIT else: return _GIT
def __init__(self, labhost: linux.Lab, port: int) -> None: """Create a new MiniSSHMachine.""" self._port = port self._username = labhost.env("USER") super().__init__(labhost)