"""
基于Python实现对git仓库进行操作,使用前需要安装模块:gitpython
pip3 install gitpython
"""
# ############## 1. clone下载代码 ##############
"""
import os
from git.repo import Repo
download_path = os.path.join('code', 'fuck')
Repo.clone_from('https://gitee.com/wupeiqi/fuck.git', to_path=download_path, branch='master')
"""
# ############## 2. pull最新代码 ##############
"""
import os
from git.repo import Repo
local_path = os.path.join('code', 'fuck')
repo = Repo(local_path)
repo.git.pull()
"""
# ############## 3. 获取所有分支 ##############
"""
import os
from git.repo import Repo
local_path = os.path.join('code', 'fuck')
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('code', 'fuck')
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('code', 'fuck')
repo = Repo(local_path)
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("\n")
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('code', 'fuck')
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. 打包代码 ##############
"""
with open(os.path.join('code', 'fuck.tar'), 'wb') as fp:
repo.archive(fp)
"""