Skip to content

Commit

Permalink
GitSCM: set Git user on local clone
Browse files Browse the repository at this point in the history
  • Loading branch information
shtrom committed Jan 10, 2025
1 parent 72f1cde commit 8375fc2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/lando/main/scm/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/lando/main/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8375fc2

Please sign in to comment.