Skip to content

Commit 7277d40

Browse files
committed
LFS: Implemented SSH support and code cleanup
1 parent d4e5dae commit 7277d40

26 files changed

+969
-658
lines changed

src/main/kotlin/com/jetpackduba/gitnuro/App.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.compose.ui.unit.sp
2222
import androidx.compose.ui.window.Window
2323
import androidx.compose.ui.window.application
2424
import androidx.compose.ui.window.rememberWindowState
25+
import com.jetpackduba.gitnuro.credentials.SshProcess
2526
import com.jetpackduba.gitnuro.di.DaggerAppComponent
2627
import com.jetpackduba.gitnuro.extensions.preferenceValue
2728
import com.jetpackduba.gitnuro.extensions.toWindowPlacement
@@ -30,7 +31,7 @@ import com.jetpackduba.gitnuro.generated.resources.logo
3031
import com.jetpackduba.gitnuro.git.AppGpgSigner
3132
import com.jetpackduba.gitnuro.keybindings.KeybindingOption
3233
import com.jetpackduba.gitnuro.keybindings.matchesBinding
33-
import com.jetpackduba.gitnuro.lfs.GLfsFactory
34+
import com.jetpackduba.gitnuro.lfs.AppLfsFactory
3435
import com.jetpackduba.gitnuro.logging.printError
3536
import com.jetpackduba.gitnuro.managers.AppStateManager
3637
import com.jetpackduba.gitnuro.managers.TempFilesManager
@@ -49,11 +50,9 @@ import com.jetpackduba.gitnuro.ui.components.TabInformation
4950
import com.jetpackduba.gitnuro.ui.context_menu.AppPopupMenu
5051
import com.jetpackduba.gitnuro.ui.dialogs.settings.ProxyType
5152
import kotlinx.coroutines.launch
52-
import org.eclipse.jgit.lfs.BuiltinLFS
5353
import org.eclipse.jgit.lib.GpgConfig
54-
import org.eclipse.jgit.lib.Signer
55-
import org.eclipse.jgit.lib.SignerFactory
5654
import org.eclipse.jgit.lib.Signers
55+
import org.eclipse.jgit.transport.URIish
5756
import org.eclipse.jgit.util.LfsFactory
5857
import java.io.File
5958
import java.io.FileOutputStream
@@ -91,7 +90,7 @@ class App {
9190
lateinit var signer: AppGpgSigner
9291

9392
@Inject
94-
lateinit var gLfsFactory: GLfsFactory
93+
lateinit var lfsFactory: AppLfsFactory
9594

9695
init {
9796
appComponent.inject(this)
@@ -123,7 +122,7 @@ class App {
123122
}
124123

125124
tabsManager.loadPersistedTabs()
126-
gLfsFactory.register()
125+
LfsFactory.setInstance(lfsFactory)
127126

128127
if (dirToOpen != null)
129128
addDirTab(dirToOpen)
@@ -289,7 +288,6 @@ class App {
289288
modifier = Modifier
290289
.background(MaterialTheme.colors.background)
291290
.onPreviewKeyEvent {
292-
println(it.toString())
293291
when {
294292
it.matchesBinding(KeybindingOption.OPEN_NEW_TAB) -> {
295293
tabsManager.addNewEmptyTab()
@@ -316,6 +314,7 @@ class App {
316314
}
317315
true
318316
}
317+
319318
else -> false
320319
}
321320
}

src/main/kotlin/com/jetpackduba/gitnuro/AppConstants.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ data class License(
3838
)
3939

4040
data class Project(val name: String, val url: String, val license: License)
41+
42+
43+
object NetworkConstants {
44+
const val ACCEPT_HEADER = "Accept"
45+
const val AUTH_HEADER = "Authorization"
46+
}

src/main/kotlin/com/jetpackduba/gitnuro/credentials/CredentialsCacheRepository.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CredentialsCacheRepository @Inject constructor() {
3737
}
3838

3939
suspend fun cacheHttpCredentials(credentials: CredentialsCacheType.HttpCredentialsCache) {
40-
cacheHttpCredentials(credentials.url, credentials.userName, credentials.password, credentials.isLfs)
40+
cacheHttpCredentials(credentials.url, credentials.user, credentials.password, credentials.isLfs)
4141
}
4242

4343
suspend fun cacheHttpCredentials(url: String, userName: String, password: String, isLfs: Boolean) {
@@ -109,7 +109,7 @@ sealed interface CredentialsCacheType {
109109

110110
data class HttpCredentialsCache(
111111
val url: String,
112-
val userName: String,
112+
val user: String,
113113
val password: String,
114114
val isLfs: Boolean,
115115
) : CredentialsCacheType

src/main/kotlin/com/jetpackduba/gitnuro/credentials/CredentialsStateManager.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class CredentialsStateManager @Inject constructor() {
1717
val credentialsState: StateFlow<CredentialsState>
1818
get() = _credentialsState
1919

20-
val currentCredentialsState: CredentialsState
21-
get() = credentialsState.value
22-
2320
suspend fun requestHttpCredentials(): CredentialsAccepted.HttpCredentialsAccepted {
2421
return requestAwaitingCredentials(CredentialsRequest.HttpCredentialsRequest)
2522
}
@@ -58,7 +55,7 @@ class CredentialsStateManager @Inject constructor() {
5855

5956
private suspend inline fun <reified T : CredentialsAccepted> requestAwaitingCredentials(credentialsRequest: CredentialsRequest): T {
6057
mutex.withLock {
61-
assert(this.currentCredentialsState is CredentialsState.None)
58+
assert(this.credentialsState.value is CredentialsState.None)
6259

6360
_credentialsState.value = credentialsRequest
6461

@@ -88,7 +85,7 @@ sealed interface CredentialsAccepted : CredentialsState {
8885
data class LfsCredentialsAccepted(val user: String, val password: String) : CredentialsAccepted {
8986
companion object {
9087
fun fromCachedCredentials(credentials: CredentialsCacheType.HttpCredentialsCache): LfsCredentialsAccepted {
91-
return LfsCredentialsAccepted(credentials.userName, credentials.password)
88+
return LfsCredentialsAccepted(credentials.user, credentials.password)
9289
}
9390
}
9491
}

src/main/kotlin/com/jetpackduba/gitnuro/credentials/GSessionManager.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.jetpackduba.gitnuro.credentials
22

3-
import com.jetpackduba.gitnuro.git.remote_operations.CredentialsCache
43
import org.eclipse.jgit.transport.CredentialsProvider
54
import org.eclipse.jgit.transport.RemoteSession
65
import org.eclipse.jgit.transport.SshSessionFactory
@@ -10,10 +9,10 @@ import javax.inject.Inject
109
import javax.inject.Provider
1110

1211
class GSessionManager @Inject constructor(
13-
private val GSshSessionFactory: GSshSessionFactory,
12+
private val sshSessionFactory: GSshSessionFactory,
1413
) {
1514
fun generateSshSessionFactory(): GSshSessionFactory {
16-
return GSshSessionFactory
15+
return sshSessionFactory
1716
}
1817
}
1918

@@ -27,7 +26,7 @@ class GSshSessionFactory @Inject constructor(
2726
tms: Int,
2827
): RemoteSession {
2928
val remoteSession = sessionProvider.get()
30-
remoteSession.setup(uri, credentialsProvider as SshCredentialsProvider)
29+
remoteSession.setup(uri, credentialsProvider)
3130

3231
return remoteSession
3332
}

src/main/kotlin/com/jetpackduba/gitnuro/credentials/HttpCredentialsProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ class HttpCredentialsProvider @AssistedInject constructor(
9090
if (appSettingsRepository.cacheCredentialsInMemory) {
9191
credentialsCached = CredentialsCacheType.HttpCredentialsCache(
9292
url = uri.toString(),
93-
userName = credentials.user,
93+
user = credentials.user,
9494
password = credentials.password,
9595
isLfs = false,
9696
)
9797
}
9898

9999
return true
100100
} else {
101-
userItem.value = cachedCredentials.userName
101+
userItem.value = cachedCredentials.user
102102
passwordItem.value = cachedCredentials.password.toCharArray()
103103

104104
return true

src/main/kotlin/com/jetpackduba/gitnuro/credentials/SshRemoteSession.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ package com.jetpackduba.gitnuro.credentials
33
import Session
44
import com.jetpackduba.gitnuro.exceptions.SshException
55
import com.jetpackduba.gitnuro.extensions.throwIfSshMessage
6-
import kotlinx.coroutines.runBlocking
76
import org.eclipse.jgit.transport.CredentialItem
7+
import org.eclipse.jgit.transport.CredentialsProvider
88
import org.eclipse.jgit.transport.RemoteSession
99
import org.eclipse.jgit.transport.URIish
10-
import java.util.concurrent.CancellationException
1110
import javax.inject.Inject
1211

1312

1413
private const val NOT_EXPLICIT_PORT = -1
1514

16-
class SshRemoteSession @Inject constructor(
17-
private val credentialsStateManager: CredentialsStateManager,
18-
) : RemoteSession {
15+
class SshRemoteSession @Inject constructor() : RemoteSession {
1916
private lateinit var session: Session
2017
private lateinit var process: SshProcess
2118
override fun exec(commandName: String, timeout: Int): Process {
@@ -33,7 +30,7 @@ class SshRemoteSession @Inject constructor(
3330
session.disconnect()
3431
}
3532

36-
fun setup(uri: URIish, sshCredentialsProvider: SshCredentialsProvider) {
33+
fun setup(uri: URIish, sshCredentialsProvider: CredentialsProvider) {
3734
val session = Session.new()
3835
?: throw SshException("Could not obtain the session, this is likely a bug. Please file a report.")
3936

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.jetpackduba.gitnuro.extensions
2+
3+
inline fun <T1: Any, T2: Any, R: Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? {
4+
return if (p1 != null && p2 != null) block(p1, p2) else null
5+
}
6+
inline fun <T1: Any, T2: Any, T3: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3)->R?): R? {
7+
return if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null
8+
}
9+
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, block: (T1, T2, T3, T4)->R?): R? {
10+
return if (p1 != null && p2 != null && p3 != null && p4 != null) block(p1, p2, p3, p4) else null
11+
}
12+
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, T5: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, p5: T5?, block: (T1, T2, T3, T4, T5)->R?): R? {
13+
return if (p1 != null && p2 != null && p3 != null && p4 != null && p5 != null) block(p1, p2, p3, p4, p5) else null
14+
}

src/main/kotlin/com/jetpackduba/gitnuro/extensions/InputStreamExtensions.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,19 @@ fun InputStream.toByteArray(): ByteArray {
99
this.transferTo(byteArrayOutStream)
1010
byteArrayOutStream.toByteArray()
1111
}
12+
}
13+
14+
fun InputStream.readUntilValue(limitValue: Int): ByteArray {
15+
var value: Byte
16+
val bytesList = mutableListOf<Byte>()
17+
18+
do {
19+
value = this.read().toByte()
20+
21+
if (value.toInt() != limitValue) {
22+
bytesList.add(value)
23+
}
24+
} while (value.toInt() != limitValue)
25+
26+
return bytesList.toByteArray()
1227
}

src/main/kotlin/com/jetpackduba/gitnuro/extensions/StringExtensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ fun String.throwIfSshMessage() {
6666
if (this.isNotEmpty()) {
6767
throw SshException(this)
6868
}
69-
}
69+
}
70+
71+
fun String.isHttpOrHttps() = this.startsWith("http://") or this.startsWith("https://")

0 commit comments

Comments
 (0)