Skip to content

Commit

Permalink
[orx-git-archiver] Fix NativeGit on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed Nov 7, 2021
1 parent 2a5e795 commit ad2b5da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
12 changes: 11 additions & 1 deletion orx-jvm/orx-git-archiver/src/main/kotlin/GitArchiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.openrndr.Program

internal interface GitProvider {
fun commitChanges(commitMessage: String)
fun headReference() : String
fun headReference(): String
}

val logger = KotlinLogging.logger { }
Expand All @@ -23,6 +23,16 @@ class GitArchiver : Extension {
private val git: GitProvider = if (nativeGitInstalled()) NativeGit() else JavaGit()

override fun setup(program: Program) {
logger.info {
"Using ${
when (git) {
is NativeGit -> "native Git"
is JavaGit -> "Java Git"
else -> "unknown Git"
}
}"
}

val oldMetadataFunction = program.assetMetadata
program.assetMetadata = {
val oldMetadata = oldMetadataFunction()
Expand Down
19 changes: 10 additions & 9 deletions orx-jvm/orx-git-archiver/src/main/kotlin/NativeGit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,33 @@ private val dir = File(".")

class NativeGit : GitProvider {
override fun commitChanges(commitMessage: String) {
val gitStatus = "git status --porcelain".runCommand(dir)!!
val gitStatus = listOf("git", "status", "--porcelain").runCommand(dir)!!
if (gitStatus.first.isNotBlank()){
if (gitStatus.first.contains("Not a git repository")){
logger.error { "Can't commit changes because the working directory is not a git repository" }
} else {
"git add .".runCommand(dir)
"git commit -m \"${commitMessage}\"".runCommand(dir)
logger.info { "git repository is now at ${headReference()}" }
listOf("git", "add", ".").runCommand(dir)
listOf("git", "commit", "-m", commitMessage).runCommand(dir)
logger.info { "git repository is at ${headReference()} after commit" }
}
} else {
logger.info { "no changes" }
}
}

override fun headReference(): String {
return "git rev-parse --short HEAD".runCommand(dir)!!.first.trimEnd()
return listOf("git", "rev-parse", "--short", "HEAD").runCommand(dir)!!.first.trimEnd()
}
}

internal fun nativeGitInstalled(): Boolean {
return "git --version".runCommand(dir) != null
return listOf("git", "--version").runCommand(dir) != null
}

// Adapted from https://stackoverflow.com/questions/35421699/how-to-invoke-external-command-from-within-kotlin-code
private fun String.runCommand(workingDir: File): Pair<String, String>? {
private fun List<String>.runCommand(workingDir: File): Pair<String, String>? {
try {
val parts = this.split("\\s".toRegex())
val proc = ProcessBuilder(*parts.toTypedArray())
val proc = ProcessBuilder(*toTypedArray())
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
Expand All @@ -44,6 +43,8 @@ private fun String.runCommand(workingDir: File): Pair<String, String>? {
proc.waitFor(60, TimeUnit.MINUTES)
return Pair(proc.inputStream.bufferedReader().readText(), proc.errorStream.bufferedReader().readText())
} catch(e: IOException) {
logger.error { e.message }
e.printStackTrace()
return null
}
}

0 comments on commit ad2b5da

Please sign in to comment.