From 8375fc224709c597d950c59ded5bae38f935ab8d Mon Sep 17 00:00:00 2001 From: Olivier Mehani Date: Thu, 9 Jan 2025 17:27:19 +1100 Subject: [PATCH] GitSCM: set Git user on local clone --- src/lando/main/scm/git.py | 15 ++++++++++++++- src/lando/main/tests/test_git.py | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lando/main/scm/git.py b/src/lando/main/scm/git.py index d39e4aad..164ade6d 100644 --- a/src/lando/main/scm/git.py +++ b/src/lando/main/scm/git.py @@ -14,6 +14,7 @@ from lando.main.scm.consts import SCM_TYPE_GIT from lando.main.scm.exceptions import SCMException +from lando.settings import LANDO_USER_EMAIL, LANDO_USER_NAME from .abstract_scm import AbstractSCM @@ -69,6 +70,12 @@ def clone(self, source: str): """Clone a repository from a source.""" # When cloning, self.path doesn't exist yet, so we need to use another CWD. self._git_run("clone", source, self.path, cwd="/") + self._git_setup_user() + + def _git_setup_user(self): + """Configure the git user locally to repo_dir so as not to mess with the real user's configuration.""" + self._git_run("config", "user.name", LANDO_USER_NAME, cwd=self.path) + self._git_run("config", "user.email", LANDO_USER_EMAIL, cwd=self.path) def push( self, @@ -231,7 +238,13 @@ def format_stack_amend(self) -> Optional[list[str]]: def format_stack_tip(self, commit_message: str) -> Optional[list[str]]: """Add an autoformat commit to the top of the patch stack.""" - self._git_run("commit", "--all", "--message", commit_message, cwd=self.path) + try: + self._git_run("commit", "--all", "--message", commit_message, cwd=self.path) + except SCMException as exc: + if "nothing to commit, working tree clean" in exc.out: + return [] + else: + raise exc return [self.get_current_node()] def get_current_node(self) -> str: diff --git a/src/lando/main/tests/test_git.py b/src/lando/main/tests/test_git.py index 5625b8e3..a879714d 100644 --- a/src/lando/main/tests/test_git.py +++ b/src/lando/main/tests/test_git.py @@ -56,7 +56,7 @@ def test_GitSCM_clone(git_repo: Path, tmp_path: Path, monkeypatch): scm.clone(str(git_repo)) - mock_git_run.assert_called_with("clone", str(git_repo), str(clone_path), cwd="/") + mock_git_run.assert_any_call("clone", str(git_repo), str(clone_path), cwd="/") assert clone_path.exists(), f"New git clone {clone_path} wasn't created" assert ( clone_path / ".git"