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()