Skip to content

Use the Git class type definition within Repo classmethods #1322

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

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use the Git class type definition within Repo classmethods
Allow the GitCommandWrapperType definition to be used within the Repo
classmethods. This change follows the intended purpose as stated in
the code, "Subclasses may easily bring in their own custom types by
placing a constructor or type here."

The usecase that prompted this change has to do with
`GIT_SSH_COMMAND`. The goal is to setup a custom `Git` class with
knowledge of the value, something like as follows

```python
from git import Git as BaseGit, Repo as BaseRepo

class Git(BaseGit):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # For example, assign the SSH command using the current flask
        # app's configured setting.
        self.update_environment(GIT_SSH_COMMAND=current_app.config['GIT_SSH_COMMAND'])

class Repo(BaseRepo):
    GitCommandWrapperType = _Git
```

With this change, the above example will allow the developer to use
`Repo.clone_from(...)` with the indended outcome. Otherwise the
developer will have two differing result when using `Repo(...)` vs
`Repo.clone_from(...)`.
  • Loading branch information
mmulich committed Aug 17, 2021
commit fe5f8d5eb6965cc4bb44cf048fb8c7e83b392e6d
4 changes: 2 additions & 2 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ def init(cls, path: Union[PathLike, None] = None, mkdir: bool = True, odbt: Type
os.makedirs(path, 0o755)

# git command automatically chdir into the directory
git = Git(path)
git = cls.GitCommandWrapperType(path)
git.init(**kwargs)
return cls(path, odbt=odbt)

Expand Down Expand Up @@ -1142,7 +1142,7 @@ def clone_from(cls, url: PathLike, to_path: PathLike, progress: Optional[Callabl
:param multi_options: See ``clone`` method
:param kwargs: see the ``clone`` method
:return: Repo instance pointing to the cloned directory"""
git = Git(os.getcwd())
git = cls.GitCommandWrapperType(os.getcwd())
if env is not None:
git.update_environment(**env)
return cls._clone(git, url, to_path, GitCmdObjectDB, progress, multi_options, **kwargs)
Expand Down