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.
(#10) Add a draft version of the change tracking
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HookEvent.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,8 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import java.nio.file.Path | ||
import java.nio.file.WatchEvent | ||
|
||
data class HookEvent( | ||
val kind: WatchEvent.Kind<Path>, | ||
val entry: HookEntry) |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HooksFolderVisitor.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,15 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import java.nio.file.* | ||
import java.nio.file.attribute.BasicFileAttributes | ||
|
||
class HooksFolderVisitor(private val watchService: WatchService) : SimpleFileVisitor<Path>() { | ||
override fun preVisitDirectory(directory: Path, attributes: BasicFileAttributes): FileVisitResult { | ||
directory.register( | ||
watchService, | ||
StandardWatchEventKinds.ENTRY_CREATE, | ||
StandardWatchEventKinds.ENTRY_DELETE) | ||
|
||
return FileVisitResult.CONTINUE | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/ObservableHooksFolder.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,74 @@ | ||
package com.github.y0ung3r.gitglobalhookslocator.git | ||
|
||
import com.github.y0ung3r.gitglobalhookslocator.git.extensions.getGlobalHooksPath | ||
import com.intellij.openapi.diagnostic.thisLogger | ||
import io.reactivex.rxjava3.subjects.PublishSubject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import java.nio.file.* | ||
import kotlin.io.path.nameWithoutExtension | ||
|
||
class ObservableHooksFolder(git: Git) { | ||
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" | ||
) | ||
} | ||
|
||
private var count: Int = 0 | ||
val hooks: PublishSubject<HookEvent> | ||
val path: Path | ||
|
||
init { | ||
path = git.getGlobalHooksPath() | ||
|
||
val files = try { | ||
Files.list(path) | ||
} | ||
catch (exception: NoSuchFileException) { | ||
thisLogger() | ||
.info("Provided hooks path is not valid", exception) | ||
|
||
emptyList<Path>() | ||
.stream() | ||
} | ||
|
||
hooks = PublishSubject.create() | ||
|
||
files | ||
.filter { | ||
supportedHooks.any { hookName -> it.fileName.nameWithoutExtension.contains(hookName) } | ||
} | ||
.map { HookEvent(StandardWatchEventKinds.ENTRY_CREATE, HookEntry.load(it)) } | ||
.forEach { hooks.onNext(it) } | ||
|
||
val watchService = FileSystems.getDefault().newWatchService() | ||
Files.walkFileTree(path, HooksFolderVisitor(watchService)) | ||
|
||
val changesTrackingJob = CoroutineScope(Dispatchers.IO).launch { | ||
while (true) { | ||
val folderEntry = watchService.take() | ||
|
||
folderEntry.pollEvents().forEach { | ||
val hookPath = Path.of(path.toString(), it.context().toString()) | ||
val hookEntry = HookEntry.load(hookPath) | ||
hooks.onNext(HookEvent(it.kind() as WatchEvent.Kind<Path>, hookEntry)) | ||
} | ||
} | ||
} | ||
} | ||
} |
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