python模块之Git模块使用

#!/usr/bin/python
# coding:utf8
# @Author: liaojiaf@chanjet.com
# @Time: 3/17/20 4:04 PM
# @Site: 
# @File: git_handle.py
# @IDE: PyCharm

from git import Git,Repo
import time


class git_handler(object):
    def __init__(self,dest_dir):
        self.repo = Repo(dest_dir)

    def git_add(self,add_file="*"):
        try:
            index = self.repo.index
            if not isinstance(add_file,list):
                add_file = [add_file,]
            if index.add(add_file):
                return True
        except:
            return False


    def git_remove(self,del_file):
        index  = self.repo.index
        if not isinstance(del_file,list):
            del_file = [del_file,]
        index.remove(del_file)


    def git_commit(self,user, information):
        #time.sleep(2)
        try:
            index = self.repo.index
            if index.commit('"{}", "{}"'.format(user, information)):
                return True
        except:
            return False


    def git_push(self,):
        # time.sleep(2)
        try:
            if self.repo.remote().push():
                return True
        except:
            return False


    def git_pull(self,dest_dir):
        try:
            remote = self.repo.remote()
            if remote.pull():
                return True
            else:
                return False
        except:
            return False


    def git_clean(self,):
        try:
            self.repo.git.add('.')
            self.repo.git.stash()
            self.repo.git.stash('drop')
            return True
        except:
            return False
posted @ 2020-03-22 18:10  温柔易淡  阅读(1092)  评论(0编辑  收藏  举报