java git batch pull

package com.dpc;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by dpc on 15-5-22.
 */
public class PullAll {

    public static void main(String[] args) throws IOException {
        String webapi = "ls ";
        String[] array = {"rpcapi", "core",
                "biz",
                "model",
                "webapi"};
        String cd = "cd  /home/dpc/idea-workspace/server/";
        String and = " && ";
        String pull = " git pull";
        for (String dir : array) {
            execute(cd + dir + and + pull);

        }

    }

    private static void execute(String cmd) {
        String result = "";
        try {
            String[] command = {"/bin/sh", "-c", "cd / && pwd"};
            command[2] = cmd;
            //System.out.println("*** command is  " + cmd);
            Process process = null;
            process = Runtime.getRuntime().exec(command);
            process.waitFor();
            
            //得到命令执行后的结果
            InputStream is = process.getInputStream();
            byte[] buffer = new byte[1024];
            while (is.read(buffer) != -1) {
                result = result + new String(buffer);
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
    }


}


console里会输出拉取的详细信息,和shell里一样的。合并需要自己手动搞


 如果要忽略本地改动 ,直接拉取远程仓库最新代码 ,可以这样:

git fetch --all   && git reset --hard origin/master



2、  优化一下,记录下更加简洁的py版本 , py版本更省代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess

cdcmd = ' cd '
andcmd = ' &&  '
rootdir = '/home/dpc/idea-workspace/server/'
gitpull = ' git pull origin master '
def call(cmd):
    print 'start execute ....' + cmd
    subprocess.call(cmd, shell=True)

def pull():  
    dirs = ["sapi", "core", "biz", "model", "webapi"]
    for idir in dirs:
        cddir = rootdir + idir
        cmd = cdcmd + cddir + andcmd + gitpull
        call(cmd)
pull()


转载于:https://my.oschina.net/ois/blog/418144

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值