<commit_before>/* sbt -- Simple Build Tool
 * Copyright 2011 <NAME>
 */

package sbt

import java.io.File
import java.net.URI

import BuildLoader.ResolveInfo
import RichURI.fromURI

object Resolvers
{
	type Resolver = BuildLoader.Resolver

	val local: Resolver = (info: ResolveInfo) => {
		val uri = info.uri
		val from = new File(uri)
		val to = uniqueSubdirectoryFor(uri, in = info.staging)

		if (from.isDirectory) Some {() => creates(to) {IO.copyDirectory(from, to)}}
		else None
	}

	val remote: Resolver = (info: ResolveInfo) => {
		val url = info.uri.toURL
		val to = uniqueSubdirectoryFor(info.uri, in = info.staging)

		Some {() => creates(to) {IO.unzipURL(url, to)}}
	}

	val subversion: Resolver = (info: ResolveInfo) => {
		def normalized(uri: URI) = uri.copy(scheme = "svn")

		val uri = info.uri.withoutMarkerScheme
		val localCopy = uniqueSubdirectoryFor(normalized(uri), in = info.staging)
		val from = uri.withoutFragment.toASCIIString
		val to = localCopy.getAbsolutePath

		if (uri.hasFragment) {
			val revision = uri.getFragment
			Some {
				() => creates(localCopy) {
					run("svn", "checkout", "-r", revision, from, to)
				}
			}
		} else
			Some {
				() => creates(localCopy) {
					run("svn", "checkout", from, to)
				}
			}
	}

	val mercurial: Resolver = new DistributedVCS
	{
		override val scheme = "hg"

		override def clone(at: String, into: File) = creates(into) {run("hg", "clone", at, into.getAbsolutePath)}

		override def checkout(branch: String, in: File)
		{
			run(Some(in), "hg", "-q", "checkout", branch)
		}
	}.toResolver

	val git: Resolver = new DistributedVCS
	{
		override val scheme = "git"

		override def clone(at: String, into: File) = creates(into) {run("git", "clone", at, into.getAbsolutePath)}

		override def checkout(branch: String, in: File)
		{
			run(Some(in), "git", "checkout", "-q", branch)
		}
	}.toResolver

	abstract class DistributedVCS
	{
		val scheme: String

		def clone(at: String, into: File): File

		def checkout(branch: String, in: File)

		def toResolver: Resolver = (info: ResolveInfo) => {
			val uri = info.uri.withoutMarkerScheme
			val staging = info.staging
			val localCopy = uniqueSubdirectoryFor(normalized(uri.withoutFragment), in = staging)

			if (uri.hasFragment) {
				val branch = uri.getFragment
				val branchCopy = uniqueSubdirectoryFor(normalized(uri), in = staging)
				Some {
					() =>
						clone(uri.withoutFragment.toASCIIString, into = localCopy)
						clone(localCopy.getAbsolutePath, into = branchCopy)
						checkout(branch, in = branchCopy)
						branchCopy
				}
			} else Some {() => clone(uri.withoutFragment.toASCIIString, into = localCopy)}
		}

		private def normalized(uri: URI) = uri.copy(scheme = scheme)
	}

	private lazy val onWindows = {
		val os = System.getenv("OSTYPE")
		val isCygwin = (os != null) && os.toLowerCase.contains("cygwin")
		val isWindows = System.getProperty("os.name", "").toLowerCase.contains("windows")
		isWindows && !isCygwin
	}

	def run(command: String*) {run(None, command: _*)}

	def run(cwd: Option[File], command: String*)
	{
		val result =
			Process(
				if (onWindows)
					"cmd" +: "/c" +: command
				else
					command,
				cwd
			) !;
		if (result != 0)
			error("Nonzero exit code (" + result + "): " + command.mkString(" "))
	}

	def creates(file: File)(f: => Unit) =
	{
		try {
			if (!file.exists)
				f
			file
		} catch {
			case e =>
				IO.delete(file)
				throw e
		}
	}

	def uniqueSubdirectoryFor(uri: URI, in: File) = new File(in, Hash.halfHashString(uri.toASCIIString))
}<commit_msg>Fix not cleaning up when checkout of a DVCS branch fails<commit_after>/* sbt -- Simple Build Tool
 * Copyright 2011 <NAME>
 */

package sbt

import java.io.File
import java.net.URI

import BuildLoader.ResolveInfo
import RichURI.fromURI

object Resolvers
{
	type Resolver = BuildLoader.Resolver

	val local: Resolver = (info: ResolveInfo) => {
		val uri = info.uri
		val from = new File(uri)
		val to = uniqueSubdirectoryFor(uri, in = info.staging)

		if (from.isDirectory) Some {() => creates(to) {IO.copyDirectory(from, to)}}
		else None
	}

	val remote: Resolver = (info: ResolveInfo) => {
		val url = info.uri.toURL
		val to = uniqueSubdirectoryFor(info.uri, in = info.staging)

		Some {() => creates(to) {IO.unzipURL(url, to)}}
	}

	val subversion: Resolver = (info: ResolveInfo) => {
		def normalized(uri: URI) = uri.copy(scheme = "svn")

		val uri = info.uri.withoutMarkerScheme
		val localCopy = uniqueSubdirectoryFor(normalized(uri), in = info.staging)
		val from = uri.withoutFragment.toASCIIString
		val to = localCopy.getAbsolutePath

		if (uri.hasFragment) {
			val revision = uri.getFragment
			Some {
				() => creates(localCopy) {
					run("svn", "checkout", "-r", revision, from, to)
				}
			}
		} else
			Some {
				() => creates(localCopy) {
					run("svn", "checkout", from, to)
				}
			}
	}

	val mercurial: Resolver = new DistributedVCS
	{
		override val scheme = "hg"

		override def clone(from: String, to: File) {
			run("hg", "clone", from, to.getAbsolutePath)
		}

		override def checkout(branch: String, in: File)
		{
			run(Some(in), "hg", "-q", "checkout", branch)
		}
	}.toResolver

	val git: Resolver = new DistributedVCS
	{
		override val scheme = "git"

		override def clone(from: String, to: File)
		{
			run("git", "clone", from, to.getAbsolutePath)
		}

		override def checkout(branch: String, in: File)
		{
			run(Some(in), "git", "checkout", "-q", branch)
		}
	}.toResolver

	abstract class DistributedVCS
	{
		val scheme: String

		def clone(from: String, to: File)

		def checkout(branch: String, in: File)

		def toResolver: Resolver = (info: ResolveInfo) => {
			val uri = info.uri.withoutMarkerScheme
			val staging = info.staging
			val localCopy = uniqueSubdirectoryFor(normalized(uri.withoutFragment), in = staging)
			val from = uri.withoutFragment.toASCIIString

			if (uri.hasFragment) {
				val branch = uri.getFragment
				val branchCopy = uniqueSubdirectoryFor(normalized(uri), in = staging)
				Some {
					() =>
						creates(localCopy) {clone(from, to = localCopy)}
						creates(branchCopy) {
							clone(localCopy.getAbsolutePath, to = branchCopy)
							checkout(branch, in = branchCopy)
						}
				}
			} else Some {() => creates(localCopy) {clone(from, to = localCopy)}}
		}

		private def normalized(uri: URI) = uri.copy(scheme = scheme)
	}

	private lazy val onWindows = {
		val os = System.getenv("OSTYPE")
		val isCygwin = (os != null) && os.toLowerCase.contains("cygwin")
		val isWindows = System.getProperty("os.name", "").toLowerCase.contains("windows")
		isWindows && !isCygwin
	}

	def run(command: String*) {run(None, command: _*)}

	def run(cwd: Option[File], command: String*)
	{
		val result =
			Process(
				if (onWindows)
					"cmd" +: "/c" +: command
				else
					command,
				cwd
			) !;
		if (result != 0)
			error("Nonzero exit code (" + result + "): " + command.mkString(" "))
	}

	def creates(file: File)(f: => Unit) =
	{
		if (!file.exists)
			try {
				f
			} catch {
				case e =>
					IO.delete(file)
					throw e
			}
		file
	}

	def uniqueSubdirectoryFor(uri: URI, in: File) = new File(in, Hash.halfHashString(uri.toASCIIString))
}
{'fraction_non_alphanumeric': 0.14130434782608695, 'fraction_numerical': 0.0013935340022296545, 'mean_word_length': 3.432983323038913, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6096126', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2446, 'n_words': 628}