Skip to content

Commit

Permalink
Introduce reduce command
Browse files Browse the repository at this point in the history
  • Loading branch information
goodwinnk committed Apr 13, 2018
1 parent 28c8a2c commit 0294fab
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/main/kotlin/nk/patchsets/git/bunchFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ fun readRuleFromFile(endSuffix: String, path: String): String? {
return ruleTargetToCurrent.reversed().joinToString(separator = "_")
}

data class UpdateInfo(val base: String, val rules: List<List<String>>)

fun readUpdatePairsFromFile(path: String): UpdateInfo? {
val file = File(path, ".bunch")
if (!file.exists()) {
System.err.println("File '${file.canonicalPath}' doesn't exist ")
return null
}

val lines = file.readLines().map { it.trim() }.filter { it.isNotEmpty() }

val currentBranchSuffix = lines.firstOrNull()
if (currentBranchSuffix == null) {
System.err.println("First line in '${file.canonicalPath}' should contain current branch name")
return null
}

val rules = ArrayList<List<String>>()

val branchRules = lines.drop(1)
for (branchRule in branchRules) {
rules.add((branchRule + "_$currentBranchSuffix").split("_").reversed())
}

return UpdateInfo(currentBranchSuffix, rules)
}

fun readExtensionFromFile(rootPath: String): List<String>? {
val file = File(rootPath, ".bunch")
if (!file.exists()) {
Expand Down
90 changes: 90 additions & 0 deletions src/main/kotlin/nk/patchsets/git/reduce.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package nk.patchsets.git.reduce

import nk.patchsets.git.file.readUpdatePairsFromFile
import nk.patchsets.git.restore.isGitDir
import nk.patchsets.git.restore.isGradleBuildDir
import nk.patchsets.git.restore.isGradleDir
import nk.patchsets.git.restore.toBunchFile
import java.io.File

data class Settings(val repoPath: String)

fun main(args: Array<String>) {
reduce(args)
}

fun reduce(args: Array<String>) {
if (args.size != 1) {
System.err.println("""
Usage: <git-path>
Check repository for unneeded files with same content
<git-path> - Directory with repository (parent directory for .git folder)
Example:
<program> C:/Projects/kotlin
""".trimIndent())

return
}

val settings = Settings(
repoPath = args[0]
)

doReduce(settings)
}

private data class UpdatePair(val from: String, val to: String)

fun doReduce(settings: Settings) {
val root = File(settings.repoPath)
if (!root.exists() || !root.isDirectory) {
System.err.println("Repository directory with branch is expected")
}

val (base, rules) = readUpdatePairsFromFile(settings.repoPath) ?: return
if (rules.isEmpty()) {
return
}

val extensions = rules.map { it.last() }.toSet() + base

val filesWithDonorExtensions = root
.walkTopDown()
.onEnter { dir -> !(isGitDir(dir) || isGradleBuildDir(dir) || isGradleDir(dir)) }
.filter { child -> child.extension in extensions }
.toList()

val affectedOriginFiles: Set<File> =
filesWithDonorExtensions.mapTo(HashSet(), { child -> File(child.parentFile, child.nameWithoutExtension) })

for (affectedOriginFile in affectedOriginFiles) {
val contentMap: Map<String, String?> = extensions.map { extension ->
val file = if (extension == base) affectedOriginFile else affectedOriginFile.toBunchFile(extension)
val content: String? = if (file.exists()) file.readText().replace(Regex("\\s*", RegexOption.MULTILINE), "") else null
extension to content
}.toMap()

val checkedPairs = HashSet<UpdatePair>()
for (rule in rules) {
var fromExtension = rule.first()

for (toExtension in rule.drop(1)) {
if (!checkedPairs.add(UpdatePair(fromExtension, toExtension))) continue

val fromContent = contentMap[fromExtension] ?: continue
val toContent = contentMap[toExtension] ?: continue

if (toContent == fromContent) {
println(affectedOriginFile.toBunchFile(toExtension))
}

fromExtension = toExtension
}
}
}
}



6 changes: 3 additions & 3 deletions src/main/kotlin/nk/patchsets/git/restore-branch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private fun doRestore(suffixes: List<String>, settings: Settings) {
return
}

val branchCopyFile = originFile.toPatchFile(originBranchExtension)
val branchCopyFile = originFile.toBunchFile(originBranchExtension)

if (branchCopyFile.exists()) {
System.err.println("Can't store copy of the origin file, because branch file is already exist: ${branchCopyFile}")
Expand All @@ -118,7 +118,7 @@ private fun doRestore(suffixes: List<String>, settings: Settings) {

val targetFile = donorExtensionsPrioritized
.asSequence()
.map { extension -> originFile.toPatchFile(extension) }
.map { extension -> originFile.toBunchFile(extension) }
.first { it.exists() }

if (targetFile.isDirectory) {
Expand Down Expand Up @@ -148,7 +148,7 @@ private fun doRestore(suffixes: List<String>, settings: Settings) {
commitChanges(settings.repoPath, changedFiles, settings.commitTitle.replace("{target}", suffixes.last()))
}

private fun File.toPatchFile(extension: String) = File(parentFile, "$name.$extension")
fun File.toBunchFile(extension: String) = File(parentFile, "$name.$extension")

fun getRuleSuffixes(settings: Settings): List<String> {
val suffixes = settings.rule.split("_")
Expand Down

0 comments on commit 0294fab

Please sign in to comment.