Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
}
}

launch {
viewModel.initError.collectLatest {
flashError(getString(R.string.git_init_failed))
}
}

combine(
viewModel.isGitRepository,
viewModel.gitStatus,
Expand All @@ -301,7 +307,8 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
!isRepo -> {
binding.apply {
emptyView.visibility = View.VISIBLE
emptyView.text = getString(R.string.not_a_git_repo)
tvRepoStatus.text = getString(R.string.not_a_git_repo)
btnInitRepo.isVisible = true
recyclerView.visibility = View.GONE
cbCheckAll.visibility = View.GONE
commitSection.visibility = View.GONE
Expand All @@ -315,7 +322,8 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
allChanges.isEmpty() -> {
binding.apply {
emptyView.visibility = View.VISIBLE
emptyView.text = getString(R.string.no_uncommitted_changes)
btnInitRepo.isVisible = false
tvRepoStatus.text = getString(R.string.no_uncommitted_changes)
recyclerView.visibility = View.GONE
cbCheckAll.visibility = View.VISIBLE
cbCheckAll.isEnabled = false
Expand All @@ -333,6 +341,7 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
val hasSelectable = allChanges.hasSelectable()
binding.apply {
emptyView.visibility = View.GONE
btnInitRepo.isVisible = false
recyclerView.visibility = View.VISIBLE
cbCheckAll.visibility = View.VISIBLE
cbCheckAll.isEnabled = hasSelectable
Expand Down Expand Up @@ -364,6 +373,8 @@ class GitBottomSheetFragment : Fragment(R.layout.fragment_git_bottom_sheet) {
}

setupPullUI()

binding.btnInitRepo.setOnClickListener { viewModel.initGitRepository() }
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class GitBottomSheetViewModel(
private val _watermarkError = MutableSharedFlow<Throwable>(extraBufferCapacity = 1)
val watermarkError: SharedFlow<Throwable> = _watermarkError.asSharedFlow()

private val _initError = MutableSharedFlow<Throwable>(extraBufferCapacity = 1)
val initError: SharedFlow<Throwable> = _initError.asSharedFlow()

private var initJob: Job? = null
private var pullResetJob: Job? = null
private var pushResetJob: Job? = null
Expand Down Expand Up @@ -184,6 +187,7 @@ class GitBottomSheetViewModel(
val status = repo.getStatus()
_gitStatus.value = status
_currentBranch.value = repo.getCurrentBranch()?.name
_isProjectWatermarkEnabled.value = repo.isCommitWatermarkEnabled()
getLocalCommitsCount()
} catch (e: CancellationException) {
throw e
Expand Down Expand Up @@ -762,4 +766,21 @@ class GitBottomSheetViewModel(
}
}
}

fun initGitRepository() {
viewModelScope.launch {
try {
val projectDirPath = IProjectManager.getInstance().projectDirPath
if (projectDirPath.isNotBlank()) {
GitRepositoryManager.initRepository(File(projectDirPath)).use { }
initializeRepository(force = true)
}
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
log.error("Failed to initialize repository", e)
_initError.tryEmit(e)
}
}
}
}
31 changes: 23 additions & 8 deletions app/src/main/res/layout/fragment_git_bottom_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
Expand Down Expand Up @@ -100,18 +99,34 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_branch_name" />

<TextView
<LinearLayout
android:id="@+id/emptyView"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="@string/no_uncommitted_changes"
android:textAppearance="?attr/textAppearanceBody2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbCheckAll" />
app:layout_constraintTop_toBottomOf="@id/cbCheckAll">

<TextView
android:id="@+id/tvRepoStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/no_uncommitted_changes"
android:textAppearance="?attr/textAppearanceBody2" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btn_init_repo"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:text="@string/initialize_repository" />
Comment thread
dara-abijo-adfa marked this conversation as resolved.
Resolved
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.itsaky.androidide.viewmodel
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.itsaky.androidide.git.core.GitCredentialsManager
import com.itsaky.androidide.git.core.GitRepository
import com.itsaky.androidide.git.core.GitRepositoryManager
import com.itsaky.androidide.git.core.models.GitBranch
import com.itsaky.androidide.projects.IProjectManager
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
Expand All @@ -24,6 +27,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.io.File
import java.io.IOException
import kotlin.time.Duration.Companion.milliseconds

Expand Down Expand Up @@ -226,18 +230,16 @@ class GitBottomSheetViewModelTest {
@Test
fun `initializeRepository clears currentRepository when opening fails`() =
runTest {
io.mockk.mockkObject(com.itsaky.androidide.projects.IProjectManager.Companion)
val mockProjectManager = mockk<com.itsaky.androidide.projects.IProjectManager>(relaxed = true)
mockkObject(IProjectManager.Companion)
val mockProjectManager = mockk<IProjectManager>(relaxed = true)
every { mockProjectManager.projectDirPath } returns "/mock/path"
every {
com.itsaky.androidide.projects.IProjectManager
.getInstance()
IProjectManager.getInstance()
} returns mockProjectManager

io.mockk.mockkObject(com.itsaky.androidide.git.core.GitRepositoryManager)
mockkObject(GitRepositoryManager)
coEvery {
com.itsaky.androidide.git.core.GitRepositoryManager
.openRepository(any())
GitRepositoryManager.openRepository(any())
} throws RuntimeException("Corrupt repository")

viewModel.initializeRepository(force = true)
Expand Down Expand Up @@ -312,7 +314,42 @@ class GitBottomSheetViewModelTest {

advanceUntilIdle()

assertEquals(true, viewModel.isProjectWatermarkEnabled.value)
org.junit.Assert.assertEquals(true, viewModel.isProjectWatermarkEnabled.value)
coVerify(exactly = 1) { repository.setCommitWatermarkEnabled(true) }
}

@Test
fun `initGitRepository delegates to GitRepositoryManager and calls initializeRepository`() =
runTest {
mockkObject(IProjectManager.Companion)
val mockProjectManager = mockk<IProjectManager>(relaxed = true)
every { mockProjectManager.projectDirPath } returns "/mock/path"
every { IProjectManager.getInstance() } returns mockProjectManager

mockkObject(GitRepositoryManager)
coEvery { GitRepositoryManager.initRepository(any()) } returns mockk(relaxed = true)

viewModel.initGitRepository()
advanceUntilIdle()

coVerify { GitRepositoryManager.initRepository(File("/mock/path")) }
}

@Test
fun `initGitRepository handles exceptions smoothly`() =
runTest {
mockkObject(IProjectManager.Companion)
val mockProjectManager = mockk<IProjectManager>(relaxed = true)
every { mockProjectManager.projectDirPath } returns "/mock/path"
every { IProjectManager.getInstance() } returns mockProjectManager

mockkObject(GitRepositoryManager)
coEvery { GitRepositoryManager.initRepository(any()) } throws RuntimeException("Init failed")

// Should not throw
viewModel.initGitRepository()
advanceUntilIdle()

coVerify { GitRepositoryManager.initRepository(File("/mock/path")) }
}
}
8 changes: 5 additions & 3 deletions git-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ dependencies {
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.androidx.security.crypto)

testImplementation(libs.tests.junit)
androidTestImplementation(libs.tests.androidx.junit)
androidTestImplementation(libs.tests.androidx.espresso.core)
testImplementation(libs.tests.junit)
testImplementation(libs.tests.kotlinx.coroutines)
testImplementation(libs.tests.mockk)
androidTestImplementation(libs.tests.androidx.junit)
androidTestImplementation(libs.tests.androidx.espresso.core)
}
Loading
Loading