Skip to content

Interacting with Git

Krzysztof Czarnecki edited this page Sep 26, 2021 · 3 revisions

Interacting with Git

Author

Some aspects of git interactions are built into mkcommit. Take for example the following config:

from mkcommit import CommitMessage, to_stdout, Author
from mkcommit.suites import semantic

author = Author.from_git()

def commit():
    return CommitMessage(
        f"{author.name} | {semantic.default_short()}"
    )

if __name__ == "__main__":
    to_stdout(commit())

Author has a from_git() method that can be used to automatically create author information based on git config.

from_git pattern

You can implement your own classes interfacing with git config easily by importing and using git_command from your own custom class. Take the Author definition for example:

from __future__ import annotations
from dataclasses import dataclass
from mkcommit.model import git_command

@dataclass
class Author:
    name: str
    email: str

    @classmethod
    def from_git(cls) -> Author:
        name = git_command("git", "config", "--get", "user.name")
        email = git_command("git", "config", "--get", "user.email")
        return cls(name, email)
Clone this wiki locally