Skip to content

Commit

Permalink
(#10) Improve tracking of changes in hooks folder
Browse files Browse the repository at this point in the history
  • Loading branch information
y0ung3r committed Feb 20, 2024
1 parent 4340079 commit f060d54
Show file tree
Hide file tree
Showing 26 changed files with 660 additions and 390 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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

0 comments on commit f060d54

Please sign in to comment.