欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于python實(shí)現(xiàn)操作git過程代碼解析

 更新時(shí)間:2020年07月27日 11:46:28   作者:小青年て  
這篇文章主要介紹了基于python實(shí)現(xiàn)操作git過程代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

安裝

pip3 install gitpython

基本使用

# 從遠(yuǎn)處倉庫下載代碼到本地
import os
from git.repo import Repo

# 創(chuàng)建本地存儲(chǔ)地址
download_path = os.path.join('jason','NB')
# 從遠(yuǎn)程倉庫下載代碼
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)
 
# 將所有提交記錄結(jié)果格式成json格式字符串 方便后續(xù)反序列化操作
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()

項(xiàng)目代碼

服務(wù)器管理

class Project(models.Model):
  """
  項(xiàng)目表
  """
  title = models.CharField(max_length=32,verbose_name='項(xiàng)目名')
  repo = models.CharField(max_length=255,verbose_name='倉庫地址')
  # choices參數(shù)
  env_choices = (
    ('prod','正式'),
    ('test','測試')
  )
  env = models.CharField(max_length=16,verbose_name='環(huán)境',choices=env_choices,default='test')

代碼優(yōu)化

1.公用添加頁面

2.將所有的modlform單獨(dú)開設(shè)文件夾存儲(chǔ)

3.對(duì)modelform再次優(yōu)化 對(duì)modelform定義一個(gè)父類

from django.forms import ModelForm
class BaseModelForm(ModelForm):
  # 將不需要bootstrap樣式的字段放入
  exclude_bootstrap = []
  def __init__(self,*args,**kwargs):
    super().__init__(*args,**kwargs)
    # 給字段加上form-control樣式
    # self.fields = {'字段名':字段對(duì)象}
    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.給項(xiàng)目表新增線上項(xiàng)目地址和服務(wù)器字段

# 擴(kuò)展字段
path = models.CharField(max_length=255,verbose_name='線上項(xiàng)目地址',default='/data/tmp')
# 項(xiàng)目與服務(wù)器的關(guān)系表
servers = models.ManyToManyField(to='Server',verbose_name='關(guān)聯(lián)服務(wù)器')

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何將 awk 腳本移植到 Python

    如何將 awk 腳本移植到 Python

    腳本是解決問題的有效方法,而 awk 是編寫腳本的出色語言。它特別擅長于簡單的文本處理,它可以帶你完成配置文件的某些復(fù)雜重寫或目錄中文件名的重新格式化。這篇文章主要介紹了如何把 awk 腳本移植到 Python,需要的朋友可以參考下
    2019-12-12
  • python GUI庫圖形界面開發(fā)之PyQt5信號(hào)與槽事件處理機(jī)制詳細(xì)介紹與實(shí)例解析

    python GUI庫圖形界面開發(fā)之PyQt5信號(hào)與槽事件處理機(jī)制詳細(xì)介紹與實(shí)例解析

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5信號(hào)與槽事件處理機(jī)制詳細(xì)介紹與實(shí)例解析,需要的朋友可以參考下
    2020-03-03
  • python如何使用騰訊云發(fā)送短信

    python如何使用騰訊云發(fā)送短信

    這篇文章主要介紹了python如何使用騰訊云發(fā)送短信,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09
  • Python3+Pygame實(shí)現(xiàn)射擊游戲完整代碼

    Python3+Pygame實(shí)現(xiàn)射擊游戲完整代碼

    這篇文章主要介紹了Python3+Pygame實(shí)現(xiàn)射擊游戲完整代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 用Python進(jìn)行數(shù)據(jù)清洗以及值處理

    用Python進(jìn)行數(shù)據(jù)清洗以及值處理

    這篇文章主要介紹了用Python進(jìn)行數(shù)據(jù)清洗以及值處理,數(shù)據(jù)分析中,數(shù)據(jù)清洗是一個(gè)必備階段。數(shù)據(jù)分析所使用的數(shù)據(jù)一般都很龐大,致使數(shù)據(jù)不可避免的出現(xiàn)重復(fù)、缺失、異常值等異常數(shù)據(jù),如果忽視這些異常數(shù)據(jù),可能導(dǎo)致分析結(jié)果的準(zhǔn)確性,需要的朋友可以參考下
    2023-07-07
  • 基于windows下pip安裝python模塊時(shí)報(bào)錯(cuò)總結(jié)

    基于windows下pip安裝python模塊時(shí)報(bào)錯(cuò)總結(jié)

    今天小編就為大家分享一篇基于windows下pip安裝python模塊時(shí)報(bào)錯(cuò)總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Pytorch BertModel的使用說明

    Pytorch BertModel的使用說明

    這篇文章主要介紹了Pytorch BertModel的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • OpenCV圖像識(shí)別之姿態(tài)估計(jì)Pose?Estimation學(xué)習(xí)

    OpenCV圖像識(shí)別之姿態(tài)估計(jì)Pose?Estimation學(xué)習(xí)

    這篇文章主要為大家介紹了OpenCV圖像識(shí)別之姿態(tài)估計(jì)Pose?Estimation學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Python中用表格格式打印列表的兩種實(shí)現(xiàn)

    Python中用表格格式打印列表的兩種實(shí)現(xiàn)

    本文將詳細(xì)介紹如何在 Python 中以表格格式打印列表,以便更好地展示和呈現(xiàn)數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 基于Python獲取docx/doc文件內(nèi)容代碼解析

    基于Python獲取docx/doc文件內(nèi)容代碼解析

    這篇文章主要介紹了基于Python獲取docx/doc文件內(nèi)容代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論