Skip to content

Commit

Permalink
Teach cleanup remove files with one particular extension
Browse files Browse the repository at this point in the history
  • Loading branch information
goodwinnk committed Apr 13, 2018
1 parent 72c4f40 commit 8192a6e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 44 additions & 23 deletions src/main/kotlin/nk/patchsets/git/cleanup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,31 @@ import nk.patchsets.git.restore.isGitDir
import nk.patchsets.git.restore.isGradleDir
import java.io.File

const val DEFAULT_CLEANUP_COMMIT_TITLE = "~~~~ cleanup ~~~~"
const val EXT_PATTERN = "{ext}"
const val DEFAULT_CLEANUP_COMMIT_TITLE = "~~~~ cleanup$EXT_PATTERN ~~~~"
const val NO_COMMIT_ = "--no-commit"
const val EXT_ = "-ext="

data class Settings(val repoPath: String, val commitTitle: String? = DEFAULT_CLEANUP_COMMIT_TITLE, val isNoCommit: Boolean)
data class Settings(
val repoPath: String,
val extension: String?,
val commitTitle: String? = DEFAULT_CLEANUP_COMMIT_TITLE,
val isNoCommit: Boolean)

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

fun cleanup(args: Array<String>) {
if (args.size != 1 && args.size != 2) {
if (!(args.size in 1..3)) {
System.err.println("""
Usage: <git-path> <commit-title>? $NO_COMMIT_?
Usage: <git-path> ($EXT_<file-extension>)? (<commit-title>|$NO_COMMIT_)?
<git-path> - Directory with repository (parent directory for .git folder).
<commit-title> - Title for cleanup commit. "$DEFAULT_CLEANUP_COMMIT_TITLE" is used by default.
$NO_COMMIT_ - option to cancel cleanup commit.
<git-path> - Directory with repository (parent directory for .git folder).
$EXT_<file-extension> - Particular extension for remove.
All files with extensions found in .bunch file will be removed if not set.
<commit-title> - Title for cleanup commit. "$DEFAULT_CLEANUP_COMMIT_TITLE" is used by default.
$NO_COMMIT_ - option to cancel cleanup commit.
Example:
<program> C:/Projects/kotlin $NO_COMMIT_
Expand All @@ -33,32 +41,43 @@ fun cleanup(args: Array<String>) {
return
}

val isNoCommit = args.size == 2 && args[1] == NO_COMMIT_
val settings = if (!isNoCommit) {
Settings(
repoPath = args[0],
commitTitle = args.getOrNull(1) ?: DEFAULT_CLEANUP_COMMIT_TITLE,
isNoCommit = false
)
} else {
Settings(
repoPath = args[0],
commitTitle = null,
isNoCommit = true
)
val repoPath = args[0]
val extension = args.getOrNull(1)?.takeIf { it.startsWith(EXT_) }?.substringAfter(EXT_)
val commitIndex = if (extension == null) 1 else 2

val commitTitleOrNoCommit = args.getOrNull(commitIndex)
if (commitIndex == 1 && args.size == 3) {
System.err.println("Unknown parameter: '${args[2]}'")
return
}

val isNoCommit = commitTitleOrNoCommit == NO_COMMIT_
val commitTitle = if (!isNoCommit) commitTitleOrNoCommit ?: DEFAULT_CLEANUP_COMMIT_TITLE else null

val settings = Settings(
repoPath = repoPath,
extension = extension,
commitTitle = commitTitle,
isNoCommit = isNoCommit
)

cleanup(settings)
}

fun cleanup(settings: Settings) {
val extensions = readExtensionFromFile(settings.repoPath)?.toSet() ?: return
val extensions = if (settings.extension != null) {
setOf(settings.extension)
} else {
readExtensionFromFile(settings.repoPath)?.toSet()
}?.map { ".$it" }

if (extensions == null) return

val root = File(settings.repoPath)
val filesWithExtensions = root
.walkTopDown()
.onEnter { dir -> !(isGitDir(dir) || isGradleDir(dir)) }
.filter { child -> child.extension in extensions }
.filter { child -> extensions.any { child.name.endsWith(it) } }
.toList()

val changedFiles = ArrayList<FileChange>()
Expand All @@ -73,6 +92,8 @@ fun cleanup(settings: Settings) {
}

if (!settings.isNoCommit) {
commitChanges(settings.repoPath, changedFiles, settings.commitTitle!!)
val extValue = if (settings.extension != null) " ${settings.extension}" else ""
val commitTitle = settings.commitTitle!!.replace(EXT_PATTERN, extValue)
commitChanges(settings.repoPath, changedFiles, commitTitle)
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/nk/patchsets/git/restore-branch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ fun restore(args: Array<String>) {
}

if (settings.doCleanup) {
cleanup(nk.patchsets.git.cleanup.Settings(settings.repoPath, RESTORE_CLEANUP_COMMIT_TITLE, false))
cleanup(nk.patchsets.git.cleanup.Settings(
settings.repoPath, extension = null, commitTitle = RESTORE_CLEANUP_COMMIT_TITLE, isNoCommit = false))
}
}

Expand Down

0 comments on commit 8192a6e

Please sign in to comment.