Skip to content

Commit

Permalink
feat: better invalid remote handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ViscousPot committed Jan 15, 2025
1 parent 2acae42 commit 6f222d5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
29 changes: 22 additions & 7 deletions app/src/main/java/com/viscouspot/gitsync/util/GitManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class GitManager(private val context: Context, private val settingsManager: Sett
setProgressMonitor(monitor)
setDirectory(File(Helper.getPathFromUri(context, userStorageUri)))
applyCredentials(this)
setRemote(settingsManager.getRemote())
}.call()

log(LogType.CloneRepo, "Repository cloned successfully")
Expand Down Expand Up @@ -183,7 +184,7 @@ class GitManager(private val context: Context, private val settingsManager: Sett
log(LogType.ForcePull, "Fetching changes")
git.fetch().apply {
applyCredentials(this)
setRemote("origin")
setRemote(settingsManager.getRemote())
setRefSpecs(RefSpec("+refs/heads/*:refs/remotes/origin/*"))
}.call()

Expand All @@ -202,6 +203,8 @@ class GitManager(private val context: Context, private val settingsManager: Sett
closeRepo(repo)

return true
} catch (e: InvalidRemoteException) {
handleInvalidRemoteException(e)
} catch (e: TransportException) {
handleTransportException(e) { }
} catch (e: Throwable) {
Expand All @@ -223,6 +226,7 @@ class GitManager(private val context: Context, private val settingsManager: Sett
log(LogType.PullFromRepo, "Fetching changes")
val fetchResult = git.fetch().apply {
applyCredentials(this)
setRemote(settingsManager.getRemote())
}.call()

if (conditionallyScheduleNetworkSync(scheduleNetworkSync)) {
Expand All @@ -239,7 +243,7 @@ class GitManager(private val context: Context, private val settingsManager: Sett
onSync.invoke()
val result = git.pull().apply {
applyCredentials(this)
remote = settingsManager.getRemote()
setRemote(settingsManager.getRemote())
}.call()

if (result.mergeResult.failingPaths != null && result.mergeResult.failingPaths.containsValue(
Expand Down Expand Up @@ -268,7 +272,7 @@ class GitManager(private val context: Context, private val settingsManager: Sett
} catch (e: CheckoutConflictException) {
log(LogType.PullFromRepo, e.stackTraceToString())
return false
}catch (e: ApiCheckoutConflictException) {
} catch (e: ApiCheckoutConflictException) {
log(LogType.PullFromRepo, e.stackTraceToString())
return false
} catch (e: WrongRepositoryStateException) {
Expand All @@ -278,6 +282,8 @@ class GitManager(private val context: Context, private val settingsManager: Sett
}
log(context, LogType.PullFromRepo, e)
return null
} catch (e: InvalidRemoteException) {
handleInvalidRemoteException(e)
} catch (e: TransportException) {
handleTransportException(e, scheduleNetworkSync)
} catch (e: Throwable) {
Expand Down Expand Up @@ -359,7 +365,7 @@ class GitManager(private val context: Context, private val settingsManager: Sett
git.push().apply {
applyCredentials(this)
setForce(true)
remote = "origin"
setRemote(settingsManager.getRemote())
}.call()

logStatus(git)
Expand All @@ -368,6 +374,8 @@ class GitManager(private val context: Context, private val settingsManager: Sett
closeRepo(repo)

return returnResult
} catch (e: InvalidRemoteException) {
handleInvalidRemoteException(e)
} catch (e: Throwable) {
log(context, LogType.PushToRepo, e)
}
Expand Down Expand Up @@ -441,7 +449,7 @@ class GitManager(private val context: Context, private val settingsManager: Sett
log(LogType.PushToRepo, "Pushing changes")
val pushResults = git.push().apply {
applyCredentials(this)
remote = settingsManager.getRemote()
setRemote(settingsManager.getRemote())
}.call()
for (pushResult in pushResults) {
for (remoteUpdate in pushResult.remoteUpdates) {
Expand Down Expand Up @@ -505,6 +513,8 @@ class GitManager(private val context: Context, private val settingsManager: Sett
closeRepo(repo)

return returnResult
} catch (e: InvalidRemoteException) {
handleInvalidRemoteException(e)
} catch (e: TransportException) {
handleTransportException(e, scheduleNetworkSync)
} catch (e: Throwable) {
Expand All @@ -521,6 +531,11 @@ class GitManager(private val context: Context, private val settingsManager: Sett
return false
}

private fun handleInvalidRemoteException(e: InvalidRemoteException) {
makeToast(context, context.getString(R.string.invalid_remote))
log(LogType.SyncException, e.stackTraceToString())
}

private fun handleTransportException(e: TransportException, scheduleNetworkSync: () -> Unit) {
if (listOf(
JGitText.get().connectionFailed,
Expand All @@ -540,11 +555,11 @@ class GitManager(private val context: Context, private val settingsManager: Sett
message = it
e.message.toString().contains(it)
}) {
log(context, LogType.TransportException, Throwable(message))
log(context, LogType.SyncException, Throwable(message))
return
}

log(context, LogType.TransportException, e)
log(context, LogType.SyncException, e)
}

private fun logStatus(git: Git) {
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/com/viscouspot/gitsync/util/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import android.net.NetworkCapabilities
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.text.Html
Expand All @@ -34,11 +36,6 @@ import com.viscouspot.gitsync.MainActivity
import com.viscouspot.gitsync.R
import com.viscouspot.gitsync.ui.dialog.BaseDialog
import com.viscouspot.gitsync.util.Logger.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.ByteArrayOutputStream
import java.io.File
Expand All @@ -53,7 +50,9 @@ object Helper {

fun makeToast(context: Context, message: String, length: Int = Toast.LENGTH_SHORT) {
if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) return
Toast.makeText(context, message, length).show()

val mainHandler = Handler(Looper.getMainLooper())
mainHandler.post { Toast.makeText(context, message, length).show() }
}

fun networkRequired(context: Context) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/viscouspot/gitsync/util/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ enum class LogType(val type: String) {
GitStatus("GitStatus"),
RecentCommits("RecentCommits"),

TransportException("TransportException"),
SyncException("SyncException"),

GithubOAuthFlow("GithubOAuthFlow"),
GithubAuthCredentials("GithubAuthCredentials"),
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<string name="select_email_client">Send email using: </string>

<string name="out_of_memory">Application ran out of memory!</string>
<string name="invalid_remote">Invalid remote!</string>
<string name="invalid_remote">Invalid remote! Modify this in settings</string>
<string name="large_file">Singular files larger than 50MB not supported!</string>
<string name="clone_failed">Failed to clone repository!</string>
<string name="inaccessible_directory_message">Inaccessible directory! Please select a different location.</string>
Expand Down

0 comments on commit 6f222d5

Please sign in to comment.