installation
pip3 install gitpython
Basic use
# From a distance download the code to a local warehouse Import os from git.repo Import Repo # Create a local store address download_path = os.path.join ( ' Jason ' , ' NB ' ) # from a remote repository to download the code Repo.clone_from ( ' HTTPS : //github.com/DominicJi/TeachTest.git ' , to_path = download_path, Branch = ' Master ' )
Common Daquan
# ############## 2. pull the latest code ############## Import os from git.repo Import Repo local_path = os.path.join ( ' Jason ' , ' NB ' ) the repo = Repo (local_path) repo.git.pull () # ############## ######### 3. Get all branches ##### Import OS from git.repo Import Repo local_path = the os.path.join ( ' Jason ' , ' NB ' ) the 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 fromgit.repo Import Repo local_path = the os.path.join ( ' Jason ' , ' NB ' ) the repo = Repo (local_path) # all results to the recording format into a format json convenient subsequent string deserialization commit_log = repo.git .log ( ' --pretty = { "the commit": "% H", "author": "% AN", "Summary": "% S", "DATE": "% CD"} ' , MAX_COUNT = 50 , DATE = ' the format: D%%% Y-M-% 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. The package of code ############## with Open (the os.path. the Join ( ' Jason ' , ' NB.tar ' ), ' WB ' ) AS FP: repo.archive (FP)
You need to use directly copy
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): " "" Get all branches : return: "" " branches = . Self.repo.remote () refs return [item.remote_head for Item in branches IF item.remote_head Not in [ ' the HEAD ' ,]] DEF commits (Self): "" " Get all records submitted : 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()
Item code
Server Management
class the Project (models.Model): "" " list of items " "" title = models.CharField (= 32 MAX_LENGTH, the verbose_name = ' item name ' ) the repo = models.CharField (MAX_LENGTH = 255, the verbose_name = ' address of the warehouse ' ) # choices parameters env_choices = ( ( ' Prod ' , ' formal ' ), ( ' the test ' , ' test ' ) ) env= models.CharField(max_length=16,verbose_name='环境',choices=env_choices,default='test')
Code optimization
1. Public Add Page
2. All modlform set up a separate folder stores
3. modelform optimize a parent class definitions for modelform again
from `` django.forms`` Import ModelForm class BaseModelForm (ModelForm): # unwanted bootstrap style field into exclude_bootstrap = [] DEF the __init__ (Self, args *, ** kwargs): Super (). the __init__ (* args, ** kwargs) # to the form-control field plus the style # self.fields = { 'field name': field object} for K, field in self.fields.items (): IF K in self.exclude_bootstrap: # exclude undesirable increase field pattern 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. The new line item list for the project server address field and
# Extension field path = models.CharField (MAX_LENGTH = 255, the verbose_name = ' line item address ' , default = ' / Data / tmp ' ) # relationship with the project server table Servers = models.ManyToManyField (to = ' Server ' , = the verbose_name ' associated server ' )