python 코드를 이용하여git 조작

5158 단어

python 조작git


설치 모듈
pip3 install gitpython

기본 사용
import os
from git.repo import Repo

#                    
download_path = os.path.join('NB')
#     
Repo.clone_from('https://github.com/DominicJi/TeachTest.git',to_path=download_path,branch='master')

기타 일반적인 작업
# ############## 2. pull     ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
repo.git.pull()


# ############## 3.        ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
branches = repo.remote().refs
for item in branches:
    print(item.remote_head)
    
# ############## 4.        ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
for tag in repo.tags:
    print(tag.name)

# ############## 5.     commit ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
#             json                
commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
                          date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("
") real_log_list = [eval(item) for item in log_list] print(real_log_list)
# ############## 6.      ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
before = repo.git.branch()
print(before)
repo.git.checkout('master')
after = repo.git.branch()
print(after)
repo.git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8')
 
# ############## 7.      ##############
import os
from git.repo import Repo

local_path = os.path.join(NB')
repo = Repo(local_path)

with open(os.path.join('NB.tar'), 'wb') as fp:
    repo.archive(fp)

후속 호출을 위해 상기 모든 방법을 클래스에 봉합니다. (후속으로git를 직접 복사해서 사용하려면 됩니다.)
import os
from git.repo import Repo
from git.repo.fun import is_git_dir


class GitRepository(object):
    """
    git    
    """
    def __init__(self, local_path, repo_url, branch='master'):
        self.local_path = local_path
        self.repo_url = repo_url
        self.repo = None
        self.initial(repo_url, branch)

    def initial(self, repo_url, branch):
        """
           git  
        :param repo_url:
        :param branch:
        :return:
        """
        if not os.path.exists(self.local_path):
            os.makedirs(self.local_path)

        git_local_path = os.path.join(self.local_path, '.git')
        if not is_git_dir(git_local_path):
            self.repo = Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)
        else:
            self.repo = Repo(self.local_path)

    def pull(self):
        """
                
        :return:
        """
        self.repo.git.pull()

    def branches(self):
        """
              
        :return:
        """
        branches = self.repo.remote().refs
        return [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]

    def commits(self):
        """
                
        :return:
        """
        commit_log = self.repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
                                       max_count=50,
                                       date='format:%Y-%m-%d %H:%M')
        log_list = commit_log.split("
") return [eval(item) for item in log_list] def tags(self): """ tag :return: """ return [tag.name for tag in self.repo.tags] def change_to_branch(self, branch): """ :param branch: :return: """ self.repo.git.checkout(branch) def change_to_commit(self, branch, commit): """ commit :param branch: :param commit: :return: """ self.change_to_branch(branch=branch) self.repo.git.reset('--hard', commit) def change_to_tag(self, tag): """ tag :param tag: :return: """ self.repo.git.checkout(tag) if __name__ == '__main__': local_path = os.path.join('codes', 'luffycity') repo = GitRepository(local_path,remote_path) branch_list = repo.branches() print(branch_list) repo.change_to_branch('dev') repo.pull()

좋은 웹페이지 즐겨찾기