Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not change Git user custom config #108

Merged
merged 4 commits into from
Apr 6, 2022

Conversation

josecelano
Copy link
Member

We were changing the Git committer configuration. We should not do that and rely on preset configuration. We only should allow the user to overwrite committer and author emails and names by passing those arguments to the git commit command (via env vars or commits options).

@github-actions
Copy link

github-actions bot commented Mar 29, 2022

MegaLinter status: ⚠️ WARNING

Descriptor Linter Files Fixed Errors Elapsed time
✅ ACTION actionlint 11 0 0.2s
✅ BASH bash-exec 4 0 0.02s
✅ BASH shellcheck 4 0 0.01s
✅ BASH shfmt 4 0 0.01s
✅ COPYPASTE jscpd yes no 2.26s
✅ CREDENTIALS secretlint yes no 2.76s
✅ DOCKERFILE dockerfilelint 1 0 0.23s
✅ DOCKERFILE hadolint 1 0 0.07s
✅ GIT git_diff yes no 0.07s
✅ JSON eslint-plugin-jsonc 8 0 0.84s
✅ JSON jsonlint 8 0 1.9s
✅ JSON prettier 8 0 0.53s
✅ JSON v8r 8 0 5.22s
⚠️ MARKDOWN markdownlint 15 1 0.38s
✅ MARKDOWN markdown-link-check 15 0 17.21s
✅ MARKDOWN markdown-table-formatter 15 0 0.23s
✅ PYTHON bandit 109 0 1.11s
✅ PYTHON black 109 0 1.74s
✅ PYTHON flake8 109 0 1.18s
✅ PYTHON isort 109 0 0.23s
✅ PYTHON mypy 109 0 40.07s
✅ PYTHON pylint 109 0 12.09s
✅ SPELL cspell 159 0 4.96s
✅ SPELL misspell 159 0 0.07s
✅ YAML prettier 16 0 0.77s
✅ YAML v8r 16 0 10.05s
✅ YAML yamllint 16 0 0.26s

See errors details in artifact MegaLinter reports on CI Job page

@josecelano josecelano linked an issue Mar 29, 2022 that may be closed by this pull request
@josecelano josecelano added the bug Something isn't working label Mar 29, 2022
@josecelano
Copy link
Member Author

@yeraydavidrodriguez @da2ce7 the change is already implemented but I would like to refactor the GitRepo class:

from git import Repo


class GitRepo:
    """A wrapper for GitPython Repo"""

    def __init__(self, git_repo_dir, git_user, gnupghome="~/.gnupg"):
        self.repo = Repo(git_repo_dir)
        self.git_user = git_user
        self.gnupghome = gnupghome

    def commit(self, filepaths, commit_message: str, env: dict[str, str] = {}):
        """
        It creates a commit.
        Filepath is an object with four optional file lists:
        {
            "added": [],
            "deleted": [],
            "modified": [],
            "renamed": [],
        }
        """
        if "added" in filepaths:
            self.repo.index.add(filepaths["added"])

        if "deleted" in filepaths:
            self.repo.index.remove(filepaths["deleted"])

        if "renamed" in filepaths:
            self.repo.index.add(filepaths["renamed"]["new"])
            self.repo.index.remove(filepaths["renamed"]["old"])

        # Write index. Needed for commit with signature:
        # https://github.com/gitpython-developers/GitPython/issues/580#issuecomment-282474086
        self.repo.index.write()

        # Unsigned commit
        if self.git_user.signingkey is None:
            with self.repo.git.custom_environment(**env):
                return self.repo.git.commit("-m", f"{commit_message}")

        # Signed commit
        with self.repo.git.custom_environment(GNUPGHOME=self.gnupghome, **env):
            return self.repo.git.commit(
                "-S",
                f"--gpg-sign={self.git_user.signingkey}",
                "-m",
                f"{commit_message}",
            )

@yeraydavidrodriguez and I agreed some weeks ago that we should not couple this mod to the specific data structure used by the dvc diff command.

TODO:

  • Add method add
  • Add method delete
  • Add method rename

The commit method should only write the index and commit.

TODO:

  • Remove gnupghome from constructor. It should be one env variable in env commit argument.
  • Remove git_user from constructor. You can overwrite the Git committer info (email and name) with an env var as it's done by Git. It only makes sense if we always want to use the same committer info, but in that case, we should change the class name. In fact we should even separate the committer, author and signing key info because they can be set independently. We also have another issue to make signed commits optional.

@josecelano josecelano mentioned this pull request Apr 6, 2022
5 tasks
@josecelano
Copy link
Member Author

@da2ce7 @yeraydavidrodriguez I changed my mind. I tend to make PR too big. I've created a new issue for the refactoring.

I'm going to merge this fix and release a new minor version and test that the new version works fine with our Library and Website.

@josecelano josecelano marked this pull request as ready for review April 6, 2022 12:01
@josecelano josecelano merged commit 91ab341 into main Apr 6, 2022
@josecelano josecelano deleted the issue-68-do-not-overwrite-git-committer-config branch April 6, 2022 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use git local configuration instead of global when available
1 participant