Skip to content

Commit

Permalink
Removed commits limit and added lazy loading
Browse files Browse the repository at this point in the history
Fixes #141
  • Loading branch information
JetpackDuba committed Feb 11, 2025
1 parent c6ce532 commit f505d2d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 155 deletions.
47 changes: 26 additions & 21 deletions src/main/kotlin/com/jetpackduba/gitnuro/git/log/GetLogUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,47 @@ import javax.inject.Inject
class GetLogUseCase @Inject constructor() {
private var graphWalkCached: GraphWalk? = null

suspend operator fun invoke(git: Git, currentBranch: Ref?, hasUncommittedChanges: Boolean, commitsLimit: Int) =
withContext(Dispatchers.IO) {
val commitList = GraphCommitList()
val repositoryState = git.repository.repositoryState

if (currentBranch != null || repositoryState.isRebasing) { // Current branch is null when there is no log (new repo) or rebasing
val logList = git.log().setMaxCount(1).call().toList()

val walk = GraphWalk(git.repository)

walk.use {
// Without this, during rebase conflicts the graph won't show the HEAD commits (new commits created
// by the rebase)
suspend operator fun invoke(
git: Git,
currentBranch: Ref?,
hasUncommittedChanges: Boolean,
commitsLimit: Int,
cachedCommitList: GraphCommitList? = null,
) = withContext(Dispatchers.IO) {
val repositoryState = git.repository.repositoryState
val commitList: GraphCommitList = cachedCommitList ?: GraphCommitList()

if (currentBranch != null || repositoryState.isRebasing) { // Current branch is null when there is no log (new repo) or rebasing
val logList = git.log().setMaxCount(1).call().toList()

val walk = GraphWalk(git.repository)

walk.use {
// Without this, during rebase conflicts the graph won't show the HEAD commits (new commits created
// by the rebase)
if (cachedCommitList == null) {
walk.markStart(walk.lookupCommit(logList.first()))

walk.markStartAllRefs(Constants.R_HEADS)
walk.markStartAllRefs(Constants.R_REMOTES)
walk.markStartAllRefs(Constants.R_TAGS)
// walk.markStartAllRefs(Constants.R_STASH)

if (hasUncommittedChanges)
commitList.addUncommittedChangesGraphCommit(logList.first())

commitList.source(walk)
commitList.fillTo(commitsLimit)
}

ensureActive()

commitList.fillTo(commitsLimit)
}

commitList.calcMaxLine()

return@withContext commitList
ensureActive()
}

commitList.calcMaxLine()

return@withContext commitList
}

private fun cachedGraphWalk(repository: Repository): GraphWalk {
val graphWalkCached = this.graphWalkCached

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ private const val PREF_LATEST_REPOSITORY_TAB_SELECTED = "latestRepositoryTabSele
private const val PREF_LAST_OPENED_REPOSITORIES_PATH = "lastOpenedRepositoriesList"
private const val PREF_THEME = "theme"
private const val PREF_LINE_HEIGHT_TYPE = "linesHeight"
private const val PREF_COMMITS_LIMIT = "commitsLimit"
private const val PREF_COMMITS_LIMIT_ENABLED = "commitsLimitEnabled"
private const val PREF_WINDOW_PLACEMENT = "windowsPlacement"
private const val PREF_CUSTOM_THEME = "customTheme"
private const val PREF_UI_SCALE = "ui_scale"
Expand All @@ -46,15 +44,12 @@ private const val PREF_CACHE_CREDENTIALS_IN_MEMORY = "credentialsInMemory"
private const val PREF_FIRST_PANE_WIDTH = "firstPaneWidth"
private const val PREF_THIRD_PANE_WIDTH = "thirdPaneWidth"


private const val PREF_GIT_FF_MERGE = "gitFFMerge"
private const val PREF_GIT_PULL_REBASE = "gitPullRebase"
private const val PREF_GIT_PUSH_WITH_LEASE = "gitPushWithLease"

private const val PREF_VERIFY_SSL = "verifySsl"

private const val DEFAULT_COMMITS_LIMIT = 1000
private const val DEFAULT_COMMITS_LIMIT_ENABLED = true
private const val DEFAULT_SWAP_UNCOMMITTED_CHANGES = false
private const val DEFAULT_SHOW_CHANGES_AS_TREE = false
private const val DEFAULT_CACHE_CREDENTIALS_IN_MEMORY = true
Expand All @@ -73,9 +68,6 @@ class AppSettingsRepository @Inject constructor() {
private val _linesHeightTypeState = MutableStateFlow(linesHeightType)
val linesHeightTypeState = _linesHeightTypeState.asStateFlow()

private val _commitsLimitEnabledFlow = MutableStateFlow(commitsLimitEnabled)
val commitsLimitEnabledFlow = _commitsLimitEnabledFlow.asStateFlow()

private val _swapUncommittedChangesFlow = MutableStateFlow(swapUncommittedChanges)
val swapUncommittedChangesFlow = _swapUncommittedChangesFlow.asStateFlow()

Expand All @@ -97,9 +89,6 @@ class AppSettingsRepository @Inject constructor() {
private val _pushWithLeaseFlow = MutableStateFlow(pushWithLease)
val pushWithLeaseFlow: StateFlow<Boolean> = _pushWithLeaseFlow.asStateFlow()

private val _commitsLimitFlow = MutableSharedFlow<Int>()
val commitsLimitFlow = _commitsLimitFlow.asSharedFlow()

private val _customThemeFlow = MutableStateFlow<ColorsScheme?>(null)
val customThemeFlow = _customThemeFlow.asStateFlow()

Expand Down Expand Up @@ -177,15 +166,6 @@ class AppSettingsRepository @Inject constructor() {
_linesHeightTypeState.value = value
}

var commitsLimitEnabled: Boolean
get() {
return preferences.getBoolean(PREF_COMMITS_LIMIT_ENABLED, DEFAULT_COMMITS_LIMIT_ENABLED)
}
set(value) {
preferences.putBoolean(PREF_COMMITS_LIMIT_ENABLED, value)
_commitsLimitEnabledFlow.value = value
}

var swapUncommittedChanges: Boolean
get() {
return preferences.getBoolean(PREF_SWAP_UNCOMMITTED_CHANGES, DEFAULT_SWAP_UNCOMMITTED_CHANGES)
Expand Down Expand Up @@ -280,16 +260,6 @@ class AppSettingsRepository @Inject constructor() {
_pushWithLeaseFlow.value = value
}

val commitsLimit: Int
get() {
return preferences.getInt(PREF_COMMITS_LIMIT, DEFAULT_COMMITS_LIMIT)
}

suspend fun setCommitsLimit(value: Int) {
preferences.putInt(PREF_COMMITS_LIMIT, value)
_commitsLimitFlow.emit(value)
}

var windowPlacement: WindowsPlacementPreference
get() {
val placement = preferences.getInt(PREF_WINDOW_PLACEMENT, defaultWindowPlacement.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ val settings = listOf(
SettingsEntry.Entry(AppIcons.LAYOUT, "Layout") { Layout(it) },

SettingsEntry.Section("GIT"),
SettingsEntry.Entry(AppIcons.LIST, "Commits history") { CommitsHistory(it) },
SettingsEntry.Entry(AppIcons.BRANCH, "Branches") { Branches(it) },
SettingsEntry.Entry(AppIcons.CLOUD, "Remote actions") { RemoteActions(it) },

Expand Down Expand Up @@ -160,11 +159,6 @@ fun SettingsDialog(
settingsViewModel: SettingsViewModel,
onDismiss: () -> Unit,
) {

LaunchedEffect(Unit) {
settingsViewModel.resetInfo()
}

var selectedCategory by remember {
mutableStateOf<SettingsEntry.Entry>(
settings.filterIsInstance(SettingsEntry.Entry::class.java).first()
Expand All @@ -174,8 +168,6 @@ fun SettingsDialog(
MaterialDialog(
background = MaterialTheme.colors.surface,
onCloseRequested = {
settingsViewModel.savePendingChanges()

onDismiss()
},
paddingHorizontal = 0.dp,
Expand Down Expand Up @@ -239,7 +231,6 @@ fun SettingsDialog(
.padding(end = 16.dp, bottom = 16.dp)
.align(Alignment.End),
onClick = {
settingsViewModel.savePendingChanges()
onDismiss()
},
)
Expand Down Expand Up @@ -291,33 +282,6 @@ private fun Section(name: String) {
)
}


@Composable
private fun CommitsHistory(settingsViewModel: SettingsViewModel) {
val commitsLimitEnabled by settingsViewModel.commitsLimitEnabledFlow.collectAsState()
var commitsLimit by remember { mutableStateOf(settingsViewModel.commitsLimit) }

SettingToggle(
title = "Limit log commits",
subtitle = "Turning off this may affect the performance",
value = commitsLimitEnabled,
onValueChanged = { value ->
settingsViewModel.commitsLimitEnabled = value
}
)

SettingIntInput(
title = "Max commits",
subtitle = "Increasing this value may affect the performance",
value = commitsLimit,
enabled = commitsLimitEnabled,
onValueChanged = { value ->
commitsLimit = value
settingsViewModel.commitsLimit = value
}
)
}

@Composable
private fun RemoteActions(settingsViewModel: SettingsViewModel) {
val pullRebase by settingsViewModel.pullRebaseFlow.collectAsState()
Expand Down
45 changes: 21 additions & 24 deletions src/main/kotlin/com/jetpackduba/gitnuro/ui/log/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.jetpackduba.gitnuro.git.graph.GraphNode
import com.jetpackduba.gitnuro.git.workspace.StatusSummary
import com.jetpackduba.gitnuro.keybindings.KeybindingOption
import com.jetpackduba.gitnuro.keybindings.matchesBinding
import com.jetpackduba.gitnuro.logging.printLog
import com.jetpackduba.gitnuro.theme.*
import com.jetpackduba.gitnuro.ui.SelectedItem
import com.jetpackduba.gitnuro.ui.components.AvatarImage
Expand Down Expand Up @@ -93,9 +94,6 @@ private const val DIVIDER_WIDTH = 8
private const val LOG_BOTTOM_PADDING = 80

// TODO Min size for message column
@OptIn(
ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class
)
@Composable
fun Log(
logViewModel: LogViewModel,
Expand All @@ -115,6 +113,7 @@ fun Log(
selectedItem = selectedItem,
repositoryState = repositoryState,
changeDefaultUpstreamBranchViewModel = changeDefaultUpstreamBranchViewModel,
onRequestMoreLogItems = { logViewModel.loadMoreLogItems() }
)

LogStatus.Loading -> LogLoading()
Expand Down Expand Up @@ -147,6 +146,7 @@ private fun LogLoaded(
selectedItem: SelectedItem,
repositoryState: RepositoryState,
changeDefaultUpstreamBranchViewModel: () -> ChangeDefaultUpstreamBranchViewModel,
onRequestMoreLogItems: () -> Unit,
) {
val scope = rememberCoroutineScope()
val hasUncommittedChanges = logStatus.hasUncommittedChanges
Expand All @@ -156,6 +156,24 @@ private fun LogLoaded(
val searchFilter = logViewModel.logSearchFilterResults.collectAsState()
val searchFilterValue = searchFilter.value

LaunchedEffect(verticalScrollState) {
snapshotFlow { verticalScrollState.firstVisibleItemIndex }
// .distinctUntilChanged()
.collect {
val commitsList = logStatus.plotCommitList

if (
commitsList.isNotEmpty() &&
commitsList.count() - it < 100 &&
commitsList.last().parentCount > 0
) {
printLog("ABDE", "Should request more items!")
onRequestMoreLogItems()
}
printLog("ABDE", "First visible item index is $it")
}
}

val selectedCommit = if (selectedItem is SelectedItem.CommitBasedItem) {
selectedItem.revCommit
} else {
Expand Down Expand Up @@ -250,7 +268,6 @@ private fun LogLoaded(
selectedItem = selectedItem,
commitList = commitList,
graphWidth = graphWidth,
commitsLimit = logStatus.commitsLimit,
onMerge = { ref -> logViewModel.mergeBranch(ref) },
onRebase = { ref -> logViewModel.rebaseBranch(ref) },
onShowLogDialog = { dialog -> logViewModel.showDialog(dialog) },
Expand Down Expand Up @@ -469,7 +486,6 @@ fun CommitsList(
repositoryState: RepositoryState,
selectedItem: SelectedItem,
commitList: GraphCommitList,
commitsLimit: Int,
onCheckoutCommit: (GraphNode) -> Unit,
onRevertCommit: (GraphNode) -> Unit,
onCherryPickCommit: (GraphNode) -> Unit,
Expand Down Expand Up @@ -571,25 +587,6 @@ fun CommitsList(
)
}

if (commitsLimit >= 0 && commitsLimit <= commitList.count()) {
item {
Box(
modifier = Modifier
.padding(start = graphWidth + 24.dp)
.height(MaterialTheme.linesHeight.logCommitHeight),
contentAlignment = Alignment.CenterStart,
) {
Text(
text = "The commits list has been limited to $commitsLimit. Access the settings to change it.",
color = MaterialTheme.colors.onBackground,
fontStyle = FontStyle.Italic,
style = MaterialTheme.typography.body2,
maxLines = 1,
)
}
}
}

item {
Box(modifier = Modifier.height(LOG_BOTTOM_PADDING.dp))
}
Expand Down
Loading

0 comments on commit f505d2d

Please sign in to comment.