Last active
August 29, 2015 14:06
-
-
Save manhole/35f7bb32670114a97522 to your computer and use it in GitHub Desktop.
直下のworking directory群でgit fetch ${remote}, git svn fetch ${svn-remote}するgroovyスクリプト。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* usage: groovy gittool.groovy path/to/parent_directory | |
* | |
* @author manhole | |
*/ | |
@Grapes([ | |
@Grab(group = 'joda-time', module = 'joda-time', version = '2.3') | |
,@Grab(group = 'ch.qos.logback', module = 'logback-core', version = '1.1.2') | |
,@Grab(group = 'ch.qos.logback', module = 'logback-classic', version = '1.1.2') | |
]) | |
import org.joda.time.DateTime | |
import org.slf4j.Logger | |
import org.slf4j.LoggerFactory | |
new GitClientUtil().main(args) | |
class GitClientUtil { | |
final Logger logger = LoggerFactory.getLogger(getClass()) | |
private String _rootPath | |
void main(String[] args) { | |
def start = new DateTime() | |
logger.info "BEGIN: ${start}" | |
logger.debug "args: ${Arrays.asList(args)}" | |
def dir = new File(".") | |
if (0 < args.length) { | |
dir = args[0] as File | |
} | |
dir = dir.canonicalFile | |
_rootPath = dir.absolutePath | |
logger.debug "rootPath: {}", _rootPath | |
fetchAll(dir) | |
def end = new DateTime() | |
logger.info "END: ${end}" | |
logger.info "elapsed: ${(end.millis - start.millis)} msec" | |
} | |
void fetchAll(File dir) { | |
try { | |
fetchDir(dir) | |
} catch (e) { | |
logger.warn(e.getMessage(), e) | |
} | |
dir.eachDir { File childDir -> | |
fetchAll(childDir) | |
} | |
} | |
private void fetchDir(File dir) { | |
def gitconfigFile = new File(dir, ".git/config") | |
if (gitconfigFile.isFile()) { | |
def configText = gitconfigFile.getText("UTF-8") | |
// [remote "origin"] | |
(configText =~ /(?sm)^\[remote "(.+?)"\]$/).each { all, g1 -> | |
executeCommand("git fetch ${g1} --prune", dir) | |
} | |
// [svn-remote "svn"] | |
(configText =~ /(?sm)^\[svn-remote "(.+?)"\]$/).each { all, g1 -> | |
executeCommand("git svn fetch ${g1}", dir) | |
} | |
} | |
} | |
void executeCommand(String command, File workDir) { | |
def path = workDir.absolutePath | |
if (path.startsWith(_rootPath) && _rootPath.length() < path.length()) { | |
path = path.substring(_rootPath.length() + 1) | |
} | |
logger.info "${path}: ${command}" | |
String[] envp = null | |
def p = command.execute(envp, workDir) | |
p.waitForProcessOutput(System.out, System.err) | |
if (p.exitValue() != 0) { | |
throw new Exception("execution failed: <${command}>") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment