mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-14 04:00:30 +00:00
Use git shortlog for gathering contributors
This commit is contained in:
parent
06163e4f25
commit
0787e17ebe
@ -2,6 +2,7 @@ package cc.tweaked.gradle
|
|||||||
|
|
||||||
import net.ltgt.gradle.errorprone.CheckSeverity
|
import net.ltgt.gradle.errorprone.CheckSeverity
|
||||||
import net.ltgt.gradle.errorprone.errorprone
|
import net.ltgt.gradle.errorprone.errorprone
|
||||||
|
import org.gradle.api.GradleException
|
||||||
import org.gradle.api.NamedDomainObjectProvider
|
import org.gradle.api.NamedDomainObjectProvider
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.Task
|
import org.gradle.api.Task
|
||||||
@ -25,10 +26,8 @@ import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
|
|||||||
import org.gradle.testing.jacoco.tasks.JacocoReport
|
import org.gradle.testing.jacoco.tasks.JacocoReport
|
||||||
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
|
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
|
||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
import java.io.BufferedWriter
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.OutputStreamWriter
|
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
@ -39,43 +38,30 @@ abstract class CCTweakedExtension(
|
|||||||
) {
|
) {
|
||||||
/** Get the hash of the latest git commit. */
|
/** Get the hash of the latest git commit. */
|
||||||
val gitHash: Provider<String> = gitProvider(project, "<no git hash>") {
|
val gitHash: Provider<String> = gitProvider(project, "<no git hash>") {
|
||||||
ProcessHelpers.captureOut("git", "-C", project.projectDir.absolutePath, "rev-parse", "HEAD").trim()
|
ProcessHelpers.captureOut("git", "-C", project.rootProject.projectDir.absolutePath, "rev-parse", "HEAD").trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the current git branch. */
|
/** Get the current git branch. */
|
||||||
val gitBranch: Provider<String> = gitProvider(project, "<no git branch>") {
|
val gitBranch: Provider<String> = gitProvider(project, "<no git branch>") {
|
||||||
ProcessHelpers.captureOut("git", "-C", project.projectDir.absolutePath, "rev-parse", "--abbrev-ref", "HEAD")
|
ProcessHelpers.captureOut("git", "-C", project.rootProject.projectDir.absolutePath, "rev-parse", "--abbrev-ref", "HEAD")
|
||||||
.trim()
|
.trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get a list of all contributors to the project. */
|
/** Get a list of all contributors to the project. */
|
||||||
val gitContributors: Provider<List<String>> = gitProvider(project, listOf()) {
|
val gitContributors: Provider<List<String>> = gitProvider(project, listOf()) {
|
||||||
val authors: Set<String> = HashSet(
|
ProcessHelpers.captureLines(
|
||||||
ProcessHelpers.captureLines(
|
"git", "-C", project.rootProject.projectDir.absolutePath, "shortlog", "-ns",
|
||||||
"git", "-C", project.projectDir.absolutePath, "log",
|
"--group=author", "--group=trailer:co-authored-by", "HEAD",
|
||||||
"--format=tformat:%an <%ae>%n%cn <%ce>%n%(trailers:key=Co-authored-by,valueonly)",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
val process = ProcessHelpers.startProcess("git", "check-mailmap", "--stdin")
|
.asSequence()
|
||||||
BufferedWriter(OutputStreamWriter(process.outputStream)).use { writer ->
|
.map {
|
||||||
for (authorName in authors) {
|
val matcher = COMMIT_COUNTS.matcher(it)
|
||||||
var author = authorName
|
matcher.find()
|
||||||
|
matcher.group(1)
|
||||||
if (author.isEmpty()) continue
|
|
||||||
if (!author.endsWith(">")) author += ">" // Some commits have broken Co-Authored-By lines!
|
|
||||||
writer.write(author)
|
|
||||||
writer.newLine()
|
|
||||||
}
|
}
|
||||||
}
|
.filter { !IGNORED_USERS.contains(it) }
|
||||||
val contributors: MutableSet<String> = HashSet()
|
.toList()
|
||||||
for (authorLine in ProcessHelpers.captureLines(process)) {
|
.sortedWith(String.CASE_INSENSITIVE_ORDER)
|
||||||
val matcher = EMAIL.matcher(authorLine)
|
|
||||||
matcher.find()
|
|
||||||
val name = matcher.group(1)
|
|
||||||
if (!IGNORED_USERS.contains(name)) contributors.add(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
contributors.sortedWith(String.CASE_INSENSITIVE_ORDER)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -257,7 +243,7 @@ abstract class CCTweakedExtension(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val EMAIL = Pattern.compile("^([^<]+) <.+>$")
|
private val COMMIT_COUNTS = Pattern.compile("""^\s*[0-9]+\s+(.*)$""")
|
||||||
private val IGNORED_USERS = setOf(
|
private val IGNORED_USERS = setOf(
|
||||||
"GitHub", "Daniel Ratcliffe", "Weblate",
|
"GitHub", "Daniel Ratcliffe", "Weblate",
|
||||||
)
|
)
|
||||||
@ -269,6 +255,9 @@ abstract class CCTweakedExtension(
|
|||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
project.logger.error("Cannot read Git repository: ${e.message}")
|
project.logger.error("Cannot read Git repository: ${e.message}")
|
||||||
default
|
default
|
||||||
|
} catch (e: GradleException) {
|
||||||
|
project.logger.error("Cannot read Git repository: ${e.message}")
|
||||||
|
default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,33 +4,37 @@ import org.codehaus.groovy.runtime.ProcessGroovyMethods
|
|||||||
import org.gradle.api.GradleException
|
import org.gradle.api.GradleException
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
|
|
||||||
internal object ProcessHelpers {
|
internal object ProcessHelpers {
|
||||||
fun startProcess(vararg command: String): Process {
|
fun startProcess(vararg command: String): Process {
|
||||||
// Something randomly passes in "GIT_DIR=" as an environment variable which clobbers everything else. Don't
|
// Something randomly passes in "GIT_DIR=" as an environment variable which clobbers everything else. Don't
|
||||||
// inherit the environment array!
|
// inherit the environment array!
|
||||||
return Runtime.getRuntime().exec(command, arrayOfNulls(0))
|
return ProcessBuilder()
|
||||||
|
.command(*command)
|
||||||
|
.redirectError(ProcessBuilder.Redirect.INHERIT)
|
||||||
|
.also { it.environment().clear() }
|
||||||
|
.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun captureOut(vararg command: String): String {
|
fun captureOut(vararg command: String): String {
|
||||||
val process = startProcess(*command)
|
val process = startProcess(*command)
|
||||||
|
process.outputStream.close()
|
||||||
|
|
||||||
val result = ProcessGroovyMethods.getText(process)
|
val result = ProcessGroovyMethods.getText(process)
|
||||||
if (process.waitFor() != 0) throw IOException("Command exited with a non-0 status")
|
process.waitForOrThrow("Failed to run command")
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun captureLines(vararg command: String): List<String> {
|
fun captureLines(vararg command: String): List<String> {
|
||||||
return captureLines(startProcess(*command))
|
val process = startProcess(*command)
|
||||||
}
|
process.outputStream.close()
|
||||||
|
|
||||||
fun captureLines(process: Process): List<String> {
|
|
||||||
val out = BufferedReader(InputStreamReader(process.inputStream)).use { reader ->
|
val out = BufferedReader(InputStreamReader(process.inputStream)).use { reader ->
|
||||||
reader.lines().filter { it.isNotEmpty() }.toList()
|
reader.lines().filter { it.isNotEmpty() }.toList()
|
||||||
}
|
}
|
||||||
ProcessGroovyMethods.closeStreams(process)
|
ProcessGroovyMethods.closeStreams(process)
|
||||||
if (process.waitFor() != 0) throw IOException("Command exited with a non-0 status")
|
process.waitForOrThrow("Failed to run command")
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,6 @@ dependencies {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tasks.javadoc {
|
tasks.javadoc {
|
||||||
// Depend on
|
// Depend on the common API when publishing javadoc
|
||||||
classpath += docApi
|
classpath += docApi.get()
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ tasks.processResources {
|
|||||||
inputs.property("gitHash", cct.gitHash)
|
inputs.property("gitHash", cct.gitHash)
|
||||||
|
|
||||||
filesMatching("data/computercraft/lua/rom/help/credits.txt") {
|
filesMatching("data/computercraft/lua/rom/help/credits.txt") {
|
||||||
expand(mapOf("gitContributors" to cct.gitContributors.get()))
|
expand(mapOf("gitContributors" to cct.gitContributors.map { it.joinToString("\n") }.get()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user