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系列:

网络安全系列:

嵌入式开发系列:

AI / 机器学习系列:


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/HowieXue/article/details/120653826
今日推荐