挑戰在於改變工作目錄,所以你可以在不同路徑上的多個git存儲庫上工作。爲了解決這個問題,我使用了具有目錄(File directory)方法的java.lang.ProcessBuilder,它改變了工作目錄。這裏是一個完整的例子:
//executes the command in the working dir
//and prints the output to a log file
def runProcess(workingDir, command) {
ProcessBuilder procBuilder = new ProcessBuilder(command);
procBuilder.directory(workingDir);
procBuilder.redirectOutput(new File("${workingDir}/groovyGit.log"))
Process proc = procBuilder.start();
def exitVal = proc.waitFor()
println "Exit value: ${exitVal}"
return proc
}
//configuring the working dir for each git repository
def repoA = "repo A working dir"
def repoB = "repo B working dir"
def repoC = "repo C working dir"
//configuring the wanted revision to checkout for each repository
def repoARevision = "repo a revision"
def repoBRevision = "repo b revision"
def commitMsg = "commit msg"
//checkout A
runProcess(new File(repoA), ["git", "checkout", repoARevision])
//checkout B
runProcess(new File(repoB), ["git", "checkout", repoBRevision])
//check status before commit
runProcess(new File(repoC), ["git", "status"])
//commit
runProcess(new File(repoC), ["git", "commit", "-a", "-m", commitMsg])