generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from y0ung3r/f/initial
Release v0.0.1
- Loading branch information
Showing
43 changed files
with
945 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
.idea | ||
.qodana | ||
build | ||
src/test/testData/hooks | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/Git.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import com.github.y0ung3r.gitglobalhookslocator.git.cli.DefaultCliCommandExecutor | ||
import com.github.y0ung3r.gitglobalhookslocator.git.cli.NotFoundCliResponse | ||
import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.GitCommandNotFoundException | ||
import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.GitIsNotInstalledException | ||
import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.GitVersionIsNotSupportedException | ||
import com.github.y0ung3r.gitglobalhookslocator.git.extensions.toGitResponse | ||
import com.github.y0ung3r.gitglobalhookslocator.git.cli.interfaces.CliCommandExecutor | ||
|
||
class Git(private val commandExecutor: CliCommandExecutor) { | ||
companion object { | ||
private const val GIT_COMMAND = "git" | ||
private const val GIT_VERSION_COMMAND = "version" | ||
const val GIT_CONFIG_COMMAND = "config" | ||
const val GIT_GLOBAL_COMMAND = "--global" | ||
const val GIT_CONFIG_GET_COMMAND = "--get" | ||
const val GIT_HOOKS_PATH_SECTION = "core.hooksPath" | ||
|
||
@JvmStatic | ||
val minRequiredVersion = SemanticVersion(2, 9, 0) | ||
|
||
@JvmStatic | ||
val instance = Git(DefaultCliCommandExecutor()) | ||
} | ||
|
||
init { | ||
if (getInstalledVersion() < minRequiredVersion) { | ||
throw GitVersionIsNotSupportedException() | ||
} | ||
} | ||
|
||
fun executeCommand(vararg params: String): GitResponse { | ||
val processBuilder = ProcessBuilder( | ||
ArrayList<String>().apply { | ||
add(GIT_COMMAND) | ||
addAll(params) | ||
} | ||
) | ||
|
||
processBuilder.redirectErrorStream(true) | ||
|
||
return when (val response = commandExecutor.execute(processBuilder)) { | ||
is NotFoundCliResponse -> throw GitCommandNotFoundException(*params) | ||
else -> response.toGitResponse() | ||
} | ||
} | ||
|
||
fun getInstalledVersion() : SemanticVersion { | ||
val installedVersion = executeCommand(GIT_VERSION_COMMAND) | ||
|
||
return when { | ||
!installedVersion.isEmpty() -> SemanticVersion.parse(installedVersion.value) | ||
else -> throw GitIsNotInstalledException() | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/GitResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import com.github.y0ung3r.gitglobalhookslocator.git.cli.CliResponse | ||
import com.github.y0ung3r.gitglobalhookslocator.git.cli.EmptyCliResponse | ||
|
||
class GitResponse(private val cliResponse: CliResponse) { | ||
val value = cliResponse.value | ||
fun isEmpty() = cliResponse is EmptyCliResponse | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HookEntry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.HookNotFoundException | ||
import java.io.File | ||
import java.nio.file.Path | ||
|
||
class HookEntry(private var file: File) { | ||
companion object { | ||
@JvmStatic | ||
fun load(filePath: Path): HookEntry { | ||
val file = filePath.toFile() | ||
|
||
if (!file.exists()) { | ||
throw HookNotFoundException(file.name.toString()) | ||
} | ||
|
||
return HookEntry(file) | ||
} | ||
} | ||
|
||
val name: String | ||
= HooksFolder | ||
.availableHooks | ||
.first { file.nameWithoutExtension.contains(it) } | ||
|
||
fun isDisabled() | ||
= HooksFolder | ||
.availableHooks | ||
.all { it != file.nameWithoutExtension } | ||
|
||
fun enable() { | ||
if (!isDisabled()) { | ||
return | ||
} | ||
|
||
renameFile(name) | ||
} | ||
|
||
fun disable() { | ||
if (isDisabled()) { | ||
return | ||
} | ||
|
||
renameFile("_${file.name}") | ||
} | ||
|
||
private fun renameFile(newName: String) { | ||
file = Path | ||
.of(file.parent, newName) | ||
.toFile() | ||
.let { | ||
file.renameTo(it) | ||
it | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HooksFolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import com.github.y0ung3r.gitglobalhookslocator.git.extensions.getGlobalHooksPath | ||
import com.intellij.openapi.diagnostic.thisLogger | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import kotlin.io.path.nameWithoutExtension | ||
import java.nio.file.NoSuchFileException | ||
|
||
class HooksFolder(git: Git) { | ||
companion object { | ||
@JvmStatic | ||
val availableHooks = arrayOf( | ||
"pre-commit", | ||
"prepare-commit-msg", | ||
"commit-msg", | ||
"post-commit", | ||
"applypatch-msg", | ||
"pre-applypatch", | ||
"post-applypatch", | ||
"pre-rebase", | ||
"post-rewrite", | ||
"post-checkout", | ||
"post-merge", | ||
"pre-push", | ||
"pre-auto-gc" | ||
) | ||
} | ||
|
||
val hooks: List<HookEntry> | ||
val path: Path | ||
|
||
fun isEmpty(): Boolean | ||
= hooks.isEmpty() | ||
|
||
init { | ||
path = Path.of(git.getGlobalHooksPath()) | ||
|
||
val files = try { | ||
Files.list(path) | ||
} | ||
catch (exception: NoSuchFileException) { | ||
thisLogger() | ||
.info("Provided hooks path doesn't exists", exception) | ||
|
||
emptyList<Path>() | ||
.stream() | ||
} | ||
|
||
hooks = files | ||
.filter { | ||
availableHooks | ||
.any { hookName -> | ||
it.fileName.nameWithoutExtension.contains(hookName) } | ||
} | ||
.map { HookEntry.load(it) } | ||
.toList() | ||
} | ||
|
||
fun isAllDisabled() | ||
= hooks.all { it.isDisabled() } | ||
|
||
fun enableAll() | ||
= hooks.forEach { it.enable() } | ||
|
||
fun disableAll() | ||
= hooks.forEach { it.disable() } | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/SemanticVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.ProvidedSemanticVersionIsInvalidException | ||
|
||
class SemanticVersion(val major: Int, val minor: Int, val patch: Int) : Comparable<SemanticVersion> { | ||
Check notice on line 5 in src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/SemanticVersion.kt GitHub Actions / Qodana Community for JVMClass member can have 'private' visibility
Check notice on line 5 in src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/SemanticVersion.kt GitHub Actions / Qodana Community for JVMClass member can have 'private' visibility
|
||
companion object { | ||
private const val COMPONENTS_DELIMITER = '.' | ||
private const val VERSION_PATTERN = "(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)?" | ||
|
||
@JvmStatic | ||
fun parse(version: String): SemanticVersion { | ||
val match = Regex(VERSION_PATTERN).findAll(version).firstOrNull() | ||
?: throw ProvidedSemanticVersionIsInvalidException(version) | ||
|
||
return match.value.split(COMPONENTS_DELIMITER).let { | ||
SemanticVersion(it[0].toInt(), it[1].toInt(), it[2].toInt()) | ||
} | ||
} | ||
} | ||
|
||
override fun compareTo(other: SemanticVersion): Int = | ||
when { | ||
major > other.major -> 1 | ||
major < other.major -> -1 | ||
minor > other.minor -> 1 | ||
minor < other.minor -> -1 | ||
patch > other.patch -> 1 | ||
patch < other.patch -> -1 | ||
else -> 0 | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
val version = other as? SemanticVersion | ||
|
||
return when { | ||
version == null -> false | ||
compareTo(version) == 0 -> true | ||
else -> false | ||
} | ||
} | ||
|
||
override fun hashCode(): Int | ||
= major.hashCode() * 31 + minor.hashCode() * 31 + patch.hashCode() | ||
|
||
override fun toString(): String | ||
= "$major.$minor.$patch" | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/cli/CliResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git.cli | ||
|
||
open class CliResponse(value: String) { | ||
val value: String = value.trim() | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
...main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/cli/DefaultCliCommandExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git.cli | ||
|
||
import com.github.y0ung3r.gitglobalhookslocator.git.cli.interfaces.CliCommandExecutor | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
import java.nio.charset.StandardCharsets | ||
|
||
class DefaultCliCommandExecutor : CliCommandExecutor { | ||
private companion object { | ||
const val TERMINATION_EXIT_CODE = 0 | ||
} | ||
|
||
override fun execute(processBuilder: ProcessBuilder) : CliResponse { | ||
val process = processBuilder.start() | ||
val streamReader = InputStreamReader(process.inputStream, StandardCharsets.UTF_8) | ||
val bufferedReader = BufferedReader(streamReader) | ||
|
||
val response = StringBuilder() | ||
var line: String? = bufferedReader.readLine() | ||
|
||
while (line != null) { | ||
response.append(line) | ||
response.append(System.lineSeparator()) | ||
line = bufferedReader.readLine() | ||
} | ||
|
||
return when (process.waitFor()) { | ||
TERMINATION_EXIT_CODE -> CliResponse(response.toString()) | ||
else -> NotFoundCliResponse(response.toString()) | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/cli/EmptyCliResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git.cli | ||
|
||
class EmptyCliResponse | ||
: CliResponse("") |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/cli/NotFoundCliResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git.cli | ||
|
||
class NotFoundCliResponse(details: String) | ||
: CliResponse(details) |
Oops, something went wrong.