Wrapping git operations in Python

Personal notes. To see how Git works in Python, use the ** subprocess ** module ** Popen **.


class GitError(BaseException): pass


def git(cmd, *args):
     command = ("git", cmd) + args
     proc = Popen(command, stdin=-1,stdout=-1,stderr=-1)
     out, err = proc.communicate()

    if len(err) == 0:
        return out.strip()
    else:
        raise GitError(err.strip())

Let's create a class called ** GitUtility ** that applies this.

from subprocess import Popen

class GitError(BaseException):
    """
    This Error raises when git command runner has error.
    """

    pass

class GitUtility(object):
    """
    The GitUtility contains utility functions for operate git.
    """

    def __init__(self):

        return

    def run(self, cmd, *args):
        """
        This command is git command runner by subprocess.
        Return output or raise GitError if runner has error.
        """

        if isinstance(args[0], tuple):
            # Avoid duplicate tuple
            # ex. self.rev_parse("--show-toplevel")
            #   ->('git', 'rev-parse', ('--show-toplevel',))
            command = ("git", cmd) + tuple([arg for arg in args[0]])
        else:
            command = ("git", cmd) + args

        proc = Popen(command, stdin=-1,stdout=-1,stderr=-1)

        out, err = proc.communicate()

        if len(err) == 0:
            return out.strip()

        else:
            raise GitError(err.strip())

    def add(self, path):
        """
        Run git add command
        """

        self.run("add", path)

    def commit(self, commit_msg):
        """
        Run git commit
        """

        return self.run("commit", "-m", commit_msg)

    def rev_parse(self, *args):
        """
        Run git rev-parse command
        """
        return self.run("rev-parse", args)

    def get_toplevel(self):
        """
        Return git top level path using git rev-parse
        """

        return self.rev_parse("--show-toplevel")

For example, you can want to commit automatically, or you can get the repository path using rev-parse. If you apply it more, you can parse the log or get the contents of the file at the time of a certain commit.

Recommended Posts

Wrapping git operations in Python
File operations in Python
File operations in Python
Four arithmetic operations in python
Perform Scala-like collection operations in Python
Parsing Git commit logs in Python
ORC, Parquet file operations in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Python garbled in Windows + Git Bash environment
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
[Python] Understand list slicing operations in seconds
flatten in python
Let's parse the git commit log in Python!
Summary of Excel operations using OpenPyXL in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python