1 Star 0 Fork 2

PersonalSummary/gitlab_sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
python_git.py 4.86 KB
一键复制 编辑 原始数据 按行查看 历史
wenfei6316 提交于 2023-02-22 18:42 . update gitlab ops
#!/usr/bin/python3
import os
from git import Repo
from git.repo.fun import is_git_dir
file = "."
ROOT_DIR = os.path.dirname(os.path.abspath(file))
repo = Repo(ROOT_DIR)
# 查看当前分支
print(ROOT_DIR, repo.active_branch)
# 切换分支
repo.git.checkout('master')
# 获取版本库暂存区
index = repo.index
#添加修改文件 – 注意这里today为你需要修改或新增的文件
index.add(['today'])
#提交修改到本地仓库
index.commit('update report folder')
#获取远程仓库
remote = repo.remote()
#拉取远程仓库数据
remote.pull()
#推送本地修改到远程仓库
remote.push()
# 创建本地路径用来存放远程仓库下载的代码
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("\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('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)
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("\n")
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, 'https://github.com/DominicJi/TeachTest.git', 'master')
branch_list = repo.branches()
print(branch_list)
repo.change_to_branch('dev')
repo.pull()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/personal-summary/gitlab_sync.git
[email protected]:personal-summary/gitlab_sync.git
personal-summary
gitlab_sync
gitlab_sync
master

搜索帮助