Skip to content

Instantly share code, notes, and snippets.

@kiy0taka
Created November 5, 2011 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiy0taka/1341311 to your computer and use it in GitHub Desktop.
Save kiy0taka/1341311 to your computer and use it in GitHub Desktop.
GroovyでGitコマンド。今のところinit/clone/statusがそれとなく使える。使用例: groovy git clone git@github.com:jenkinsci/mongodb-plugin.git
@Grab('com.madgag:org.eclipse.jgit:1.0.99.0.7-UNOFFICIAL-ROBERTO-RELEASE')
import org.eclipse.jgit.api.*
if (!args) System.exit(0)
def params = [*args]
def commands = [
init: {
def cli = new CliBuilder()
cli._ longOpt: 'bare', 'bare'
def opt = cli.parse(params)
Git.init().setDirectory(new File(opt.arguments() ? opt.arguments()[0] : '.')).setBare(opt.b).call()
},
clone: {
def cli = new CliBuilder()
cli.with {
_ longOpt:'bare', 'bare'
b longOpt:'branch', 'branch'
n longOpt:'no-checkout', 'no-checkout'
}
def opt = cli.parse(params)
def (uri, dir) = opt.arguments()
if (!uri) {
cli.usage()
System.exit(1)
}
def cmd = Git.cloneRepository().setURI(uri).setBare(opt.bare).setNoCheckout(opt['no-checkout'])
if (opt.b) cmd.branch = opt.b
cmd.directory = new File(dir?:((uri =~ /.*\/(.*)?/)[0][1] - '.git'))
cmd.call()
},
status: {
Git.open(new File('.')).status().call().with { status ->
['added', 'changed', 'removed', 'missing', 'modified', 'untracked'].each { name ->
status[name].each { println "$name: $it"}
}
}
}
]
(commands[params.remove(0)]?:{println 'no such command.'})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment