Skip to content

Instantly share code, notes, and snippets.

@zenon
Created November 14, 2012 09:55
Show Gist options
  • Save zenon/4071282 to your computer and use it in GitHub Desktop.
Save zenon/4071282 to your computer and use it in GitHub Desktop.
Scala Worksheet to test the usage of JGit
object GitTest {
println("Testing JGit with Scala")
/*
- I used the Typesafe Scala IDE (Eclipse and a bit more)
- Downloaded JGit (2.1.0.201209190230-r) from http://eclipse.org/jgit/download/
- Donloaded JSch (Java Secure Channel, jsch-0.1.49.jar) from http://www.jcraft.com/jsch/
- Put both in the build path.
- Created a worksheet. (This one.)
- Play around.
*/
import java.io.File
import org.eclipse.jgit.api._
import org.eclipse.jgit.storage.file.FileRepository
import scala.collection.JavaConverters._
// FETCH A REPO FROM GITHUB.
var dirName = "d:/temp/JGitTest"
var dir = new File(dirName)
if(new File(dir, ".git").exists) {
println("Please clear the directory first: " + dir.getAbsolutePath())
} else {
// Creating an object and then changing it. Uh.
val clone = new CloneCommand()
.setURI("https://github.com/scalatron/scalatron.git")
.setDirectory(dir)
// This will clone the repository into the given directory
// and create a reference of type Git to play with.
val g0 = clone.call()
val log = g0.log.call()
log.asScala.take(5).foreach { entry =>
println(entry.getAuthorIdent().getName() + " - " + entry.getShortMessage()) }
}
// NOW WE HAVE A LOCAL REPOSITORY.
// Mind the .git
var localRepo = new FileRepository(dirName + "/.git")
val g1 = new Git(localRepo)
g1.getRepository().getAllRefs()
// Not needed - is already checked out.
// g1.checkout().setName("master").call()
//let's create a file and add it to the repo.
val testFile = new File(dir, "test_file.txt")
testFile.createNewFile()
g1.add().addFilepattern("test_file.txt").call()
for(s <- g1.status().call().getAdded().asScala){
println(s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment