diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2016-08-14 21:19:00 -0400 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-08-29 09:24:27 -0400 |
commit | c4fed700bf1ee86e34897831824a29b4e12c4c7f (patch) | |
tree | 0054b07a93dc5a6022596bb16625f6e019f0b4cf | |
parent | 51be413e5ed81cad683ef209660af36234d2c83d (diff) | |
download | cbt-c4fed700bf1ee86e34897831824a29b4e12c4c7f.tar.gz cbt-c4fed700bf1ee86e34897831824a29b4e12c4c7f.tar.bz2 cbt-c4fed700bf1ee86e34897831824a29b4e12c4c7f.zip |
Some cosmetic changes to git auth
-rw-r--r-- | stage2/GitDependency.scala | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/stage2/GitDependency.scala b/stage2/GitDependency.scala index 6519012..8faabc5 100644 --- a/stage2/GitDependency.scala +++ b/stage2/GitDependency.scala @@ -1,5 +1,6 @@ package cbt import java.io._ +import java.nio.file.Files.readAllBytes import java.net._ import org.eclipse.jgit.api._ import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider @@ -17,38 +18,38 @@ case class GitDependency( // TODO: add support for authentication via ssh and/or https // See http://www.codeaffine.com/2014/12/09/jgit-authentication/ private val GitUrl( _, domain, path ) = url - case class GitCredentials( - username: String, - password: String - ) + + private val credentialsFile = context.projectDirectory ++ "/git.login" private object checkoutCache extends Cache[File] def checkout: File = checkoutCache{ - def authenticate(creds: Option[GitCredentials], git: CloneCommand): CloneCommand = creds match { - case Some(credentials) => git.setCredentialsProvider(new UsernamePasswordCredentialsProvider(credentials.username, credentials.password)) - case None => git - } val checkoutDirectory = context.cache ++ s"/git/$domain/$path/$ref" if(checkoutDirectory.exists){ logger.git(s"Found existing checkout of $url#$ref in $checkoutDirectory") } else { logger.git(s"Cloning $url into $checkoutDirectory") - val credentials = { - try { - val credentials = scala.io.Source.fromFile(context.projectDirectory + "/git.login").mkString.split("\n") - Some(GitCredentials(credentials(0), credentials(1))) - } catch { - case e: FileNotFoundException => None - } - } - val git = authenticate(credentials, Git.cloneRepository().setURI(url)) + val git = { + val _git = Git + .cloneRepository() + .setURI(url) .setDirectory(checkoutDirectory) - .call() + + if(!credentialsFile.exists){ + _git + } else { + val (user, password) = { + // TODO: implement safer method than reading credentials from plain text file + val c = new String(readAllBytes(credentialsFile.toPath)).split("\n").head.trim.split(":") + (c(0), c.drop(1).mkString(":")) + } + _git.setCredentialsProvider( new UsernamePasswordCredentialsProvider(user, password) ) + } + }.call() logger.git(s"Checking out ref $ref") git.checkout() - .setName(ref) - .call() + .setName(ref) + .call() } checkoutDirectory } |