安装
pip3 install gitpython
基本使用
# 从远处仓库下载代码到本地 import os from git.repo import Repo # 创建本地存储地址 download_path = os.path.join('jason','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('jason', 'NB') repo = Repo(local_path) repo.git.pull() # ############## 3. 获取所有分支 ############## import os from git.repo import Repo local_path = os.path.join('jason', '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('jason', '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('jason', '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('jason', '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. 打包代码 ############## with open(os.path.join('jason', 'NB.tar'), 'wb') as fp: repo.archive(fp)
封装成类
需要使用直接拷贝即可
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("\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,remote_path) branch_list = repo.branches() print(branch_list) repo.change_to_branch('dev') repo.pull()
项目代码
服务器管理
class Project(models.Model): """ 项目表 """ title = models.CharField(max_length=32,verbose_name='项目名') repo = models.CharField(max_length=255,verbose_name='仓库地址') # choices参数 env_choices = ( ('prod','正式'), ('test','测试') ) env = models.CharField(max_length=16,verbose_name='环境',choices=env_choices,default='test')
代码优化
1.公用添加页面
2.将所有的modlform单独开设文件夹存储
3.对modelform再次优化 对modelform定义一个父类
from django.forms import ModelForm class BaseModelForm(ModelForm): # 将不需要bootstrap样式的字段放入 exclude_bootstrap = [] def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) # 给字段加上form-control样式 # self.fields = {'字段名':字段对象} for k,field in self.fields.items(): if k in self.exclude_bootstrap: # 排除不需要加样式的字段 continue field.widget.attrs['class'] = 'form-control' # 其他modelform书写 from app01 import models from app01.myform.mybase import BaseModelForm class ProjectModelForm(BaseModelForm): class Meta: model = models.Project fields = '__all__'
4.给项目表新增线上项目地址和服务器字段
# 扩展字段
path = models.CharField(max_length=255,verbose_name='线上项目地址',default='/data/tmp')
# 项目与服务器的关系表
servers = models.ManyToManyField(to='Server',verbose_name='关联服务器')
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
python,操作git
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“基于python实现操作git过程代码解析”评论...
更新日志
2024年12月25日
2024年12月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]