All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sbt.git.JGitRunner.scala Maven / Gradle / Ivy

The newest version!
package com.github.sbt.git

import sbt._
import Keys._

/** A mechanism to run GIT using the pure java JGit implementation. */
object JGitRunner extends GitRunner {

  override def apply(args: String*)(cwd: File, log: Logger = ConsoleLogger()): String = {
    args.headOption match {
      // For now, let's just use the JGIT comand line and see what happens.
      // Eventually we can aim to speed up commands...
      //case Some("push") => push(args drop 1)(cwd, log)
      //case Some("pull") => pull(args drop 1)(cwd, log)
      //case Some("clone") => clone(args drop 1)(cwd, log)
      case _ =>  forkJGitMain(args:_*)(cwd, log)
    }
  }

  /**
       git clone [--template=]
                 [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
                 [-o ] [-b ] [-u ] [--reference ]
                 [--separate-git-dir ]
                 [--depth ] [--[no-]single-branch]
                 [--recursive|--recurse-submodules] [--] 
                 []
   */
  private def clone(args: Seq[String])(cwd: File, log: Logger = ConsoleLogger()): String = {
    // TODO - Parse args...
    // TODO - this should not just work for ghpages plugin
    val git = JGit(cwd)
    args match {
      case Seq("-b", branch, remote, ".") =>
        // TODO - Logging?
        JGit.clone(remote, cwd).checkoutBranch(branch);
      case Seq(remote, ".") =>
        JGit.clone(remote, cwd)
      case _ => sys.error("Unable to run clone command: clone " + args.mkString(" "))
    }
    ""
  }

  private def pull(args: Seq[String])(cwd: File, log: Logger = ConsoleLogger()): String = {
    val git = JGit(cwd)
    // TODO - Parse options...
    // TODO - Set logging/progress monitor.
    git.porcelain.pull().call();
    ""
  }

   private def push(args: Seq[String])(cwd: File, log: Logger = ConsoleLogger()): String = {
    val git = JGit(cwd)
    // TODO - Parse options...
    // TODO - Set logging/progress monitor.
    git.porcelain.push().call();
    ""
  }

  private def forkJGitMain(args: String*)(cwd: File, log: Logger = ConsoleLogger()): String =
    getClass.getClassLoader match {
       case cl: java.net.URLClassLoader =>
         val cp = cl.getURLs map (_.getFile) mkString ":"
         val baos = new java.io.ByteArrayOutputStream
			// NOTE: this will always return 0 until sbt 0.13.1 due to the use of CustomOutput
         val code = Fork.java.fork(ForkOptions(
          javaHome = None,
          outputStrategy = Some(CustomOutput(baos)),
          bootJars = Vector.empty[File],
          workingDirectory = Some(cwd),
          runJVMOptions = Vector.empty[String],
          connectInput = false,
          envVars = Map.empty[String, String]),
          Vector("-classpath", cp, "org.eclipse.jgit.pgm.Main") ++ args).exitValue()
         val result = baos.toString
         log.info(result)
         if(code == 0) result else throw new MessageOnlyException("Nonzero exit code (" + code + ") running JGit.")
       case _ => sys.error("Could not find classpath for JGit!")
    }

  override def toString = "jgit"

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy