一键git push脚本(python版)

#!/usr/bin/env python

import os
import subprocess
import sys
import time

gitconfig = {
    'cwd': './blog/public',
    'git': {
        'github': ['git@github.com:akkuman/akkuman.github.io.git', 'master'],
        'coding': ['git@git.coding.net:Akkuman/Akkuman.git', 'coding-pages'],
    }
}

def main():
    global gitconfig

    # change working directory
    os.chdir(gitconfig.get('cwd', '.'))

    # check if git init
    if '.git' not in os.listdir():
        subprocess.check_call(['git', 'init'])

    # check if remote in config, if not, add the remote
    git_remotes = subprocess.check_output(['git', 'remote', '-v'])
    git_remotes_str = bytes.decode(git_remotes).strip()
    git_remotes_list = [line.split()[0] for line in git_remotes_str.split('\n')]
    for k,v in gitconfig['git'].items():
        if k not in git_remotes_list:
            subprocess.check_call(['git', 'remote', 'add', k, v[0]])

    # add . & commit with message
    subprocess.check_call(['git', 'add', '.'])
    commit_message = 'Site updated: %s' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    if len(sys.argv) == 2:
        commit_message = sys.argv[1]
    subprocess.call(['git', 'commit', '-m', commit_message])
    
    # push to every remote repo
    for k,v in gitconfig['git'].items():
        subprocess.check_call(['git', 'push', k, 'master:%s' % v[1]])

if __name__ == '__main__':
    if len(sys.argv) == 2:
        if sys.argv[1] == '-h':
            print('Usage:\n\t%s [commit_message]' % sys.argv[0])
    main()

转载于:https://www.cnblogs.com/Akkuman/p/9519200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值