Skip to content

Commit

Permalink
Fixed crash when watch init has failed
Browse files Browse the repository at this point in the history
  • Loading branch information
JetpackDuba committed Oct 22, 2024
1 parent 1ef596a commit 101d0fe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.jetpackduba.gitnuro.exceptions

class WatcherInitException(
code: Int,
message: String = codeToMessage(code),
) : GitnuroException(message)

private fun codeToMessage(code: Int): String {
fun codeToMessage(code: Int): String {
return when (code) {
1 /*is WatcherInitException.Generic*/, 2 /*is WatcherInitException.Io*/ -> "Could not watch directory. Check if it exists and you have read permissions."
3 /*is WatcherInitException.PathNotFound*/ -> "Path not found, check if your repository still exists"
Expand Down
16 changes: 11 additions & 5 deletions src/main/kotlin/com/jetpackduba/gitnuro/git/FileChangesWatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.jetpackduba.gitnuro.git
import FileWatcher
import WatchDirectoryNotifier
import com.jetpackduba.gitnuro.di.TabScope
import com.jetpackduba.gitnuro.exceptions.WatcherInitException
import com.jetpackduba.gitnuro.git.workspace.GetIgnoreRulesUseCase
import com.jetpackduba.gitnuro.system.systemSeparator
import kotlinx.coroutines.CoroutineScope
Expand All @@ -25,8 +24,8 @@ class FileChangesWatcher @Inject constructor(
private val getIgnoreRulesUseCase: GetIgnoreRulesUseCase,
private val tabScope: CoroutineScope,
) : AutoCloseable {
private val _changesNotifier = MutableSharedFlow<Boolean>()
val changesNotifier: SharedFlow<Boolean> = _changesNotifier
private val _changesNotifier = MutableSharedFlow<WatcherEvent>()
val changesNotifier: SharedFlow<WatcherEvent> = _changesNotifier
private val fileWatcher = FileWatcher.new()
private var shouldKeepLooping = true

Expand Down Expand Up @@ -71,14 +70,16 @@ class FileChangesWatcher @Inject constructor(

if (!areAllPathsIgnored) {
println("Emitting changes $hasGitIgnoreChanged")
_changesNotifier.emit(hasGitDirChanged)
_changesNotifier.emit(WatcherEvent.RepositoryChanged(hasGitDirChanged))
}

}
}

override fun onError(code: Int) {
throw WatcherInitException(code)
tabScope.launch {
_changesNotifier.emit(WatcherEvent.WatchInitError(code))
}
}
}

Expand All @@ -90,4 +91,9 @@ class FileChangesWatcher @Inject constructor(
fileWatcher.close()
}

}

sealed interface WatcherEvent {
data class RepositoryChanged(val hasGitDirChanged: Boolean) : WatcherEvent
data class WatchInitError(val code: Int) : WatcherEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.jetpackduba.gitnuro.viewmodels

import com.jetpackduba.gitnuro.SharedRepositoryStateManager
import com.jetpackduba.gitnuro.TaskType
import com.jetpackduba.gitnuro.exceptions.WatcherInitException
import com.jetpackduba.gitnuro.exceptions.codeToMessage
import com.jetpackduba.gitnuro.git.*
import com.jetpackduba.gitnuro.git.branches.CreateBranchUseCase
import com.jetpackduba.gitnuro.git.rebase.RebaseInteractiveState
Expand Down Expand Up @@ -110,6 +110,8 @@ class RepositoryOpenViewModel @Inject constructor(
var authorViewModel: AuthorViewModel? = null
private set

private var hasGitDirChanged = false

init {
tabScope.run {
launch {
Expand All @@ -119,7 +121,7 @@ class RepositoryOpenViewModel @Inject constructor(
}

launch {
watchRepositoryChanges(tabState.git)
watchRepositoryChanges()
}
}
}
Expand Down Expand Up @@ -158,55 +160,53 @@ class RepositoryOpenViewModel @Inject constructor(
* the app by constantly running "git status" or even full refreshes.
*
*/
private suspend fun watchRepositoryChanges(git: Git) = tabScope.launch(Dispatchers.IO) {
var hasGitDirChanged = false

private suspend fun watchRepositoryChanges() = tabScope.launch(Dispatchers.IO) {
launch {
fileChangesWatcher.changesNotifier.collect { latestUpdateChangedGitDir ->
val isOperationRunning = tabState.operationRunning
fileChangesWatcher.changesNotifier.collect { watcherEvent ->
when (watcherEvent) {
is WatcherEvent.RepositoryChanged -> repositoryChanged(watcherEvent.hasGitDirChanged)
is WatcherEvent.WatchInitError -> {
val message = codeToMessage(watcherEvent.code)
errorsManager.addError(
newErrorNow(
exception = Exception(message),
taskType = TaskType.CHANGES_DETECTION,
),
)

if (!isOperationRunning) { // Only update if there isn't any process running
printDebug(TAG, "Detected changes in the repository's directory")
}
}
}
}
}

val currentTimeMillis = System.currentTimeMillis()
private suspend fun CoroutineScope.repositoryChanged(hasGitDirChanged: Boolean) {
val isOperationRunning = tabState.operationRunning

if (
latestUpdateChangedGitDir &&
currentTimeMillis - tabState.lastOperation < MIN_TIME_AFTER_GIT_OPERATION
) {
printDebug(TAG, "Git operation was executed recently, ignoring file system change")
return@collect
}
if (!isOperationRunning) { // Only update if there isn't any process running
printDebug(TAG, "Detected changes in the repository's directory")

if (latestUpdateChangedGitDir) {
hasGitDirChanged = true
}
val currentTimeMillis = System.currentTimeMillis()

if (isActive) {
updateApp(hasGitDirChanged)
}
if (
hasGitDirChanged &&
currentTimeMillis - tabState.lastOperation < MIN_TIME_AFTER_GIT_OPERATION
) {
printDebug(TAG, "Git operation was executed recently, ignoring file system change")
return
}

hasGitDirChanged = false
} else {
printDebug(TAG, "Ignored file events during operation")
}
if (hasGitDirChanged) {
this@RepositoryOpenViewModel.hasGitDirChanged = true
}
}

try {
fileChangesWatcher.watchDirectoryPath(
repository = git.repository,
)
} catch (ex: WatcherInitException) {
val message = ex.message
if (message != null) {
errorsManager.addError(
newErrorNow(
exception = ex,
taskType = TaskType.CHANGES_DETECTION,
),
)
if (isActive) {
updateApp(hasGitDirChanged)
}

this@RepositoryOpenViewModel.hasGitDirChanged = false
} else {
printDebug(TAG, "Ignored file events during operation")
}
}

Expand Down

0 comments on commit 101d0fe

Please sign in to comment.