Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#10) Add change tracking in the hooks folder #31

Draft
wants to merge 5 commits into
base: v0.1.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

# Changelog

## [Unreleased]
## [0.1.0]
### Added
- Detect changes in the Git hooks folder

### Changed
- Make the main tool window immediately available during project reindexing

## [0.0.2]
### Fixed
Expand All @@ -13,4 +18,5 @@
- Git version handling

## [0.0.1]
### Added
- Initial release
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ repositories {
// Dependencies are managed with Gradle version catalog - read more: https://docs.gradle.org/current/userguide/platforms.html#sub:version-catalog
dependencies {
// implementation(libs.annotations)
implementation(libs.rxkotlin)
}

// Set the JVM language level used to build the project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
# libraries
annotations = "24.1.0"
rxkotlin = "3.0.1"

# plugins
kotlin = "1.9.21"
Expand All @@ -11,6 +12,7 @@ kover = "0.7.5"

[libraries]
annotations = { group = "org.jetbrains", name = "annotations", version.ref = "annotations" }
rxkotlin = { group = "io.reactivex.rxjava3", name = "rxkotlin", version.ref = "rxkotlin" }

[plugins]
changelog = { id = "org.jetbrains.changelog", version.ref = "changelog" }
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.y0ung3r.gitglobalhookslocator.git.Git
import com.github.y0ung3r.gitglobalhookslocator.git.GitResponse
import com.github.y0ung3r.gitglobalhookslocator.git.cli.CliResponse
import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.GitCommandNotFoundException
import com.github.y0ung3r.gitglobalhookslocator.git.utils.SystemPathUtils
import com.github.y0ung3r.gitglobalhookslocator.utils.SystemPathUtils
import java.nio.file.Path

private const val SLASHES_PATTERN = "[/\\\\]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package com.github.y0ung3r.gitglobalhookslocator.git
package com.github.y0ung3r.gitglobalhookslocator.git.hooks

import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.HookNotFoundException
import com.github.y0ung3r.gitglobalhookslocator.git.hooks.exceptions.HookNotFoundException
import java.io.File
import java.nio.file.Path

class HookEntry(private var file: File) {
class HookEntry private constructor(private var file: File) {
companion object {
@JvmStatic
fun load(filePath: Path): HookEntry {
val file = filePath.toFile()

if (!file.exists()) {
throw HookNotFoundException(file.name.toString())
throw HookNotFoundException(file.nameWithoutExtension)
}

return HookEntry(file)
}
}

val name: String
= HooksFolder
.supportedHooks
.first { file.nameWithoutExtension.contains(it) }
val name: HookName
= HookName.create(file)

fun isDisabled()
= HooksFolder
= HookName
.supportedHooks
.all { it != file.nameWithoutExtension }

Expand All @@ -33,7 +31,7 @@ class HookEntry(private var file: File) {
return
}

renameFile(name)
renameFile(name.value)
}

fun disable() {
Expand All @@ -48,9 +46,8 @@ class HookEntry(private var file: File) {
file = Path
.of(file.parent, newName)
.toFile()
.let {
file.renameTo(it)
it
.apply {
file.renameTo(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.y0ung3r.gitglobalhookslocator.git.hooks

import com.github.y0ung3r.gitglobalhookslocator.git.hooks.exceptions.UnsupportedHookNameException
import gnu.trove.Equality
import java.io.File
import java.nio.file.Path
import kotlin.io.path.nameWithoutExtension

class HookName private constructor(private val source: String) : Comparable<HookName> {
companion object {
@JvmStatic
val supportedHooks = 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"
)

@JvmStatic
fun isSupportedHook(hookPath: Path): Boolean
= supportedHooks.any {
hookPath.nameWithoutExtension.contains(it) }

@JvmStatic
fun create(file: File): HookName
= HookName(file.nameWithoutExtension)

@JvmStatic
fun create(path: Path): HookName
= HookName(path.nameWithoutExtension)
}

val value: String
= supportedHooks
.firstOrNull { source.contains(it) }
?: throw UnsupportedHookNameException(source)

override fun compareTo(other: HookName): Int
= other.value.compareTo(value)

override fun equals(other: Any?): Boolean {
val name = other as? HookName

return when {
name == null -> false
compareTo(name) == 0 -> true
else -> false
}
}

override fun hashCode(): Int
= value.hashCode()
}
Loading
Loading