fix: support Git config without user identity

This commit is contained in:
Sigurd Spieckermann 2024-07-30 12:11:43 +02:00 committed by Sigurd Spieckermann
parent 78c4843798
commit 0258635da7
23 changed files with 55 additions and 52 deletions

View File

@ -1104,8 +1104,6 @@ class Worker:
def _git_commit(self, message: str = "dumb commit") -> None: def _git_commit(self, message: str = "dumb commit") -> None:
git = get_git() git = get_git()
git("config", "user.name", "Copier")
git("config", "user.email", "copier@copier")
# 1st commit could fail if any pre-commit hook reformats code # 1st commit could fail if any pre-commit hook reformats code
# 2nd commit uses --no-verify to disable pre-commit-like checks # 2nd commit uses --no-verify to disable pre-commit-like checks
git( git(
@ -1124,8 +1122,6 @@ class Worker:
"--no-gpg-sign", "--no-gpg-sign",
"--no-verify", "--no-verify",
) )
git("config", "--unset", "user.name")
git("config", "--unset", "user.email")
def run_copy( def run_copy(

View File

@ -17,10 +17,18 @@ from plumbum.machines import LocalCommand
from .errors import DirtyLocalWarning, ShallowCloneWarning from .errors import DirtyLocalWarning, ShallowCloneWarning
from .types import OptBool, OptStrOrPath, StrOrPath from .types import OptBool, OptStrOrPath, StrOrPath
GIT_USER_NAME = "Copier"
GIT_USER_EMAIL = "copier@copier"
def get_git(context_dir: OptStrOrPath = None) -> LocalCommand: def get_git(context_dir: OptStrOrPath = None) -> LocalCommand:
"""Gets `git` command, or fails if it's not available.""" """Gets `git` command, or fails if it's not available."""
command = local["git"] command = local["git"].with_env(
GIT_AUTHOR_NAME=GIT_USER_NAME,
GIT_AUTHOR_EMAIL=GIT_USER_EMAIL,
GIT_COMMITTER_NAME=GIT_USER_NAME,
GIT_COMMITTER_EMAIL=GIT_USER_EMAIL,
)
if context_dir: if context_dir:
command = command["-C", context_dir] command = command["-C", context_dir]
return command return command

View File

@ -2,13 +2,13 @@ from __future__ import annotations
import platform import platform
import sys import sys
from typing import Iterator from typing import Any, Iterator
import pytest import pytest
from coverage.tracer import CTracer from coverage.tracer import CTracer
from pexpect.popen_spawn import PopenSpawn from pexpect.popen_spawn import PopenSpawn
from plumbum import local from plumbum import local
from pytest_gitconfig.plugin import GitConfig from pytest_gitconfig.plugin import DELETE, GitConfig
from .helpers import Spawn from .helpers import Spawn
@ -38,6 +38,18 @@ def spawn() -> Spawn:
return _spawn return _spawn
@pytest.fixture(scope="session")
def default_git_user_name() -> Any:
"""Unset the default Git user name."""
return DELETE
@pytest.fixture(scope="session")
def default_git_user_email() -> Any:
"""Unset the default Git user email."""
return DELETE
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
def default_gitconfig(default_gitconfig: GitConfig) -> GitConfig: def default_gitconfig(default_gitconfig: GitConfig) -> GitConfig:
""" """

View File

@ -12,9 +12,11 @@ from typing import Any, Mapping, Protocol
from pexpect.popen_spawn import PopenSpawn from pexpect.popen_spawn import PopenSpawn
from plumbum import local from plumbum import local
from plumbum.cmd import git from plumbum.cmd import git as _git
from plumbum.machines import LocalCommand
from prompt_toolkit.input.ansi_escape_sequences import REVERSE_ANSI_SEQUENCES from prompt_toolkit.input.ansi_escape_sequences import REVERSE_ANSI_SEQUENCES
from prompt_toolkit.keys import Keys from prompt_toolkit.keys import Keys
from pytest_gitconfig.plugin import DEFAULT_GIT_USER_EMAIL, DEFAULT_GIT_USER_NAME
import copier import copier
from copier.types import StrOrPath from copier.types import StrOrPath
@ -139,6 +141,14 @@ def expect_prompt(
tui.expect_exact(f"({expected_type})") tui.expect_exact(f"({expected_type})")
git: LocalCommand = _git.with_env(
GIT_AUTHOR_NAME=DEFAULT_GIT_USER_NAME,
GIT_AUTHOR_EMAIL=DEFAULT_GIT_USER_EMAIL,
GIT_COMMITTER_NAME=DEFAULT_GIT_USER_NAME,
GIT_COMMITTER_EMAIL=DEFAULT_GIT_USER_EMAIL,
)
def git_save( def git_save(
dst: StrOrPath = ".", dst: StrOrPath = ".",
message: str = "Test commit", message: str = "Test commit",
@ -168,7 +178,5 @@ def git_init(message: str = "hello world") -> None:
message: The first commit message. message: The first commit message.
""" """
git("init") git("init")
git("config", "user.name", "Copier Test")
git("config", "user.email", "test@copier")
git("add", ".") git("add", ".")
git("commit", "-m", message) git("commit", "-m", message)

View File

@ -6,11 +6,10 @@ from typing import Callable, Generator
import pytest import pytest
import yaml import yaml
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier.cli import CopierApp from copier.cli import CopierApp
from .helpers import COPIER_CMD, build_file_tree from .helpers import COPIER_CMD, build_file_tree, git
@pytest.fixture(scope="module") @pytest.fixture(scope="module")

View File

@ -10,7 +10,6 @@ import pytest
import yaml import yaml
from pexpect.popen_spawn import PopenSpawn from pexpect.popen_spawn import PopenSpawn
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier import run_copy, run_update from copier import run_copy, run_update
@ -23,6 +22,7 @@ from .helpers import (
Spawn, Spawn,
build_file_tree, build_file_tree,
expect_prompt, expect_prompt,
git,
) )
BLK_START = BRACKET_ENVOPS["block_start_string"] BLK_START = BRACKET_ENVOPS["block_start_string"]

View File

@ -1,12 +1,11 @@
import pexpect import pexpect
import pytest import pytest
from plumbum import local from plumbum import local
from plumbum.cmd import git
from pytest import TempPathFactory from pytest import TempPathFactory
import copier import copier
from .helpers import COPIER_PATH, Spawn, build_file_tree, expect_prompt from .helpers import COPIER_PATH, Spawn, build_file_tree, expect_prompt, git
def test_render_conditional(tmp_path_factory: TempPathFactory) -> None: def test_render_conditional(tmp_path_factory: TempPathFactory) -> None:

View File

@ -15,7 +15,6 @@ from typing import Any, ContextManager
import pytest import pytest
import yaml import yaml
from plumbum import local from plumbum import local
from plumbum.cmd import git
import copier import copier
from copier import run_copy from copier import run_copy
@ -27,6 +26,7 @@ from .helpers import (
PROJECT_TEMPLATE, PROJECT_TEMPLATE,
assert_file, assert_file,
build_file_tree, build_file_tree,
git,
git_save, git_save,
render, render,
) )

View File

@ -1,10 +1,9 @@
import pytest import pytest
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier import run_copy, run_update from copier import run_copy, run_update
from .helpers import build_file_tree from .helpers import build_file_tree, git
def test_update_tasks(tmp_path_factory: pytest.TempPathFactory) -> None: def test_update_tasks(tmp_path_factory: pytest.TempPathFactory) -> None:
@ -58,9 +57,6 @@ def test_update_tasks(tmp_path_factory: pytest.TempPathFactory) -> None:
# Init destination as a new independent git repo # Init destination as a new independent git repo
with local.cwd(dst): with local.cwd(dst):
git("init") git("init")
# Configure git in case you're running in CI
git("config", "user.name", "Copier Test")
git("config", "user.email", "test@copier")
# Commit changes # Commit changes
git("add", ".") git("add", ".")
git("commit", "-m", "hello world") git("commit", "-m", "hello world")

View File

@ -3,14 +3,13 @@ from shutil import copy2, copytree
import pytest import pytest
from plumbum import local from plumbum import local
from plumbum.cmd import git
from pytest_gitconfig.plugin import GitConfig from pytest_gitconfig.plugin import GitConfig
import copier import copier
from copier.errors import DirtyLocalWarning from copier.errors import DirtyLocalWarning
from copier.main import run_copy, run_update from copier.main import run_copy, run_update
from .helpers import DATA, PROJECT_TEMPLATE, build_file_tree from .helpers import DATA, PROJECT_TEMPLATE, build_file_tree, git
def test_copy(tmp_path_factory: pytest.TempPathFactory) -> None: def test_copy(tmp_path_factory: pytest.TempPathFactory) -> None:

View File

@ -6,12 +6,11 @@ from shutil import copytree
import pytest import pytest
import yaml import yaml
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier import run_copy, run_update from copier import run_copy, run_update
from copier.errors import UserMessageError from copier.errors import UserMessageError
from .helpers import BRACKET_ENVOPS_JSON, PROJECT_TEMPLATE, build_file_tree from .helpers import BRACKET_ENVOPS_JSON, PROJECT_TEMPLATE, build_file_tree, git
SRC = Path(f"{PROJECT_TEMPLATE}_legacy_migrations").absolute() SRC = Path(f"{PROJECT_TEMPLATE}_legacy_migrations").absolute()
@ -33,8 +32,6 @@ def test_migrations_and_tasks(tmp_path: Path, skip_tasks: bool) -> None:
copytree(SRC, src) copytree(SRC, src)
with local.cwd(src): with local.cwd(src):
git("init") git("init")
git("config", "user.name", "Copier Test")
git("config", "user.email", "test@copier")
git("add", ".") git("add", ".")
git("commit", "-m1") git("commit", "-m1")
git("tag", "v1.0.0") git("tag", "v1.0.0")
@ -66,8 +63,6 @@ def test_migrations_and_tasks(tmp_path: Path, skip_tasks: bool) -> None:
with local.cwd(dst): with local.cwd(dst):
git("init") git("init")
git("add", ".") git("add", ".")
git("config", "user.name", "Copier Test")
git("config", "user.email", "test@copier")
git("commit", "-m1") git("commit", "-m1")
# Update it to v2 # Update it to v2
run_update( run_update(

View File

@ -3,7 +3,6 @@ from pathlib import Path
import pytest import pytest
import yaml import yaml
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier import run_copy, run_update from copier import run_copy, run_update
from copier.errors import UnsafeTemplateError, UserMessageError from copier.errors import UnsafeTemplateError, UserMessageError
@ -12,6 +11,7 @@ from .helpers import (
COPIER_ANSWERS_FILE, COPIER_ANSWERS_FILE,
PROJECT_TEMPLATE, PROJECT_TEMPLATE,
build_file_tree, build_file_tree,
git,
git_save, git_save,
) )

View File

@ -4,7 +4,6 @@ from pathlib import Path
import pytest import pytest
from packaging.version import Version from packaging.version import Version
from plumbum import local from plumbum import local
from plumbum.cmd import git
import copier import copier
from copier.errors import ( from copier.errors import (
@ -13,7 +12,7 @@ from copier.errors import (
UnsupportedVersionError, UnsupportedVersionError,
) )
from .helpers import build_file_tree from .helpers import build_file_tree, git
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
@ -64,8 +63,6 @@ def test_minimum_version_update(
with local.cwd(tmp_path): with local.cwd(tmp_path):
git("init") git("init")
git("config", "user.name", "Copier Test")
git("config", "user.email", "test@copier")
git("add", ".") git("add", ".")
git("commit", "-m", "hello world") git("commit", "-m", "hello world")

View File

@ -4,12 +4,11 @@ from pathlib import Path
import pexpect import pexpect
import pytest import pytest
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier.errors import InvalidTypeError from copier.errors import InvalidTypeError
from copier.main import run_copy, run_recopy, run_update from copier.main import run_copy, run_recopy, run_update
from .helpers import COPIER_PATH, Spawn, build_file_tree, expect_prompt, render from .helpers import COPIER_PATH, Spawn, build_file_tree, expect_prompt, git, render
def test_output(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None: def test_output(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None:

View File

@ -9,7 +9,6 @@ import pytest
import yaml import yaml
from pexpect.popen_spawn import PopenSpawn from pexpect.popen_spawn import PopenSpawn
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier.types import StrOrPath from copier.types import StrOrPath
@ -22,6 +21,7 @@ from .helpers import (
Spawn, Spawn,
build_file_tree, build_file_tree,
expect_prompt, expect_prompt,
git,
git_save, git_save,
) )

View File

@ -5,11 +5,10 @@ from typing import Literal
import pytest import pytest
import yaml import yaml
from plumbum import local from plumbum import local
from plumbum.cmd import git
import copier import copier
from .helpers import BRACKET_ENVOPS_JSON, SUFFIX_TMPL, build_file_tree, git_init from .helpers import BRACKET_ENVOPS_JSON, SUFFIX_TMPL, build_file_tree, git, git_init
@pytest.fixture(scope="module") @pytest.fixture(scope="module")

View File

@ -3,13 +3,12 @@ from pathlib import Path
import pytest import pytest
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier import run_copy, run_update from copier import run_copy, run_update
from copier.errors import DirtyLocalWarning from copier.errors import DirtyLocalWarning
from copier.tools import readlink from copier.tools import readlink
from .helpers import build_file_tree from .helpers import build_file_tree, git
def test_copy_symlink(tmp_path_factory: pytest.TempPathFactory) -> None: def test_copy_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:

View File

@ -9,7 +9,6 @@ import pytest
import yaml import yaml
from pexpect.popen_spawn import PopenSpawn from pexpect.popen_spawn import PopenSpawn
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier import Worker from copier import Worker
from copier.errors import InvalidTypeError from copier.errors import InvalidTypeError
@ -24,6 +23,7 @@ from .helpers import (
Spawn, Spawn,
build_file_tree, build_file_tree,
expect_prompt, expect_prompt,
git,
git_init, git_init,
) )

View File

@ -1,12 +1,11 @@
from pathlib import Path from pathlib import Path
import pytest import pytest
from plumbum.cmd import git
from copier.cli import CopierApp from copier.cli import CopierApp
from copier.main import run_copy, run_recopy, run_update from copier.main import run_copy, run_recopy, run_update
from .helpers import build_file_tree from .helpers import build_file_tree, git
@pytest.fixture(scope="module") @pytest.fixture(scope="module")

View File

@ -3,11 +3,12 @@ from stat import S_IREAD
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
import pytest import pytest
from plumbum.cmd import git
from poethepoet.app import PoeThePoet from poethepoet.app import PoeThePoet
from copier.tools import normalize_git_path from copier.tools import normalize_git_path
from .helpers import git
def test_types() -> None: def test_types() -> None:
"""Ensure source code static typing.""" """Ensure source code static typing."""

View File

@ -7,14 +7,13 @@ import pytest
import yaml import yaml
from jinja2.ext import Extension from jinja2.ext import Extension
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier.cli import CopierApp from copier.cli import CopierApp
from copier.errors import UnsafeTemplateError from copier.errors import UnsafeTemplateError
from copier.main import run_copy, run_update from copier.main import run_copy, run_update
from copier.types import AnyByStrDict from copier.types import AnyByStrDict
from .helpers import build_file_tree from .helpers import build_file_tree, git
class JinjaExtension(Extension): class JinjaExtension(Extension):

View File

@ -10,7 +10,6 @@ import pexpect
import pytest import pytest
import yaml import yaml
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier.cli import CopierApp from copier.cli import CopierApp
from copier.errors import UserMessageError from copier.errors import UserMessageError
@ -24,6 +23,7 @@ from .helpers import (
SUFFIX_TMPL, SUFFIX_TMPL,
Spawn, Spawn,
build_file_tree, build_file_tree,
git,
git_init, git_init,
) )
@ -185,9 +185,6 @@ def test_updatediff(tmp_path_factory: pytest.TempPathFactory) -> None:
# Init destination as a new independent git repo # Init destination as a new independent git repo
with local.cwd(target): with local.cwd(target):
git("init") git("init")
# Configure git in case you're running in CI
git("config", "user.name", "Copier Test")
git("config", "user.email", "test@copier")
# Commit changes # Commit changes
git("add", ".") git("add", ".")
commit("-m", "hello world") commit("-m", "hello world")

View File

@ -7,12 +7,13 @@ import pytest
import yaml import yaml
from packaging.version import Version from packaging.version import Version
from plumbum import local from plumbum import local
from plumbum.cmd import git
from copier import Worker, run_copy, run_update from copier import Worker, run_copy, run_update
from copier.errors import ShallowCloneWarning from copier.errors import ShallowCloneWarning
from copier.vcs import checkout_latest_tag, clone, get_git_version, get_repo from copier.vcs import checkout_latest_tag, clone, get_git_version, get_repo
from .helpers import git
def test_get_repo() -> None: def test_get_repo() -> None:
get = get_repo get = get_repo