importosfrom git.repo importRepofrom git.repo.fun importis_git_dirclassGitRepository(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)definitial(self, repo_url, branch):"""初始化git仓库

:param repo_url:

:param branch:

:return:"""

if notos.path.exists(self.local_path):

os.makedirs(self.local_path)

git_local_path= os.path.join(self.local_path, ‘.git‘)if notis_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)defpull(self):"""从线上拉最新代码

:return:"""self.repo.git.pull()defbranches(self):"""获取所有分支

:return:"""branches=self.repo.remote().refsreturn [item.remote_head for item in branches if item.remote_head not in [‘HEAD‘, ]]defcommits(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("\n")return [eval(item) for item inlog_list]deftags(self):"""获取所有tag

:return:"""

return [tag.name for tag inself.repo.tags]defchange_to_branch(self, branch):"""切换分值

:param branch:

:return:"""self.repo.git.checkout(branch)defchange_to_commit(self, branch, commit):"""切换commit

:param branch:

:param commit:

:return:"""self.change_to_branch(branch=branch)

self.repo.git.reset(‘--hard‘, commit)defchange_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, ‘https://gitee.com/wupeiqi/fuck.git‘)

branch_list=repo.branches()print(branch_list)

repo.change_to_branch(‘dev‘)

repo.pull()

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐