Skip to content

Instantly share code, notes, and snippets.

@jlcanela
Created April 9, 2015 22:05
Show Gist options
  • Select an option

  • Save jlcanela/49fd4cd4a0ce41c189c7 to your computer and use it in GitHub Desktop.

Select an option

Save jlcanela/49fd4cd4a0ce41c189c7 to your computer and use it in GitHub Desktop.
object MyJGitSample {
import scalaz._
import scalaz.effect.IO
def lift[T](f: => T) = EitherT(IO(\/.fromTryCatchNonFatal(f)))
def abortIf(b: Boolean, msg: String) = EitherT(IO(if (b) -\/(new Throwable(msg)) else \/-(())))
def createCommand = {
import org.eclipse.jgit.api._
import org.eclipse.jgit.internal.storage.file.FileRepository
val localPath = ".";
val localRepo = new FileRepository(localPath + "/.git")
val git = new Git(localRepo)
for {
addAll <- lift(git.add().addFilepattern("*").call.getEntryCount)
st <- lift(git.status.call())
_ = println(st.getUncommittedChanges)
_ <- abortIf(st.getUncommittedChanges.isEmpty, "No committed change")
} yield addAll
}
def main(args: Array[String]) = {
println(createCommand.run.unsafePerformIO)
}
}
@jlcanela
Copy link
Author

jlcanela commented Apr 9, 2015

Here are the sbt dependencies used in this sample : 

  • "org.eclipse.jgit" % "org.eclipse.jgit" % "3.7.0.201502260915-r"
  • "org.scalaz" %% "scalaz-core" % "7.1.1"
  • "org.scalaz" %% "scalaz-effect" % "7.1.1"

@YannMoisan
Copy link

looks good, but pretty tough to read due to scalaz.
Thanks a lot for pointing me to this implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment