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

Refactor: GitRepo class #111

Open
5 tasks
josecelano opened this issue Apr 6, 2022 · 0 comments
Open
5 tasks

Refactor: GitRepo class #111

josecelano opened this issue Apr 6, 2022 · 0 comments

Comments

@josecelano
Copy link
Member

@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.

Originally posted by @josecelano in #108 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant