gitpython: 实现自动Git Pull/Merge/Push 脚本
背景
通过gitpython lib,编写python脚本自动实现git pull/merge/push 操作,这里以一个常用git操作案例来介绍,主要操作如下
-> checkout & pull 更新 source branch
-> checkout & pull 更新 target branch
-> merge source branch到 target branch
-> push to target branch
安装gitpython
首先安装gitpython:
- pip install gitpython
关键代码:
branch_merge()
branch_merge就是先切换到from_branch,pull到最新,然后merge 到to_branch 中,push到to_branch。
代码上 通过gitpython的repo.git, 就可以执行git.checkout/pull/merge/push指令了 (下面有完整代码)
gitpython比较好用的一点是支持git cmd参数,如下 git.pull 可以加上 --no-rebase等参数,这样保持一个API就可以通过cmd来实现各类子功能
autoMerge()
首先assert 入参的路径是否存在,然后通过gitpython的Repo() 方法,获取repo的handle,通过repo.is_dirty确定local没有修改后,根据repo名称、branch名称,调用branch_merge(),
执行完后可以查看下remote的version(hash值),以确认merge、push成功
main中比较简单,只调用autoMerge传入 workpatch、repo name、branch name可以
如果要确保两个branch remote始终一致,可以通过threading.Timer定时执行脚本,可参考:https://howiexue.blog.csdn.net/article/details/120364354
完整代码:
AutoGitMerge.py
import git
import os
from git import Repo
# File: RunGitAutoUpdate_forBlog.py
# Author: HowardXue https://howiexue.blog.csdn.net/
WORK_PATH = r'C:\Work\xxxx\\'
# Merge branch name, FROM_BRANCH -> TO_BRANCH
FROM_BRANCH = 'xxxx'
TO_BRANCH = 'xxxx'
# Repo name
REPO_NAME = "xxxx"
def (workpath, repo_name, fromBranch, toBranch):
if(os.path.isdir(workpath) == False):
print("Error: Folder not exist, tool exit: " + workpath)
exit()
# get repo path
repo_path = workpath + repo_name
print('repopath:' + repo_path)
repo = Repo(repo_path)
print(repo)
# check if local has dirty data
if repo.is_dirty():
print('{0}: There is dirty data in your local git, please commit or clean it'.format(repo_name))
exit()
print('>>> start merge ' + repo_name + ' branch ' + fromBranch + ' -> ' + toBranch)
# do branch merge
branch_merge(repo, FROM_BRANCH, TO_BRANCH)
print('end merge <<< ' + repo_name + '\n')
git = repo.git
remoteVer = git.rev_parse("@{u}")
print("Remote Current Rev Parse:" + remoteVer)
def branch_merge(repo, from_branch, to_branch):
git = repo.git
# Check out origin branch
print('git checkout origin ' + from_branch)
git.checkout(from_branch)
git.pull('--progress', '--no-rebase', 'origin', from_branch)
# switch to target branch, pull latest commit
print('git checkout target ' + to_branch)
git.checkout(to_branch)
git.pull('--progress', '--no-rebase', 'origin', to_branch)
# merge branch
print('git merge ' + from_branch)
git.merge(from_branch)
# push merged branch to remote
print('git push ' + to_branch)
git.push('--progress', 'origin', to_branch)
def main():
print('Auto Git Merge Tool... \n')
autoMerge(WORK_PATH, REPO_NAME, FROM_BRANCH, TO_BRANCH)
if __name__ == '__main__':
main()
博主热门文章推荐:
一篇读懂系列:
LoRa Mesh系列:
网络安全系列:
- ATECC508A芯片开发笔记(一):初识加密芯片
- SHA/HMAC/AES-CBC/CTR 算法执行效率及RAM消耗 测试结果
- 常见加密/签名/哈希算法性能比较 (多平台 AES/DES, DH, ECDSA, RSA等)
- AES加解密效率测试(纯软件AES128/256)–以嵌入式Cortex-M0与M3 平台为例
嵌入式开发系列:
- 嵌入式学习中较好的练手项目和课题整理(附代码资料、学习视频和嵌入式学习规划)
- IAR调试使用技巧汇总:数据断点、CallStack、设置堆栈、查看栈使用和栈深度、Memory、Set Next Statement等
- Linux内核编译配置(Menuconfig)、制作文件系统 详细步骤
- Android底层调用C代码(JNI实现)
- 树莓派到手第一步:上电启动、安装中文字体、虚拟键盘、开启SSH等
- Android/Linux设备有线&无线 双网共存(同时上内、外网)
AI / 机器学习系列:
- AI: 机器学习必须懂的几个术语:Lable、Feature、Model…
- AI:卷积神经网络CNN 解决过拟合的方法 (Overcome Overfitting)
- AI: 什么是机器学习的数据清洗(Data Cleaning)
- AI: 机器学习的模型是如何训练的?(在试错中学习)
- 数据可视化:TensorboardX安装及使用(安装测试+实例演示)