Fixed fetch command not fetching every remote if one of them has failed.
Also removed a weird 4s delay that was left previously, probably due to some old dev code
This commit is contained in:
parent
9a7cc6a988
commit
d6e5e22aa0
@ -0,0 +1,3 @@
|
|||||||
|
package com.jetpackduba.gitnuro.exceptions
|
||||||
|
|
||||||
|
class FetchException(msg: String): GitnuroException(msg)
|
@ -1,48 +1,63 @@
|
|||||||
package com.jetpackduba.gitnuro.git.remote_operations
|
package com.jetpackduba.gitnuro.git.remote_operations
|
||||||
|
|
||||||
|
import com.jetpackduba.gitnuro.exceptions.FetchException
|
||||||
|
import com.jetpackduba.gitnuro.exceptions.GitnuroException
|
||||||
|
import com.jetpackduba.gitnuro.logging.printError
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
import org.eclipse.jgit.lib.ProgressMonitor
|
import org.eclipse.jgit.lib.ProgressMonitor
|
||||||
import org.eclipse.jgit.transport.CredentialsProvider
|
import org.eclipse.jgit.transport.CredentialsProvider
|
||||||
import org.eclipse.jgit.transport.RefSpec
|
import org.eclipse.jgit.transport.RemoteConfig
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
private const val TAG = "FetchAllBranchesUseCase"
|
||||||
|
|
||||||
class FetchAllBranchesUseCase @Inject constructor(
|
class FetchAllBranchesUseCase @Inject constructor(
|
||||||
private val handleTransportUseCase: HandleTransportUseCase,
|
private val handleTransportUseCase: HandleTransportUseCase,
|
||||||
) {
|
) {
|
||||||
suspend operator fun invoke(git: Git) = withContext(Dispatchers.IO) {
|
suspend operator fun invoke(git: Git) = withContext(Dispatchers.IO) {
|
||||||
val remotes = git.remoteList().call()
|
val remotes = git.remoteList().call()
|
||||||
|
val errors = mutableListOf<Pair<RemoteConfig, Exception>>()
|
||||||
delay(4000)
|
|
||||||
|
|
||||||
for (remote in remotes) {
|
for (remote in remotes) {
|
||||||
val refSpecs = remote.fetchRefSpecs.ifEmpty {
|
try {
|
||||||
listOf(RefSpec("refs/heads/*:refs/remotes/${remote.name}/*"))
|
git.fetch()
|
||||||
|
.setRemote(remote.name)
|
||||||
|
.setRefSpecs(remote.fetchRefSpecs)
|
||||||
|
.setRemoveDeletedRefs(true)
|
||||||
|
.setTransportConfigCallback { handleTransportUseCase(it, git) }
|
||||||
|
.setCredentialsProvider(CredentialsProvider.getDefault())
|
||||||
|
.setProgressMonitor(object : ProgressMonitor {
|
||||||
|
override fun start(totalTasks: Int) {}
|
||||||
|
|
||||||
|
override fun beginTask(title: String?, totalWork: Int) {}
|
||||||
|
|
||||||
|
override fun update(completed: Int) {}
|
||||||
|
|
||||||
|
override fun endTask() {}
|
||||||
|
|
||||||
|
override fun isCancelled(): Boolean = isActive
|
||||||
|
|
||||||
|
override fun showDuration(enabled: Boolean) {}
|
||||||
|
})
|
||||||
|
.call()
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
printError(TAG, "Fetch failed for remote ${remote.name} with error ${ex.message}", ex)
|
||||||
|
|
||||||
|
if(ex.message != "Cancelled authentication" && ex !is CancellationException) {
|
||||||
|
errors.add(remote to ex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(errors.isNotEmpty()) {
|
||||||
|
val errorText = errors.joinToString("\n") {
|
||||||
|
"Fetch failed for remote ${it.first.name}: ${it.second.message}"
|
||||||
}
|
}
|
||||||
|
|
||||||
git.fetch()
|
throw FetchException(errorText)
|
||||||
.setRemote(remote.name)
|
|
||||||
.setRefSpecs(refSpecs)
|
|
||||||
.setRemoveDeletedRefs(true)
|
|
||||||
.setTransportConfigCallback { handleTransportUseCase(it, git) }
|
|
||||||
.setCredentialsProvider(CredentialsProvider.getDefault())
|
|
||||||
.setProgressMonitor(object : ProgressMonitor {
|
|
||||||
override fun start(totalTasks: Int) {}
|
|
||||||
|
|
||||||
override fun beginTask(title: String?, totalWork: Int) {}
|
|
||||||
|
|
||||||
override fun update(completed: Int) {}
|
|
||||||
|
|
||||||
override fun endTask() {}
|
|
||||||
|
|
||||||
override fun isCancelled(): Boolean = isActive
|
|
||||||
|
|
||||||
override fun showDuration(enabled: Boolean) {}
|
|
||||||
})
|
|
||||||
.call()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user