Simplify migration logic and fix tests

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-08-26 18:46:14 +05:30
parent 7a3bd134a0
commit 1c4c4ba5fb
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 4 additions and 15 deletions

View file

@ -62,7 +62,7 @@ class MigrationsTest {
checkOldKeysAreRemoved(context)
assertEquals(
context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL),
"msfjarvis@192.168.0.102:/mnt/disk3/pass-repo"
"ssh://msfjarvis@192.168.0.102:22/mnt/disk3/pass-repo"
)
}

View file

@ -30,24 +30,13 @@ private fun migrateToGitUrlBasedConfig(context: Context) {
val serverPath = context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_LOCATION) ?: ""
val protocol = Protocol.fromString(context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_PROTOCOL))
// Whether we need the leading ssh:// depends on the use of a custom port.
val hostnamePart = serverHostname.removePrefix("ssh://")
val url = when (protocol) {
Protocol.Ssh -> {
val portPart = if (serverPort.isEmpty()) "22" else serverPort
val userPart = if (serverUser.isEmpty()) "" else "${serverUser.trimEnd('@')}@"
val portPart =
if (serverPort == "22" || serverPort.isEmpty()) "" else ":$serverPort"
if (portPart.isEmpty()) {
"$userPart$hostnamePart:$serverPath"
} else {
// Only absolute paths are supported with custom ports.
if (!serverPath.startsWith('/'))
null
else
// We have to specify the ssh scheme as this is the only way to pass a custom
// port.
"ssh://$userPart$hostnamePart$portPart$serverPath"
}
val serverPart = if (serverPath.startsWith('/')) serverPath else "/$serverPath"
"ssh://$userPart$hostnamePart:$portPart$serverPart"
}
Protocol.Https -> {
val portPart =