现在的位置: 首页 > 综合 > 正文

两则快速git pull push的脚本

2013年08月18日 ⁄ 综合 ⁄ 共 1466字 ⁄ 字号 评论关闭

在团队中使用git有一段时间了,强烈到领悟的git的方便,方便地认为这个世界上只需要git,不需要svn这样的东东。
然而,即使在方便的工具也有遇到不方便的瓶颈的时候,举例来说,git pull 和push的语法基本都是差不多,基本上是我们需要输入origin branchName
可不可以直接填写上origin 和 当前的分支名呢,这样不是更方便么,当然可以。请参考下列代码:
gpull.py 

#!/usr/bin/env python
# coding=utf-8
from subprocess import Popen,PIPE,STDOUT
from os import system


def gpull():
    branchColorRule = readFromShell('git config color.branch')
    if ('always' == branchColorRule):
        system('git config color.branch auto')


    getBranch = "git branch | sed -n '/\* /s///p'"
    gitBranch = readFromShell(getBranch)
    command = 'git pull origin %s'%(gitBranch)
    print command
    system(command)
    if ('always' == branchColorRule):
        system('git config color.branch always')




def readFromShell(command):
    p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    result = p.stdout.read().strip()
    return result


gpull()

gpush.py

#!/usr/bin/env python
# coding=utf-8
from subprocess import Popen,PIPE,STDOUT
from os import system


def gpush():
    branchColorRule = readFromShell('git config color.branch')
    if ('always' == branchColorRule):
        system('git config color.branch auto')


    getBranch = "git branch | sed -n '/\* /s///p'"
    gitBranch = readFromShell(getBranch)
    command = 'git push origin %s'%(gitBranch)
    print command
    system(command)
    if ('always' == branchColorRule):
        system('git config color.branch always')




def readFromShell(command):
    p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    result = p.stdout.read().strip()
    return result


gpush()

仓库地址:https://github.com/androidyue/DroidPy
关于为什么处理git status color always 情况,因为如果不处理,得到的不是一个Plain Text的分支(带着颜色),这样会导致无法使用。

抱歉!评论已关闭.