Skip to content

Commit

Permalink
Updated to JGit 7.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JetpackDuba committed Nov 18, 2024
1 parent 038603c commit becbb93
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 37 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ repositories {
}

dependencies {
val jgit = "6.9.0.202403050737-r"
val jgit = "7.0.0.202409031743-r"

if (currentOs() == OS.LINUX && isLinuxAarch64) {
implementation(compose.desktop.linux_arm64)
Expand All @@ -74,6 +74,7 @@ dependencies {
implementation("org.slf4j:slf4j-api:2.0.7")
implementation("org.slf4j:slf4j-reload4j:2.0.7")
implementation("androidx.datastore:datastore-preferences-core:1.0.0")
implementation("org.bouncycastle:bcpg-jdk18on:1.78.1")
}

fun currentOs(): OS {
Expand Down
15 changes: 9 additions & 6 deletions src/main/kotlin/com/jetpackduba/gitnuro/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ import com.jetpackduba.gitnuro.ui.components.TabInformation
import com.jetpackduba.gitnuro.ui.context_menu.AppPopupMenu
import com.jetpackduba.gitnuro.ui.dialogs.settings.ProxyType
import kotlinx.coroutines.launch
import org.eclipse.jgit.lib.GpgSigner
import org.eclipse.jgit.lib.GpgConfig
import org.eclipse.jgit.lib.Signer
import org.eclipse.jgit.lib.SignerFactory
import org.eclipse.jgit.lib.Signers
import java.io.File
import java.io.FileOutputStream
import java.net.Authenticator
Expand All @@ -67,9 +70,6 @@ class App {
@Inject
lateinit var appSettingsRepository: AppSettingsRepository

@Inject
lateinit var appGpgSigner: AppGpgSigner

@Inject
lateinit var appEnvInfo: AppEnvInfo

Expand All @@ -82,6 +82,9 @@ class App {
@Inject
lateinit var logging: Logging

@Inject
lateinit var signer: AppGpgSigner

init {
appComponent.inject(this)
}
Expand All @@ -94,6 +97,8 @@ class App {
logging.initLogging()
initProxySettings()

Signers.set(GpgConfig.GpgFormat.OPENPGP, signer)

val windowPlacement = appSettingsRepository.windowPlacement.toWindowPlacement
val dirToOpen = getDirToOpen(args)

Expand All @@ -111,8 +116,6 @@ class App {

tabsManager.loadPersistedTabs()

GpgSigner.setDefault(appGpgSigner)

if (dirToOpen != null)
addDirTab(dirToOpen)

Expand Down
65 changes: 36 additions & 29 deletions src/main/kotlin/com/jetpackduba/gitnuro/git/AppGpgSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,65 @@ import com.jetpackduba.gitnuro.credentials.GpgCredentialsProvider
import org.bouncycastle.openpgp.PGPException
import org.eclipse.jgit.api.errors.CanceledException
import org.eclipse.jgit.gpg.bc.internal.BouncyCastleGpgSigner
import org.eclipse.jgit.lib.CommitBuilder
import org.eclipse.jgit.lib.GpgConfig
import org.eclipse.jgit.lib.ObjectBuilder
import org.eclipse.jgit.lib.PersonIdent
import org.eclipse.jgit.lib.*
import org.eclipse.jgit.transport.CredentialsProvider
import javax.inject.Inject
import javax.inject.Provider

private const val INVALID_PASSWORD_MESSAGE = "Is the entered passphrase correct?"

class AppGpgSigner @Inject constructor(
private val gpgCredentialsProvider: Provider<GpgCredentialsProvider>,
private val gpgCredentials: GpgCredentialsProvider,
) : BouncyCastleGpgSigner() {

override fun sign(
commit: CommitBuilder,
gpgSigningKey: String,
committer: PersonIdent,
repository: Repository?,
config: GpgConfig?,
data: ByteArray?,
committer: PersonIdent?,
signingKey: String?,
credentialsProvider: CredentialsProvider?
) {
super.sign(commit, gpgSigningKey, committer, gpgCredentialsProvider.get())
}
): GpgSignature {
return try {
var gpgSignature: GpgSignature? = null
retryIfWrongPassphrase { isRetry ->
gpgCredentials.isRetry = isRetry
gpgSignature = super.sign(repository, config, data, committer, signingKey, gpgCredentials)
gpgCredentials.savePasswordInMemory()
}

gpgSignature!!
} catch (ex: CanceledException) {
println("Signing cancelled")
throw ex
}

override fun canLocateSigningKey(
gpgSigningKey: String,
committer: PersonIdent,
credentialsProvider: CredentialsProvider?
): Boolean {
return super.canLocateSigningKey(gpgSigningKey, committer, gpgCredentialsProvider.get())
}

override fun canLocateSigningKey(
gpgSigningKey: String,
committer: PersonIdent,
credentialsProvider: CredentialsProvider?,
config: GpgConfig?
repository: Repository?,
config: GpgConfig?,
committer: PersonIdent?,
signingKey: String?,
credentialsProvider: CredentialsProvider?
): Boolean {
return super.canLocateSigningKey(gpgSigningKey, committer, gpgCredentialsProvider.get(), config)
return super.canLocateSigningKey(repository, config, committer, signingKey, gpgCredentials)
}

override fun signObject(
`object`: ObjectBuilder,
gpgSigningKey: String?,
committer: PersonIdent,
credentialsProvider: CredentialsProvider?,
config: GpgConfig?
repository: Repository?,
config: GpgConfig?,
`object`: ObjectBuilder?,
committer: PersonIdent?,
signingKey: String?,
credentialsProvider: CredentialsProvider?
) {
val gpgCredentialsProvider = gpgCredentialsProvider.get()
val gpgCredentialsProvider = gpgCredentials

try {
retryIfWrongPassphrase { isRetry ->
gpgCredentialsProvider.isRetry = isRetry
super.signObject(`object`, gpgSigningKey, committer, gpgCredentialsProvider, config)
super.signObject(repository, config, `object`, committer, signingKey, credentialsProvider)
gpgCredentialsProvider.savePasswordInMemory()
}
} catch (ex: CanceledException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jetpackduba.gitnuro.git.workspace

import com.jetpackduba.gitnuro.extensions.isMerging
import com.jetpackduba.gitnuro.git.AppGpgSigner
import com.jetpackduba.gitnuro.git.author.LoadAuthorUseCase
import com.jetpackduba.gitnuro.git.config.LoadSignOffConfigUseCase
import com.jetpackduba.gitnuro.git.config.LocalConfigConstants
Expand All @@ -15,14 +16,16 @@ import javax.inject.Inject
class DoCommitUseCase @Inject constructor(
private val loadSignOffConfigUseCase: LoadSignOffConfigUseCase,
private val loadAuthorUseCase: LoadAuthorUseCase,
private val getRepositoryStateUseCase: GetRepositoryStateUseCase
private val getRepositoryStateUseCase: GetRepositoryStateUseCase,
private val appGpgSigner: AppGpgSigner,
) {
suspend operator fun invoke(
git: Git,
message: String,
amend: Boolean,
author: PersonIdent?,
): RevCommit = withContext(Dispatchers.IO) {

val signOffConfig = loadSignOffConfigUseCase(git.repository)

val finalMessage = if (signOffConfig.isEnabled) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/com/jetpackduba/gitnuro/main.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.jetpackduba.gitnuro

import com.jetpackduba.gitnuro.repositories.initPreferencesPath
import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.security.Security



fun main(args: Array<String>) {
Security.addProvider(BouncyCastleProvider())

initPreferencesPath()

val app = App()
Expand Down

0 comments on commit becbb93

Please sign in to comment.