<commit_before>package com.zoltu.gradle.plugin

import org.codehaus.groovy.runtime.MethodClosure
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.bundling.Jar
import java.io.File

class GitVersioning : Plugin<Project> {
	companion object {
		val regexSimpleVersionInfo = Regex("""v([0-9]+?)\.([0-9]+?)(?:\-([0-9A-Za-z\.\-]+))?\-([0-9]+?)\-g(.*)""")
		val regexSemanticVersionInfo = Regex("""v([0-9]+?)\.([0-9]+?)\.([0-9]+?)(?:\-([0-9A-Za-z\.\-]+))?\-([0-9]+?)\-g([a-zA-Z0-9]+?)""")
	}

	override fun apply(project: Project?) {
		if (project == null) return;
		project.task("version").doLast { println("version ${project.version}") }
		val describeResults = getGitDescribeResults(project.rootDir)
		val versionInfo = getVersionInfo(describeResults)
		setProjectVersion(project, versionInfo)
		setJarManifestVersion(project, versionInfo)
	}

	fun getVersionInfo(describeResults: String): VersionInfo {
		return tryGetSemanticVersionInfo(describeResults) ?: getSimpleVersionInfo(describeResults)
	}

	private fun getSimpleVersionInfo(describeResults: String): VersionInfo {
		val match = regexSimpleVersionInfo.matchEntire(describeResults) ?: throw Exception("Git describe didn't return the expected format.")
		val major = match.groups[1]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 1.")
		val minor = match.groups[2]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 2.")
		val tags = match.groups[3]?.value
		val commitCount = match.groups[4]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 3.")
		val sha = match.groups[5]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 4.")
		return VersionInfo(major = major, minor = minor, commitCount = commitCount, sha = sha, tags = tags)
	}

	private fun tryGetSemanticVersionInfo(describeResults: String): VersionInfo? {
		val match = regexSemanticVersionInfo.matchEntire(describeResults) ?: return null

		val major = match.groups[1]?.value ?: return null
		val minor = match.groups[2]?.value ?: return null
		val patch = match.groups[3]?.value ?: return null
		val tags = match.groups[4]?.value
		val commitCount = match.groups[5]?.value ?: return null
		val sha = match.groups[6]?.value ?: return null<commit_msg>Minor formatting and text cleanup.<commit_after>package com.zoltu.gradle.plugin

import org.codehaus.groovy.runtime.MethodClosure
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.bundling.Jar
import java.io.File

class GitVersioning : Plugin<Project> {
	companion object {
		val regexSimpleVersionInfo = Regex("""v([0-9]+?)\.([0-9]+?)(?:\-([0-9A-Za-z\.\-]+))?\-([0-9]+?)\-g(.*)""")
		val regexSemanticVersionInfo = Regex("""v([0-9]+?)\.([0-9]+?)\.([0-9]+?)(?:\-([0-9A-Za-z\.\-]+))?\-([0-9]+?)\-g([a-zA-Z0-9]+?)""")
	}

	override fun apply(project: Project?) {
		if (project == null) return;

		project.task("version").doLast { println("Version: ${project.version}") }

		val describeResults = getGitDescribeResults(project.rootDir)
		val versionInfo = getVersionInfo(describeResults)
		setProjectVersion(project, versionInfo)
		setJarManifestVersion(project, versionInfo)
	}

	fun getVersionInfo(describeResults: String): VersionInfo {
		return tryGetSemanticVersionInfo(describeResults) ?: getSimpleVersionInfo(describeResults)
	}

	private fun getSimpleVersionInfo(describeResults: String): VersionInfo {
		val match = regexSimpleVersionInfo.matchEntire(describeResults) ?: throw Exception("Git describe didn't return the expected format.")
		val major = match.groups[1]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 1.")
		val minor = match.groups[2]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 2.")
		val tags = match.groups[3]?.value
		val commitCount = match.groups[4]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 3.")
		val sha = match.groups[5]?.value ?: throw Exception("Git describe matched the expected format but the matcher didn't return group 4.")
		return VersionInfo(major = major, minor = minor, commitCount = commitCount, sha = sha, tags = tags)
	}

	private fun tryGetSemanticVersionInfo(describeResults: String): VersionInfo? {
		val match = regexSemanticVersionInfo.matchEntire(describeResults) ?: return null

		val major = match.groups[1]?.value ?: return null
		val minor = match.groups[2]?.value ?: return null
		val patch = match.groups[3]?.value ?: return null
		val tags = match.groups[4]?.value
		val commitCount = match.groups[5]?.value ?: return null
		val sha = match.groups[6]?.value ?: return null
{'fraction_non_alphanumeric': 0.14732673267326732, 'fraction_numerical': 0.013861386138613862, 'mean_word_length': 5.17481662591687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6436768', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1554, 'n_words': 385}